cs4280.c revision 1.7.2.4 1 1.7.2.4 bouyer /* $NetBSD: cs4280.c,v 1.7.2.4 2001/01/05 17:36:02 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.2 bouyer * Note: CS4610 + CS423x ISA codec should be worked with
42 1.7.2.2 bouyer * wss* at pnpbios?
43 1.7.2.2 bouyer *
44 1.7.2.2 bouyer */
45 1.7.2.2 bouyer
46 1.7.2.2 bouyer /*
47 1.7.2.2 bouyer * TODO
48 1.7.2.2 bouyer * Joystick support
49 1.7.2.2 bouyer */
50 1.7.2.2 bouyer
51 1.7.2.2 bouyer #if defined(CS4280_DEBUG)
52 1.7.2.2 bouyer #define DPRINTF(x) if (cs4280debug) printf x
53 1.7.2.2 bouyer #define DPRINTFN(n,x) if (cs4280debug>(n)) printf x
54 1.7.2.2 bouyer int cs4280debug = 0;
55 1.7.2.2 bouyer #else
56 1.7.2.2 bouyer #define DPRINTF(x)
57 1.7.2.2 bouyer #define DPRINTFN(n,x)
58 1.7.2.2 bouyer #endif
59 1.7.2.2 bouyer
60 1.7.2.2 bouyer #include "midi.h"
61 1.7.2.2 bouyer
62 1.7.2.2 bouyer #include <sys/param.h>
63 1.7.2.2 bouyer #include <sys/systm.h>
64 1.7.2.2 bouyer #include <sys/kernel.h>
65 1.7.2.2 bouyer #include <sys/fcntl.h>
66 1.7.2.2 bouyer #include <sys/malloc.h>
67 1.7.2.2 bouyer #include <sys/device.h>
68 1.7.2.2 bouyer #include <sys/types.h>
69 1.7.2.2 bouyer #include <sys/systm.h>
70 1.7.2.2 bouyer
71 1.7.2.2 bouyer #include <dev/pci/pcidevs.h>
72 1.7.2.2 bouyer #include <dev/pci/pcivar.h>
73 1.7.2.2 bouyer #include <dev/pci/cs4280reg.h>
74 1.7.2.2 bouyer #include <dev/pci/cs4280_image.h>
75 1.7.2.2 bouyer
76 1.7.2.2 bouyer #include <sys/audioio.h>
77 1.7.2.2 bouyer #include <dev/audio_if.h>
78 1.7.2.2 bouyer #include <dev/midi_if.h>
79 1.7.2.2 bouyer #include <dev/mulaw.h>
80 1.7.2.2 bouyer #include <dev/auconv.h>
81 1.7.2.2 bouyer
82 1.7.2.2 bouyer #include <dev/ic/ac97reg.h>
83 1.7.2.2 bouyer #include <dev/ic/ac97var.h>
84 1.7.2.2 bouyer
85 1.7.2.2 bouyer #include <machine/bus.h>
86 1.7.2.2 bouyer #include <machine/bswap.h>
87 1.7.2.2 bouyer
88 1.7.2.2 bouyer #define CSCC_PCI_BA0 0x10
89 1.7.2.2 bouyer #define CSCC_PCI_BA1 0x14
90 1.7.2.2 bouyer
91 1.7.2.2 bouyer struct cs4280_dma {
92 1.7.2.2 bouyer bus_dmamap_t map;
93 1.7.2.2 bouyer caddr_t addr; /* real dma buffer */
94 1.7.2.2 bouyer caddr_t dum; /* dummy buffer for audio driver */
95 1.7.2.2 bouyer bus_dma_segment_t segs[1];
96 1.7.2.2 bouyer int nsegs;
97 1.7.2.2 bouyer size_t size;
98 1.7.2.2 bouyer struct cs4280_dma *next;
99 1.7.2.2 bouyer };
100 1.7.2.2 bouyer #define DMAADDR(p) ((p)->map->dm_segs[0].ds_addr)
101 1.7.2.2 bouyer #define BUFADDR(p) ((void *)((p)->dum))
102 1.7.2.2 bouyer #define KERNADDR(p) ((void *)((p)->addr))
103 1.7.2.2 bouyer
104 1.7.2.2 bouyer /*
105 1.7.2.2 bouyer * Software state
106 1.7.2.2 bouyer */
107 1.7.2.2 bouyer struct cs4280_softc {
108 1.7.2.2 bouyer struct device sc_dev;
109 1.7.2.2 bouyer
110 1.7.2.2 bouyer pci_intr_handle_t * sc_ih;
111 1.7.2.2 bouyer
112 1.7.2.2 bouyer /* I/O (BA0) */
113 1.7.2.2 bouyer bus_space_tag_t ba0t;
114 1.7.2.2 bouyer bus_space_handle_t ba0h;
115 1.7.2.2 bouyer
116 1.7.2.2 bouyer /* BA1 */
117 1.7.2.2 bouyer bus_space_tag_t ba1t;
118 1.7.2.2 bouyer bus_space_handle_t ba1h;
119 1.7.2.2 bouyer
120 1.7.2.2 bouyer /* DMA */
121 1.7.2.2 bouyer bus_dma_tag_t sc_dmatag;
122 1.7.2.2 bouyer struct cs4280_dma *sc_dmas;
123 1.7.2.2 bouyer
124 1.7.2.2 bouyer void (*sc_pintr)(void *); /* dma completion intr handler */
125 1.7.2.2 bouyer void *sc_parg; /* arg for sc_intr() */
126 1.7.2.2 bouyer char *sc_ps, *sc_pe, *sc_pn;
127 1.7.2.2 bouyer int sc_pcount;
128 1.7.2.2 bouyer int sc_pi;
129 1.7.2.2 bouyer struct cs4280_dma *sc_pdma;
130 1.7.2.2 bouyer char *sc_pbuf;
131 1.7.2.2 bouyer #ifdef DIAGNOSTIC
132 1.7.2.2 bouyer char sc_prun;
133 1.7.2.2 bouyer #endif
134 1.7.2.2 bouyer
135 1.7.2.2 bouyer void (*sc_rintr)(void *); /* dma completion intr handler */
136 1.7.2.2 bouyer void *sc_rarg; /* arg for sc_intr() */
137 1.7.2.2 bouyer char *sc_rs, *sc_re, *sc_rn;
138 1.7.2.2 bouyer int sc_rcount;
139 1.7.2.2 bouyer int sc_ri;
140 1.7.2.2 bouyer struct cs4280_dma *sc_rdma;
141 1.7.2.2 bouyer char *sc_rbuf;
142 1.7.2.2 bouyer int sc_rparam; /* record format */
143 1.7.2.2 bouyer #ifdef DIAGNOSTIC
144 1.7.2.2 bouyer char sc_rrun;
145 1.7.2.2 bouyer #endif
146 1.7.2.2 bouyer
147 1.7.2.2 bouyer #if NMIDI > 0
148 1.7.2.2 bouyer void (*sc_iintr)(void *, int); /* midi input ready handler */
149 1.7.2.2 bouyer void (*sc_ointr)(void *); /* midi output ready handler */
150 1.7.2.2 bouyer void *sc_arg;
151 1.7.2.2 bouyer #endif
152 1.7.2.2 bouyer
153 1.7.2.2 bouyer u_int32_t pctl;
154 1.7.2.2 bouyer u_int32_t cctl;
155 1.7.2.2 bouyer
156 1.7.2.2 bouyer struct ac97_codec_if *codec_if;
157 1.7.2.2 bouyer struct ac97_host_if host_if;
158 1.7.2.2 bouyer
159 1.7.2.2 bouyer char sc_suspend;
160 1.7.2.2 bouyer void *sc_powerhook; /* Power Hook */
161 1.7.2.2 bouyer u_int16_t ac97_reg[CS4280_SAVE_REG_MAX + 1]; /* Save ac97 registers */
162 1.7.2.2 bouyer };
163 1.7.2.2 bouyer
164 1.7.2.2 bouyer #define BA0READ4(sc, r) bus_space_read_4((sc)->ba0t, (sc)->ba0h, (r))
165 1.7.2.2 bouyer #define BA0WRITE4(sc, r, x) bus_space_write_4((sc)->ba0t, (sc)->ba0h, (r), (x))
166 1.7.2.2 bouyer #define BA1READ4(sc, r) bus_space_read_4((sc)->ba1t, (sc)->ba1h, (r))
167 1.7.2.2 bouyer #define BA1WRITE4(sc, r, x) bus_space_write_4((sc)->ba1t, (sc)->ba1h, (r), (x))
168 1.7.2.2 bouyer
169 1.7.2.2 bouyer int cs4280_match __P((struct device *, struct cfdata *, void *));
170 1.7.2.2 bouyer void cs4280_attach __P((struct device *, struct device *, void *));
171 1.7.2.2 bouyer int cs4280_intr __P((void *));
172 1.7.2.2 bouyer void cs4280_reset __P((void *));
173 1.7.2.2 bouyer int cs4280_download_image __P((struct cs4280_softc *));
174 1.7.2.2 bouyer
175 1.7.2.2 bouyer int cs4280_download(struct cs4280_softc *, u_int32_t *, u_int32_t, u_int32_t);
176 1.7.2.2 bouyer int cs4280_allocmem __P((struct cs4280_softc *, size_t, size_t,
177 1.7.2.2 bouyer struct cs4280_dma *));
178 1.7.2.2 bouyer int cs4280_freemem __P((struct cs4280_softc *, struct cs4280_dma *));
179 1.7.2.2 bouyer
180 1.7.2.2 bouyer #ifdef CS4280_DEBUG
181 1.7.2.2 bouyer int cs4280_check_images __P((struct cs4280_softc *));
182 1.7.2.2 bouyer int cs4280_checkimage(struct cs4280_softc *, u_int32_t *, u_int32_t,
183 1.7.2.2 bouyer u_int32_t);
184 1.7.2.2 bouyer #endif
185 1.7.2.2 bouyer
186 1.7.2.2 bouyer struct cfattach clcs_ca = {
187 1.7.2.2 bouyer sizeof(struct cs4280_softc), cs4280_match, cs4280_attach
188 1.7.2.2 bouyer };
189 1.7.2.2 bouyer
190 1.7.2.2 bouyer int cs4280_init __P((struct cs4280_softc *, int));
191 1.7.2.2 bouyer int cs4280_open __P((void *, int));
192 1.7.2.2 bouyer void cs4280_close __P((void *));
193 1.7.2.2 bouyer
194 1.7.2.2 bouyer int cs4280_query_encoding __P((void *, struct audio_encoding *));
195 1.7.2.2 bouyer int cs4280_set_params __P((void *, int, int, struct audio_params *, struct audio_params *));
196 1.7.2.2 bouyer int cs4280_round_blocksize __P((void *, int));
197 1.7.2.2 bouyer
198 1.7.2.2 bouyer int cs4280_halt_output __P((void *));
199 1.7.2.2 bouyer int cs4280_halt_input __P((void *));
200 1.7.2.2 bouyer
201 1.7.2.2 bouyer int cs4280_getdev __P((void *, struct audio_device *));
202 1.7.2.2 bouyer
203 1.7.2.2 bouyer int cs4280_mixer_set_port __P((void *, mixer_ctrl_t *));
204 1.7.2.2 bouyer int cs4280_mixer_get_port __P((void *, mixer_ctrl_t *));
205 1.7.2.2 bouyer int cs4280_query_devinfo __P((void *addr, mixer_devinfo_t *dip));
206 1.7.2.2 bouyer void *cs4280_malloc __P((void *, int, size_t, int, int));
207 1.7.2.2 bouyer void cs4280_free __P((void *, void *, int));
208 1.7.2.2 bouyer size_t cs4280_round_buffersize __P((void *, int, size_t));
209 1.7.2.2 bouyer paddr_t cs4280_mappage __P((void *, void *, off_t, int));
210 1.7.2.2 bouyer int cs4280_get_props __P((void *));
211 1.7.2.2 bouyer int cs4280_trigger_output __P((void *, void *, void *, int, void (*)(void *),
212 1.7.2.2 bouyer void *, struct audio_params *));
213 1.7.2.2 bouyer int cs4280_trigger_input __P((void *, void *, void *, int, void (*)(void *),
214 1.7.2.2 bouyer void *, struct audio_params *));
215 1.7.2.2 bouyer
216 1.7.2.2 bouyer
217 1.7.2.2 bouyer void cs4280_set_dac_rate __P((struct cs4280_softc *, int ));
218 1.7.2.2 bouyer void cs4280_set_adc_rate __P((struct cs4280_softc *, int ));
219 1.7.2.2 bouyer int cs4280_get_portnum_by_name __P((struct cs4280_softc *, char *, char *,
220 1.7.2.2 bouyer char *));
221 1.7.2.2 bouyer int cs4280_src_wait __P((struct cs4280_softc *));
222 1.7.2.2 bouyer int cs4280_attach_codec __P((void *sc, struct ac97_codec_if *));
223 1.7.2.2 bouyer int cs4280_read_codec __P((void *sc, u_int8_t a, u_int16_t *d));
224 1.7.2.2 bouyer int cs4280_write_codec __P((void *sc, u_int8_t a, u_int16_t d));
225 1.7.2.2 bouyer void cs4280_reset_codec __P((void *sc));
226 1.7.2.2 bouyer
227 1.7.2.2 bouyer void cs4280_power __P((int, void *));
228 1.7.2.2 bouyer
229 1.7.2.2 bouyer void cs4280_clear_fifos __P((struct cs4280_softc *));
230 1.7.2.2 bouyer
231 1.7.2.2 bouyer #if NMIDI > 0
232 1.7.2.2 bouyer void cs4280_midi_close __P((void*));
233 1.7.2.2 bouyer void cs4280_midi_getinfo __P((void *, struct midi_info *));
234 1.7.2.2 bouyer int cs4280_midi_open __P((void *, int, void (*)(void *, int),
235 1.7.2.2 bouyer void (*)(void *), void *));
236 1.7.2.2 bouyer int cs4280_midi_output __P((void *, int));
237 1.7.2.2 bouyer #endif
238 1.7.2.2 bouyer
239 1.7.2.2 bouyer struct audio_hw_if cs4280_hw_if = {
240 1.7.2.2 bouyer cs4280_open,
241 1.7.2.2 bouyer cs4280_close,
242 1.7.2.2 bouyer NULL,
243 1.7.2.2 bouyer cs4280_query_encoding,
244 1.7.2.2 bouyer cs4280_set_params,
245 1.7.2.2 bouyer cs4280_round_blocksize,
246 1.7.2.2 bouyer NULL,
247 1.7.2.2 bouyer NULL,
248 1.7.2.2 bouyer NULL,
249 1.7.2.2 bouyer NULL,
250 1.7.2.2 bouyer NULL,
251 1.7.2.2 bouyer cs4280_halt_output,
252 1.7.2.2 bouyer cs4280_halt_input,
253 1.7.2.2 bouyer NULL,
254 1.7.2.2 bouyer cs4280_getdev,
255 1.7.2.2 bouyer NULL,
256 1.7.2.2 bouyer cs4280_mixer_set_port,
257 1.7.2.2 bouyer cs4280_mixer_get_port,
258 1.7.2.2 bouyer cs4280_query_devinfo,
259 1.7.2.2 bouyer cs4280_malloc,
260 1.7.2.2 bouyer cs4280_free,
261 1.7.2.2 bouyer cs4280_round_buffersize,
262 1.7.2.2 bouyer cs4280_mappage,
263 1.7.2.2 bouyer cs4280_get_props,
264 1.7.2.2 bouyer cs4280_trigger_output,
265 1.7.2.2 bouyer cs4280_trigger_input,
266 1.7.2.2 bouyer };
267 1.7.2.2 bouyer
268 1.7.2.2 bouyer #if NMIDI > 0
269 1.7.2.2 bouyer struct midi_hw_if cs4280_midi_hw_if = {
270 1.7.2.2 bouyer cs4280_midi_open,
271 1.7.2.2 bouyer cs4280_midi_close,
272 1.7.2.2 bouyer cs4280_midi_output,
273 1.7.2.2 bouyer cs4280_midi_getinfo,
274 1.7.2.2 bouyer 0,
275 1.7.2.2 bouyer };
276 1.7.2.2 bouyer #endif
277 1.7.2.2 bouyer
278 1.7.2.2 bouyer
279 1.7.2.2 bouyer
280 1.7.2.2 bouyer struct audio_device cs4280_device = {
281 1.7.2.2 bouyer "CS4280",
282 1.7.2.2 bouyer "",
283 1.7.2.2 bouyer "cs4280"
284 1.7.2.2 bouyer };
285 1.7.2.2 bouyer
286 1.7.2.2 bouyer
287 1.7.2.2 bouyer int
288 1.7.2.2 bouyer cs4280_match(parent, match, aux)
289 1.7.2.2 bouyer struct device *parent;
290 1.7.2.2 bouyer struct cfdata *match;
291 1.7.2.2 bouyer void *aux;
292 1.7.2.2 bouyer {
293 1.7.2.2 bouyer struct pci_attach_args *pa = (struct pci_attach_args *)aux;
294 1.7.2.2 bouyer
295 1.7.2.2 bouyer if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_CIRRUS)
296 1.7.2.2 bouyer return (0);
297 1.7.2.2 bouyer if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_CIRRUS_CS4280
298 1.7.2.2 bouyer #if 0 /* I can't confirm */
299 1.7.2.2 bouyer || PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_CIRRUS_CS4610
300 1.7.2.2 bouyer #endif
301 1.7.2.2 bouyer )
302 1.7.2.2 bouyer return (1);
303 1.7.2.2 bouyer return (0);
304 1.7.2.2 bouyer }
305 1.7.2.2 bouyer
306 1.7.2.2 bouyer int
307 1.7.2.2 bouyer cs4280_read_codec(sc_, add, data)
308 1.7.2.2 bouyer void *sc_;
309 1.7.2.2 bouyer u_int8_t add;
310 1.7.2.2 bouyer u_int16_t *data;
311 1.7.2.2 bouyer {
312 1.7.2.2 bouyer struct cs4280_softc *sc = sc_;
313 1.7.2.2 bouyer int n;
314 1.7.2.2 bouyer
315 1.7.2.2 bouyer DPRINTFN(5,("read_codec: add=0x%02x ", add));
316 1.7.2.2 bouyer /*
317 1.7.2.2 bouyer * Make sure that there is not data sitting around from a preivous
318 1.7.2.2 bouyer * uncompleted access.
319 1.7.2.2 bouyer */
320 1.7.2.2 bouyer BA0READ4(sc, CS4280_ACSDA);
321 1.7.2.2 bouyer
322 1.7.2.2 bouyer /* Set up AC97 control registers. */
323 1.7.2.2 bouyer BA0WRITE4(sc, CS4280_ACCAD, add);
324 1.7.2.2 bouyer BA0WRITE4(sc, CS4280_ACCDA, 0);
325 1.7.2.2 bouyer BA0WRITE4(sc, CS4280_ACCTL,
326 1.7.2.2 bouyer ACCTL_RSTN | ACCTL_ESYN | ACCTL_VFRM | ACCTL_CRW | ACCTL_DCV );
327 1.7.2.2 bouyer
328 1.7.2.2 bouyer if (cs4280_src_wait(sc) < 0) {
329 1.7.2.2 bouyer printf("%s: AC97 read prob. (DCV!=0) for add=0x%0x\n",
330 1.7.2.2 bouyer sc->sc_dev.dv_xname, add);
331 1.7.2.2 bouyer return (1);
332 1.7.2.2 bouyer }
333 1.7.2.2 bouyer
334 1.7.2.2 bouyer /* wait for valid status bit is active */
335 1.7.2.2 bouyer n = 0;
336 1.7.2.2 bouyer while (!(BA0READ4(sc, CS4280_ACSTS) & ACSTS_VSTS)) {
337 1.7.2.2 bouyer delay(1);
338 1.7.2.2 bouyer while (++n > 1000) {
339 1.7.2.2 bouyer printf("%s: AC97 read fail (VSTS==0) for add=0x%0x\n",
340 1.7.2.2 bouyer sc->sc_dev.dv_xname, add);
341 1.7.2.2 bouyer return (1);
342 1.7.2.2 bouyer }
343 1.7.2.2 bouyer }
344 1.7.2.2 bouyer *data = BA0READ4(sc, CS4280_ACSDA);
345 1.7.2.2 bouyer DPRINTFN(5,("data=0x%04x\n", *data));
346 1.7.2.2 bouyer return (0);
347 1.7.2.2 bouyer }
348 1.7.2.2 bouyer
349 1.7.2.2 bouyer int
350 1.7.2.2 bouyer cs4280_write_codec(sc_, add, data)
351 1.7.2.2 bouyer void *sc_;
352 1.7.2.2 bouyer u_int8_t add;
353 1.7.2.2 bouyer u_int16_t data;
354 1.7.2.2 bouyer {
355 1.7.2.2 bouyer struct cs4280_softc *sc = sc_;
356 1.7.2.2 bouyer
357 1.7.2.2 bouyer DPRINTFN(5,("write_codec: add=0x%02x data=0x%04x\n", add, data));
358 1.7.2.2 bouyer BA0WRITE4(sc, CS4280_ACCAD, add);
359 1.7.2.2 bouyer BA0WRITE4(sc, CS4280_ACCDA, data);
360 1.7.2.2 bouyer BA0WRITE4(sc, CS4280_ACCTL,
361 1.7.2.2 bouyer ACCTL_RSTN | ACCTL_ESYN | ACCTL_VFRM | ACCTL_DCV );
362 1.7.2.2 bouyer
363 1.7.2.2 bouyer if (cs4280_src_wait(sc) < 0) {
364 1.7.2.2 bouyer printf("%s: AC97 write fail (DCV!=0) for add=0x%02x data="
365 1.7.2.2 bouyer "0x%04x\n", sc->sc_dev.dv_xname, add, data);
366 1.7.2.2 bouyer return (1);
367 1.7.2.2 bouyer }
368 1.7.2.2 bouyer return (0);
369 1.7.2.2 bouyer }
370 1.7.2.2 bouyer
371 1.7.2.2 bouyer int
372 1.7.2.2 bouyer cs4280_src_wait(sc)
373 1.7.2.2 bouyer struct cs4280_softc *sc;
374 1.7.2.2 bouyer {
375 1.7.2.2 bouyer int n;
376 1.7.2.2 bouyer n = 0;
377 1.7.2.2 bouyer while ((BA0READ4(sc, CS4280_ACCTL) & ACCTL_DCV)) {
378 1.7.2.2 bouyer delay(1000);
379 1.7.2.2 bouyer while (++n > 1000)
380 1.7.2.2 bouyer return (-1);
381 1.7.2.2 bouyer }
382 1.7.2.2 bouyer return (0);
383 1.7.2.2 bouyer }
384 1.7.2.2 bouyer
385 1.7.2.2 bouyer
386 1.7.2.2 bouyer void
387 1.7.2.2 bouyer cs4280_set_adc_rate(sc, rate)
388 1.7.2.2 bouyer struct cs4280_softc *sc;
389 1.7.2.2 bouyer int rate;
390 1.7.2.2 bouyer {
391 1.7.2.2 bouyer /* calculate capture rate:
392 1.7.2.2 bouyer *
393 1.7.2.2 bouyer * capture_coefficient_increment = -round(rate*128*65536/48000;
394 1.7.2.2 bouyer * capture_phase_increment = floor(48000*65536*1024/rate);
395 1.7.2.2 bouyer * cx = round(48000*65536*1024 - capture_phase_increment*rate);
396 1.7.2.2 bouyer * cy = floor(cx/200);
397 1.7.2.2 bouyer * capture_sample_rate_correction = cx - 200*cy;
398 1.7.2.2 bouyer * capture_delay = ceil(24*48000/rate);
399 1.7.2.2 bouyer * capture_num_triplets = floor(65536*rate/24000);
400 1.7.2.2 bouyer * capture_group_length = 24000/GCD(rate, 24000);
401 1.7.2.2 bouyer * where GCD means "Greatest Common Divisor".
402 1.7.2.2 bouyer *
403 1.7.2.2 bouyer * capture_coefficient_increment, capture_phase_increment and
404 1.7.2.2 bouyer * capture_num_triplets are 32-bit signed quantities.
405 1.7.2.2 bouyer * capture_sample_rate_correction and capture_group_length are
406 1.7.2.2 bouyer * 16-bit signed quantities.
407 1.7.2.2 bouyer * capture_delay is a 14-bit unsigned quantity.
408 1.7.2.2 bouyer */
409 1.7.2.2 bouyer u_int32_t cci,cpi,cnt,cx,cy, tmp1;
410 1.7.2.2 bouyer u_int16_t csrc, cgl, cdlay;
411 1.7.2.2 bouyer
412 1.7.2.2 bouyer /* XXX
413 1.7.2.2 bouyer * Even though, embedded_audio_spec says capture rate range 11025 to
414 1.7.2.2 bouyer * 48000, dhwiface.cpp says,
415 1.7.2.2 bouyer *
416 1.7.2.2 bouyer * "We can only decimate by up to a factor of 1/9th the hardware rate.
417 1.7.2.2 bouyer * Return an error if an attempt is made to stray outside that limit."
418 1.7.2.2 bouyer *
419 1.7.2.2 bouyer * so assume range as 48000/9 to 48000
420 1.7.2.2 bouyer */
421 1.7.2.2 bouyer
422 1.7.2.2 bouyer if (rate < 8000)
423 1.7.2.2 bouyer rate = 8000;
424 1.7.2.2 bouyer if (rate > 48000)
425 1.7.2.2 bouyer rate = 48000;
426 1.7.2.2 bouyer
427 1.7.2.2 bouyer cx = rate << 16;
428 1.7.2.2 bouyer cci = cx / 48000;
429 1.7.2.2 bouyer cx -= cci * 48000;
430 1.7.2.2 bouyer cx <<= 7;
431 1.7.2.2 bouyer cci <<= 7;
432 1.7.2.2 bouyer cci += cx / 48000;
433 1.7.2.2 bouyer cci = - cci;
434 1.7.2.2 bouyer
435 1.7.2.2 bouyer cx = 48000 << 16;
436 1.7.2.2 bouyer cpi = cx / rate;
437 1.7.2.2 bouyer cx -= cpi * rate;
438 1.7.2.2 bouyer cx <<= 10;
439 1.7.2.2 bouyer cpi <<= 10;
440 1.7.2.2 bouyer cy = cx / rate;
441 1.7.2.2 bouyer cpi += cy;
442 1.7.2.2 bouyer cx -= cy * rate;
443 1.7.2.2 bouyer
444 1.7.2.2 bouyer cy = cx / 200;
445 1.7.2.2 bouyer csrc = cx - 200*cy;
446 1.7.2.2 bouyer
447 1.7.2.2 bouyer cdlay = ((48000 * 24) + rate - 1) / rate;
448 1.7.2.2 bouyer #if 0
449 1.7.2.2 bouyer cdlay &= 0x3fff; /* make sure cdlay is 14-bit */
450 1.7.2.2 bouyer #endif
451 1.7.2.2 bouyer
452 1.7.2.2 bouyer cnt = rate << 16;
453 1.7.2.2 bouyer cnt /= 24000;
454 1.7.2.2 bouyer
455 1.7.2.2 bouyer cgl = 1;
456 1.7.2.2 bouyer for (tmp1 = 2; tmp1 <= 64; tmp1 *= 2) {
457 1.7.2.2 bouyer if (((rate / tmp1) * tmp1) != rate)
458 1.7.2.2 bouyer cgl *= 2;
459 1.7.2.2 bouyer }
460 1.7.2.2 bouyer if (((rate / 3) * 3) != rate)
461 1.7.2.2 bouyer cgl *= 3;
462 1.7.2.2 bouyer for (tmp1 = 5; tmp1 <= 125; tmp1 *= 5) {
463 1.7.2.2 bouyer if (((rate / tmp1) * tmp1) != rate)
464 1.7.2.2 bouyer cgl *= 5;
465 1.7.2.2 bouyer }
466 1.7.2.2 bouyer #if 0
467 1.7.2.2 bouyer /* XXX what manual says */
468 1.7.2.2 bouyer tmp1 = BA1READ4(sc, CS4280_CSRC) & ~CSRC_MASK;
469 1.7.2.2 bouyer tmp1 |= csrc<<16;
470 1.7.2.2 bouyer BA1WRITE4(sc, CS4280_CSRC, tmp1);
471 1.7.2.2 bouyer #else
472 1.7.2.2 bouyer /* suggested by cs461x.c (ALSA driver) */
473 1.7.2.2 bouyer BA1WRITE4(sc, CS4280_CSRC, CS4280_MK_CSRC(csrc, cy));
474 1.7.2.2 bouyer #endif
475 1.7.2.2 bouyer
476 1.7.2.2 bouyer #if 0
477 1.7.2.2 bouyer /* I am confused. The sample rate calculation section says
478 1.7.2.2 bouyer * cci *is* 32-bit signed quantity but in the parameter description
479 1.7.2.2 bouyer * section, CCI only assigned 16bit.
480 1.7.2.2 bouyer * I believe size of the variable.
481 1.7.2.2 bouyer */
482 1.7.2.2 bouyer tmp1 = BA1READ4(sc, CS4280_CCI) & ~CCI_MASK;
483 1.7.2.2 bouyer tmp1 |= cci<<16;
484 1.7.2.2 bouyer BA1WRITE4(sc, CS4280_CCI, tmp1);
485 1.7.2.2 bouyer #else
486 1.7.2.2 bouyer BA1WRITE4(sc, CS4280_CCI, cci);
487 1.7.2.2 bouyer #endif
488 1.7.2.2 bouyer
489 1.7.2.2 bouyer tmp1 = BA1READ4(sc, CS4280_CD) & ~CD_MASK;
490 1.7.2.2 bouyer tmp1 |= cdlay <<18;
491 1.7.2.2 bouyer BA1WRITE4(sc, CS4280_CD, tmp1);
492 1.7.2.2 bouyer
493 1.7.2.2 bouyer BA1WRITE4(sc, CS4280_CPI, cpi);
494 1.7.2.2 bouyer
495 1.7.2.2 bouyer tmp1 = BA1READ4(sc, CS4280_CGL) & ~CGL_MASK;
496 1.7.2.2 bouyer tmp1 |= cgl;
497 1.7.2.2 bouyer BA1WRITE4(sc, CS4280_CGL, tmp1);
498 1.7.2.2 bouyer
499 1.7.2.2 bouyer BA1WRITE4(sc, CS4280_CNT, cnt);
500 1.7.2.2 bouyer
501 1.7.2.2 bouyer tmp1 = BA1READ4(sc, CS4280_CGC) & ~CGC_MASK;
502 1.7.2.2 bouyer tmp1 |= cgl;
503 1.7.2.2 bouyer BA1WRITE4(sc, CS4280_CGC, tmp1);
504 1.7.2.2 bouyer }
505 1.7.2.2 bouyer
506 1.7.2.2 bouyer void
507 1.7.2.2 bouyer cs4280_set_dac_rate(sc, rate)
508 1.7.2.2 bouyer struct cs4280_softc *sc;
509 1.7.2.2 bouyer int rate;
510 1.7.2.2 bouyer {
511 1.7.2.2 bouyer /*
512 1.7.2.2 bouyer * playback rate may range from 8000Hz to 48000Hz
513 1.7.2.2 bouyer *
514 1.7.2.2 bouyer * play_phase_increment = floor(rate*65536*1024/48000)
515 1.7.2.2 bouyer * px = round(rate*65536*1024 - play_phase_incremnt*48000)
516 1.7.2.2 bouyer * py=floor(px/200)
517 1.7.2.2 bouyer * play_sample_rate_correction = px - 200*py
518 1.7.2.2 bouyer *
519 1.7.2.2 bouyer * play_phase_increment is a 32bit signed quantity.
520 1.7.2.2 bouyer * play_sample_rate_correction is a 16bit signed quantity.
521 1.7.2.2 bouyer */
522 1.7.2.2 bouyer int32_t ppi;
523 1.7.2.2 bouyer int16_t psrc;
524 1.7.2.2 bouyer u_int32_t px, py;
525 1.7.2.2 bouyer
526 1.7.2.2 bouyer if (rate < 8000)
527 1.7.2.2 bouyer rate = 8000;
528 1.7.2.2 bouyer if (rate > 48000)
529 1.7.2.2 bouyer rate = 48000;
530 1.7.2.2 bouyer px = rate << 16;
531 1.7.2.2 bouyer ppi = px/48000;
532 1.7.2.2 bouyer px -= ppi*48000;
533 1.7.2.2 bouyer ppi <<= 10;
534 1.7.2.2 bouyer px <<= 10;
535 1.7.2.2 bouyer py = px / 48000;
536 1.7.2.2 bouyer ppi += py;
537 1.7.2.2 bouyer px -= py*48000;
538 1.7.2.2 bouyer py = px/200;
539 1.7.2.2 bouyer px -= py*200;
540 1.7.2.2 bouyer psrc = px;
541 1.7.2.2 bouyer #if 0
542 1.7.2.2 bouyer /* what manual says */
543 1.7.2.2 bouyer px = BA1READ4(sc, CS4280_PSRC) & ~PSRC_MASK;
544 1.7.2.2 bouyer BA1WRITE4(sc, CS4280_PSRC,
545 1.7.2.2 bouyer ( ((psrc<<16) & PSRC_MASK) | px ));
546 1.7.2.2 bouyer #else
547 1.7.2.2 bouyer /* suggested by cs461x.c (ALSA driver) */
548 1.7.2.2 bouyer BA1WRITE4(sc, CS4280_PSRC, CS4280_MK_PSRC(psrc,py));
549 1.7.2.2 bouyer #endif
550 1.7.2.2 bouyer BA1WRITE4(sc, CS4280_PPI, ppi);
551 1.7.2.2 bouyer }
552 1.7.2.2 bouyer
553 1.7.2.2 bouyer void
554 1.7.2.2 bouyer cs4280_attach(parent, self, aux)
555 1.7.2.2 bouyer struct device *parent;
556 1.7.2.2 bouyer struct device *self;
557 1.7.2.2 bouyer void *aux;
558 1.7.2.2 bouyer {
559 1.7.2.2 bouyer struct cs4280_softc *sc = (struct cs4280_softc *)self;
560 1.7.2.2 bouyer struct pci_attach_args *pa = (struct pci_attach_args *)aux;
561 1.7.2.2 bouyer pci_chipset_tag_t pc = pa->pa_pc;
562 1.7.2.2 bouyer char const *intrstr;
563 1.7.2.2 bouyer pci_intr_handle_t ih;
564 1.7.2.2 bouyer pcireg_t csr;
565 1.7.2.2 bouyer char devinfo[256];
566 1.7.2.2 bouyer mixer_ctrl_t ctl;
567 1.7.2.2 bouyer u_int32_t mem;
568 1.7.2.2 bouyer
569 1.7.2.2 bouyer pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo);
570 1.7.2.2 bouyer printf(": %s (rev. 0x%02x)\n", devinfo, PCI_REVISION(pa->pa_class));
571 1.7.2.2 bouyer
572 1.7.2.2 bouyer /* Map I/O register */
573 1.7.2.2 bouyer if (pci_mapreg_map(pa, CSCC_PCI_BA0,
574 1.7.2.2 bouyer PCI_MAPREG_TYPE_MEM|PCI_MAPREG_MEM_TYPE_32BIT, 0,
575 1.7.2.2 bouyer &sc->ba0t, &sc->ba0h, NULL, NULL)) {
576 1.7.2.2 bouyer printf("%s: can't map BA0 space\n", sc->sc_dev.dv_xname);
577 1.7.2.2 bouyer return;
578 1.7.2.2 bouyer }
579 1.7.2.2 bouyer if (pci_mapreg_map(pa, CSCC_PCI_BA1,
580 1.7.2.2 bouyer PCI_MAPREG_TYPE_MEM|PCI_MAPREG_MEM_TYPE_32BIT, 0,
581 1.7.2.2 bouyer &sc->ba1t, &sc->ba1h, NULL, NULL)) {
582 1.7.2.2 bouyer printf("%s: can't map BA1 space\n", sc->sc_dev.dv_xname);
583 1.7.2.2 bouyer return;
584 1.7.2.2 bouyer }
585 1.7.2.2 bouyer
586 1.7.2.2 bouyer sc->sc_dmatag = pa->pa_dmat;
587 1.7.2.2 bouyer
588 1.7.2.2 bouyer /* Enable the device (set bus master flag) */
589 1.7.2.2 bouyer csr = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
590 1.7.2.2 bouyer pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
591 1.7.2.2 bouyer csr | PCI_COMMAND_MASTER_ENABLE);
592 1.7.2.2 bouyer
593 1.7.2.2 bouyer /* LATENCY_TIMER setting */
594 1.7.2.2 bouyer mem = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_BHLC_REG);
595 1.7.2.2 bouyer if ( PCI_LATTIMER(mem) < 32 ) {
596 1.7.2.2 bouyer mem &= 0xffff00ff;
597 1.7.2.2 bouyer mem |= 0x00002000;
598 1.7.2.2 bouyer pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_BHLC_REG, mem);
599 1.7.2.2 bouyer }
600 1.7.2.2 bouyer
601 1.7.2.2 bouyer /* Map and establish the interrupt. */
602 1.7.2.4 bouyer if (pci_intr_map(pa, &ih)) {
603 1.7.2.2 bouyer printf("%s: couldn't map interrupt\n", sc->sc_dev.dv_xname);
604 1.7.2.2 bouyer return;
605 1.7.2.2 bouyer }
606 1.7.2.2 bouyer intrstr = pci_intr_string(pc, ih);
607 1.7.2.2 bouyer
608 1.7.2.2 bouyer sc->sc_ih = pci_intr_establish(pc, ih, IPL_AUDIO, cs4280_intr, sc);
609 1.7.2.2 bouyer if (sc->sc_ih == NULL) {
610 1.7.2.2 bouyer printf("%s: couldn't establish interrupt",sc->sc_dev.dv_xname);
611 1.7.2.2 bouyer if (intrstr != NULL)
612 1.7.2.2 bouyer printf(" at %s", intrstr);
613 1.7.2.2 bouyer printf("\n");
614 1.7.2.2 bouyer return;
615 1.7.2.2 bouyer }
616 1.7.2.2 bouyer printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
617 1.7.2.2 bouyer
618 1.7.2.2 bouyer /* Initialization */
619 1.7.2.2 bouyer if(cs4280_init(sc, 1) != 0)
620 1.7.2.2 bouyer return;
621 1.7.2.2 bouyer
622 1.7.2.2 bouyer /* AC 97 attachement */
623 1.7.2.2 bouyer sc->host_if.arg = sc;
624 1.7.2.2 bouyer sc->host_if.attach = cs4280_attach_codec;
625 1.7.2.2 bouyer sc->host_if.read = cs4280_read_codec;
626 1.7.2.2 bouyer sc->host_if.write = cs4280_write_codec;
627 1.7.2.2 bouyer sc->host_if.reset = cs4280_reset_codec;
628 1.7.2.2 bouyer
629 1.7.2.2 bouyer if (ac97_attach(&sc->host_if) != 0) {
630 1.7.2.2 bouyer printf("%s: ac97_attach failed\n", sc->sc_dev.dv_xname);
631 1.7.2.2 bouyer return;
632 1.7.2.2 bouyer }
633 1.7.2.2 bouyer
634 1.7.2.2 bouyer /* Turn mute off of DAC, CD and master volumes by default */
635 1.7.2.2 bouyer ctl.type = AUDIO_MIXER_ENUM;
636 1.7.2.2 bouyer ctl.un.ord = 0; /* off */
637 1.7.2.2 bouyer
638 1.7.2.2 bouyer ctl.dev = cs4280_get_portnum_by_name(sc, AudioCoutputs,
639 1.7.2.2 bouyer AudioNmaster, AudioNmute);
640 1.7.2.2 bouyer cs4280_mixer_set_port(sc, &ctl);
641 1.7.2.2 bouyer
642 1.7.2.2 bouyer ctl.dev = cs4280_get_portnum_by_name(sc, AudioCinputs,
643 1.7.2.2 bouyer AudioNdac, AudioNmute);
644 1.7.2.2 bouyer cs4280_mixer_set_port(sc, &ctl);
645 1.7.2.2 bouyer
646 1.7.2.2 bouyer ctl.dev = cs4280_get_portnum_by_name(sc, AudioCinputs,
647 1.7.2.2 bouyer AudioNcd, AudioNmute);
648 1.7.2.2 bouyer cs4280_mixer_set_port(sc, &ctl);
649 1.7.2.2 bouyer
650 1.7.2.2 bouyer audio_attach_mi(&cs4280_hw_if, sc, &sc->sc_dev);
651 1.7.2.2 bouyer
652 1.7.2.2 bouyer #if NMIDI > 0
653 1.7.2.2 bouyer midi_attach_mi(&cs4280_midi_hw_if, sc, &sc->sc_dev);
654 1.7.2.2 bouyer #endif
655 1.7.2.2 bouyer sc->sc_suspend = PWR_RESUME;
656 1.7.2.2 bouyer sc->sc_powerhook = powerhook_establish(cs4280_power, sc);
657 1.7.2.2 bouyer }
658 1.7.2.2 bouyer
659 1.7.2.2 bouyer int
660 1.7.2.2 bouyer cs4280_intr(p)
661 1.7.2.2 bouyer void *p;
662 1.7.2.2 bouyer {
663 1.7.2.2 bouyer /*
664 1.7.2.2 bouyer * XXX
665 1.7.2.2 bouyer *
666 1.7.2.2 bouyer * Since CS4280 has only 4kB dma buffer and
667 1.7.2.2 bouyer * interrupt occurs every 2kB block, I create dummy buffer
668 1.7.2.2 bouyer * which returns to audio driver and actual dma buffer
669 1.7.2.2 bouyer * using in DMA transfer.
670 1.7.2.2 bouyer *
671 1.7.2.2 bouyer *
672 1.7.2.2 bouyer * ring buffer in audio.c is pointed by BUFADDR
673 1.7.2.2 bouyer * <------ ring buffer size == 64kB ------>
674 1.7.2.2 bouyer * <-----> blksize == 2048*(sc->sc_[pr]count) kB
675 1.7.2.2 bouyer * |= = = =|= = = =|= = = =|= = = =|= = = =|
676 1.7.2.2 bouyer * | | | | | | <- call audio_intp every
677 1.7.2.2 bouyer * sc->sc_[pr]_count time.
678 1.7.2.2 bouyer *
679 1.7.2.2 bouyer * actual dma buffer is pointed by KERNADDR
680 1.7.2.2 bouyer * <-> dma buffer size = 4kB
681 1.7.2.2 bouyer * |= =|
682 1.7.2.2 bouyer *
683 1.7.2.2 bouyer *
684 1.7.2.2 bouyer */
685 1.7.2.2 bouyer struct cs4280_softc *sc = p;
686 1.7.2.2 bouyer u_int32_t intr, mem;
687 1.7.2.2 bouyer char * empty_dma;
688 1.7.2.2 bouyer
689 1.7.2.2 bouyer /* grab interrupt register then clear it */
690 1.7.2.2 bouyer intr = BA0READ4(sc, CS4280_HISR);
691 1.7.2.2 bouyer BA0WRITE4(sc, CS4280_HICR, HICR_CHGM | HICR_IEV);
692 1.7.2.2 bouyer
693 1.7.2.2 bouyer /* not for me */
694 1.7.2.2 bouyer if((intr & HISR_INTENA) == 0 )
695 1.7.2.2 bouyer return 0;
696 1.7.2.2 bouyer
697 1.7.2.2 bouyer /* Playback Interrupt */
698 1.7.2.2 bouyer if (intr & HISR_PINT) {
699 1.7.2.2 bouyer mem = BA1READ4(sc, CS4280_PFIE);
700 1.7.2.2 bouyer BA1WRITE4(sc, CS4280_PFIE, (mem & ~PFIE_PI_MASK) | PFIE_PI_DISABLE);
701 1.7.2.2 bouyer if (sc->sc_pintr) {
702 1.7.2.2 bouyer if ((sc->sc_pi%sc->sc_pcount) == 0)
703 1.7.2.2 bouyer sc->sc_pintr(sc->sc_parg);
704 1.7.2.2 bouyer } else {
705 1.7.2.2 bouyer printf("unexpected play intr\n");
706 1.7.2.2 bouyer }
707 1.7.2.2 bouyer /* copy buffer */
708 1.7.2.2 bouyer ++sc->sc_pi;
709 1.7.2.2 bouyer empty_dma = sc->sc_pdma->addr;
710 1.7.2.2 bouyer if (sc->sc_pi&1)
711 1.7.2.2 bouyer empty_dma += CS4280_ICHUNK;
712 1.7.2.2 bouyer memcpy(empty_dma, sc->sc_pn, CS4280_ICHUNK);
713 1.7.2.2 bouyer sc->sc_pn += CS4280_ICHUNK;
714 1.7.2.2 bouyer if (sc->sc_pn >= sc->sc_pe)
715 1.7.2.2 bouyer sc->sc_pn = sc->sc_ps;
716 1.7.2.2 bouyer BA1WRITE4(sc, CS4280_PFIE, mem);
717 1.7.2.2 bouyer }
718 1.7.2.2 bouyer /* Capture Interrupt */
719 1.7.2.2 bouyer if (intr & HISR_CINT) {
720 1.7.2.2 bouyer int i;
721 1.7.2.2 bouyer int16_t rdata;
722 1.7.2.2 bouyer
723 1.7.2.2 bouyer mem = BA1READ4(sc, CS4280_CIE);
724 1.7.2.2 bouyer BA1WRITE4(sc, CS4280_CIE, (mem & ~CIE_CI_MASK) | CIE_CI_DISABLE);
725 1.7.2.2 bouyer ++sc->sc_ri;
726 1.7.2.2 bouyer empty_dma = sc->sc_rdma->addr;
727 1.7.2.2 bouyer if ((sc->sc_ri&1) == 0)
728 1.7.2.2 bouyer empty_dma += CS4280_ICHUNK;
729 1.7.2.2 bouyer
730 1.7.2.2 bouyer /*
731 1.7.2.2 bouyer * XXX
732 1.7.2.2 bouyer * I think this audio data conversion should be
733 1.7.2.2 bouyer * happend in upper layer, but I put this here
734 1.7.2.2 bouyer * since there is no conversion function available.
735 1.7.2.2 bouyer */
736 1.7.2.2 bouyer switch(sc->sc_rparam) {
737 1.7.2.2 bouyer case CF_16BIT_STEREO:
738 1.7.2.2 bouyer /* just copy it */
739 1.7.2.2 bouyer memcpy(sc->sc_rn, empty_dma, CS4280_ICHUNK);
740 1.7.2.2 bouyer sc->sc_rn += CS4280_ICHUNK;
741 1.7.2.2 bouyer break;
742 1.7.2.2 bouyer case CF_16BIT_MONO:
743 1.7.2.2 bouyer for (i = 0; i < 512; i++) {
744 1.7.2.2 bouyer rdata = *((int16_t *)empty_dma)++>>1;
745 1.7.2.2 bouyer rdata += *((int16_t *)empty_dma)++>>1;
746 1.7.2.2 bouyer *((int16_t *)sc->sc_rn)++ = rdata;
747 1.7.2.2 bouyer }
748 1.7.2.2 bouyer break;
749 1.7.2.2 bouyer case CF_8BIT_STEREO:
750 1.7.2.2 bouyer for (i = 0; i < 512; i++) {
751 1.7.2.2 bouyer rdata = *((int16_t*)empty_dma)++;
752 1.7.2.2 bouyer *sc->sc_rn++ = rdata >> 8;
753 1.7.2.2 bouyer rdata = *((int16_t*)empty_dma)++;
754 1.7.2.2 bouyer *sc->sc_rn++ = rdata >> 8;
755 1.7.2.2 bouyer }
756 1.7.2.2 bouyer break;
757 1.7.2.2 bouyer case CF_8BIT_MONO:
758 1.7.2.2 bouyer for (i = 0; i < 512; i++) {
759 1.7.2.2 bouyer rdata = *((int16_t*)empty_dma)++ >>1;
760 1.7.2.2 bouyer rdata += *((int16_t*)empty_dma)++ >>1;
761 1.7.2.2 bouyer *sc->sc_rn++ = rdata >>8;
762 1.7.2.2 bouyer }
763 1.7.2.2 bouyer break;
764 1.7.2.2 bouyer default:
765 1.7.2.2 bouyer /* Should not reach here */
766 1.7.2.2 bouyer printf("unknown sc->sc_rparam: %d\n", sc->sc_rparam);
767 1.7.2.2 bouyer }
768 1.7.2.2 bouyer if (sc->sc_rn >= sc->sc_re)
769 1.7.2.2 bouyer sc->sc_rn = sc->sc_rs;
770 1.7.2.2 bouyer BA1WRITE4(sc, CS4280_CIE, mem);
771 1.7.2.2 bouyer if (sc->sc_rintr) {
772 1.7.2.2 bouyer if ((sc->sc_ri%(sc->sc_rcount)) == 0)
773 1.7.2.2 bouyer sc->sc_rintr(sc->sc_rarg);
774 1.7.2.2 bouyer } else {
775 1.7.2.2 bouyer printf("unexpected record intr\n");
776 1.7.2.2 bouyer }
777 1.7.2.2 bouyer }
778 1.7.2.2 bouyer
779 1.7.2.2 bouyer #if NMIDI > 0
780 1.7.2.2 bouyer /* Midi port Interrupt */
781 1.7.2.2 bouyer if (intr & HISR_MIDI) {
782 1.7.2.2 bouyer int data;
783 1.7.2.2 bouyer
784 1.7.2.2 bouyer DPRINTF(("i: %d: ",
785 1.7.2.2 bouyer BA0READ4(sc, CS4280_MIDSR)));
786 1.7.2.2 bouyer /* Read the received data */
787 1.7.2.2 bouyer while ((sc->sc_iintr != NULL) &&
788 1.7.2.2 bouyer ((BA0READ4(sc, CS4280_MIDSR) & MIDSR_RBE) == 0)) {
789 1.7.2.2 bouyer data = BA0READ4(sc, CS4280_MIDRP) & MIDRP_MASK;
790 1.7.2.2 bouyer DPRINTF(("r:%x\n",data));
791 1.7.2.2 bouyer sc->sc_iintr(sc->sc_arg, data);
792 1.7.2.2 bouyer }
793 1.7.2.2 bouyer
794 1.7.2.2 bouyer /* Write the data */
795 1.7.2.2 bouyer #if 1
796 1.7.2.2 bouyer /* XXX:
797 1.7.2.2 bouyer * It seems "Transmit Buffer Full" never activate until EOI
798 1.7.2.2 bouyer * is deliverd. Shall I throw EOI top of this routine ?
799 1.7.2.2 bouyer */
800 1.7.2.2 bouyer if ((BA0READ4(sc, CS4280_MIDSR) & MIDSR_TBF) == 0) {
801 1.7.2.2 bouyer DPRINTF(("w: "));
802 1.7.2.2 bouyer if (sc->sc_ointr != NULL)
803 1.7.2.2 bouyer sc->sc_ointr(sc->sc_arg);
804 1.7.2.2 bouyer }
805 1.7.2.2 bouyer #else
806 1.7.2.2 bouyer while ((sc->sc_ointr != NULL) &&
807 1.7.2.2 bouyer ((BA0READ4(sc, CS4280_MIDSR) & MIDSR_TBF) == 0)) {
808 1.7.2.2 bouyer DPRINTF(("w: "));
809 1.7.2.2 bouyer sc->sc_ointr(sc->sc_arg);
810 1.7.2.2 bouyer }
811 1.7.2.2 bouyer #endif
812 1.7.2.2 bouyer DPRINTF(("\n"));
813 1.7.2.2 bouyer }
814 1.7.2.2 bouyer #endif
815 1.7.2.2 bouyer
816 1.7.2.2 bouyer return (1);
817 1.7.2.2 bouyer }
818 1.7.2.2 bouyer
819 1.7.2.2 bouyer
820 1.7.2.2 bouyer /* Download Proceessor Code and Data image */
821 1.7.2.2 bouyer
822 1.7.2.2 bouyer int
823 1.7.2.2 bouyer cs4280_download(sc, src, offset, len)
824 1.7.2.2 bouyer struct cs4280_softc *sc;
825 1.7.2.2 bouyer u_int32_t *src;
826 1.7.2.2 bouyer u_int32_t offset, len;
827 1.7.2.2 bouyer {
828 1.7.2.2 bouyer u_int32_t ctr;
829 1.7.2.2 bouyer
830 1.7.2.2 bouyer #ifdef CS4280_DEBUG
831 1.7.2.2 bouyer u_int32_t con, data;
832 1.7.2.2 bouyer u_int8_t c0,c1,c2,c3;
833 1.7.2.2 bouyer #endif
834 1.7.2.2 bouyer if ((offset&3) || (len&3))
835 1.7.2.2 bouyer return (-1);
836 1.7.2.2 bouyer
837 1.7.2.2 bouyer len /= sizeof(u_int32_t);
838 1.7.2.2 bouyer for (ctr = 0; ctr < len; ctr++) {
839 1.7.2.2 bouyer /* XXX:
840 1.7.2.2 bouyer * I cannot confirm this is the right thing or not
841 1.7.2.2 bouyer * on BIG-ENDIAN machines.
842 1.7.2.2 bouyer */
843 1.7.2.2 bouyer BA1WRITE4(sc, offset+ctr*4, htole32(*(src+ctr)));
844 1.7.2.2 bouyer #ifdef CS4280_DEBUG
845 1.7.2.2 bouyer data = htole32(*(src+ctr));
846 1.7.2.2 bouyer c0 = bus_space_read_1(sc->ba1t, sc->ba1h, offset+ctr*4+0);
847 1.7.2.2 bouyer c1 = bus_space_read_1(sc->ba1t, sc->ba1h, offset+ctr*4+1);
848 1.7.2.2 bouyer c2 = bus_space_read_1(sc->ba1t, sc->ba1h, offset+ctr*4+2);
849 1.7.2.2 bouyer c3 = bus_space_read_1(sc->ba1t, sc->ba1h, offset+ctr*4+3);
850 1.7.2.2 bouyer con = ( (c3<<24) | (c2<<16) | (c1<<8) | c0 );
851 1.7.2.2 bouyer if (data != con ) {
852 1.7.2.2 bouyer printf("0x%06x: write=0x%08x read=0x%08x\n",
853 1.7.2.2 bouyer offset+ctr*4, data, con);
854 1.7.2.2 bouyer return (-1);
855 1.7.2.2 bouyer }
856 1.7.2.2 bouyer #endif
857 1.7.2.2 bouyer }
858 1.7.2.2 bouyer return (0);
859 1.7.2.2 bouyer }
860 1.7.2.2 bouyer
861 1.7.2.2 bouyer int
862 1.7.2.2 bouyer cs4280_download_image(sc)
863 1.7.2.2 bouyer struct cs4280_softc *sc;
864 1.7.2.2 bouyer {
865 1.7.2.2 bouyer int idx, err;
866 1.7.2.2 bouyer u_int32_t offset = 0;
867 1.7.2.2 bouyer
868 1.7.2.2 bouyer err = 0;
869 1.7.2.2 bouyer for (idx = 0; idx < BA1_MEMORY_COUNT; ++idx) {
870 1.7.2.2 bouyer err = cs4280_download(sc, &BA1Struct.map[offset],
871 1.7.2.2 bouyer BA1Struct.memory[idx].offset,
872 1.7.2.2 bouyer BA1Struct.memory[idx].size);
873 1.7.2.2 bouyer if (err != 0) {
874 1.7.2.2 bouyer printf("%s: load_image failed at %d\n",
875 1.7.2.2 bouyer sc->sc_dev.dv_xname, idx);
876 1.7.2.2 bouyer return (-1);
877 1.7.2.2 bouyer }
878 1.7.2.2 bouyer offset += BA1Struct.memory[idx].size / sizeof(u_int32_t);
879 1.7.2.2 bouyer }
880 1.7.2.2 bouyer return (err);
881 1.7.2.2 bouyer }
882 1.7.2.2 bouyer
883 1.7.2.2 bouyer #ifdef CS4280_DEBUG
884 1.7.2.2 bouyer int
885 1.7.2.2 bouyer cs4280_checkimage(sc, src, offset, len)
886 1.7.2.2 bouyer struct cs4280_softc *sc;
887 1.7.2.2 bouyer u_int32_t *src;
888 1.7.2.2 bouyer u_int32_t offset, len;
889 1.7.2.2 bouyer {
890 1.7.2.2 bouyer u_int32_t ctr, data;
891 1.7.2.2 bouyer int err = 0;
892 1.7.2.2 bouyer
893 1.7.2.2 bouyer if ((offset&3) || (len&3))
894 1.7.2.2 bouyer return -1;
895 1.7.2.2 bouyer
896 1.7.2.2 bouyer len /= sizeof(u_int32_t);
897 1.7.2.2 bouyer for (ctr = 0; ctr < len; ctr++) {
898 1.7.2.2 bouyer /* I cannot confirm this is the right thing
899 1.7.2.2 bouyer * on BIG-ENDIAN machines
900 1.7.2.2 bouyer */
901 1.7.2.2 bouyer data = BA1READ4(sc, offset+ctr*4);
902 1.7.2.2 bouyer if (data != htole32(*(src+ctr))) {
903 1.7.2.2 bouyer printf("0x%06x: 0x%08x(0x%08x)\n",
904 1.7.2.2 bouyer offset+ctr*4, data, *(src+ctr));
905 1.7.2.2 bouyer *(src+ctr) = data;
906 1.7.2.2 bouyer ++err;
907 1.7.2.2 bouyer }
908 1.7.2.2 bouyer }
909 1.7.2.2 bouyer return (err);
910 1.7.2.2 bouyer }
911 1.7.2.2 bouyer
912 1.7.2.2 bouyer int
913 1.7.2.2 bouyer cs4280_check_images(sc)
914 1.7.2.2 bouyer struct cs4280_softc *sc;
915 1.7.2.2 bouyer {
916 1.7.2.2 bouyer int idx, err;
917 1.7.2.2 bouyer u_int32_t offset = 0;
918 1.7.2.2 bouyer
919 1.7.2.2 bouyer err = 0;
920 1.7.2.2 bouyer /*for (idx=0; idx < BA1_MEMORY_COUNT; ++idx) { */
921 1.7.2.2 bouyer for (idx = 0; idx < 1; ++idx) {
922 1.7.2.2 bouyer err = cs4280_checkimage(sc, &BA1Struct.map[offset],
923 1.7.2.2 bouyer BA1Struct.memory[idx].offset,
924 1.7.2.2 bouyer BA1Struct.memory[idx].size);
925 1.7.2.2 bouyer if (err != 0) {
926 1.7.2.2 bouyer printf("%s: check_image failed at %d\n",
927 1.7.2.2 bouyer sc->sc_dev.dv_xname, idx);
928 1.7.2.2 bouyer }
929 1.7.2.2 bouyer offset += BA1Struct.memory[idx].size / sizeof(u_int32_t);
930 1.7.2.2 bouyer }
931 1.7.2.2 bouyer return (err);
932 1.7.2.2 bouyer }
933 1.7.2.2 bouyer
934 1.7.2.2 bouyer #endif
935 1.7.2.2 bouyer
936 1.7.2.2 bouyer int
937 1.7.2.2 bouyer cs4280_attach_codec(sc_, codec_if)
938 1.7.2.2 bouyer void *sc_;
939 1.7.2.2 bouyer struct ac97_codec_if *codec_if;
940 1.7.2.2 bouyer {
941 1.7.2.2 bouyer struct cs4280_softc *sc = sc_;
942 1.7.2.2 bouyer
943 1.7.2.2 bouyer sc->codec_if = codec_if;
944 1.7.2.2 bouyer return (0);
945 1.7.2.2 bouyer }
946 1.7.2.2 bouyer
947 1.7.2.2 bouyer void
948 1.7.2.2 bouyer cs4280_reset_codec(sc_)
949 1.7.2.2 bouyer void *sc_;
950 1.7.2.2 bouyer {
951 1.7.2.2 bouyer struct cs4280_softc *sc = sc_;
952 1.7.2.2 bouyer int n;
953 1.7.2.2 bouyer
954 1.7.2.2 bouyer /* Reset codec */
955 1.7.2.2 bouyer BA0WRITE4(sc, CS4280_ACCTL, 0);
956 1.7.2.2 bouyer delay(100); /* delay 100us */
957 1.7.2.2 bouyer BA0WRITE4(sc, CS4280_ACCTL, ACCTL_RSTN);
958 1.7.2.2 bouyer
959 1.7.2.2 bouyer /*
960 1.7.2.2 bouyer * It looks like we do the following procedure, too
961 1.7.2.2 bouyer */
962 1.7.2.2 bouyer
963 1.7.2.2 bouyer /* Enable AC-link sync generation */
964 1.7.2.2 bouyer BA0WRITE4(sc, CS4280_ACCTL, ACCTL_ESYN | ACCTL_RSTN);
965 1.7.2.2 bouyer delay(50*1000); /* XXX delay 50ms */
966 1.7.2.2 bouyer
967 1.7.2.2 bouyer /* Assert valid frame signal */
968 1.7.2.2 bouyer BA0WRITE4(sc, CS4280_ACCTL, ACCTL_VFRM | ACCTL_ESYN | ACCTL_RSTN);
969 1.7.2.2 bouyer
970 1.7.2.2 bouyer /* Wait for valid AC97 input slot */
971 1.7.2.2 bouyer n = 0;
972 1.7.2.2 bouyer while (BA0READ4(sc, CS4280_ACISV) != (ACISV_ISV3 | ACISV_ISV4)) {
973 1.7.2.2 bouyer delay(1000);
974 1.7.2.2 bouyer if (++n > 1000) {
975 1.7.2.2 bouyer printf("reset_codec: AC97 inputs slot ready timeout\n");
976 1.7.2.2 bouyer return;
977 1.7.2.2 bouyer }
978 1.7.2.2 bouyer }
979 1.7.2.2 bouyer }
980 1.7.2.2 bouyer
981 1.7.2.2 bouyer
982 1.7.2.2 bouyer /* Processor Soft Reset */
983 1.7.2.2 bouyer void
984 1.7.2.2 bouyer cs4280_reset(sc_)
985 1.7.2.2 bouyer void *sc_;
986 1.7.2.2 bouyer {
987 1.7.2.2 bouyer struct cs4280_softc *sc = sc_;
988 1.7.2.2 bouyer
989 1.7.2.2 bouyer /* Set RSTSP bit in SPCR (also clear RUN, RUNFR, and DRQEN) */
990 1.7.2.2 bouyer BA1WRITE4(sc, CS4280_SPCR, SPCR_RSTSP);
991 1.7.2.2 bouyer delay(100);
992 1.7.2.2 bouyer /* Clear RSTSP bit in SPCR */
993 1.7.2.2 bouyer BA1WRITE4(sc, CS4280_SPCR, 0);
994 1.7.2.2 bouyer /* enable DMA reqest */
995 1.7.2.2 bouyer BA1WRITE4(sc, CS4280_SPCR, SPCR_DRQEN);
996 1.7.2.2 bouyer }
997 1.7.2.2 bouyer
998 1.7.2.2 bouyer int
999 1.7.2.2 bouyer cs4280_open(addr, flags)
1000 1.7.2.2 bouyer void *addr;
1001 1.7.2.2 bouyer int flags;
1002 1.7.2.2 bouyer {
1003 1.7.2.2 bouyer return (0);
1004 1.7.2.2 bouyer }
1005 1.7.2.2 bouyer
1006 1.7.2.2 bouyer void
1007 1.7.2.2 bouyer cs4280_close(addr)
1008 1.7.2.2 bouyer void *addr;
1009 1.7.2.2 bouyer {
1010 1.7.2.2 bouyer struct cs4280_softc *sc = addr;
1011 1.7.2.2 bouyer
1012 1.7.2.2 bouyer cs4280_halt_output(sc);
1013 1.7.2.2 bouyer cs4280_halt_input(sc);
1014 1.7.2.2 bouyer
1015 1.7.2.2 bouyer sc->sc_pintr = 0;
1016 1.7.2.2 bouyer sc->sc_rintr = 0;
1017 1.7.2.2 bouyer }
1018 1.7.2.2 bouyer
1019 1.7.2.2 bouyer int
1020 1.7.2.2 bouyer cs4280_query_encoding(addr, fp)
1021 1.7.2.2 bouyer void *addr;
1022 1.7.2.2 bouyer struct audio_encoding *fp;
1023 1.7.2.2 bouyer {
1024 1.7.2.2 bouyer switch (fp->index) {
1025 1.7.2.2 bouyer case 0:
1026 1.7.2.2 bouyer strcpy(fp->name, AudioEulinear);
1027 1.7.2.2 bouyer fp->encoding = AUDIO_ENCODING_ULINEAR;
1028 1.7.2.2 bouyer fp->precision = 8;
1029 1.7.2.2 bouyer fp->flags = 0;
1030 1.7.2.2 bouyer break;
1031 1.7.2.2 bouyer case 1:
1032 1.7.2.2 bouyer strcpy(fp->name, AudioEmulaw);
1033 1.7.2.2 bouyer fp->encoding = AUDIO_ENCODING_ULAW;
1034 1.7.2.2 bouyer fp->precision = 8;
1035 1.7.2.2 bouyer fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
1036 1.7.2.2 bouyer break;
1037 1.7.2.2 bouyer case 2:
1038 1.7.2.2 bouyer strcpy(fp->name, AudioEalaw);
1039 1.7.2.2 bouyer fp->encoding = AUDIO_ENCODING_ALAW;
1040 1.7.2.2 bouyer fp->precision = 8;
1041 1.7.2.2 bouyer fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
1042 1.7.2.2 bouyer break;
1043 1.7.2.2 bouyer case 3:
1044 1.7.2.2 bouyer strcpy(fp->name, AudioEslinear);
1045 1.7.2.2 bouyer fp->encoding = AUDIO_ENCODING_SLINEAR;
1046 1.7.2.2 bouyer fp->precision = 8;
1047 1.7.2.2 bouyer fp->flags = 0;
1048 1.7.2.2 bouyer break;
1049 1.7.2.2 bouyer case 4:
1050 1.7.2.2 bouyer strcpy(fp->name, AudioEslinear_le);
1051 1.7.2.2 bouyer fp->encoding = AUDIO_ENCODING_SLINEAR_LE;
1052 1.7.2.2 bouyer fp->precision = 16;
1053 1.7.2.2 bouyer fp->flags = 0;
1054 1.7.2.2 bouyer break;
1055 1.7.2.2 bouyer case 5:
1056 1.7.2.2 bouyer strcpy(fp->name, AudioEulinear_le);
1057 1.7.2.2 bouyer fp->encoding = AUDIO_ENCODING_ULINEAR_LE;
1058 1.7.2.2 bouyer fp->precision = 16;
1059 1.7.2.2 bouyer fp->flags = 0;
1060 1.7.2.2 bouyer break;
1061 1.7.2.2 bouyer case 6:
1062 1.7.2.2 bouyer strcpy(fp->name, AudioEslinear_be);
1063 1.7.2.2 bouyer fp->encoding = AUDIO_ENCODING_SLINEAR_BE;
1064 1.7.2.2 bouyer fp->precision = 16;
1065 1.7.2.2 bouyer fp->flags = 0;
1066 1.7.2.2 bouyer break;
1067 1.7.2.2 bouyer case 7:
1068 1.7.2.2 bouyer strcpy(fp->name, AudioEulinear_be);
1069 1.7.2.2 bouyer fp->encoding = AUDIO_ENCODING_ULINEAR_BE;
1070 1.7.2.2 bouyer fp->precision = 16;
1071 1.7.2.2 bouyer fp->flags = 0;
1072 1.7.2.2 bouyer break;
1073 1.7.2.2 bouyer default:
1074 1.7.2.2 bouyer return (EINVAL);
1075 1.7.2.2 bouyer }
1076 1.7.2.2 bouyer return (0);
1077 1.7.2.2 bouyer }
1078 1.7.2.2 bouyer
1079 1.7.2.2 bouyer int
1080 1.7.2.2 bouyer cs4280_set_params(addr, setmode, usemode, play, rec)
1081 1.7.2.2 bouyer void *addr;
1082 1.7.2.2 bouyer int setmode, usemode;
1083 1.7.2.2 bouyer struct audio_params *play, *rec;
1084 1.7.2.2 bouyer {
1085 1.7.2.2 bouyer struct cs4280_softc *sc = addr;
1086 1.7.2.2 bouyer struct audio_params *p;
1087 1.7.2.2 bouyer int mode;
1088 1.7.2.2 bouyer
1089 1.7.2.2 bouyer for (mode = AUMODE_RECORD; mode != -1;
1090 1.7.2.2 bouyer mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1 ) {
1091 1.7.2.2 bouyer if ((setmode & mode) == 0)
1092 1.7.2.2 bouyer continue;
1093 1.7.2.2 bouyer
1094 1.7.2.2 bouyer p = mode == AUMODE_PLAY ? play : rec;
1095 1.7.2.2 bouyer
1096 1.7.2.2 bouyer if (p == play) {
1097 1.7.2.2 bouyer DPRINTFN(5,("play: sample=%ld precision=%d channels=%d\n",
1098 1.7.2.2 bouyer p->sample_rate, p->precision, p->channels));
1099 1.7.2.2 bouyer /* play back data format may be 8- or 16-bit and
1100 1.7.2.2 bouyer * either stereo or mono.
1101 1.7.2.2 bouyer * playback rate may range from 8000Hz to 48000Hz
1102 1.7.2.2 bouyer */
1103 1.7.2.2 bouyer if (p->sample_rate < 8000 || p->sample_rate > 48000 ||
1104 1.7.2.2 bouyer (p->precision != 8 && p->precision != 16) ||
1105 1.7.2.2 bouyer (p->channels != 1 && p->channels != 2) ) {
1106 1.7.2.2 bouyer return (EINVAL);
1107 1.7.2.2 bouyer }
1108 1.7.2.2 bouyer } else {
1109 1.7.2.2 bouyer DPRINTFN(5,("rec: sample=%ld precision=%d channels=%d\n",
1110 1.7.2.2 bouyer p->sample_rate, p->precision, p->channels));
1111 1.7.2.2 bouyer /* capture data format must be 16bit stereo
1112 1.7.2.2 bouyer * and sample rate range from 11025Hz to 48000Hz.
1113 1.7.2.2 bouyer *
1114 1.7.2.2 bouyer * XXX: it looks like to work with 8000Hz,
1115 1.7.2.2 bouyer * although data sheets say lower limit is
1116 1.7.2.2 bouyer * 11025 Hz.
1117 1.7.2.2 bouyer */
1118 1.7.2.2 bouyer
1119 1.7.2.2 bouyer if (p->sample_rate < 8000 || p->sample_rate > 48000 ||
1120 1.7.2.2 bouyer (p->precision != 8 && p->precision != 16) ||
1121 1.7.2.2 bouyer (p->channels != 1 && p->channels != 2) ) {
1122 1.7.2.2 bouyer return (EINVAL);
1123 1.7.2.2 bouyer }
1124 1.7.2.2 bouyer }
1125 1.7.2.2 bouyer p->factor = 1;
1126 1.7.2.2 bouyer p->sw_code = 0;
1127 1.7.2.2 bouyer
1128 1.7.2.2 bouyer /* capturing data is slinear */
1129 1.7.2.2 bouyer switch (p->encoding) {
1130 1.7.2.2 bouyer case AUDIO_ENCODING_SLINEAR_BE:
1131 1.7.2.2 bouyer if (mode == AUMODE_RECORD) {
1132 1.7.2.2 bouyer if (p->precision == 16)
1133 1.7.2.2 bouyer p->sw_code = swap_bytes;
1134 1.7.2.2 bouyer }
1135 1.7.2.2 bouyer break;
1136 1.7.2.2 bouyer case AUDIO_ENCODING_SLINEAR_LE:
1137 1.7.2.2 bouyer break;
1138 1.7.2.2 bouyer case AUDIO_ENCODING_ULINEAR_BE:
1139 1.7.2.2 bouyer if (mode == AUMODE_RECORD) {
1140 1.7.2.2 bouyer if (p->precision == 16)
1141 1.7.2.2 bouyer p->sw_code = change_sign16_swap_bytes_le;
1142 1.7.2.2 bouyer else
1143 1.7.2.2 bouyer p->sw_code = change_sign8;
1144 1.7.2.2 bouyer }
1145 1.7.2.2 bouyer break;
1146 1.7.2.2 bouyer case AUDIO_ENCODING_ULINEAR_LE:
1147 1.7.2.2 bouyer if (mode == AUMODE_RECORD) {
1148 1.7.2.2 bouyer if (p->precision == 16)
1149 1.7.2.2 bouyer p->sw_code = change_sign16_le;
1150 1.7.2.2 bouyer else
1151 1.7.2.2 bouyer p->sw_code = change_sign8;
1152 1.7.2.2 bouyer }
1153 1.7.2.2 bouyer break;
1154 1.7.2.2 bouyer case AUDIO_ENCODING_ULAW:
1155 1.7.2.2 bouyer if (mode == AUMODE_PLAY) {
1156 1.7.2.2 bouyer p->factor = 2;
1157 1.7.2.2 bouyer p->sw_code = mulaw_to_slinear16_le;
1158 1.7.2.2 bouyer } else {
1159 1.7.2.2 bouyer p->sw_code = slinear8_to_mulaw;
1160 1.7.2.2 bouyer }
1161 1.7.2.2 bouyer break;
1162 1.7.2.2 bouyer case AUDIO_ENCODING_ALAW:
1163 1.7.2.2 bouyer if (mode == AUMODE_PLAY) {
1164 1.7.2.2 bouyer p->factor = 2;
1165 1.7.2.2 bouyer p->sw_code = alaw_to_slinear16_le;
1166 1.7.2.2 bouyer } else {
1167 1.7.2.2 bouyer p->sw_code = slinear8_to_alaw;
1168 1.7.2.2 bouyer }
1169 1.7.2.2 bouyer break;
1170 1.7.2.2 bouyer default:
1171 1.7.2.2 bouyer return (EINVAL);
1172 1.7.2.2 bouyer }
1173 1.7.2.2 bouyer }
1174 1.7.2.2 bouyer
1175 1.7.2.2 bouyer /* set sample rate */
1176 1.7.2.2 bouyer cs4280_set_dac_rate(sc, play->sample_rate);
1177 1.7.2.2 bouyer cs4280_set_adc_rate(sc, rec->sample_rate);
1178 1.7.2.2 bouyer return (0);
1179 1.7.2.2 bouyer }
1180 1.7.2.2 bouyer
1181 1.7.2.2 bouyer int
1182 1.7.2.2 bouyer cs4280_round_blocksize(hdl, blk)
1183 1.7.2.2 bouyer void *hdl;
1184 1.7.2.2 bouyer int blk;
1185 1.7.2.2 bouyer {
1186 1.7.2.2 bouyer return (blk < CS4280_ICHUNK ? CS4280_ICHUNK : blk & -CS4280_ICHUNK);
1187 1.7.2.2 bouyer }
1188 1.7.2.2 bouyer
1189 1.7.2.2 bouyer size_t
1190 1.7.2.2 bouyer cs4280_round_buffersize(addr, direction, size)
1191 1.7.2.2 bouyer void *addr;
1192 1.7.2.2 bouyer int direction;
1193 1.7.2.2 bouyer size_t size;
1194 1.7.2.2 bouyer {
1195 1.7.2.2 bouyer /* although real dma buffer size is 4KB,
1196 1.7.2.2 bouyer * let the audio.c driver use a larger buffer.
1197 1.7.2.2 bouyer * ( suggested by Lennart Augustsson. )
1198 1.7.2.2 bouyer */
1199 1.7.2.2 bouyer return (size);
1200 1.7.2.2 bouyer }
1201 1.7.2.2 bouyer
1202 1.7.2.2 bouyer int
1203 1.7.2.2 bouyer cs4280_get_props(hdl)
1204 1.7.2.2 bouyer void *hdl;
1205 1.7.2.2 bouyer {
1206 1.7.2.2 bouyer return (AUDIO_PROP_INDEPENDENT | AUDIO_PROP_FULLDUPLEX);
1207 1.7.2.2 bouyer #ifdef notyet
1208 1.7.2.2 bouyer /* XXX
1209 1.7.2.2 bouyer * How can I mmap ?
1210 1.7.2.2 bouyer */
1211 1.7.2.2 bouyer AUDIO_PROP_MMAP
1212 1.7.2.2 bouyer #endif
1213 1.7.2.2 bouyer
1214 1.7.2.2 bouyer }
1215 1.7.2.2 bouyer
1216 1.7.2.2 bouyer int
1217 1.7.2.2 bouyer cs4280_mixer_get_port(addr, cp)
1218 1.7.2.2 bouyer void *addr;
1219 1.7.2.2 bouyer mixer_ctrl_t *cp;
1220 1.7.2.2 bouyer {
1221 1.7.2.2 bouyer struct cs4280_softc *sc = addr;
1222 1.7.2.2 bouyer
1223 1.7.2.2 bouyer return (sc->codec_if->vtbl->mixer_get_port(sc->codec_if, cp));
1224 1.7.2.2 bouyer }
1225 1.7.2.2 bouyer
1226 1.7.2.2 bouyer paddr_t
1227 1.7.2.2 bouyer cs4280_mappage(addr, mem, off, prot)
1228 1.7.2.2 bouyer void *addr;
1229 1.7.2.2 bouyer void *mem;
1230 1.7.2.2 bouyer off_t off;
1231 1.7.2.2 bouyer int prot;
1232 1.7.2.2 bouyer {
1233 1.7.2.2 bouyer struct cs4280_softc *sc = addr;
1234 1.7.2.2 bouyer struct cs4280_dma *p;
1235 1.7.2.2 bouyer
1236 1.7.2.2 bouyer if (off < 0)
1237 1.7.2.2 bouyer return (-1);
1238 1.7.2.2 bouyer for (p = sc->sc_dmas; p && BUFADDR(p) != mem; p = p->next)
1239 1.7.2.2 bouyer ;
1240 1.7.2.2 bouyer if (!p) {
1241 1.7.2.2 bouyer DPRINTF(("cs4280_mappage: bad buffer address\n"));
1242 1.7.2.2 bouyer return (-1);
1243 1.7.2.2 bouyer }
1244 1.7.2.2 bouyer return (bus_dmamem_mmap(sc->sc_dmatag, p->segs, p->nsegs,
1245 1.7.2.2 bouyer off, prot, BUS_DMA_WAITOK));
1246 1.7.2.2 bouyer }
1247 1.7.2.2 bouyer
1248 1.7.2.2 bouyer
1249 1.7.2.2 bouyer int
1250 1.7.2.2 bouyer cs4280_query_devinfo(addr, dip)
1251 1.7.2.2 bouyer void *addr;
1252 1.7.2.2 bouyer mixer_devinfo_t *dip;
1253 1.7.2.2 bouyer {
1254 1.7.2.2 bouyer struct cs4280_softc *sc = addr;
1255 1.7.2.2 bouyer
1256 1.7.2.2 bouyer return (sc->codec_if->vtbl->query_devinfo(sc->codec_if, dip));
1257 1.7.2.2 bouyer }
1258 1.7.2.2 bouyer
1259 1.7.2.2 bouyer int
1260 1.7.2.2 bouyer cs4280_get_portnum_by_name(sc, class, device, qualifier)
1261 1.7.2.2 bouyer struct cs4280_softc *sc;
1262 1.7.2.2 bouyer char *class, *device, *qualifier;
1263 1.7.2.2 bouyer {
1264 1.7.2.2 bouyer return (sc->codec_if->vtbl->get_portnum_by_name(sc->codec_if, class,
1265 1.7.2.2 bouyer device, qualifier));
1266 1.7.2.2 bouyer }
1267 1.7.2.2 bouyer
1268 1.7.2.2 bouyer int
1269 1.7.2.2 bouyer cs4280_halt_output(addr)
1270 1.7.2.2 bouyer void *addr;
1271 1.7.2.2 bouyer {
1272 1.7.2.2 bouyer struct cs4280_softc *sc = addr;
1273 1.7.2.2 bouyer u_int32_t mem;
1274 1.7.2.2 bouyer
1275 1.7.2.2 bouyer mem = BA1READ4(sc, CS4280_PCTL);
1276 1.7.2.2 bouyer BA1WRITE4(sc, CS4280_PCTL, mem & ~PCTL_MASK);
1277 1.7.2.2 bouyer #ifdef DIAGNOSTIC
1278 1.7.2.2 bouyer sc->sc_prun = 0;
1279 1.7.2.2 bouyer #endif
1280 1.7.2.2 bouyer return (0);
1281 1.7.2.2 bouyer }
1282 1.7.2.2 bouyer
1283 1.7.2.2 bouyer int
1284 1.7.2.2 bouyer cs4280_halt_input(addr)
1285 1.7.2.2 bouyer void *addr;
1286 1.7.2.2 bouyer {
1287 1.7.2.2 bouyer struct cs4280_softc *sc = addr;
1288 1.7.2.2 bouyer u_int32_t mem;
1289 1.7.2.2 bouyer
1290 1.7.2.2 bouyer mem = BA1READ4(sc, CS4280_CCTL);
1291 1.7.2.2 bouyer BA1WRITE4(sc, CS4280_CCTL, mem & ~CCTL_MASK);
1292 1.7.2.2 bouyer #ifdef DIAGNOSTIC
1293 1.7.2.2 bouyer sc->sc_rrun = 0;
1294 1.7.2.2 bouyer #endif
1295 1.7.2.2 bouyer return (0);
1296 1.7.2.2 bouyer }
1297 1.7.2.2 bouyer
1298 1.7.2.2 bouyer int
1299 1.7.2.2 bouyer cs4280_getdev(addr, retp)
1300 1.7.2.2 bouyer void *addr;
1301 1.7.2.2 bouyer struct audio_device *retp;
1302 1.7.2.2 bouyer {
1303 1.7.2.2 bouyer *retp = cs4280_device;
1304 1.7.2.2 bouyer return (0);
1305 1.7.2.2 bouyer }
1306 1.7.2.2 bouyer
1307 1.7.2.2 bouyer int
1308 1.7.2.2 bouyer cs4280_mixer_set_port(addr, cp)
1309 1.7.2.2 bouyer void *addr;
1310 1.7.2.2 bouyer mixer_ctrl_t *cp;
1311 1.7.2.2 bouyer {
1312 1.7.2.2 bouyer struct cs4280_softc *sc = addr;
1313 1.7.2.2 bouyer int val;
1314 1.7.2.2 bouyer
1315 1.7.2.2 bouyer val = sc->codec_if->vtbl->mixer_set_port(sc->codec_if, cp);
1316 1.7.2.2 bouyer DPRINTFN(3,("mixer_set_port: val=%d\n", val));
1317 1.7.2.2 bouyer return (val);
1318 1.7.2.2 bouyer }
1319 1.7.2.2 bouyer
1320 1.7.2.2 bouyer
1321 1.7.2.2 bouyer int
1322 1.7.2.2 bouyer cs4280_freemem(sc, p)
1323 1.7.2.2 bouyer struct cs4280_softc *sc;
1324 1.7.2.2 bouyer struct cs4280_dma *p;
1325 1.7.2.2 bouyer {
1326 1.7.2.2 bouyer bus_dmamap_unload(sc->sc_dmatag, p->map);
1327 1.7.2.2 bouyer bus_dmamap_destroy(sc->sc_dmatag, p->map);
1328 1.7.2.2 bouyer bus_dmamem_unmap(sc->sc_dmatag, p->addr, p->size);
1329 1.7.2.2 bouyer bus_dmamem_free(sc->sc_dmatag, p->segs, p->nsegs);
1330 1.7.2.2 bouyer return (0);
1331 1.7.2.2 bouyer }
1332 1.7.2.2 bouyer
1333 1.7.2.2 bouyer int
1334 1.7.2.2 bouyer cs4280_allocmem(sc, size, align, p)
1335 1.7.2.2 bouyer struct cs4280_softc *sc;
1336 1.7.2.2 bouyer size_t size;
1337 1.7.2.2 bouyer size_t align;
1338 1.7.2.2 bouyer struct cs4280_dma *p;
1339 1.7.2.2 bouyer {
1340 1.7.2.2 bouyer int error;
1341 1.7.2.2 bouyer
1342 1.7.2.2 bouyer /* XXX */
1343 1.7.2.2 bouyer p->size = size;
1344 1.7.2.2 bouyer error = bus_dmamem_alloc(sc->sc_dmatag, p->size, align, 0,
1345 1.7.2.2 bouyer p->segs, sizeof(p->segs)/sizeof(p->segs[0]),
1346 1.7.2.2 bouyer &p->nsegs, BUS_DMA_NOWAIT);
1347 1.7.2.2 bouyer if (error) {
1348 1.7.2.2 bouyer printf("%s: unable to allocate dma, error=%d\n",
1349 1.7.2.2 bouyer sc->sc_dev.dv_xname, error);
1350 1.7.2.2 bouyer return (error);
1351 1.7.2.2 bouyer }
1352 1.7.2.2 bouyer
1353 1.7.2.2 bouyer error = bus_dmamem_map(sc->sc_dmatag, p->segs, p->nsegs, p->size,
1354 1.7.2.2 bouyer &p->addr, BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
1355 1.7.2.2 bouyer if (error) {
1356 1.7.2.2 bouyer printf("%s: unable to map dma, error=%d\n",
1357 1.7.2.2 bouyer sc->sc_dev.dv_xname, error);
1358 1.7.2.2 bouyer goto free;
1359 1.7.2.2 bouyer }
1360 1.7.2.2 bouyer
1361 1.7.2.2 bouyer error = bus_dmamap_create(sc->sc_dmatag, p->size, 1, p->size,
1362 1.7.2.2 bouyer 0, BUS_DMA_NOWAIT, &p->map);
1363 1.7.2.2 bouyer if (error) {
1364 1.7.2.2 bouyer printf("%s: unable to create dma map, error=%d\n",
1365 1.7.2.2 bouyer sc->sc_dev.dv_xname, error);
1366 1.7.2.2 bouyer goto unmap;
1367 1.7.2.2 bouyer }
1368 1.7.2.2 bouyer
1369 1.7.2.2 bouyer error = bus_dmamap_load(sc->sc_dmatag, p->map, p->addr, p->size, NULL,
1370 1.7.2.2 bouyer BUS_DMA_NOWAIT);
1371 1.7.2.2 bouyer if (error) {
1372 1.7.2.2 bouyer printf("%s: unable to load dma map, error=%d\n",
1373 1.7.2.2 bouyer sc->sc_dev.dv_xname, error);
1374 1.7.2.2 bouyer goto destroy;
1375 1.7.2.2 bouyer }
1376 1.7.2.2 bouyer return (0);
1377 1.7.2.2 bouyer
1378 1.7.2.2 bouyer destroy:
1379 1.7.2.2 bouyer bus_dmamap_destroy(sc->sc_dmatag, p->map);
1380 1.7.2.2 bouyer unmap:
1381 1.7.2.2 bouyer bus_dmamem_unmap(sc->sc_dmatag, p->addr, p->size);
1382 1.7.2.2 bouyer free:
1383 1.7.2.2 bouyer bus_dmamem_free(sc->sc_dmatag, p->segs, p->nsegs);
1384 1.7.2.2 bouyer return (error);
1385 1.7.2.2 bouyer }
1386 1.7.2.2 bouyer
1387 1.7.2.2 bouyer
1388 1.7.2.2 bouyer void *
1389 1.7.2.2 bouyer cs4280_malloc(addr, direction, size, pool, flags)
1390 1.7.2.2 bouyer void *addr;
1391 1.7.2.2 bouyer int direction;
1392 1.7.2.2 bouyer size_t size;
1393 1.7.2.2 bouyer int pool, flags;
1394 1.7.2.2 bouyer {
1395 1.7.2.2 bouyer struct cs4280_softc *sc = addr;
1396 1.7.2.2 bouyer struct cs4280_dma *p;
1397 1.7.2.2 bouyer caddr_t q;
1398 1.7.2.2 bouyer int error;
1399 1.7.2.2 bouyer
1400 1.7.2.2 bouyer DPRINTFN(5,("cs4280_malloc: size=%d pool=%d flags=%d\n", size, pool, flags));
1401 1.7.2.2 bouyer q = malloc(size, pool, flags);
1402 1.7.2.2 bouyer if (!q)
1403 1.7.2.2 bouyer return (0);
1404 1.7.2.2 bouyer p = malloc(sizeof(*p), pool, flags);
1405 1.7.2.2 bouyer if (!p) {
1406 1.7.2.2 bouyer free(q,pool);
1407 1.7.2.2 bouyer return (0);
1408 1.7.2.2 bouyer }
1409 1.7.2.2 bouyer /*
1410 1.7.2.2 bouyer * cs4280 has fixed 4kB buffer
1411 1.7.2.2 bouyer */
1412 1.7.2.2 bouyer error = cs4280_allocmem(sc, CS4280_DCHUNK, CS4280_DALIGN, p);
1413 1.7.2.2 bouyer
1414 1.7.2.2 bouyer if (error) {
1415 1.7.2.2 bouyer free(q, pool);
1416 1.7.2.2 bouyer free(p, pool);
1417 1.7.2.2 bouyer return (0);
1418 1.7.2.2 bouyer }
1419 1.7.2.2 bouyer
1420 1.7.2.2 bouyer p->next = sc->sc_dmas;
1421 1.7.2.2 bouyer sc->sc_dmas = p;
1422 1.7.2.2 bouyer p->dum = q; /* return to audio driver */
1423 1.7.2.2 bouyer
1424 1.7.2.2 bouyer return (p->dum);
1425 1.7.2.2 bouyer }
1426 1.7.2.2 bouyer
1427 1.7.2.2 bouyer void
1428 1.7.2.2 bouyer cs4280_free(addr, ptr, pool)
1429 1.7.2.2 bouyer void *addr;
1430 1.7.2.2 bouyer void *ptr;
1431 1.7.2.2 bouyer int pool;
1432 1.7.2.2 bouyer {
1433 1.7.2.2 bouyer struct cs4280_softc *sc = addr;
1434 1.7.2.2 bouyer struct cs4280_dma **pp, *p;
1435 1.7.2.2 bouyer
1436 1.7.2.2 bouyer for (pp = &sc->sc_dmas; (p = *pp) != NULL; pp = &p->next) {
1437 1.7.2.2 bouyer if (BUFADDR(p) == ptr) {
1438 1.7.2.2 bouyer cs4280_freemem(sc, p);
1439 1.7.2.2 bouyer *pp = p->next;
1440 1.7.2.2 bouyer free(p->dum, pool);
1441 1.7.2.2 bouyer free(p, pool);
1442 1.7.2.2 bouyer return;
1443 1.7.2.2 bouyer }
1444 1.7.2.2 bouyer }
1445 1.7.2.2 bouyer }
1446 1.7.2.2 bouyer
1447 1.7.2.2 bouyer int
1448 1.7.2.2 bouyer cs4280_trigger_output(addr, start, end, blksize, intr, arg, param)
1449 1.7.2.2 bouyer void *addr;
1450 1.7.2.2 bouyer void *start, *end;
1451 1.7.2.2 bouyer int blksize;
1452 1.7.2.2 bouyer void (*intr) __P((void *));
1453 1.7.2.2 bouyer void *arg;
1454 1.7.2.2 bouyer struct audio_params *param;
1455 1.7.2.2 bouyer {
1456 1.7.2.2 bouyer struct cs4280_softc *sc = addr;
1457 1.7.2.2 bouyer u_int32_t pfie, pctl, mem, pdtc;
1458 1.7.2.2 bouyer struct cs4280_dma *p;
1459 1.7.2.2 bouyer
1460 1.7.2.2 bouyer #ifdef DIAGNOSTIC
1461 1.7.2.2 bouyer if (sc->sc_prun)
1462 1.7.2.2 bouyer printf("cs4280_trigger_output: already running\n");
1463 1.7.2.2 bouyer sc->sc_prun = 1;
1464 1.7.2.2 bouyer #endif
1465 1.7.2.2 bouyer
1466 1.7.2.2 bouyer DPRINTF(("cs4280_trigger_output: sc=%p start=%p end=%p "
1467 1.7.2.2 bouyer "blksize=%d intr=%p(%p)\n", addr, start, end, blksize, intr, arg));
1468 1.7.2.2 bouyer sc->sc_pintr = intr;
1469 1.7.2.2 bouyer sc->sc_parg = arg;
1470 1.7.2.2 bouyer
1471 1.7.2.2 bouyer /* stop playback DMA */
1472 1.7.2.2 bouyer mem = BA1READ4(sc, CS4280_PCTL);
1473 1.7.2.2 bouyer BA1WRITE4(sc, CS4280_PCTL, mem & ~PCTL_MASK);
1474 1.7.2.2 bouyer
1475 1.7.2.2 bouyer /* setup PDTC */
1476 1.7.2.2 bouyer pdtc = BA1READ4(sc, CS4280_PDTC);
1477 1.7.2.2 bouyer pdtc &= ~PDTC_MASK;
1478 1.7.2.2 bouyer pdtc |= CS4280_MK_PDTC(param->precision * param->channels);
1479 1.7.2.2 bouyer BA1WRITE4(sc, CS4280_PDTC, pdtc);
1480 1.7.2.2 bouyer
1481 1.7.2.2 bouyer DPRINTF(("param: precision=%d factor=%d channels=%d encoding=%d\n",
1482 1.7.2.2 bouyer param->precision, param->factor, param->channels,
1483 1.7.2.2 bouyer param->encoding));
1484 1.7.2.2 bouyer for (p = sc->sc_dmas; p != NULL && BUFADDR(p) != start; p = p->next)
1485 1.7.2.2 bouyer ;
1486 1.7.2.2 bouyer if (p == NULL) {
1487 1.7.2.2 bouyer printf("cs4280_trigger_output: bad addr %p\n", start);
1488 1.7.2.2 bouyer return (EINVAL);
1489 1.7.2.2 bouyer }
1490 1.7.2.2 bouyer if (DMAADDR(p) % CS4280_DALIGN != 0 ) {
1491 1.7.2.2 bouyer printf("cs4280_trigger_output: DMAADDR(p)=0x%lx does not start"
1492 1.7.2.2 bouyer "4kB align\n", DMAADDR(p));
1493 1.7.2.2 bouyer return (EINVAL);
1494 1.7.2.2 bouyer }
1495 1.7.2.2 bouyer
1496 1.7.2.2 bouyer sc->sc_pcount = blksize / CS4280_ICHUNK; /* CS4280_ICHUNK is fixed hardware blksize*/
1497 1.7.2.2 bouyer sc->sc_ps = (char *)start;
1498 1.7.2.2 bouyer sc->sc_pe = (char *)end;
1499 1.7.2.2 bouyer sc->sc_pdma = p;
1500 1.7.2.2 bouyer sc->sc_pbuf = KERNADDR(p);
1501 1.7.2.2 bouyer sc->sc_pi = 0;
1502 1.7.2.2 bouyer sc->sc_pn = sc->sc_ps;
1503 1.7.2.2 bouyer if (blksize >= CS4280_DCHUNK) {
1504 1.7.2.2 bouyer sc->sc_pn = sc->sc_ps + CS4280_DCHUNK;
1505 1.7.2.2 bouyer memcpy(sc->sc_pbuf, start, CS4280_DCHUNK);
1506 1.7.2.2 bouyer ++sc->sc_pi;
1507 1.7.2.2 bouyer } else {
1508 1.7.2.2 bouyer sc->sc_pn = sc->sc_ps + CS4280_ICHUNK;
1509 1.7.2.2 bouyer memcpy(sc->sc_pbuf, start, CS4280_ICHUNK);
1510 1.7.2.2 bouyer }
1511 1.7.2.2 bouyer
1512 1.7.2.2 bouyer /* initiate playback dma */
1513 1.7.2.2 bouyer BA1WRITE4(sc, CS4280_PBA, DMAADDR(p));
1514 1.7.2.2 bouyer
1515 1.7.2.2 bouyer /* set PFIE */
1516 1.7.2.2 bouyer pfie = BA1READ4(sc, CS4280_PFIE) & ~PFIE_MASK;
1517 1.7.2.2 bouyer
1518 1.7.2.2 bouyer if (param->precision * param->factor == 8)
1519 1.7.2.2 bouyer pfie |= PFIE_8BIT;
1520 1.7.2.2 bouyer if (param->channels == 1)
1521 1.7.2.2 bouyer pfie |= PFIE_MONO;
1522 1.7.2.2 bouyer
1523 1.7.2.2 bouyer if (param->encoding == AUDIO_ENCODING_ULINEAR_BE ||
1524 1.7.2.2 bouyer param->encoding == AUDIO_ENCODING_SLINEAR_BE)
1525 1.7.2.2 bouyer pfie |= PFIE_SWAPPED;
1526 1.7.2.2 bouyer if (param->encoding == AUDIO_ENCODING_ULINEAR_BE ||
1527 1.7.2.2 bouyer param->encoding == AUDIO_ENCODING_ULINEAR_LE)
1528 1.7.2.2 bouyer pfie |= PFIE_UNSIGNED;
1529 1.7.2.2 bouyer
1530 1.7.2.2 bouyer BA1WRITE4(sc, CS4280_PFIE, pfie | PFIE_PI_ENABLE);
1531 1.7.2.2 bouyer
1532 1.7.2.2 bouyer cs4280_set_dac_rate(sc, param->sample_rate);
1533 1.7.2.2 bouyer
1534 1.7.2.2 bouyer pctl = BA1READ4(sc, CS4280_PCTL) & ~PCTL_MASK;
1535 1.7.2.2 bouyer pctl |= sc->pctl;
1536 1.7.2.2 bouyer BA1WRITE4(sc, CS4280_PCTL, pctl);
1537 1.7.2.2 bouyer return (0);
1538 1.7.2.2 bouyer }
1539 1.7.2.2 bouyer
1540 1.7.2.2 bouyer int
1541 1.7.2.2 bouyer cs4280_trigger_input(addr, start, end, blksize, intr, arg, param)
1542 1.7.2.2 bouyer void *addr;
1543 1.7.2.2 bouyer void *start, *end;
1544 1.7.2.2 bouyer int blksize;
1545 1.7.2.2 bouyer void (*intr) __P((void *));
1546 1.7.2.2 bouyer void *arg;
1547 1.7.2.2 bouyer struct audio_params *param;
1548 1.7.2.2 bouyer {
1549 1.7.2.2 bouyer struct cs4280_softc *sc = addr;
1550 1.7.2.2 bouyer u_int32_t cctl, cie;
1551 1.7.2.2 bouyer struct cs4280_dma *p;
1552 1.7.2.2 bouyer
1553 1.7.2.2 bouyer #ifdef DIAGNOSTIC
1554 1.7.2.2 bouyer if (sc->sc_rrun)
1555 1.7.2.2 bouyer printf("cs4280_trigger_input: already running\n");
1556 1.7.2.2 bouyer sc->sc_rrun = 1;
1557 1.7.2.2 bouyer #endif
1558 1.7.2.2 bouyer DPRINTF(("cs4280_trigger_input: sc=%p start=%p end=%p "
1559 1.7.2.2 bouyer "blksize=%d intr=%p(%p)\n", addr, start, end, blksize, intr, arg));
1560 1.7.2.2 bouyer sc->sc_rintr = intr;
1561 1.7.2.2 bouyer sc->sc_rarg = arg;
1562 1.7.2.2 bouyer
1563 1.7.2.2 bouyer sc->sc_ri = 0;
1564 1.7.2.2 bouyer sc->sc_rcount = blksize / CS4280_ICHUNK; /* CS4280_ICHUNK is fixed hardware blksize*/
1565 1.7.2.2 bouyer sc->sc_rs = (char *)start;
1566 1.7.2.2 bouyer sc->sc_re = (char *)end;
1567 1.7.2.2 bouyer sc->sc_rn = sc->sc_rs;
1568 1.7.2.2 bouyer
1569 1.7.2.2 bouyer /* setup format information for internal converter */
1570 1.7.2.2 bouyer sc->sc_rparam = 0;
1571 1.7.2.2 bouyer if (param->precision == 8) {
1572 1.7.2.2 bouyer sc->sc_rparam += CF_8BIT;
1573 1.7.2.2 bouyer sc->sc_rcount <<= 1;
1574 1.7.2.2 bouyer }
1575 1.7.2.2 bouyer if (param->channels == 1) {
1576 1.7.2.2 bouyer sc->sc_rparam += CF_MONO;
1577 1.7.2.2 bouyer sc->sc_rcount <<= 1;
1578 1.7.2.2 bouyer }
1579 1.7.2.2 bouyer
1580 1.7.2.2 bouyer /* stop capture DMA */
1581 1.7.2.2 bouyer cctl = BA1READ4(sc, CS4280_CCTL) & ~CCTL_MASK;
1582 1.7.2.2 bouyer BA1WRITE4(sc, CS4280_CCTL, cctl);
1583 1.7.2.2 bouyer
1584 1.7.2.2 bouyer for (p = sc->sc_dmas; p && BUFADDR(p) != start; p = p->next)
1585 1.7.2.2 bouyer ;
1586 1.7.2.2 bouyer if (!p) {
1587 1.7.2.2 bouyer printf("cs4280_trigger_input: bad addr %p\n", start);
1588 1.7.2.2 bouyer return (EINVAL);
1589 1.7.2.2 bouyer }
1590 1.7.2.2 bouyer if (DMAADDR(p) % CS4280_DALIGN != 0) {
1591 1.7.2.2 bouyer printf("cs4280_trigger_input: DMAADDR(p)=0x%lx does not start"
1592 1.7.2.2 bouyer "4kB align\n", DMAADDR(p));
1593 1.7.2.2 bouyer return (EINVAL);
1594 1.7.2.2 bouyer }
1595 1.7.2.2 bouyer sc->sc_rdma = p;
1596 1.7.2.2 bouyer sc->sc_rbuf = KERNADDR(p);
1597 1.7.2.2 bouyer
1598 1.7.2.2 bouyer /* initiate capture dma */
1599 1.7.2.2 bouyer BA1WRITE4(sc, CS4280_CBA, DMAADDR(p));
1600 1.7.2.2 bouyer
1601 1.7.2.2 bouyer /* set CIE */
1602 1.7.2.2 bouyer cie = BA1READ4(sc, CS4280_CIE) & ~CIE_CI_MASK;
1603 1.7.2.2 bouyer BA1WRITE4(sc, CS4280_CIE, cie | CIE_CI_ENABLE);
1604 1.7.2.2 bouyer
1605 1.7.2.2 bouyer cs4280_set_adc_rate(sc, param->sample_rate);
1606 1.7.2.2 bouyer
1607 1.7.2.2 bouyer cctl = BA1READ4(sc, CS4280_CCTL) & ~CCTL_MASK;
1608 1.7.2.2 bouyer cctl |= sc->cctl;
1609 1.7.2.2 bouyer BA1WRITE4(sc, CS4280_CCTL, cctl);
1610 1.7.2.2 bouyer return (0);
1611 1.7.2.2 bouyer }
1612 1.7.2.2 bouyer
1613 1.7.2.2 bouyer int
1614 1.7.2.2 bouyer cs4280_init(sc, init)
1615 1.7.2.2 bouyer struct cs4280_softc *sc;
1616 1.7.2.2 bouyer int init;
1617 1.7.2.2 bouyer {
1618 1.7.2.2 bouyer int n;
1619 1.7.2.2 bouyer u_int32_t mem;
1620 1.7.2.2 bouyer
1621 1.7.2.2 bouyer /* Start PLL out in known state */
1622 1.7.2.2 bouyer BA0WRITE4(sc, CS4280_CLKCR1, 0);
1623 1.7.2.2 bouyer /* Start serial ports out in known state */
1624 1.7.2.2 bouyer BA0WRITE4(sc, CS4280_SERMC1, 0);
1625 1.7.2.2 bouyer
1626 1.7.2.2 bouyer /* Specify type of CODEC */
1627 1.7.2.2 bouyer /* XXX should not be here */
1628 1.7.2.2 bouyer #define SERACC_CODEC_TYPE_1_03
1629 1.7.2.2 bouyer #ifdef SERACC_CODEC_TYPE_1_03
1630 1.7.2.2 bouyer BA0WRITE4(sc, CS4280_SERACC, SERACC_HSP | SERACC_CTYPE_1_03); /* AC 97 1.03 */
1631 1.7.2.2 bouyer #else
1632 1.7.2.2 bouyer BA0WRITE4(sc, CS4280_SERACC, SERACC_HSP | SERACC_CTYPE_2_0); /* AC 97 2.0 */
1633 1.7.2.2 bouyer #endif
1634 1.7.2.2 bouyer
1635 1.7.2.2 bouyer /* Reset codec */
1636 1.7.2.2 bouyer BA0WRITE4(sc, CS4280_ACCTL, 0);
1637 1.7.2.2 bouyer delay(100); /* delay 100us */
1638 1.7.2.2 bouyer BA0WRITE4(sc, CS4280_ACCTL, ACCTL_RSTN);
1639 1.7.2.2 bouyer
1640 1.7.2.2 bouyer /* Enable AC-link sync generation */
1641 1.7.2.2 bouyer BA0WRITE4(sc, CS4280_ACCTL, ACCTL_ESYN | ACCTL_RSTN);
1642 1.7.2.2 bouyer delay(50*1000); /* delay 50ms */
1643 1.7.2.2 bouyer
1644 1.7.2.2 bouyer /* Set the serial port timing configuration */
1645 1.7.2.2 bouyer BA0WRITE4(sc, CS4280_SERMC1, SERMC1_PTC_AC97);
1646 1.7.2.2 bouyer
1647 1.7.2.2 bouyer /* Setup clock control */
1648 1.7.2.2 bouyer BA0WRITE4(sc, CS4280_PLLCC, PLLCC_CDR_STATE|PLLCC_LPF_STATE);
1649 1.7.2.2 bouyer BA0WRITE4(sc, CS4280_PLLM, PLLM_STATE);
1650 1.7.2.2 bouyer BA0WRITE4(sc, CS4280_CLKCR2, CLKCR2_PDIVS_8);
1651 1.7.2.2 bouyer
1652 1.7.2.2 bouyer /* Power up the PLL */
1653 1.7.2.2 bouyer BA0WRITE4(sc, CS4280_CLKCR1, CLKCR1_PLLP);
1654 1.7.2.2 bouyer delay(50*1000); /* delay 50ms */
1655 1.7.2.2 bouyer
1656 1.7.2.2 bouyer /* Turn on clock */
1657 1.7.2.2 bouyer mem = BA0READ4(sc, CS4280_CLKCR1) | CLKCR1_SWCE;
1658 1.7.2.2 bouyer BA0WRITE4(sc, CS4280_CLKCR1, mem);
1659 1.7.2.2 bouyer
1660 1.7.2.2 bouyer /* Set the serial port FIFO pointer to the
1661 1.7.2.2 bouyer * first sample in FIFO. (not documented) */
1662 1.7.2.2 bouyer cs4280_clear_fifos(sc);
1663 1.7.2.2 bouyer
1664 1.7.2.2 bouyer #if 0
1665 1.7.2.2 bouyer /* Set the serial port FIFO pointer to the first sample in the FIFO */
1666 1.7.2.2 bouyer BA0WRITE4(sc, CS4280_SERBSP, 0);
1667 1.7.2.2 bouyer #endif
1668 1.7.2.2 bouyer
1669 1.7.2.2 bouyer /* Configure the serial port */
1670 1.7.2.2 bouyer BA0WRITE4(sc, CS4280_SERC1, SERC1_SO1EN | SERC1_SO1F_AC97);
1671 1.7.2.2 bouyer BA0WRITE4(sc, CS4280_SERC2, SERC2_SI1EN | SERC2_SI1F_AC97);
1672 1.7.2.2 bouyer BA0WRITE4(sc, CS4280_SERMC1, SERMC1_MSPE | SERMC1_PTC_AC97);
1673 1.7.2.2 bouyer
1674 1.7.2.2 bouyer /* Wait for CODEC ready */
1675 1.7.2.2 bouyer n = 0;
1676 1.7.2.2 bouyer while ((BA0READ4(sc, CS4280_ACSTS) & ACSTS_CRDY) == 0) {
1677 1.7.2.2 bouyer delay(125);
1678 1.7.2.2 bouyer if (++n > 1000) {
1679 1.7.2.2 bouyer printf("%s: codec ready timeout\n",
1680 1.7.2.2 bouyer sc->sc_dev.dv_xname);
1681 1.7.2.2 bouyer return(1);
1682 1.7.2.2 bouyer }
1683 1.7.2.2 bouyer }
1684 1.7.2.2 bouyer
1685 1.7.2.2 bouyer /* Assert valid frame signal */
1686 1.7.2.2 bouyer BA0WRITE4(sc, CS4280_ACCTL, ACCTL_VFRM | ACCTL_ESYN | ACCTL_RSTN);
1687 1.7.2.2 bouyer
1688 1.7.2.2 bouyer /* Wait for valid AC97 input slot */
1689 1.7.2.2 bouyer n = 0;
1690 1.7.2.2 bouyer while ((BA0READ4(sc, CS4280_ACISV) & (ACISV_ISV3 | ACISV_ISV4)) !=
1691 1.7.2.2 bouyer (ACISV_ISV3 | ACISV_ISV4)) {
1692 1.7.2.2 bouyer delay(1000);
1693 1.7.2.2 bouyer if (++n > 1000) {
1694 1.7.2.2 bouyer printf("AC97 inputs slot ready timeout\n");
1695 1.7.2.2 bouyer return(1);
1696 1.7.2.2 bouyer }
1697 1.7.2.2 bouyer }
1698 1.7.2.2 bouyer
1699 1.7.2.2 bouyer /* Set AC97 output slot valid signals */
1700 1.7.2.2 bouyer BA0WRITE4(sc, CS4280_ACOSV, ACOSV_SLV3 | ACOSV_SLV4);
1701 1.7.2.2 bouyer
1702 1.7.2.2 bouyer /* reset the processor */
1703 1.7.2.2 bouyer cs4280_reset(sc);
1704 1.7.2.2 bouyer
1705 1.7.2.2 bouyer /* Download the image to the processor */
1706 1.7.2.2 bouyer if (cs4280_download_image(sc) != 0) {
1707 1.7.2.2 bouyer printf("%s: image download error\n", sc->sc_dev.dv_xname);
1708 1.7.2.2 bouyer return(1);
1709 1.7.2.2 bouyer }
1710 1.7.2.2 bouyer
1711 1.7.2.2 bouyer /* Save playback parameter and then write zero.
1712 1.7.2.2 bouyer * this ensures that DMA doesn't immediately occur upon
1713 1.7.2.2 bouyer * starting the processor core
1714 1.7.2.2 bouyer */
1715 1.7.2.2 bouyer mem = BA1READ4(sc, CS4280_PCTL);
1716 1.7.2.2 bouyer sc->pctl = mem & PCTL_MASK; /* save startup value */
1717 1.7.2.2 bouyer cs4280_halt_output(sc);
1718 1.7.2.2 bouyer
1719 1.7.2.2 bouyer /* Save capture parameter and then write zero.
1720 1.7.2.2 bouyer * this ensures that DMA doesn't immediately occur upon
1721 1.7.2.2 bouyer * starting the processor core
1722 1.7.2.2 bouyer */
1723 1.7.2.2 bouyer mem = BA1READ4(sc, CS4280_CCTL);
1724 1.7.2.2 bouyer sc->cctl = mem & CCTL_MASK; /* save startup value */
1725 1.7.2.2 bouyer cs4280_halt_input(sc);
1726 1.7.2.2 bouyer
1727 1.7.2.2 bouyer /* Processor Startup Procedure */
1728 1.7.2.2 bouyer BA1WRITE4(sc, CS4280_FRMT, FRMT_FTV);
1729 1.7.2.2 bouyer BA1WRITE4(sc, CS4280_SPCR, SPCR_RUN | SPCR_RUNFR | SPCR_DRQEN);
1730 1.7.2.2 bouyer
1731 1.7.2.2 bouyer /* Monitor RUNFR bit in SPCR for 1 to 0 transition */
1732 1.7.2.2 bouyer n = 0;
1733 1.7.2.2 bouyer while (BA1READ4(sc, CS4280_SPCR) & SPCR_RUNFR) {
1734 1.7.2.2 bouyer delay(10);
1735 1.7.2.2 bouyer if (++n > 1000) {
1736 1.7.2.2 bouyer printf("SPCR 1->0 transition timeout\n");
1737 1.7.2.2 bouyer return(1);
1738 1.7.2.2 bouyer }
1739 1.7.2.2 bouyer }
1740 1.7.2.2 bouyer
1741 1.7.2.2 bouyer n = 0;
1742 1.7.2.2 bouyer while (!(BA1READ4(sc, CS4280_SPCS) & SPCS_SPRUN)) {
1743 1.7.2.2 bouyer delay(10);
1744 1.7.2.2 bouyer if (++n > 1000) {
1745 1.7.2.2 bouyer printf("SPCS 0->1 transition timeout\n");
1746 1.7.2.2 bouyer return(1);
1747 1.7.2.2 bouyer }
1748 1.7.2.2 bouyer }
1749 1.7.2.2 bouyer /* Processor is now running !!! */
1750 1.7.2.2 bouyer
1751 1.7.2.2 bouyer /* Setup volume */
1752 1.7.2.2 bouyer BA1WRITE4(sc, CS4280_PVOL, 0x80008000);
1753 1.7.2.2 bouyer BA1WRITE4(sc, CS4280_CVOL, 0x80008000);
1754 1.7.2.2 bouyer
1755 1.7.2.2 bouyer /* Interrupt enable */
1756 1.7.2.2 bouyer BA0WRITE4(sc, CS4280_HICR, HICR_IEV|HICR_CHGM);
1757 1.7.2.2 bouyer
1758 1.7.2.2 bouyer /* playback interrupt enable */
1759 1.7.2.2 bouyer mem = BA1READ4(sc, CS4280_PFIE) & ~PFIE_PI_MASK;
1760 1.7.2.2 bouyer mem |= PFIE_PI_ENABLE;
1761 1.7.2.2 bouyer BA1WRITE4(sc, CS4280_PFIE, mem);
1762 1.7.2.2 bouyer /* capture interrupt enable */
1763 1.7.2.2 bouyer mem = BA1READ4(sc, CS4280_CIE) & ~CIE_CI_MASK;
1764 1.7.2.2 bouyer mem |= CIE_CI_ENABLE;
1765 1.7.2.2 bouyer BA1WRITE4(sc, CS4280_CIE, mem);
1766 1.7.2.2 bouyer
1767 1.7.2.2 bouyer #if NMIDI > 0
1768 1.7.2.2 bouyer /* Reset midi port */
1769 1.7.2.2 bouyer mem = BA0READ4(sc, CS4280_MIDCR) & ~MIDCR_MASK;
1770 1.7.2.2 bouyer BA0WRITE4(sc, CS4280_MIDCR, mem | MIDCR_MRST);
1771 1.7.2.2 bouyer DPRINTF(("midi reset: 0x%x\n", BA0READ4(sc, CS4280_MIDCR)));
1772 1.7.2.2 bouyer /* midi interrupt enable */
1773 1.7.2.2 bouyer mem |= MIDCR_TXE | MIDCR_RXE | MIDCR_RIE | MIDCR_TIE;
1774 1.7.2.2 bouyer BA0WRITE4(sc, CS4280_MIDCR, mem);
1775 1.7.2.2 bouyer #endif
1776 1.7.2.2 bouyer return(0);
1777 1.7.2.2 bouyer }
1778 1.7.2.2 bouyer
1779 1.7.2.2 bouyer void
1780 1.7.2.2 bouyer cs4280_power(why, v)
1781 1.7.2.2 bouyer int why;
1782 1.7.2.2 bouyer void *v;
1783 1.7.2.2 bouyer {
1784 1.7.2.2 bouyer struct cs4280_softc *sc = (struct cs4280_softc *)v;
1785 1.7.2.2 bouyer int i;
1786 1.7.2.2 bouyer
1787 1.7.2.2 bouyer DPRINTF(("%s: cs4280_power why=%d\n",
1788 1.7.2.2 bouyer sc->sc_dev.dv_xname, why));
1789 1.7.2.3 bouyer switch (why) {
1790 1.7.2.3 bouyer case PWR_SUSPEND:
1791 1.7.2.3 bouyer case PWR_STANDBY:
1792 1.7.2.2 bouyer sc->sc_suspend = why;
1793 1.7.2.2 bouyer
1794 1.7.2.2 bouyer cs4280_halt_output(sc);
1795 1.7.2.2 bouyer cs4280_halt_input(sc);
1796 1.7.2.2 bouyer /* Save AC97 registers */
1797 1.7.2.2 bouyer for(i = 1; i <= CS4280_SAVE_REG_MAX; i++) {
1798 1.7.2.2 bouyer if(i == 0x04) /* AC97_REG_MASTER_TONE */
1799 1.7.2.2 bouyer continue;
1800 1.7.2.2 bouyer cs4280_read_codec(sc, 2*i, &sc->ac97_reg[i]);
1801 1.7.2.2 bouyer }
1802 1.7.2.2 bouyer /* should I powerdown here ? */
1803 1.7.2.2 bouyer cs4280_write_codec(sc, AC97_REG_POWER, CS4280_POWER_DOWN_ALL);
1804 1.7.2.3 bouyer break;
1805 1.7.2.3 bouyer case PWR_RESUME:
1806 1.7.2.2 bouyer if (sc->sc_suspend == PWR_RESUME) {
1807 1.7.2.2 bouyer printf("cs4280_power: odd, resume without suspend.\n");
1808 1.7.2.2 bouyer sc->sc_suspend = why;
1809 1.7.2.2 bouyer return;
1810 1.7.2.2 bouyer }
1811 1.7.2.2 bouyer sc->sc_suspend = why;
1812 1.7.2.2 bouyer cs4280_init(sc, 0);
1813 1.7.2.2 bouyer cs4280_reset_codec(sc);
1814 1.7.2.2 bouyer
1815 1.7.2.2 bouyer /* restore ac97 registers */
1816 1.7.2.2 bouyer for(i = 1; i <= CS4280_SAVE_REG_MAX; i++) {
1817 1.7.2.2 bouyer if(i == 0x04) /* AC97_REG_MASTER_TONE */
1818 1.7.2.2 bouyer continue;
1819 1.7.2.2 bouyer cs4280_write_codec(sc, 2*i, sc->ac97_reg[i]);
1820 1.7.2.2 bouyer }
1821 1.7.2.3 bouyer break;
1822 1.7.2.3 bouyer case PWR_SOFTSUSPEND:
1823 1.7.2.3 bouyer case PWR_SOFTSTANDBY:
1824 1.7.2.3 bouyer case PWR_SOFTRESUME:
1825 1.7.2.3 bouyer break;
1826 1.7.2.2 bouyer }
1827 1.7.2.2 bouyer }
1828 1.7.2.2 bouyer
1829 1.7.2.2 bouyer void
1830 1.7.2.2 bouyer cs4280_clear_fifos(sc)
1831 1.7.2.2 bouyer struct cs4280_softc *sc;
1832 1.7.2.2 bouyer {
1833 1.7.2.2 bouyer int pd = 0, cnt, n;
1834 1.7.2.2 bouyer u_int32_t mem;
1835 1.7.2.2 bouyer
1836 1.7.2.2 bouyer /*
1837 1.7.2.2 bouyer * If device power down, power up the device and keep power down
1838 1.7.2.2 bouyer * state.
1839 1.7.2.2 bouyer */
1840 1.7.2.2 bouyer mem = BA0READ4(sc, CS4280_CLKCR1);
1841 1.7.2.2 bouyer if (!(mem & CLKCR1_SWCE)) {
1842 1.7.2.2 bouyer printf("cs4280_clear_fifo: power down found.\n");
1843 1.7.2.2 bouyer BA0WRITE4(sc, CS4280_CLKCR1, mem | CLKCR1_SWCE);
1844 1.7.2.2 bouyer pd = 1;
1845 1.7.2.2 bouyer }
1846 1.7.2.2 bouyer BA0WRITE4(sc, CS4280_SERBWP, 0);
1847 1.7.2.2 bouyer for (cnt = 0; cnt < 256; cnt++) {
1848 1.7.2.2 bouyer n = 0;
1849 1.7.2.2 bouyer while (BA0READ4(sc, CS4280_SERBST) & SERBST_WBSY) {
1850 1.7.2.2 bouyer delay(1000);
1851 1.7.2.2 bouyer if (++n > 1000) {
1852 1.7.2.2 bouyer printf("clear_fifo: fist timeout cnt=%d\n", cnt);
1853 1.7.2.2 bouyer break;
1854 1.7.2.2 bouyer }
1855 1.7.2.2 bouyer }
1856 1.7.2.2 bouyer BA0WRITE4(sc, CS4280_SERBAD, cnt);
1857 1.7.2.2 bouyer BA0WRITE4(sc, CS4280_SERBCM, SERBCM_WRC);
1858 1.7.2.2 bouyer }
1859 1.7.2.2 bouyer if (pd)
1860 1.7.2.2 bouyer BA0WRITE4(sc, CS4280_CLKCR1, mem);
1861 1.7.2.2 bouyer }
1862 1.7.2.2 bouyer
1863 1.7.2.2 bouyer #if NMIDI > 0
1864 1.7.2.2 bouyer int
1865 1.7.2.2 bouyer cs4280_midi_open(addr, flags, iintr, ointr, arg)
1866 1.7.2.2 bouyer void *addr;
1867 1.7.2.2 bouyer int flags;
1868 1.7.2.2 bouyer void (*iintr)__P((void *, int));
1869 1.7.2.2 bouyer void (*ointr)__P((void *));
1870 1.7.2.2 bouyer void *arg;
1871 1.7.2.2 bouyer {
1872 1.7.2.2 bouyer struct cs4280_softc *sc = addr;
1873 1.7.2.2 bouyer u_int32_t mem;
1874 1.7.2.2 bouyer
1875 1.7.2.2 bouyer DPRINTF(("midi_open\n"));
1876 1.7.2.2 bouyer sc->sc_iintr = iintr;
1877 1.7.2.2 bouyer sc->sc_ointr = ointr;
1878 1.7.2.2 bouyer sc->sc_arg = arg;
1879 1.7.2.2 bouyer
1880 1.7.2.2 bouyer /* midi interrupt enable */
1881 1.7.2.2 bouyer mem = BA0READ4(sc, CS4280_MIDCR) & ~MIDCR_MASK;
1882 1.7.2.2 bouyer mem |= MIDCR_TXE | MIDCR_RXE | MIDCR_RIE | MIDCR_TIE | MIDCR_MLB;
1883 1.7.2.2 bouyer BA0WRITE4(sc, CS4280_MIDCR, mem);
1884 1.7.2.2 bouyer #ifdef CS4280_DEBUG
1885 1.7.2.2 bouyer if (mem != BA0READ4(sc, CS4280_MIDCR)) {
1886 1.7.2.2 bouyer DPRINTF(("midi_open: MIDCR=%d\n", BA0READ4(sc, CS4280_MIDCR)));
1887 1.7.2.2 bouyer return(EINVAL);
1888 1.7.2.2 bouyer }
1889 1.7.2.2 bouyer DPRINTF(("MIDCR=0x%x\n", BA0READ4(sc, CS4280_MIDCR)));
1890 1.7.2.2 bouyer #endif
1891 1.7.2.2 bouyer return (0);
1892 1.7.2.2 bouyer }
1893 1.7.2.2 bouyer
1894 1.7.2.2 bouyer void
1895 1.7.2.2 bouyer cs4280_midi_close(addr)
1896 1.7.2.2 bouyer void *addr;
1897 1.7.2.2 bouyer {
1898 1.7.2.2 bouyer struct cs4280_softc *sc = addr;
1899 1.7.2.2 bouyer u_int32_t mem;
1900 1.7.2.2 bouyer
1901 1.7.2.2 bouyer DPRINTF(("midi_close\n"));
1902 1.7.2.2 bouyer mem = BA0READ4(sc, CS4280_MIDCR);
1903 1.7.2.2 bouyer mem &= ~MIDCR_MASK;
1904 1.7.2.2 bouyer BA0WRITE4(sc, CS4280_MIDCR, mem);
1905 1.7.2.2 bouyer
1906 1.7.2.2 bouyer sc->sc_iintr = 0;
1907 1.7.2.2 bouyer sc->sc_ointr = 0;
1908 1.7.2.2 bouyer }
1909 1.7.2.2 bouyer
1910 1.7.2.2 bouyer int
1911 1.7.2.2 bouyer cs4280_midi_output(addr, d)
1912 1.7.2.2 bouyer void *addr;
1913 1.7.2.2 bouyer int d;
1914 1.7.2.2 bouyer {
1915 1.7.2.2 bouyer struct cs4280_softc *sc = addr;
1916 1.7.2.2 bouyer u_int32_t mem;
1917 1.7.2.2 bouyer int x;
1918 1.7.2.2 bouyer
1919 1.7.2.2 bouyer for (x = 0; x != MIDI_BUSY_WAIT; x++) {
1920 1.7.2.2 bouyer if ((BA0READ4(sc, CS4280_MIDSR) & MIDSR_TBF) == 0) {
1921 1.7.2.2 bouyer mem = BA0READ4(sc, CS4280_MIDWP) & ~MIDWP_MASK;
1922 1.7.2.2 bouyer mem |= d & MIDWP_MASK;
1923 1.7.2.2 bouyer DPRINTFN(5,("midi_output d=0x%08x",d));
1924 1.7.2.2 bouyer BA0WRITE4(sc, CS4280_MIDWP, mem);
1925 1.7.2.2 bouyer #ifdef DIAGNOSTIC
1926 1.7.2.2 bouyer if (mem != BA0READ4(sc, CS4280_MIDWP)) {
1927 1.7.2.2 bouyer DPRINTF(("Bad write data: %d %d",
1928 1.7.2.2 bouyer mem, BA0READ4(sc, CS4280_MIDWP)));
1929 1.7.2.2 bouyer return(EIO);
1930 1.7.2.2 bouyer }
1931 1.7.2.2 bouyer #endif
1932 1.7.2.2 bouyer return (0);
1933 1.7.2.2 bouyer }
1934 1.7.2.2 bouyer delay(MIDI_BUSY_DELAY);
1935 1.7.2.2 bouyer }
1936 1.7.2.2 bouyer return (EIO);
1937 1.7.2.2 bouyer }
1938 1.7.2.2 bouyer
1939 1.7.2.2 bouyer void
1940 1.7.2.2 bouyer cs4280_midi_getinfo(addr, mi)
1941 1.7.2.2 bouyer void *addr;
1942 1.7.2.2 bouyer struct midi_info *mi;
1943 1.7.2.2 bouyer {
1944 1.7.2.2 bouyer mi->name = "CS4280 MIDI UART";
1945 1.7.2.2 bouyer mi->props = MIDI_PROP_CAN_INPUT | MIDI_PROP_OUT_INTR;
1946 1.7.2.2 bouyer }
1947 1.7.2.2 bouyer
1948 1.7.2.2 bouyer #endif
1949