pxa2x0_ac97.c revision 1.21 1 1.21 skrll /* $NetBSD: pxa2x0_ac97.c,v 1.21 2022/09/27 06:36:43 skrll Exp $ */
2 1.1 scw
3 1.1 scw /*
4 1.1 scw * Copyright (c) 2003, 2005 Wasabi Systems, Inc.
5 1.1 scw * All rights reserved.
6 1.1 scw *
7 1.1 scw * Written by Steve C. Woodford for Wasabi Systems, Inc.
8 1.1 scw *
9 1.1 scw * Redistribution and use in source and binary forms, with or without
10 1.1 scw * modification, are permitted provided that the following conditions
11 1.1 scw * are met:
12 1.1 scw * 1. Redistributions of source code must retain the above copyright
13 1.1 scw * notice, this list of conditions and the following disclaimer.
14 1.1 scw * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 scw * notice, this list of conditions and the following disclaimer in the
16 1.1 scw * documentation and/or other materials provided with the distribution.
17 1.1 scw * 3. All advertising materials mentioning features or use of this software
18 1.1 scw * must display the following acknowledgement:
19 1.1 scw * This product includes software developed for the NetBSD Project by
20 1.1 scw * Wasabi Systems, Inc.
21 1.1 scw * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 1.1 scw * or promote products derived from this software without specific prior
23 1.1 scw * written permission.
24 1.1 scw *
25 1.1 scw * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 1.1 scw * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 1.1 scw * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 1.1 scw * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29 1.1 scw * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 1.1 scw * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 1.1 scw * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 1.1 scw * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 1.1 scw * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 1.1 scw * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 1.1 scw * POSSIBILITY OF SUCH DAMAGE.
36 1.1 scw */
37 1.1 scw
38 1.1 scw #include <sys/param.h>
39 1.1 scw #include <sys/systm.h>
40 1.1 scw #include <sys/device.h>
41 1.1 scw #include <sys/kernel.h>
42 1.1 scw #include <sys/select.h>
43 1.1 scw #include <sys/audioio.h>
44 1.10 jmcneill #include <sys/kmem.h>
45 1.1 scw
46 1.1 scw #include <machine/intr.h>
47 1.9 dyoung #include <sys/bus.h>
48 1.1 scw
49 1.16 isaki #include <dev/audio/audio_if.h>
50 1.1 scw #include <dev/ic/ac97reg.h>
51 1.1 scw #include <dev/ic/ac97var.h>
52 1.1 scw
53 1.6 kiyohara #include <arm/xscale/pxa2x0cpu.h>
54 1.1 scw #include <arm/xscale/pxa2x0reg.h>
55 1.1 scw #include <arm/xscale/pxa2x0var.h>
56 1.1 scw #include <arm/xscale/pxa2x0_gpio.h>
57 1.1 scw #include <arm/xscale/pxa2x0_dmac.h>
58 1.1 scw
59 1.1 scw #include "locators.h"
60 1.1 scw
61 1.1 scw struct acu_dma {
62 1.1 scw bus_dmamap_t ad_map;
63 1.5 christos void *ad_addr;
64 1.1 scw #define ACU_N_SEGS 1 /* XXX: We don't support > 1 */
65 1.1 scw bus_dma_segment_t ad_segs[ACU_N_SEGS];
66 1.1 scw int ad_nsegs;
67 1.1 scw size_t ad_size;
68 1.1 scw struct dmac_xfer *ad_dx;
69 1.1 scw struct acu_dma *ad_next;
70 1.1 scw };
71 1.1 scw
72 1.1 scw #define KERNADDR(ad) ((void *)((ad)->ad_addr))
73 1.1 scw
74 1.1 scw struct acu_softc {
75 1.8 nonaka device_t sc_dev;
76 1.1 scw bus_space_tag_t sc_bust;
77 1.1 scw bus_dma_tag_t sc_dmat;
78 1.1 scw bus_space_handle_t sc_bush;
79 1.1 scw void *sc_irqcookie;
80 1.1 scw int sc_in_reset;
81 1.1 scw u_int sc_dac_rate;
82 1.1 scw u_int sc_adc_rate;
83 1.1 scw
84 1.1 scw /* List of DMA ring-buffers allocated by acu_malloc() */
85 1.1 scw struct acu_dma *sc_dmas;
86 1.1 scw
87 1.1 scw /* Dummy DMA segment which points to the AC97 PCM Fifo register */
88 1.1 scw bus_dma_segment_t sc_dr;
89 1.1 scw
90 1.1 scw /* PCM Output (Tx) state */
91 1.1 scw dmac_peripheral_t sc_txp;
92 1.1 scw struct acu_dma *sc_txdma;
93 1.1 scw void (*sc_txfunc)(void *);
94 1.1 scw void *sc_txarg;
95 1.1 scw
96 1.1 scw /* PCM Input (Rx) state */
97 1.1 scw dmac_peripheral_t sc_rxp;
98 1.1 scw struct acu_dma *sc_rxdma;
99 1.1 scw void (*sc_rxfunc)(void *);
100 1.1 scw void *sc_rxarg;
101 1.1 scw
102 1.1 scw /* AC97 Codec State */
103 1.1 scw struct ac97_codec_if *sc_codec_if;
104 1.1 scw struct ac97_host_if sc_host_if;
105 1.1 scw
106 1.1 scw /* Child audio(4) device */
107 1.12 chs device_t sc_audiodev;
108 1.1 scw
109 1.10 jmcneill /* MPSAFE interfaces */
110 1.10 jmcneill kmutex_t sc_lock;
111 1.10 jmcneill kmutex_t sc_intr_lock;
112 1.1 scw };
113 1.1 scw
114 1.8 nonaka static int pxaacu_match(device_t, cfdata_t, void *);
115 1.8 nonaka static void pxaacu_attach(device_t, device_t, void *);
116 1.1 scw
117 1.8 nonaka CFATTACH_DECL_NEW(pxaacu, sizeof(struct acu_softc),
118 1.1 scw pxaacu_match, pxaacu_attach, NULL, NULL);
119 1.1 scw
120 1.1 scw static int acu_codec_attach(void *, struct ac97_codec_if *);
121 1.13 skrll static int acu_codec_read(void *, uint8_t, uint16_t *);
122 1.13 skrll static int acu_codec_write(void *, uint8_t, uint16_t);
123 1.1 scw static int acu_codec_reset(void *);
124 1.1 scw static int acu_intr(void *);
125 1.1 scw
126 1.1 scw static int acu_open(void *, int);
127 1.1 scw static void acu_close(void *);
128 1.16 isaki static int acu_query_format(void *, audio_format_query_t *);
129 1.16 isaki static int acu_set_format(void *, int,
130 1.16 isaki const audio_params_t *, const audio_params_t *,
131 1.16 isaki audio_filter_reg_t *, audio_filter_reg_t *);
132 1.1 scw static int acu_round_blocksize(void *, int, int, const audio_params_t *);
133 1.1 scw static int acu_halt_output(void *);
134 1.1 scw static int acu_halt_input(void *);
135 1.1 scw static int acu_trigger_output(void *, void *, void *, int, void (*)(void *),
136 1.1 scw void *, const audio_params_t *);
137 1.1 scw static int acu_trigger_input(void *, void *, void *, int, void (*)(void *),
138 1.1 scw void *, const audio_params_t *);
139 1.1 scw static void acu_tx_loop_segment(struct dmac_xfer *, int);
140 1.1 scw static void acu_rx_loop_segment(struct dmac_xfer *, int);
141 1.1 scw static int acu_getdev(void *, struct audio_device *);
142 1.1 scw static int acu_mixer_set_port(void *, mixer_ctrl_t *);
143 1.1 scw static int acu_mixer_get_port(void *, mixer_ctrl_t *);
144 1.1 scw static int acu_query_devinfo(void *, mixer_devinfo_t *);
145 1.10 jmcneill static void *acu_malloc(void *, int, size_t);
146 1.10 jmcneill static void acu_free(void *, void *, size_t);
147 1.1 scw static int acu_get_props(void *);
148 1.10 jmcneill static void acu_get_locks(void *, kmutex_t **, kmutex_t **);
149 1.1 scw
150 1.1 scw struct audio_hw_if acu_hw_if = {
151 1.15 isaki .open = acu_open,
152 1.15 isaki .close = acu_close,
153 1.16 isaki .query_format = acu_query_format,
154 1.16 isaki .set_format = acu_set_format,
155 1.15 isaki .round_blocksize = acu_round_blocksize,
156 1.15 isaki .halt_output = acu_halt_output,
157 1.15 isaki .halt_input = acu_halt_input,
158 1.15 isaki .getdev = acu_getdev,
159 1.15 isaki .set_port = acu_mixer_set_port,
160 1.15 isaki .get_port = acu_mixer_get_port,
161 1.15 isaki .query_devinfo = acu_query_devinfo,
162 1.15 isaki .allocm = acu_malloc,
163 1.15 isaki .freem = acu_free,
164 1.15 isaki .get_props = acu_get_props,
165 1.15 isaki .trigger_output = acu_trigger_output,
166 1.15 isaki .trigger_input = acu_trigger_input,
167 1.15 isaki .get_locks = acu_get_locks,
168 1.1 scw };
169 1.1 scw
170 1.1 scw struct audio_device acu_device = {
171 1.1 scw "PXA250 AC97",
172 1.1 scw "",
173 1.1 scw "acu"
174 1.1 scw };
175 1.1 scw
176 1.1 scw static const struct audio_format acu_formats[] = {
177 1.16 isaki {
178 1.16 isaki .mode = AUMODE_PLAY | AUMODE_RECORD,
179 1.16 isaki .encoding = AUDIO_ENCODING_SLINEAR_LE,
180 1.16 isaki .validbits = 16,
181 1.16 isaki .precision = 16,
182 1.16 isaki .channels = 2,
183 1.16 isaki .channel_mask = AUFMT_STEREO,
184 1.16 isaki .frequency_type = 0,
185 1.16 isaki /* XXX Need an accurate list of frequencies. */
186 1.16 isaki .frequency = { 4000, 48000 },
187 1.16 isaki },
188 1.1 scw };
189 1.1 scw #define ACU_NFORMATS (sizeof(acu_formats) / sizeof(struct audio_format))
190 1.1 scw
191 1.13 skrll static inline uint32_t
192 1.1 scw acu_reg_read(struct acu_softc *sc, int reg)
193 1.1 scw {
194 1.1 scw
195 1.1 scw return (bus_space_read_4(sc->sc_bust, sc->sc_bush, reg));
196 1.1 scw }
197 1.1 scw
198 1.3 perry static inline void
199 1.13 skrll acu_reg_write(struct acu_softc *sc, int reg, uint32_t val)
200 1.1 scw {
201 1.1 scw
202 1.1 scw bus_space_write_4(sc->sc_bust, sc->sc_bush, reg, val);
203 1.1 scw }
204 1.1 scw
205 1.3 perry static inline int
206 1.1 scw acu_codec_ready(struct acu_softc *sc)
207 1.1 scw {
208 1.1 scw
209 1.1 scw return (acu_reg_read(sc, AC97_GSR) & GSR_PCR);
210 1.1 scw }
211 1.1 scw
212 1.3 perry static inline int
213 1.13 skrll acu_wait_gsr(struct acu_softc *sc, uint32_t bit)
214 1.1 scw {
215 1.1 scw int timeout;
216 1.13 skrll uint32_t rv;
217 1.1 scw
218 1.1 scw for (timeout = 5000; timeout; timeout--) {
219 1.1 scw if ((rv = acu_reg_read(sc, AC97_GSR)) & bit) {
220 1.1 scw acu_reg_write(sc, AC97_GSR, rv | bit);
221 1.1 scw return (0);
222 1.1 scw }
223 1.1 scw delay(1);
224 1.1 scw }
225 1.1 scw
226 1.1 scw return (1);
227 1.1 scw }
228 1.1 scw
229 1.1 scw static int
230 1.8 nonaka pxaacu_match(device_t parent, cfdata_t cf, void *aux)
231 1.1 scw {
232 1.1 scw struct pxaip_attach_args *pxa = aux;
233 1.6 kiyohara struct pxa2x0_gpioconf *gpioconf;
234 1.6 kiyohara u_int gpio;
235 1.6 kiyohara int i;
236 1.1 scw
237 1.1 scw if (pxa->pxa_addr != PXA2X0_AC97_BASE ||
238 1.1 scw pxa->pxa_intr != PXA2X0_INT_AC97)
239 1.1 scw return (0);
240 1.1 scw
241 1.6 kiyohara gpioconf = CPU_IS_PXA250 ? pxa25x_pxaacu_gpioconf :
242 1.6 kiyohara pxa27x_pxaacu_gpioconf;
243 1.6 kiyohara for (i = 0; gpioconf[i].pin != -1; i++) {
244 1.6 kiyohara gpio = pxa2x0_gpio_get_function(gpioconf[i].pin);
245 1.6 kiyohara if (GPIO_FN(gpio) != GPIO_FN(gpioconf[i].value) ||
246 1.6 kiyohara GPIO_FN_IS_OUT(gpio) != GPIO_FN_IS_OUT(gpioconf[i].value))
247 1.6 kiyohara return (0);
248 1.6 kiyohara }
249 1.6 kiyohara
250 1.1 scw pxa->pxa_size = PXA2X0_AC97_SIZE;
251 1.1 scw
252 1.1 scw return (1);
253 1.1 scw }
254 1.1 scw
255 1.1 scw static void
256 1.8 nonaka pxaacu_attach(device_t parent, device_t self, void *aux)
257 1.1 scw {
258 1.8 nonaka struct acu_softc *sc = device_private(self);
259 1.1 scw struct pxaip_attach_args *pxa = aux;
260 1.1 scw
261 1.8 nonaka sc->sc_dev = self;
262 1.1 scw sc->sc_bust = pxa->pxa_iot;
263 1.1 scw sc->sc_dmat = pxa->pxa_dmat;
264 1.1 scw
265 1.1 scw aprint_naive("\n");
266 1.1 scw aprint_normal(": AC97 Controller\n");
267 1.1 scw
268 1.1 scw if (bus_space_map(sc->sc_bust, pxa->pxa_addr, pxa->pxa_size, 0,
269 1.1 scw &sc->sc_bush)) {
270 1.8 nonaka aprint_error_dev(self, "Can't map registers!\n");
271 1.1 scw return;
272 1.1 scw }
273 1.1 scw
274 1.11 mrg sc->sc_irqcookie = pxa2x0_intr_establish(pxa->pxa_intr, IPL_AUDIO,
275 1.1 scw acu_intr, sc);
276 1.1 scw KASSERT(sc->sc_irqcookie != NULL);
277 1.1 scw
278 1.1 scw /* Make sure the AC97 clock is enabled */
279 1.4 thorpej pxa2x0_clkman_config(CKEN_AC97, true);
280 1.1 scw delay(100);
281 1.1 scw
282 1.1 scw /* Do a cold reset */
283 1.1 scw acu_reg_write(sc, AC97_GCR, 0);
284 1.1 scw delay(100);
285 1.1 scw acu_reg_write(sc, AC97_GCR, GCR_COLD_RST);
286 1.1 scw delay(100);
287 1.1 scw acu_reg_write(sc, AC97_CAR, 0);
288 1.1 scw
289 1.1 scw if (acu_wait_gsr(sc, GSR_PCR)) {
290 1.1 scw acu_reg_write(sc, AC97_GCR, 0);
291 1.1 scw delay(100);
292 1.4 thorpej pxa2x0_clkman_config(CKEN_AC97, false);
293 1.1 scw bus_space_unmap(sc->sc_bust, sc->sc_bush, pxa->pxa_size);
294 1.8 nonaka aprint_error_dev(self, "Primary codec not ready\n");
295 1.1 scw return;
296 1.1 scw }
297 1.1 scw
298 1.1 scw sc->sc_dr.ds_addr = pxa->pxa_addr + AC97_PCDR;
299 1.1 scw sc->sc_dr.ds_len = 4;
300 1.1 scw
301 1.1 scw sc->sc_codec_if = NULL;
302 1.1 scw sc->sc_host_if.arg = sc;
303 1.1 scw sc->sc_host_if.attach = acu_codec_attach;
304 1.1 scw sc->sc_host_if.read = acu_codec_read;
305 1.1 scw sc->sc_host_if.write = acu_codec_write;
306 1.1 scw sc->sc_host_if.reset = acu_codec_reset;
307 1.1 scw sc->sc_host_if.flags = NULL;
308 1.1 scw sc->sc_in_reset = 0;
309 1.1 scw sc->sc_dac_rate = sc->sc_adc_rate = 0;
310 1.1 scw
311 1.10 jmcneill if (ac97_attach(&sc->sc_host_if, sc->sc_dev, &sc->sc_lock)) {
312 1.8 nonaka aprint_error_dev(self, "Failed to attach primary codec\n");
313 1.1 scw acu_reg_write(sc, AC97_GCR, 0);
314 1.1 scw delay(100);
315 1.4 thorpej pxa2x0_clkman_config(CKEN_AC97, false);
316 1.1 scw bus_space_unmap(sc->sc_bust, sc->sc_bush, pxa->pxa_size);
317 1.1 scw return;
318 1.1 scw }
319 1.1 scw
320 1.8 nonaka sc->sc_audiodev = audio_attach_mi(&acu_hw_if, sc, sc->sc_dev);
321 1.1 scw
322 1.1 scw /*
323 1.1 scw * As a work-around for braindamage in the PXA250's AC97 controller
324 1.1 scw * (see errata #125), we hold the ACUNIT/Codec in Cold Reset until
325 1.1 scw * acu_open() is called. acu_close() also puts the controller into
326 1.1 scw * Cold Reset.
327 1.1 scw *
328 1.1 scw * While this won't necessarily prevent Rx FIFO overruns, it at least
329 1.1 scw * allows the user to recover by closing then re-opening the audio
330 1.1 scw * device.
331 1.1 scw */
332 1.1 scw acu_reg_write(sc, AC97_GCR, 0);
333 1.1 scw sc->sc_in_reset = 1;
334 1.1 scw }
335 1.1 scw
336 1.1 scw static int
337 1.1 scw acu_codec_attach(void *arg, struct ac97_codec_if *aci)
338 1.1 scw {
339 1.1 scw struct acu_softc *sc = arg;
340 1.1 scw
341 1.1 scw sc->sc_codec_if = aci;
342 1.1 scw return (0);
343 1.1 scw }
344 1.1 scw
345 1.1 scw static int
346 1.13 skrll acu_codec_read(void *arg, uint8_t codec_reg, uint16_t *valp)
347 1.1 scw {
348 1.1 scw struct acu_softc *sc = arg;
349 1.13 skrll uint32_t val;
350 1.10 jmcneill int reg, rv = 1;
351 1.1 scw
352 1.1 scw /*
353 1.1 scw * If we're currently closed, return non-zero. The ac97 frontend
354 1.1 scw * will use its cached copy of the register instead.
355 1.1 scw */
356 1.1 scw if (sc->sc_in_reset)
357 1.1 scw return (1);
358 1.1 scw
359 1.1 scw reg = AC97_CODEC_BASE(0) + codec_reg * 2;
360 1.1 scw
361 1.10 jmcneill mutex_spin_enter(&sc->sc_intr_lock);
362 1.1 scw
363 1.1 scw if (!acu_codec_ready(sc) || (acu_reg_read(sc, AC97_CAR) & CAR_CAIP))
364 1.1 scw goto out_nocar;
365 1.1 scw
366 1.1 scw val = acu_reg_read(sc, AC97_GSR);
367 1.1 scw val |= GSR_RDCS | GSR_SDONE;
368 1.1 scw acu_reg_write(sc, AC97_GSR, val);
369 1.1 scw
370 1.1 scw /*
371 1.1 scw * Dummy read to initiate the real read access
372 1.1 scw */
373 1.1 scw (void) acu_reg_read(sc, reg);
374 1.1 scw if (acu_wait_gsr(sc, GSR_SDONE))
375 1.1 scw goto out;
376 1.1 scw
377 1.1 scw (void) acu_reg_read(sc, reg);
378 1.1 scw if (acu_wait_gsr(sc, GSR_SDONE))
379 1.1 scw goto out;
380 1.1 scw
381 1.1 scw val = acu_reg_read(sc, AC97_GSR);
382 1.1 scw if (val & GSR_RDCS)
383 1.1 scw goto out;
384 1.1 scw
385 1.1 scw *valp = acu_reg_read(sc, reg);
386 1.1 scw if (acu_wait_gsr(sc, GSR_SDONE))
387 1.1 scw goto out;
388 1.1 scw
389 1.1 scw rv = 0;
390 1.1 scw
391 1.1 scw out:
392 1.1 scw acu_reg_write(sc, AC97_CAR, 0);
393 1.1 scw out_nocar:
394 1.10 jmcneill mutex_spin_exit(&sc->sc_intr_lock);
395 1.1 scw delay(10);
396 1.1 scw return (rv);
397 1.1 scw }
398 1.1 scw
399 1.1 scw static int
400 1.13 skrll acu_codec_write(void *arg, uint8_t codec_reg, uint16_t val)
401 1.1 scw {
402 1.1 scw struct acu_softc *sc = arg;
403 1.13 skrll uint16_t rv;
404 1.1 scw
405 1.1 scw /*
406 1.1 scw * If we're currently closed, chances are the user is just
407 1.1 scw * tweaking mixer settings. Pretend the write succeeded.
408 1.1 scw * The ac97 frontend will cache the value anyway, and it'll
409 1.1 scw * be written correctly when the driver is opened.
410 1.1 scw */
411 1.1 scw if (sc->sc_in_reset)
412 1.1 scw return (0);
413 1.1 scw
414 1.10 jmcneill mutex_spin_enter(&sc->sc_intr_lock);
415 1.1 scw
416 1.1 scw if (!acu_codec_ready(sc) || (acu_reg_read(sc, AC97_CAR) & CAR_CAIP)) {
417 1.10 jmcneill mutex_spin_exit(&sc->sc_intr_lock);
418 1.1 scw return (1);
419 1.1 scw }
420 1.1 scw
421 1.1 scw rv = acu_reg_read(sc, AC97_GSR);
422 1.1 scw rv |= GSR_RDCS | GSR_CDONE;
423 1.1 scw acu_reg_write(sc, AC97_GSR, rv);
424 1.1 scw
425 1.1 scw acu_reg_write(sc, AC97_CODEC_BASE(0) + codec_reg * 2, val);
426 1.1 scw
427 1.1 scw /*
428 1.1 scw * Wait for the write to complete
429 1.1 scw */
430 1.1 scw (void) acu_wait_gsr(sc, GSR_CDONE);
431 1.1 scw acu_reg_write(sc, AC97_CAR, 0);
432 1.1 scw
433 1.10 jmcneill mutex_spin_exit(&sc->sc_intr_lock);
434 1.1 scw delay(10);
435 1.1 scw return (0);
436 1.1 scw }
437 1.1 scw
438 1.1 scw static int
439 1.1 scw acu_codec_reset(void *arg)
440 1.1 scw {
441 1.1 scw struct acu_softc *sc = arg;
442 1.13 skrll uint32_t rv;
443 1.1 scw
444 1.1 scw rv = acu_reg_read(sc, AC97_GCR);
445 1.1 scw acu_reg_write(sc, AC97_GCR, rv | GCR_WARM_RST);
446 1.1 scw delay(100);
447 1.1 scw acu_reg_write(sc, AC97_GCR, rv);
448 1.1 scw delay(100);
449 1.1 scw
450 1.1 scw if (acu_wait_gsr(sc, GSR_PCR)) {
451 1.8 nonaka aprint_error_dev(sc->sc_dev,
452 1.8 nonaka "acu_codec_reset: failed to ready after reset\n");
453 1.1 scw return (ETIMEDOUT);
454 1.1 scw }
455 1.1 scw
456 1.1 scw return (0);
457 1.1 scw }
458 1.1 scw
459 1.1 scw static int
460 1.1 scw acu_intr(void *arg)
461 1.1 scw {
462 1.1 scw struct acu_softc *sc = arg;
463 1.13 skrll uint32_t gsr, reg;
464 1.1 scw
465 1.10 jmcneill mutex_spin_enter(&sc->sc_intr_lock);
466 1.1 scw gsr = acu_reg_read(sc, AC97_GSR);
467 1.1 scw
468 1.1 scw /*
469 1.1 scw * Tx FIFO underruns are no big deal. Just log it and ignore and
470 1.1 scw * subsequent underruns until the next time acu_trigger_output()
471 1.1 scw * is called.
472 1.1 scw */
473 1.1 scw if ((gsr & GSR_POINT) && (acu_reg_read(sc, AC97_POCR) & AC97_FEFIE)) {
474 1.1 scw acu_reg_write(sc, AC97_POCR, 0);
475 1.1 scw reg = acu_reg_read(sc, AC97_POSR);
476 1.1 scw acu_reg_write(sc, AC97_POSR, reg);
477 1.8 nonaka aprint_error_dev(sc->sc_dev, "Tx PCM Fifo underrun\n");
478 1.1 scw }
479 1.1 scw
480 1.1 scw /*
481 1.1 scw * Rx FIFO overruns are a different story. See PAX250 Errata #125
482 1.1 scw * for the gory details.
483 1.1 scw * I don't see any way to gracefully recover from this problem,
484 1.1 scw * other than a issuing a Cold Reset in acu_close().
485 1.1 scw * The best we can do here is to report the problem on the console.
486 1.1 scw */
487 1.1 scw if ((gsr & GSR_PIINT) && (acu_reg_read(sc, AC97_PICR) & AC97_FEFIE)) {
488 1.1 scw acu_reg_write(sc, AC97_PICR, 0);
489 1.1 scw reg = acu_reg_read(sc, AC97_PISR);
490 1.1 scw acu_reg_write(sc, AC97_PISR, reg);
491 1.8 nonaka aprint_error_dev(sc->sc_dev, "Rx PCM Fifo overrun\n");
492 1.1 scw }
493 1.1 scw
494 1.10 jmcneill mutex_spin_exit(&sc->sc_intr_lock);
495 1.10 jmcneill
496 1.1 scw return (1);
497 1.1 scw }
498 1.1 scw
499 1.1 scw static int
500 1.1 scw acu_open(void *arg, int flags)
501 1.1 scw {
502 1.1 scw struct acu_softc *sc = arg;
503 1.1 scw
504 1.1 scw /*
505 1.1 scw * Deassert Cold Reset
506 1.1 scw */
507 1.1 scw acu_reg_write(sc, AC97_GCR, GCR_COLD_RST);
508 1.1 scw delay(100);
509 1.1 scw acu_reg_write(sc, AC97_CAR, 0);
510 1.1 scw
511 1.1 scw /*
512 1.1 scw * Wait for the primary codec to become ready
513 1.1 scw */
514 1.1 scw if (acu_wait_gsr(sc, GSR_PCR))
515 1.1 scw return (EIO);
516 1.1 scw sc->sc_in_reset = 0;
517 1.1 scw
518 1.1 scw /*
519 1.1 scw * Restore the codec port settings
520 1.1 scw */
521 1.1 scw sc->sc_codec_if->vtbl->restore_ports(sc->sc_codec_if);
522 1.1 scw
523 1.1 scw /*
524 1.1 scw * Need to reprogram the sample rates, since 'restore_ports'
525 1.1 scw * doesn't do it.
526 1.1 scw *
527 1.1 scw * XXX: These aren't the only two sample rate registers ...
528 1.1 scw */
529 1.1 scw if (sc->sc_dac_rate)
530 1.1 scw (void) sc->sc_codec_if->vtbl->set_rate(sc->sc_codec_if,
531 1.1 scw AC97_REG_PCM_FRONT_DAC_RATE, &sc->sc_dac_rate);
532 1.1 scw if (sc->sc_adc_rate)
533 1.1 scw (void) sc->sc_codec_if->vtbl->set_rate(sc->sc_codec_if,
534 1.1 scw AC97_REG_PCM_LR_ADC_RATE, &sc->sc_adc_rate);
535 1.1 scw
536 1.1 scw return (0);
537 1.1 scw }
538 1.1 scw
539 1.1 scw static void
540 1.1 scw acu_close(void *arg)
541 1.1 scw {
542 1.1 scw struct acu_softc *sc = arg;
543 1.1 scw
544 1.1 scw /*
545 1.1 scw * Make sure the hardware is quiescent
546 1.1 scw */
547 1.1 scw delay(100);
548 1.1 scw
549 1.1 scw /* Assert Cold Reset */
550 1.1 scw acu_reg_write(sc, AC97_GCR, 0);
551 1.1 scw sc->sc_in_reset = 1;
552 1.1 scw }
553 1.1 scw
554 1.1 scw static int
555 1.16 isaki acu_query_format(void *arg, audio_format_query_t *afp)
556 1.1 scw {
557 1.1 scw
558 1.16 isaki return audio_query_format(acu_formats, ACU_NFORMATS, afp);
559 1.1 scw }
560 1.1 scw
561 1.1 scw static int
562 1.16 isaki acu_set_format(void *arg, int setmode,
563 1.16 isaki const audio_params_t *play, const audio_params_t *rec,
564 1.16 isaki audio_filter_reg_t *pfil, audio_filter_reg_t *rfil)
565 1.1 scw {
566 1.1 scw struct acu_softc *sc = arg;
567 1.16 isaki int rate;
568 1.16 isaki int err;
569 1.1 scw
570 1.16 isaki if ((setmode & AUMODE_PLAY)) {
571 1.16 isaki rate = play->sample_rate;
572 1.16 isaki err = sc->sc_codec_if->vtbl->set_rate(sc->sc_codec_if,
573 1.16 isaki AC97_REG_PCM_FRONT_DAC_RATE, &rate);
574 1.16 isaki if (err)
575 1.16 isaki return EINVAL;
576 1.16 isaki sc->sc_dac_rate = play->sample_rate;
577 1.16 isaki }
578 1.16 isaki if ((setmode & AUMODE_RECORD)) {
579 1.16 isaki rate = rec->sample_rate;
580 1.16 isaki err = sc->sc_codec_if->vtbl->set_rate(sc->sc_codec_if,
581 1.16 isaki AC97_REG_PCM_LR_ADC_RATE, &rate);
582 1.1 scw if (err)
583 1.16 isaki return EINVAL;
584 1.16 isaki sc->sc_adc_rate = rec->sample_rate;
585 1.1 scw }
586 1.16 isaki return 0;
587 1.1 scw }
588 1.1 scw
589 1.1 scw static int
590 1.1 scw acu_round_blocksize(void *arg, int blk, int mode, const audio_params_t *param)
591 1.1 scw {
592 1.1 scw
593 1.18 isaki blk = (blk & ~0x1f);
594 1.18 isaki if (blk < 0x20)
595 1.18 isaki blk = 0x20;
596 1.18 isaki return blk;
597 1.1 scw }
598 1.1 scw
599 1.1 scw static int
600 1.1 scw acu_getdev(void *addr, struct audio_device *retp)
601 1.1 scw {
602 1.1 scw
603 1.1 scw *retp = acu_device;
604 1.1 scw return (0);
605 1.1 scw }
606 1.1 scw
607 1.1 scw static int
608 1.1 scw acu_mixer_set_port(void *arg, mixer_ctrl_t *cp)
609 1.1 scw {
610 1.1 scw struct acu_softc *sc = arg;
611 1.1 scw
612 1.1 scw return (sc->sc_codec_if->vtbl->mixer_set_port(sc->sc_codec_if, cp));
613 1.1 scw }
614 1.1 scw
615 1.1 scw static int
616 1.1 scw acu_mixer_get_port(void *arg, mixer_ctrl_t *cp)
617 1.1 scw {
618 1.1 scw struct acu_softc *sc = arg;
619 1.1 scw
620 1.1 scw return (sc->sc_codec_if->vtbl->mixer_get_port(sc->sc_codec_if, cp));
621 1.1 scw }
622 1.1 scw
623 1.1 scw static int
624 1.1 scw acu_query_devinfo(void *arg, mixer_devinfo_t *dip)
625 1.1 scw {
626 1.1 scw struct acu_softc *sc = arg;
627 1.1 scw
628 1.1 scw return (sc->sc_codec_if->vtbl->query_devinfo(sc->sc_codec_if, dip));
629 1.1 scw }
630 1.1 scw
631 1.1 scw static void *
632 1.10 jmcneill acu_malloc(void *arg, int direction, size_t size)
633 1.1 scw {
634 1.1 scw struct acu_softc *sc = arg;
635 1.1 scw struct acu_dma *ad;
636 1.1 scw int error;
637 1.1 scw
638 1.14 chs ad = kmem_alloc(sizeof(*ad), KM_SLEEP);
639 1.1 scw
640 1.10 jmcneill /* XXX */
641 1.10 jmcneill if ((ad->ad_dx = pxa2x0_dmac_allocate_xfer()) == NULL)
642 1.1 scw goto error;
643 1.1 scw
644 1.1 scw ad->ad_size = size;
645 1.1 scw
646 1.1 scw error = bus_dmamem_alloc(sc->sc_dmat, size, 16, 0, ad->ad_segs,
647 1.10 jmcneill ACU_N_SEGS, &ad->ad_nsegs, BUS_DMA_WAITOK);
648 1.1 scw if (error)
649 1.1 scw goto free_xfer;
650 1.1 scw
651 1.1 scw error = bus_dmamem_map(sc->sc_dmat, ad->ad_segs, ad->ad_nsegs, size,
652 1.10 jmcneill &ad->ad_addr, BUS_DMA_WAITOK | BUS_DMA_COHERENT | BUS_DMA_NOCACHE);
653 1.1 scw if (error)
654 1.1 scw goto free_dmamem;
655 1.1 scw
656 1.1 scw error = bus_dmamap_create(sc->sc_dmat, size, 1, size, 0,
657 1.10 jmcneill BUS_DMA_WAITOK | BUS_DMA_ALLOCNOW, &ad->ad_map);
658 1.1 scw if (error)
659 1.1 scw goto unmap_dmamem;
660 1.1 scw
661 1.1 scw error = bus_dmamap_load(sc->sc_dmat, ad->ad_map, ad->ad_addr, size,
662 1.10 jmcneill NULL, BUS_DMA_WAITOK);
663 1.1 scw if (error) {
664 1.1 scw bus_dmamap_destroy(sc->sc_dmat, ad->ad_map);
665 1.1 scw unmap_dmamem: bus_dmamem_unmap(sc->sc_dmat, ad->ad_addr, size);
666 1.1 scw free_dmamem: bus_dmamem_free(sc->sc_dmat, ad->ad_segs, ad->ad_nsegs);
667 1.1 scw free_xfer: pxa2x0_dmac_free_xfer(ad->ad_dx);
668 1.10 jmcneill error: kmem_free(ad, sizeof(*ad));
669 1.1 scw return (NULL);
670 1.1 scw }
671 1.1 scw
672 1.1 scw ad->ad_dx->dx_cookie = sc;
673 1.1 scw ad->ad_dx->dx_priority = DMAC_PRIORITY_HIGH;
674 1.1 scw ad->ad_dx->dx_dev_width = DMAC_DEV_WIDTH_4;
675 1.1 scw ad->ad_dx->dx_burst_size = DMAC_BURST_SIZE_32;
676 1.1 scw
677 1.1 scw ad->ad_next = sc->sc_dmas;
678 1.1 scw sc->sc_dmas = ad;
679 1.1 scw return (KERNADDR(ad));
680 1.1 scw }
681 1.1 scw
682 1.1 scw static void
683 1.10 jmcneill acu_free(void *arg, void *ptr, size_t size)
684 1.1 scw {
685 1.1 scw struct acu_softc *sc = arg;
686 1.1 scw struct acu_dma *ad, **adp;
687 1.1 scw
688 1.1 scw for (adp = &sc->sc_dmas; (ad = *adp) != NULL; adp = &ad->ad_next) {
689 1.1 scw if (KERNADDR(ad) == ptr) {
690 1.1 scw pxa2x0_dmac_abort_xfer(ad->ad_dx);
691 1.1 scw pxa2x0_dmac_free_xfer(ad->ad_dx);
692 1.1 scw ad->ad_segs[0].ds_len = ad->ad_size; /* XXX */
693 1.1 scw bus_dmamap_unload(sc->sc_dmat, ad->ad_map);
694 1.1 scw bus_dmamap_destroy(sc->sc_dmat, ad->ad_map);
695 1.1 scw bus_dmamem_unmap(sc->sc_dmat, ad->ad_addr, ad->ad_size);
696 1.1 scw bus_dmamem_free(sc->sc_dmat, ad->ad_segs, ad->ad_nsegs);
697 1.1 scw *adp = ad->ad_next;
698 1.10 jmcneill kmem_free(ad, sizeof(*ad));
699 1.1 scw return;
700 1.1 scw }
701 1.1 scw }
702 1.1 scw }
703 1.1 scw
704 1.1 scw static int
705 1.1 scw acu_get_props(void *arg)
706 1.1 scw {
707 1.1 scw
708 1.17 isaki return (AUDIO_PROP_PLAYBACK | AUDIO_PROP_CAPTURE |
709 1.17 isaki AUDIO_PROP_INDEPENDENT | AUDIO_PROP_FULLDUPLEX);
710 1.1 scw }
711 1.1 scw
712 1.10 jmcneill static void
713 1.10 jmcneill acu_get_locks(void *opaque, kmutex_t **intr, kmutex_t **thread)
714 1.10 jmcneill {
715 1.10 jmcneill struct acu_softc *sc = opaque;
716 1.10 jmcneill
717 1.10 jmcneill *intr = &sc->sc_intr_lock;
718 1.10 jmcneill *thread = &sc->sc_lock;
719 1.10 jmcneill }
720 1.10 jmcneill
721 1.1 scw static int
722 1.1 scw acu_halt_output(void *arg)
723 1.1 scw {
724 1.1 scw struct acu_softc *sc = arg;
725 1.1 scw
726 1.1 scw if (sc->sc_txdma) {
727 1.1 scw acu_reg_write(sc, AC97_POCR, 0);
728 1.1 scw acu_reg_write(sc, AC97_POSR, AC97_FIFOE);
729 1.1 scw pxa2x0_dmac_abort_xfer(sc->sc_txdma->ad_dx);
730 1.1 scw sc->sc_txdma = NULL;
731 1.1 scw }
732 1.1 scw return (0);
733 1.1 scw }
734 1.1 scw
735 1.1 scw static int
736 1.1 scw acu_halt_input(void *arg)
737 1.1 scw {
738 1.1 scw struct acu_softc *sc = arg;
739 1.1 scw
740 1.1 scw if (sc->sc_rxdma) {
741 1.1 scw acu_reg_write(sc, AC97_PICR, 0);
742 1.1 scw acu_reg_write(sc, AC97_PISR, AC97_FIFOE);
743 1.1 scw pxa2x0_dmac_abort_xfer(sc->sc_rxdma->ad_dx);
744 1.1 scw sc->sc_rxdma = NULL;
745 1.1 scw }
746 1.1 scw return (0);
747 1.1 scw }
748 1.1 scw
749 1.1 scw static int
750 1.1 scw acu_trigger_output(void *arg, void *start, void *end, int blksize,
751 1.1 scw void (*tx_func)(void *), void *tx_arg, const audio_params_t *param)
752 1.1 scw {
753 1.1 scw struct acu_softc *sc = arg;
754 1.1 scw struct dmac_xfer *dx;
755 1.1 scw struct acu_dma *ad;
756 1.1 scw int rv;
757 1.1 scw
758 1.1 scw if (sc->sc_txdma)
759 1.1 scw return (EBUSY);
760 1.1 scw
761 1.1 scw sc->sc_txfunc = tx_func;
762 1.1 scw sc->sc_txarg = tx_arg;
763 1.1 scw
764 1.1 scw for (ad = sc->sc_dmas; ad && KERNADDR(ad) != start; ad = ad->ad_next)
765 1.1 scw ;
766 1.1 scw if (ad == NULL) {
767 1.1 scw printf("acu_trigger_output: bad addr %p\n", start);
768 1.1 scw return (EINVAL);
769 1.1 scw }
770 1.1 scw
771 1.1 scw sc->sc_txdma = ad;
772 1.1 scw ad->ad_segs[0].ds_addr = ad->ad_map->dm_segs[0].ds_addr;
773 1.1 scw ad->ad_segs[0].ds_len = (uintptr_t)end - (uintptr_t)start;
774 1.1 scw
775 1.1 scw /*
776 1.1 scw * Fix up a looping DMA request.
777 1.1 scw * The 'done' function will be called for every 'blksize' bytes
778 1.1 scw * transferred by the DMA engine.
779 1.1 scw */
780 1.1 scw dx = ad->ad_dx;
781 1.1 scw dx->dx_done = acu_tx_loop_segment;
782 1.1 scw dx->dx_peripheral = DMAC_PERIPH_AC97AUDIOTX;
783 1.1 scw dx->dx_flow = DMAC_FLOW_CTRL_DEST;
784 1.1 scw dx->dx_loop_notify = blksize;
785 1.4 thorpej dx->dx_desc[DMAC_DESC_SRC].xd_addr_hold = false;
786 1.1 scw dx->dx_desc[DMAC_DESC_SRC].xd_nsegs = ad->ad_nsegs;
787 1.1 scw dx->dx_desc[DMAC_DESC_SRC].xd_dma_segs = ad->ad_segs;
788 1.4 thorpej dx->dx_desc[DMAC_DESC_DST].xd_addr_hold = true;
789 1.1 scw dx->dx_desc[DMAC_DESC_DST].xd_nsegs = 1;
790 1.1 scw dx->dx_desc[DMAC_DESC_DST].xd_dma_segs = &sc->sc_dr;
791 1.1 scw
792 1.1 scw rv = pxa2x0_dmac_start_xfer(dx);
793 1.1 scw if (rv == 0) {
794 1.1 scw /*
795 1.1 scw * XXX: We should only do this once the request has been
796 1.1 scw * loaded into a DMAC channel.
797 1.1 scw */
798 1.1 scw acu_reg_write(sc, AC97_POSR, AC97_FIFOE);
799 1.1 scw acu_reg_write(sc, AC97_POCR, AC97_FEFIE);
800 1.1 scw }
801 1.1 scw
802 1.1 scw return (rv);
803 1.1 scw }
804 1.1 scw
805 1.1 scw static int
806 1.1 scw acu_trigger_input(void *arg, void *start, void *end, int blksize,
807 1.1 scw void (*rx_func)(void *), void *rx_arg, const audio_params_t *param)
808 1.1 scw {
809 1.1 scw struct acu_softc *sc = arg;
810 1.1 scw struct dmac_xfer *dx;
811 1.1 scw struct acu_dma *ad;
812 1.1 scw int rv;
813 1.1 scw
814 1.1 scw if (sc->sc_rxdma)
815 1.1 scw return (EBUSY);
816 1.1 scw
817 1.1 scw sc->sc_rxfunc = rx_func;
818 1.1 scw sc->sc_rxarg = rx_arg;
819 1.1 scw
820 1.1 scw for (ad = sc->sc_dmas; ad && KERNADDR(ad) != start; ad = ad->ad_next)
821 1.1 scw ;
822 1.1 scw if (ad == NULL) {
823 1.1 scw printf("acu_trigger_input: bad addr %p\n", start);
824 1.1 scw return (EINVAL);
825 1.1 scw }
826 1.1 scw
827 1.1 scw sc->sc_rxdma = ad;
828 1.1 scw ad->ad_segs[0].ds_addr = ad->ad_map->dm_segs[0].ds_addr;
829 1.1 scw ad->ad_segs[0].ds_len = (uintptr_t)end - (uintptr_t)start;
830 1.1 scw
831 1.1 scw /*
832 1.1 scw * Fix up a looping DMA request.
833 1.1 scw * The 'done' function will be called for every 'blksize' bytes
834 1.1 scw * transferred by the DMA engine.
835 1.1 scw */
836 1.1 scw dx = ad->ad_dx;
837 1.1 scw dx->dx_done = acu_rx_loop_segment;
838 1.1 scw dx->dx_peripheral = DMAC_PERIPH_AC97AUDIORX;
839 1.1 scw dx->dx_flow = DMAC_FLOW_CTRL_SRC;
840 1.1 scw dx->dx_loop_notify = blksize;
841 1.4 thorpej dx->dx_desc[DMAC_DESC_DST].xd_addr_hold = false;
842 1.1 scw dx->dx_desc[DMAC_DESC_DST].xd_nsegs = ad->ad_nsegs;
843 1.1 scw dx->dx_desc[DMAC_DESC_DST].xd_dma_segs = ad->ad_segs;
844 1.4 thorpej dx->dx_desc[DMAC_DESC_SRC].xd_addr_hold = true;
845 1.1 scw dx->dx_desc[DMAC_DESC_SRC].xd_nsegs = 1;
846 1.1 scw dx->dx_desc[DMAC_DESC_SRC].xd_dma_segs = &sc->sc_dr;
847 1.1 scw
848 1.1 scw rv = pxa2x0_dmac_start_xfer(dx);
849 1.1 scw
850 1.1 scw if (rv == 0) {
851 1.1 scw /*
852 1.1 scw * XXX: We should only do this once the request has been
853 1.1 scw * loaded into a DMAC channel.
854 1.1 scw */
855 1.1 scw acu_reg_write(sc, AC97_PISR, AC97_FIFOE);
856 1.1 scw acu_reg_write(sc, AC97_PICR, AC97_FEFIE);
857 1.1 scw }
858 1.1 scw
859 1.1 scw return (rv);
860 1.1 scw }
861 1.1 scw
862 1.1 scw static void
863 1.1 scw acu_tx_loop_segment(struct dmac_xfer *dx, int status)
864 1.1 scw {
865 1.1 scw struct acu_softc *sc = dx->dx_cookie;
866 1.1 scw struct acu_dma *ad;
867 1.1 scw
868 1.1 scw if ((ad = sc->sc_txdma) == NULL)
869 1.1 scw panic("acu_tx_loop_segment: bad TX dma descriptor!");
870 1.1 scw
871 1.1 scw if (ad->ad_dx != dx)
872 1.1 scw panic("acu_tx_loop_segment: xfer mismatch!");
873 1.1 scw
874 1.1 scw if (status) {
875 1.8 nonaka aprint_error_dev(sc->sc_dev,
876 1.8 nonaka "acu_tx_loop_segment: non-zero completion status %d\n",
877 1.8 nonaka status);
878 1.1 scw }
879 1.1 scw
880 1.10 jmcneill mutex_spin_enter(&sc->sc_intr_lock);
881 1.1 scw (sc->sc_txfunc)(sc->sc_txarg);
882 1.10 jmcneill mutex_spin_exit(&sc->sc_intr_lock);
883 1.1 scw }
884 1.1 scw
885 1.1 scw static void
886 1.1 scw acu_rx_loop_segment(struct dmac_xfer *dx, int status)
887 1.1 scw {
888 1.1 scw struct acu_softc *sc = dx->dx_cookie;
889 1.1 scw struct acu_dma *ad;
890 1.1 scw
891 1.1 scw if ((ad = sc->sc_rxdma) == NULL)
892 1.1 scw panic("acu_rx_loop_segment: bad RX dma descriptor!");
893 1.1 scw
894 1.1 scw if (ad->ad_dx != dx)
895 1.1 scw panic("acu_rx_loop_segment: xfer mismatch!");
896 1.1 scw
897 1.1 scw if (status) {
898 1.8 nonaka aprint_error_dev(sc->sc_dev,
899 1.8 nonaka "acu_rx_loop_segment: non-zero completion status %d\n",
900 1.8 nonaka status);
901 1.1 scw }
902 1.1 scw
903 1.10 jmcneill mutex_spin_enter(&sc->sc_intr_lock);
904 1.1 scw (sc->sc_rxfunc)(sc->sc_rxarg);
905 1.10 jmcneill mutex_spin_exit(&sc->sc_intr_lock);
906 1.1 scw }
907