fms.c revision 1.35 1 1.35 cegger /* $NetBSD: fms.c,v 1.35 2009/05/06 10:34:32 cegger Exp $ */
2 1.1 augustss
3 1.1 augustss /*-
4 1.1 augustss * Copyright (c) 1999 The NetBSD Foundation, Inc.
5 1.1 augustss * All rights reserved.
6 1.1 augustss *
7 1.1 augustss * This code is derived from software contributed to The NetBSD Foundation
8 1.1 augustss * by Witold J. Wnuk.
9 1.1 augustss *
10 1.1 augustss * Redistribution and use in source and binary forms, with or without
11 1.1 augustss * modification, are permitted provided that the following conditions
12 1.1 augustss * are met:
13 1.1 augustss * 1. Redistributions of source code must retain the above copyright
14 1.1 augustss * notice, this list of conditions and the following disclaimer.
15 1.1 augustss * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 augustss * notice, this list of conditions and the following disclaimer in the
17 1.1 augustss * documentation and/or other materials provided with the distribution.
18 1.1 augustss *
19 1.1 augustss * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 augustss * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 augustss * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 augustss * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 augustss * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 augustss * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 augustss * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 augustss * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 augustss * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 augustss * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 augustss * POSSIBILITY OF SUCH DAMAGE.
30 1.1 augustss */
31 1.1 augustss
32 1.1 augustss /*
33 1.1 augustss * Forte Media FM801 Audio Device Driver
34 1.1 augustss */
35 1.11 lukem
36 1.11 lukem #include <sys/cdefs.h>
37 1.35 cegger __KERNEL_RCSID(0, "$NetBSD: fms.c,v 1.35 2009/05/06 10:34:32 cegger Exp $");
38 1.1 augustss
39 1.8 abs #include "mpu.h"
40 1.8 abs
41 1.1 augustss #include <sys/param.h>
42 1.1 augustss #include <sys/systm.h>
43 1.1 augustss #include <sys/kernel.h>
44 1.1 augustss #include <sys/malloc.h>
45 1.1 augustss #include <sys/device.h>
46 1.1 augustss #include <sys/audioio.h>
47 1.1 augustss
48 1.7 thorpej #include <uvm/uvm_extern.h>
49 1.7 thorpej
50 1.30 ad #include <sys/bus.h>
51 1.30 ad #include <sys/cpu.h>
52 1.1 augustss
53 1.1 augustss #include <dev/pci/pcidevs.h>
54 1.1 augustss #include <dev/pci/pcivar.h>
55 1.1 augustss
56 1.1 augustss #include <dev/audio_if.h>
57 1.1 augustss #include <dev/mulaw.h>
58 1.1 augustss #include <dev/auconv.h>
59 1.1 augustss
60 1.5 thorpej #include <dev/ic/ac97var.h>
61 1.1 augustss #include <dev/ic/mpuvar.h>
62 1.1 augustss
63 1.1 augustss #include <dev/pci/fmsvar.h>
64 1.1 augustss
65 1.1 augustss
66 1.1 augustss struct fms_dma {
67 1.1 augustss struct fms_dma *next;
68 1.29 christos void *addr;
69 1.1 augustss size_t size;
70 1.1 augustss bus_dmamap_t map;
71 1.1 augustss bus_dma_segment_t seg;
72 1.1 augustss };
73 1.1 augustss
74 1.1 augustss
75 1.1 augustss
76 1.35 cegger static int fms_match(device_t, cfdata_t, void *);
77 1.35 cegger static void fms_attach(device_t, device_t, void *);
78 1.24 thorpej static int fms_intr(void *);
79 1.24 thorpej
80 1.24 thorpej static int fms_query_encoding(void *, struct audio_encoding *);
81 1.24 thorpej static int fms_set_params(void *, int, int, audio_params_t *,
82 1.24 thorpej audio_params_t *, stream_filter_list_t *,
83 1.24 thorpej stream_filter_list_t *);
84 1.24 thorpej static int fms_round_blocksize(void *, int, int, const audio_params_t *);
85 1.24 thorpej static int fms_halt_output(void *);
86 1.24 thorpej static int fms_halt_input(void *);
87 1.24 thorpej static int fms_getdev(void *, struct audio_device *);
88 1.24 thorpej static int fms_set_port(void *, mixer_ctrl_t *);
89 1.24 thorpej static int fms_get_port(void *, mixer_ctrl_t *);
90 1.24 thorpej static int fms_query_devinfo(void *, mixer_devinfo_t *);
91 1.24 thorpej static void *fms_malloc(void *, int, size_t, struct malloc_type *, int);
92 1.24 thorpej static void fms_free(void *, void *, struct malloc_type *);
93 1.24 thorpej static size_t fms_round_buffersize(void *, int, size_t);
94 1.24 thorpej static paddr_t fms_mappage(void *, void *, off_t, int);
95 1.24 thorpej static int fms_get_props(void *);
96 1.24 thorpej static int fms_trigger_output(void *, void *, void *, int,
97 1.24 thorpej void (*)(void *), void *,
98 1.24 thorpej const audio_params_t *);
99 1.24 thorpej static int fms_trigger_input(void *, void *, void *, int,
100 1.24 thorpej void (*)(void *), void *,
101 1.24 thorpej const audio_params_t *);
102 1.1 augustss
103 1.14 thorpej CFATTACH_DECL(fms, sizeof (struct fms_softc),
104 1.15 thorpej fms_match, fms_attach, NULL, NULL);
105 1.1 augustss
106 1.24 thorpej static struct audio_device fms_device = {
107 1.1 augustss "Forte Media 801",
108 1.1 augustss "1.0",
109 1.1 augustss "fms"
110 1.1 augustss };
111 1.1 augustss
112 1.1 augustss
113 1.24 thorpej static const struct audio_hw_if fms_hw_if = {
114 1.22 kent NULL, /* open */
115 1.22 kent NULL, /* close */
116 1.1 augustss NULL,
117 1.1 augustss fms_query_encoding,
118 1.1 augustss fms_set_params,
119 1.1 augustss fms_round_blocksize,
120 1.1 augustss NULL,
121 1.1 augustss NULL,
122 1.1 augustss NULL,
123 1.1 augustss NULL,
124 1.1 augustss NULL,
125 1.1 augustss fms_halt_output,
126 1.1 augustss fms_halt_input,
127 1.1 augustss NULL,
128 1.1 augustss fms_getdev,
129 1.1 augustss NULL,
130 1.1 augustss fms_set_port,
131 1.1 augustss fms_get_port,
132 1.1 augustss fms_query_devinfo,
133 1.1 augustss fms_malloc,
134 1.1 augustss fms_free,
135 1.1 augustss fms_round_buffersize,
136 1.1 augustss fms_mappage,
137 1.1 augustss fms_get_props,
138 1.1 augustss fms_trigger_output,
139 1.1 augustss fms_trigger_input,
140 1.10 augustss NULL,
141 1.26 christos NULL,
142 1.1 augustss };
143 1.1 augustss
144 1.24 thorpej static int fms_attach_codec(void *, struct ac97_codec_if *);
145 1.24 thorpej static int fms_read_codec(void *, uint8_t, uint16_t *);
146 1.24 thorpej static int fms_write_codec(void *, uint8_t, uint16_t);
147 1.24 thorpej static int fms_reset_codec(void *);
148 1.1 augustss
149 1.1 augustss #define FM_PCM_VOLUME 0x00
150 1.1 augustss #define FM_FM_VOLUME 0x02
151 1.1 augustss #define FM_I2S_VOLUME 0x04
152 1.1 augustss #define FM_RECORD_SOURCE 0x06
153 1.1 augustss
154 1.1 augustss #define FM_PLAY_CTL 0x08
155 1.1 augustss #define FM_PLAY_RATE_MASK 0x0f00
156 1.1 augustss #define FM_PLAY_BUF1_LAST 0x0001
157 1.1 augustss #define FM_PLAY_BUF2_LAST 0x0002
158 1.1 augustss #define FM_PLAY_START 0x0020
159 1.1 augustss #define FM_PLAY_PAUSE 0x0040
160 1.1 augustss #define FM_PLAY_STOPNOW 0x0080
161 1.1 augustss #define FM_PLAY_16BIT 0x4000
162 1.1 augustss #define FM_PLAY_STEREO 0x8000
163 1.1 augustss
164 1.1 augustss #define FM_PLAY_DMALEN 0x0a
165 1.1 augustss #define FM_PLAY_DMABUF1 0x0c
166 1.1 augustss #define FM_PLAY_DMABUF2 0x10
167 1.1 augustss
168 1.1 augustss
169 1.1 augustss #define FM_REC_CTL 0x14
170 1.1 augustss #define FM_REC_RATE_MASK 0x0f00
171 1.1 augustss #define FM_REC_BUF1_LAST 0x0001
172 1.1 augustss #define FM_REC_BUF2_LAST 0x0002
173 1.1 augustss #define FM_REC_START 0x0020
174 1.1 augustss #define FM_REC_PAUSE 0x0040
175 1.1 augustss #define FM_REC_STOPNOW 0x0080
176 1.1 augustss #define FM_REC_16BIT 0x4000
177 1.1 augustss #define FM_REC_STEREO 0x8000
178 1.1 augustss
179 1.1 augustss
180 1.1 augustss #define FM_REC_DMALEN 0x16
181 1.1 augustss #define FM_REC_DMABUF1 0x18
182 1.1 augustss #define FM_REC_DMABUF2 0x1c
183 1.1 augustss
184 1.1 augustss #define FM_CODEC_CTL 0x22
185 1.1 augustss #define FM_VOLUME 0x26
186 1.1 augustss #define FM_VOLUME_MUTE 0x8000
187 1.1 augustss
188 1.1 augustss #define FM_CODEC_CMD 0x2a
189 1.1 augustss #define FM_CODEC_CMD_READ 0x0080
190 1.1 augustss #define FM_CODEC_CMD_VALID 0x0100
191 1.1 augustss #define FM_CODEC_CMD_BUSY 0x0200
192 1.1 augustss
193 1.1 augustss #define FM_CODEC_DATA 0x2c
194 1.1 augustss
195 1.1 augustss #define FM_IO_CTL 0x52
196 1.1 augustss #define FM_CARD_CTL 0x54
197 1.1 augustss
198 1.1 augustss #define FM_INTMASK 0x56
199 1.1 augustss #define FM_INTMASK_PLAY 0x0001
200 1.1 augustss #define FM_INTMASK_REC 0x0002
201 1.1 augustss #define FM_INTMASK_VOL 0x0040
202 1.1 augustss #define FM_INTMASK_MPU 0x0080
203 1.1 augustss
204 1.1 augustss #define FM_INTSTATUS 0x5a
205 1.1 augustss #define FM_INTSTATUS_PLAY 0x0100
206 1.1 augustss #define FM_INTSTATUS_REC 0x0200
207 1.1 augustss #define FM_INTSTATUS_VOL 0x4000
208 1.1 augustss #define FM_INTSTATUS_MPU 0x8000
209 1.1 augustss
210 1.1 augustss
211 1.24 thorpej static int
212 1.35 cegger fms_match(device_t parent, cfdata_t match, void *aux)
213 1.1 augustss {
214 1.23 kent struct pci_attach_args *pa;
215 1.1 augustss
216 1.23 kent pa = (struct pci_attach_args *)aux;
217 1.1 augustss if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_FORTEMEDIA)
218 1.1 augustss return 0;
219 1.1 augustss if (PCI_PRODUCT(pa->pa_id) != PCI_PRODUCT_FORTEMEDIA_FM801)
220 1.1 augustss return 0;
221 1.23 kent
222 1.1 augustss return 1;
223 1.1 augustss }
224 1.1 augustss
225 1.24 thorpej static void
226 1.35 cegger fms_attach(device_t parent, device_t self, void *aux)
227 1.1 augustss {
228 1.23 kent struct pci_attach_args *pa;
229 1.23 kent struct fms_softc *sc;
230 1.1 augustss struct audio_attach_args aa;
231 1.23 kent const char *intrstr;
232 1.23 kent pci_chipset_tag_t pc;
233 1.23 kent pcitag_t pt;
234 1.1 augustss pci_intr_handle_t ih;
235 1.23 kent uint16_t k1;
236 1.16 thorpej
237 1.23 kent pa = aux;
238 1.23 kent sc = (struct fms_softc *)self;
239 1.23 kent intrstr = NULL;
240 1.23 kent pc = pa->pa_pc;
241 1.23 kent pt = pa->pa_tag;
242 1.16 thorpej aprint_naive(": Audio controller\n");
243 1.16 thorpej aprint_normal(": Forte Media FM-801\n");
244 1.23 kent
245 1.9 sommerfe if (pci_intr_map(pa, &ih)) {
246 1.32 cegger aprint_error_dev(&sc->sc_dev, "couldn't map interrupt\n");
247 1.1 augustss return;
248 1.1 augustss }
249 1.1 augustss intrstr = pci_intr_string(pc, ih);
250 1.23 kent
251 1.1 augustss sc->sc_ih = pci_intr_establish(pc, ih, IPL_AUDIO, fms_intr, sc);
252 1.1 augustss if (sc->sc_ih == NULL) {
253 1.32 cegger aprint_error_dev(&sc->sc_dev, "couldn't establish interrupt");
254 1.1 augustss if (intrstr != NULL)
255 1.16 thorpej aprint_normal(" at %s", intrstr);
256 1.16 thorpej aprint_normal("\n");
257 1.1 augustss return;
258 1.1 augustss }
259 1.23 kent
260 1.1 augustss sc->sc_dmat = pa->pa_dmat;
261 1.23 kent
262 1.32 cegger aprint_normal_dev(&sc->sc_dev, "interrupting at %s\n", intrstr);
263 1.23 kent
264 1.2 augustss if (pci_mapreg_map(pa, 0x10, PCI_MAPREG_TYPE_IO, 0, &sc->sc_iot,
265 1.2 augustss &sc->sc_ioh, &sc->sc_ioaddr, &sc->sc_iosize)) {
266 1.32 cegger aprint_error_dev(&sc->sc_dev, "can't map i/o space\n");
267 1.1 augustss return;
268 1.1 augustss }
269 1.23 kent
270 1.23 kent if (bus_space_subregion(sc->sc_iot, sc->sc_ioh, 0x30, 2,
271 1.2 augustss &sc->sc_mpu_ioh))
272 1.1 augustss panic("fms_attach: can't get mpu subregion handle");
273 1.1 augustss
274 1.2 augustss if (bus_space_subregion(sc->sc_iot, sc->sc_ioh, 0x68, 4,
275 1.2 augustss &sc->sc_opl_ioh))
276 1.1 augustss panic("fms_attach: can't get opl subregion handle");
277 1.1 augustss
278 1.1 augustss /* Disable legacy audio (SBPro compatibility) */
279 1.1 augustss pci_conf_write(pc, pt, 0x40, 0);
280 1.23 kent
281 1.1 augustss /* Reset codec and AC'97 */
282 1.3 augustss bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_CODEC_CTL, 0x0020);
283 1.3 augustss delay(2); /* > 1us according to AC'97 documentation */
284 1.1 augustss bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_CODEC_CTL, 0x0000);
285 1.3 augustss delay(1); /* > 168.2ns according to AC'97 documentation */
286 1.23 kent
287 1.1 augustss /* Set up volume */
288 1.1 augustss bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_PCM_VOLUME, 0x0808);
289 1.1 augustss bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_FM_VOLUME, 0x0808);
290 1.1 augustss bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_I2S_VOLUME, 0x0808);
291 1.23 kent
292 1.1 augustss bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_RECORD_SOURCE, 0x0000);
293 1.23 kent
294 1.1 augustss /* Unmask playback, record and mpu interrupts, mask the rest */
295 1.1 augustss k1 = bus_space_read_2(sc->sc_iot, sc->sc_ioh, FM_INTMASK);
296 1.23 kent bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_INTMASK,
297 1.2 augustss (k1 & ~(FM_INTMASK_PLAY | FM_INTMASK_REC | FM_INTMASK_MPU)) |
298 1.2 augustss FM_INTMASK_VOL);
299 1.23 kent bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_INTSTATUS,
300 1.23 kent FM_INTSTATUS_PLAY | FM_INTSTATUS_REC | FM_INTSTATUS_MPU |
301 1.2 augustss FM_INTSTATUS_VOL);
302 1.23 kent
303 1.1 augustss sc->host_if.arg = sc;
304 1.1 augustss sc->host_if.attach = fms_attach_codec;
305 1.1 augustss sc->host_if.read = fms_read_codec;
306 1.1 augustss sc->host_if.write = fms_write_codec;
307 1.1 augustss sc->host_if.reset = fms_reset_codec;
308 1.1 augustss
309 1.22 kent if (ac97_attach(&sc->host_if, self) != 0)
310 1.1 augustss return;
311 1.1 augustss
312 1.1 augustss audio_attach_mi(&fms_hw_if, sc, &sc->sc_dev);
313 1.1 augustss
314 1.1 augustss aa.type = AUDIODEV_TYPE_OPL;
315 1.1 augustss aa.hwif = NULL;
316 1.1 augustss aa.hdl = NULL;
317 1.1 augustss config_found(&sc->sc_dev, &aa, audioprint);
318 1.1 augustss
319 1.1 augustss aa.type = AUDIODEV_TYPE_MPU;
320 1.1 augustss aa.hwif = NULL;
321 1.1 augustss aa.hdl = NULL;
322 1.1 augustss sc->sc_mpu_dev = config_found(&sc->sc_dev, &aa, audioprint);
323 1.1 augustss }
324 1.1 augustss
325 1.3 augustss /*
326 1.3 augustss * Each AC-link frame takes 20.8us, data should be ready in next frame,
327 1.3 augustss * we allow more than two.
328 1.3 augustss */
329 1.3 augustss #define TIMO 50
330 1.24 thorpej static int
331 1.23 kent fms_read_codec(void *addr, uint8_t reg, uint16_t *val)
332 1.1 augustss {
333 1.23 kent struct fms_softc *sc;
334 1.1 augustss int i;
335 1.2 augustss
336 1.23 kent sc = addr;
337 1.1 augustss /* Poll until codec is ready */
338 1.23 kent for (i = 0; i < TIMO && bus_space_read_2(sc->sc_iot, sc->sc_ioh,
339 1.2 augustss FM_CODEC_CMD) & FM_CODEC_CMD_BUSY; i++)
340 1.3 augustss delay(1);
341 1.2 augustss if (i >= TIMO) {
342 1.1 augustss printf("fms: codec busy\n");
343 1.1 augustss return 1;
344 1.1 augustss }
345 1.1 augustss
346 1.1 augustss /* Write register index, read access */
347 1.23 kent bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_CODEC_CMD,
348 1.2 augustss reg | FM_CODEC_CMD_READ);
349 1.23 kent
350 1.1 augustss /* Poll until we have valid data */
351 1.23 kent for (i = 0; i < TIMO && !(bus_space_read_2(sc->sc_iot, sc->sc_ioh,
352 1.2 augustss FM_CODEC_CMD) & FM_CODEC_CMD_VALID); i++)
353 1.3 augustss delay(1);
354 1.2 augustss if (i >= TIMO) {
355 1.1 augustss printf("fms: no data from codec\n");
356 1.1 augustss return 1;
357 1.1 augustss }
358 1.23 kent
359 1.1 augustss /* Read data */
360 1.1 augustss *val = bus_space_read_2(sc->sc_iot, sc->sc_ioh, FM_CODEC_DATA);
361 1.1 augustss return 0;
362 1.1 augustss }
363 1.1 augustss
364 1.24 thorpej static int
365 1.23 kent fms_write_codec(void *addr, uint8_t reg, uint16_t val)
366 1.1 augustss {
367 1.1 augustss struct fms_softc *sc = addr;
368 1.1 augustss int i;
369 1.23 kent
370 1.1 augustss /* Poll until codec is ready */
371 1.23 kent for (i = 0; i < TIMO && bus_space_read_2(sc->sc_iot, sc->sc_ioh,
372 1.2 augustss FM_CODEC_CMD) & FM_CODEC_CMD_BUSY; i++)
373 1.3 augustss delay(1);
374 1.2 augustss if (i >= TIMO) {
375 1.1 augustss printf("fms: codec busy\n");
376 1.1 augustss return 1;
377 1.1 augustss }
378 1.1 augustss
379 1.1 augustss /* Write data */
380 1.1 augustss bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_CODEC_DATA, val);
381 1.1 augustss /* Write index register, write access */
382 1.1 augustss bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_CODEC_CMD, reg);
383 1.1 augustss return 0;
384 1.1 augustss }
385 1.2 augustss #undef TIMO
386 1.1 augustss
387 1.24 thorpej static int
388 1.23 kent fms_attach_codec(void *addr, struct ac97_codec_if *cif)
389 1.1 augustss {
390 1.23 kent struct fms_softc *sc;
391 1.1 augustss
392 1.23 kent sc = addr;
393 1.1 augustss sc->codec_if = cif;
394 1.1 augustss return 0;
395 1.1 augustss }
396 1.1 augustss
397 1.3 augustss /* Cold Reset */
398 1.24 thorpej static int
399 1.23 kent fms_reset_codec(void *addr)
400 1.1 augustss {
401 1.23 kent struct fms_softc *sc;
402 1.23 kent
403 1.23 kent sc = addr;
404 1.3 augustss bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_CODEC_CTL, 0x0020);
405 1.3 augustss delay(2);
406 1.1 augustss bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_CODEC_CTL, 0x0000);
407 1.3 augustss delay(1);
408 1.19 kent return 0;
409 1.1 augustss }
410 1.1 augustss
411 1.24 thorpej static int
412 1.23 kent fms_intr(void *arg)
413 1.1 augustss {
414 1.31 xtraeme struct fms_softc *sc = arg;
415 1.31 xtraeme #if NMPU > 0
416 1.31 xtraeme struct mpu_softc *sc_mpu = device_private(sc->sc_mpu_dev);
417 1.31 xtraeme #endif
418 1.23 kent uint16_t istat;
419 1.23 kent
420 1.1 augustss istat = bus_space_read_2(sc->sc_iot, sc->sc_ioh, FM_INTSTATUS);
421 1.1 augustss
422 1.1 augustss if (istat & FM_INTSTATUS_PLAY) {
423 1.23 kent if ((sc->sc_play_nextblk += sc->sc_play_blksize) >=
424 1.2 augustss sc->sc_play_end)
425 1.1 augustss sc->sc_play_nextblk = sc->sc_play_start;
426 1.1 augustss
427 1.23 kent bus_space_write_4(sc->sc_iot, sc->sc_ioh,
428 1.23 kent sc->sc_play_flip++ & 1 ?
429 1.2 augustss FM_PLAY_DMABUF2 : FM_PLAY_DMABUF1, sc->sc_play_nextblk);
430 1.1 augustss
431 1.1 augustss if (sc->sc_pintr)
432 1.1 augustss sc->sc_pintr(sc->sc_parg);
433 1.1 augustss else
434 1.1 augustss printf("unexpected play intr\n");
435 1.1 augustss }
436 1.1 augustss
437 1.1 augustss if (istat & FM_INTSTATUS_REC) {
438 1.23 kent if ((sc->sc_rec_nextblk += sc->sc_rec_blksize) >=
439 1.2 augustss sc->sc_rec_end)
440 1.1 augustss sc->sc_rec_nextblk = sc->sc_rec_start;
441 1.1 augustss
442 1.23 kent bus_space_write_4(sc->sc_iot, sc->sc_ioh,
443 1.23 kent sc->sc_rec_flip++ & 1 ?
444 1.2 augustss FM_REC_DMABUF2 : FM_REC_DMABUF1, sc->sc_rec_nextblk);
445 1.1 augustss
446 1.1 augustss if (sc->sc_rintr)
447 1.1 augustss sc->sc_rintr(sc->sc_rarg);
448 1.1 augustss else
449 1.1 augustss printf("unexpected rec intr\n");
450 1.1 augustss }
451 1.23 kent
452 1.8 abs #if NMPU > 0
453 1.1 augustss if (istat & FM_INTSTATUS_MPU)
454 1.31 xtraeme mpu_intr(sc_mpu);
455 1.8 abs #endif
456 1.1 augustss
457 1.23 kent bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_INTSTATUS,
458 1.2 augustss istat & (FM_INTSTATUS_PLAY | FM_INTSTATUS_REC));
459 1.23 kent
460 1.1 augustss return 1;
461 1.1 augustss }
462 1.1 augustss
463 1.24 thorpej static int
464 1.28 christos fms_query_encoding(void *addr, struct audio_encoding *fp)
465 1.1 augustss {
466 1.1 augustss
467 1.1 augustss switch (fp->index) {
468 1.1 augustss case 0:
469 1.1 augustss strcpy(fp->name, AudioEmulaw);
470 1.1 augustss fp->encoding = AUDIO_ENCODING_ULAW;
471 1.1 augustss fp->precision = 8;
472 1.1 augustss fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
473 1.23 kent return 0;
474 1.1 augustss case 1:
475 1.1 augustss strcpy(fp->name, AudioEslinear_le);
476 1.1 augustss fp->encoding = AUDIO_ENCODING_SLINEAR_LE;
477 1.1 augustss fp->precision = 16;
478 1.1 augustss fp->flags = 0;
479 1.1 augustss return 0;
480 1.1 augustss case 2:
481 1.1 augustss strcpy(fp->name, AudioEulinear);
482 1.1 augustss fp->encoding = AUDIO_ENCODING_ULINEAR;
483 1.1 augustss fp->precision = 8;
484 1.1 augustss fp->flags = 0;
485 1.1 augustss return 0;
486 1.1 augustss case 3:
487 1.1 augustss strcpy(fp->name, AudioEalaw);
488 1.1 augustss fp->encoding = AUDIO_ENCODING_ALAW;
489 1.1 augustss fp->precision = 8;
490 1.1 augustss fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
491 1.1 augustss return 0;
492 1.1 augustss case 4:
493 1.1 augustss strcpy(fp->name, AudioEulinear_le);
494 1.1 augustss fp->encoding = AUDIO_ENCODING_ULINEAR_LE;
495 1.1 augustss fp->precision = 16;
496 1.1 augustss fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
497 1.1 augustss return 0;
498 1.1 augustss case 5:
499 1.1 augustss strcpy(fp->name, AudioEslinear);
500 1.1 augustss fp->encoding = AUDIO_ENCODING_SLINEAR;
501 1.1 augustss fp->precision = 8;
502 1.1 augustss fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
503 1.1 augustss return 0;
504 1.1 augustss case 6:
505 1.1 augustss strcpy(fp->name, AudioEulinear_be);
506 1.1 augustss fp->encoding = AUDIO_ENCODING_ULINEAR_BE;
507 1.1 augustss fp->precision = 16;
508 1.1 augustss fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
509 1.1 augustss return 0;
510 1.1 augustss case 7:
511 1.1 augustss strcpy(fp->name, AudioEslinear_be);
512 1.1 augustss fp->encoding = AUDIO_ENCODING_SLINEAR_BE;
513 1.1 augustss fp->precision = 16;
514 1.1 augustss fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
515 1.1 augustss return 0;
516 1.1 augustss default:
517 1.1 augustss return EINVAL;
518 1.1 augustss }
519 1.1 augustss }
520 1.1 augustss
521 1.1 augustss /*
522 1.1 augustss * Range below -limit- is set to -rate-
523 1.1 augustss * What a pity FM801 does not have 24000
524 1.1 augustss * 24000 -> 22050 sounds rather poor
525 1.1 augustss */
526 1.26 christos static struct {
527 1.1 augustss int limit;
528 1.1 augustss int rate;
529 1.26 christos } const fms_rates[11] = {
530 1.1 augustss { 6600, 5500 },
531 1.1 augustss { 8750, 8000 },
532 1.1 augustss { 10250, 9600 },
533 1.1 augustss { 13200, 11025 },
534 1.1 augustss { 17500, 16000 },
535 1.1 augustss { 20500, 19200 },
536 1.1 augustss { 26500, 22050 },
537 1.1 augustss { 35000, 32000 },
538 1.1 augustss { 41000, 38400 },
539 1.1 augustss { 46000, 44100 },
540 1.1 augustss { 48000, 48000 },
541 1.1 augustss /* anything above -> 48000 */
542 1.1 augustss };
543 1.1 augustss
544 1.22 kent #define FMS_NFORMATS 4
545 1.22 kent static const struct audio_format fms_formats[FMS_NFORMATS] = {
546 1.22 kent {NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_SLINEAR_LE, 16, 16,
547 1.22 kent 2, AUFMT_STEREO, 11, {5500, 8000, 9600, 11025, 16000, 19200, 22050,
548 1.22 kent 32000, 38400, 44100, 48000}},
549 1.22 kent {NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_SLINEAR_LE, 16, 16,
550 1.22 kent 1, AUFMT_MONAURAL, 11, {5500, 8000, 9600, 11025, 16000, 19200, 22050,
551 1.22 kent 32000, 38400, 44100, 48000}},
552 1.22 kent {NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_ULINEAR_LE, 8, 8,
553 1.22 kent 2, AUFMT_STEREO, 11, {5500, 8000, 9600, 11025, 16000, 19200, 22050,
554 1.22 kent 32000, 38400, 44100, 48000}},
555 1.22 kent {NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_ULINEAR_LE, 8, 8,
556 1.22 kent 1, AUFMT_MONAURAL, 11, {5500, 8000, 9600, 11025, 16000, 19200, 22050,
557 1.22 kent 32000, 38400, 44100, 48000}},
558 1.22 kent };
559 1.22 kent
560 1.24 thorpej static int
561 1.28 christos fms_set_params(void *addr, int setmode, int usemode,
562 1.27 christos audio_params_t *play, audio_params_t *rec, stream_filter_list_t *pfil,
563 1.27 christos stream_filter_list_t *rfil)
564 1.1 augustss {
565 1.23 kent struct fms_softc *sc;
566 1.22 kent int i, index;
567 1.1 augustss
568 1.23 kent sc = addr;
569 1.1 augustss if (setmode & AUMODE_PLAY) {
570 1.3 augustss for (i = 0; i < 10 && play->sample_rate > fms_rates[i].limit;
571 1.3 augustss i++)
572 1.23 kent continue;
573 1.1 augustss play->sample_rate = fms_rates[i].rate;
574 1.22 kent index = auconv_set_converter(fms_formats, FMS_NFORMATS,
575 1.22 kent AUMODE_PLAY, play, FALSE, pfil);
576 1.22 kent if (index < 0)
577 1.22 kent return EINVAL;
578 1.22 kent sc->sc_play_reg = i << 8;
579 1.22 kent if (fms_formats[index].channels == 2)
580 1.22 kent sc->sc_play_reg |= FM_PLAY_STEREO;
581 1.22 kent if (fms_formats[index].precision == 16)
582 1.22 kent sc->sc_play_reg |= FM_PLAY_16BIT;
583 1.1 augustss }
584 1.1 augustss
585 1.1 augustss if (setmode & AUMODE_RECORD) {
586 1.22 kent for (i = 0; i < 10 && rec->sample_rate > fms_rates[i].limit;
587 1.2 augustss i++)
588 1.23 kent continue;
589 1.1 augustss rec->sample_rate = fms_rates[i].rate;
590 1.22 kent index = auconv_set_converter(fms_formats, FMS_NFORMATS,
591 1.22 kent AUMODE_RECORD, rec, FALSE, rfil);
592 1.22 kent if (index < 0)
593 1.22 kent return EINVAL;
594 1.22 kent sc->sc_rec_reg = i << 8;
595 1.22 kent if (fms_formats[index].channels == 2)
596 1.22 kent sc->sc_rec_reg |= FM_REC_STEREO;
597 1.22 kent if (fms_formats[index].precision == 16)
598 1.22 kent sc->sc_rec_reg |= FM_REC_16BIT;
599 1.1 augustss }
600 1.22 kent
601 1.1 augustss return 0;
602 1.1 augustss }
603 1.1 augustss
604 1.24 thorpej static int
605 1.28 christos fms_round_blocksize(void *addr, int blk, int mode,
606 1.28 christos const audio_params_t *param)
607 1.1 augustss {
608 1.23 kent
609 1.1 augustss return blk & ~0xf;
610 1.1 augustss }
611 1.1 augustss
612 1.24 thorpej static int
613 1.23 kent fms_halt_output(void *addr)
614 1.1 augustss {
615 1.23 kent struct fms_softc *sc;
616 1.23 kent uint16_t k1;
617 1.23 kent
618 1.23 kent sc = addr;
619 1.1 augustss k1 = bus_space_read_2(sc->sc_iot, sc->sc_ioh, FM_PLAY_CTL);
620 1.23 kent bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_PLAY_CTL,
621 1.23 kent (k1 & ~(FM_PLAY_STOPNOW | FM_PLAY_START)) |
622 1.2 augustss FM_PLAY_BUF1_LAST | FM_PLAY_BUF2_LAST);
623 1.23 kent
624 1.1 augustss return 0;
625 1.1 augustss }
626 1.1 augustss
627 1.24 thorpej static int
628 1.23 kent fms_halt_input(void *addr)
629 1.1 augustss {
630 1.23 kent struct fms_softc *sc;
631 1.23 kent uint16_t k1;
632 1.23 kent
633 1.23 kent sc = addr;
634 1.1 augustss k1 = bus_space_read_2(sc->sc_iot, sc->sc_ioh, FM_REC_CTL);
635 1.23 kent bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_REC_CTL,
636 1.2 augustss (k1 & ~(FM_REC_STOPNOW | FM_REC_START)) |
637 1.2 augustss FM_REC_BUF1_LAST | FM_REC_BUF2_LAST);
638 1.23 kent
639 1.1 augustss return 0;
640 1.1 augustss }
641 1.1 augustss
642 1.24 thorpej static int
643 1.28 christos fms_getdev(void *addr, struct audio_device *retp)
644 1.1 augustss {
645 1.23 kent
646 1.1 augustss *retp = fms_device;
647 1.1 augustss return 0;
648 1.1 augustss }
649 1.1 augustss
650 1.24 thorpej static int
651 1.23 kent fms_set_port(void *addr, mixer_ctrl_t *cp)
652 1.1 augustss {
653 1.23 kent struct fms_softc *sc;
654 1.1 augustss
655 1.23 kent sc = addr;
656 1.23 kent return sc->codec_if->vtbl->mixer_set_port(sc->codec_if, cp);
657 1.1 augustss }
658 1.1 augustss
659 1.24 thorpej static int
660 1.23 kent fms_get_port(void *addr, mixer_ctrl_t *cp)
661 1.1 augustss {
662 1.23 kent struct fms_softc *sc;
663 1.23 kent
664 1.23 kent sc = addr;
665 1.23 kent return sc->codec_if->vtbl->mixer_get_port(sc->codec_if, cp);
666 1.1 augustss }
667 1.1 augustss
668 1.24 thorpej static void *
669 1.28 christos fms_malloc(void *addr, int direction, size_t size,
670 1.23 kent struct malloc_type *pool, int flags)
671 1.1 augustss {
672 1.23 kent struct fms_softc *sc;
673 1.1 augustss struct fms_dma *p;
674 1.1 augustss int error;
675 1.1 augustss int rseg;
676 1.23 kent
677 1.23 kent sc = addr;
678 1.1 augustss p = malloc(sizeof(*p), pool, flags);
679 1.23 kent if (p == NULL)
680 1.23 kent return NULL;
681 1.23 kent
682 1.4 tsarna p->size = size;
683 1.7 thorpej if ((error = bus_dmamem_alloc(sc->sc_dmat, size, PAGE_SIZE, 0, &p->seg,
684 1.7 thorpej 1, &rseg, BUS_DMA_NOWAIT)) != 0) {
685 1.32 cegger aprint_error_dev(&sc->sc_dev, "unable to allocate DMA, error = %d\n", error);
686 1.1 augustss goto fail_alloc;
687 1.1 augustss }
688 1.23 kent
689 1.2 augustss if ((error = bus_dmamem_map(sc->sc_dmat, &p->seg, rseg, size, &p->addr,
690 1.2 augustss BUS_DMA_NOWAIT | BUS_DMA_COHERENT)) != 0) {
691 1.32 cegger aprint_error_dev(&sc->sc_dev, "unable to map DMA, error = %d\n",
692 1.32 cegger error);
693 1.1 augustss goto fail_map;
694 1.1 augustss }
695 1.23 kent
696 1.23 kent if ((error = bus_dmamap_create(sc->sc_dmat, size, 1, size, 0,
697 1.2 augustss BUS_DMA_NOWAIT, &p->map)) != 0) {
698 1.32 cegger aprint_error_dev(&sc->sc_dev, "unable to create DMA map, error = %d\n",
699 1.32 cegger error);
700 1.1 augustss goto fail_create;
701 1.1 augustss }
702 1.23 kent
703 1.2 augustss if ((error = bus_dmamap_load(sc->sc_dmat, p->map, p->addr, size, NULL,
704 1.2 augustss BUS_DMA_NOWAIT)) != 0) {
705 1.32 cegger aprint_error_dev(&sc->sc_dev, "unable to load DMA map, error = %d\n",
706 1.32 cegger error);
707 1.1 augustss goto fail_load;
708 1.1 augustss }
709 1.23 kent
710 1.1 augustss p->next = sc->sc_dmas;
711 1.1 augustss sc->sc_dmas = p;
712 1.1 augustss
713 1.1 augustss return p->addr;
714 1.1 augustss
715 1.1 augustss
716 1.1 augustss fail_load:
717 1.1 augustss bus_dmamap_destroy(sc->sc_dmat, p->map);
718 1.1 augustss fail_create:
719 1.1 augustss bus_dmamem_unmap(sc->sc_dmat, p->addr, size);
720 1.1 augustss fail_map:
721 1.1 augustss bus_dmamem_free(sc->sc_dmat, &p->seg, 1);
722 1.1 augustss fail_alloc:
723 1.1 augustss free(p, pool);
724 1.23 kent return NULL;
725 1.1 augustss }
726 1.1 augustss
727 1.24 thorpej static void
728 1.23 kent fms_free(void *addr, void *ptr, struct malloc_type *pool)
729 1.1 augustss {
730 1.23 kent struct fms_softc *sc;
731 1.4 tsarna struct fms_dma **pp, *p;
732 1.4 tsarna
733 1.23 kent sc = addr;
734 1.4 tsarna for (pp = &(sc->sc_dmas); (p = *pp) != NULL; pp = &p->next)
735 1.4 tsarna if (p->addr == ptr) {
736 1.4 tsarna bus_dmamap_unload(sc->sc_dmat, p->map);
737 1.4 tsarna bus_dmamap_destroy(sc->sc_dmat, p->map);
738 1.4 tsarna bus_dmamem_unmap(sc->sc_dmat, p->addr, p->size);
739 1.4 tsarna bus_dmamem_free(sc->sc_dmat, &p->seg, 1);
740 1.23 kent
741 1.4 tsarna *pp = p->next;
742 1.4 tsarna free(p, pool);
743 1.4 tsarna return;
744 1.4 tsarna }
745 1.4 tsarna
746 1.4 tsarna panic("fms_free: trying to free unallocated memory");
747 1.1 augustss }
748 1.1 augustss
749 1.24 thorpej static size_t
750 1.28 christos fms_round_buffersize(void *addr, int direction, size_t size)
751 1.1 augustss {
752 1.23 kent
753 1.1 augustss return size;
754 1.1 augustss }
755 1.1 augustss
756 1.24 thorpej static paddr_t
757 1.23 kent fms_mappage(void *addr, void *mem, off_t off, int prot)
758 1.1 augustss {
759 1.23 kent struct fms_softc *sc;
760 1.1 augustss struct fms_dma *p;
761 1.23 kent
762 1.23 kent sc = addr;
763 1.1 augustss if (off < 0)
764 1.1 augustss return -1;
765 1.23 kent
766 1.1 augustss for (p = sc->sc_dmas; p && p->addr != mem; p = p->next)
767 1.23 kent continue;
768 1.23 kent if (p == NULL)
769 1.1 augustss return -1;
770 1.23 kent
771 1.23 kent return bus_dmamem_mmap(sc->sc_dmat, &p->seg, 1, off, prot,
772 1.2 augustss BUS_DMA_WAITOK);
773 1.1 augustss }
774 1.1 augustss
775 1.24 thorpej static int
776 1.28 christos fms_get_props(void *addr)
777 1.1 augustss {
778 1.23 kent return AUDIO_PROP_MMAP | AUDIO_PROP_INDEPENDENT |
779 1.2 augustss AUDIO_PROP_FULLDUPLEX;
780 1.1 augustss }
781 1.1 augustss
782 1.24 thorpej static int
783 1.23 kent fms_query_devinfo(void *addr, mixer_devinfo_t *dip)
784 1.1 augustss {
785 1.23 kent struct fms_softc *sc;
786 1.1 augustss
787 1.23 kent sc = addr;
788 1.23 kent return sc->codec_if->vtbl->query_devinfo(sc->codec_if, dip);
789 1.1 augustss }
790 1.1 augustss
791 1.24 thorpej static int
792 1.23 kent fms_trigger_output(void *addr, void *start, void *end, int blksize,
793 1.28 christos void (*intr)(void *), void *arg, const audio_params_t *param)
794 1.1 augustss {
795 1.23 kent struct fms_softc *sc;
796 1.1 augustss struct fms_dma *p;
797 1.23 kent
798 1.23 kent sc = addr;
799 1.1 augustss sc->sc_pintr = intr;
800 1.1 augustss sc->sc_parg = arg;
801 1.23 kent
802 1.1 augustss for (p = sc->sc_dmas; p && p->addr != start; p = p->next)
803 1.23 kent continue;
804 1.23 kent
805 1.23 kent if (p == NULL)
806 1.2 augustss panic("fms_trigger_output: request with bad start "
807 1.12 provos "address (%p)", start);
808 1.1 augustss
809 1.1 augustss sc->sc_play_start = p->map->dm_segs[0].ds_addr;
810 1.1 augustss sc->sc_play_end = sc->sc_play_start + ((char *)end - (char *)start);
811 1.1 augustss sc->sc_play_blksize = blksize;
812 1.23 kent sc->sc_play_nextblk = sc->sc_play_start + sc->sc_play_blksize;
813 1.1 augustss sc->sc_play_flip = 0;
814 1.1 augustss bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_PLAY_DMALEN, blksize - 1);
815 1.23 kent bus_space_write_4(sc->sc_iot, sc->sc_ioh, FM_PLAY_DMABUF1,
816 1.2 augustss sc->sc_play_start);
817 1.23 kent bus_space_write_4(sc->sc_iot, sc->sc_ioh, FM_PLAY_DMABUF2,
818 1.2 augustss sc->sc_play_nextblk);
819 1.23 kent bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_PLAY_CTL,
820 1.2 augustss FM_PLAY_START | FM_PLAY_STOPNOW | sc->sc_play_reg);
821 1.1 augustss return 0;
822 1.1 augustss }
823 1.1 augustss
824 1.1 augustss
825 1.24 thorpej static int
826 1.23 kent fms_trigger_input(void *addr, void *start, void *end, int blksize,
827 1.28 christos void (*intr)(void *), void *arg, const audio_params_t *param)
828 1.1 augustss {
829 1.23 kent struct fms_softc *sc;
830 1.1 augustss struct fms_dma *p;
831 1.23 kent
832 1.23 kent sc = addr;
833 1.1 augustss sc->sc_rintr = intr;
834 1.1 augustss sc->sc_rarg = arg;
835 1.23 kent
836 1.1 augustss for (p = sc->sc_dmas; p && p->addr != start; p = p->next)
837 1.23 kent continue;
838 1.23 kent
839 1.23 kent if (p == NULL)
840 1.2 augustss panic("fms_trigger_input: request with bad start "
841 1.12 provos "address (%p)", start);
842 1.1 augustss
843 1.1 augustss sc->sc_rec_start = p->map->dm_segs[0].ds_addr;
844 1.1 augustss sc->sc_rec_end = sc->sc_rec_start + ((char *)end - (char *)start);
845 1.1 augustss sc->sc_rec_blksize = blksize;
846 1.23 kent sc->sc_rec_nextblk = sc->sc_rec_start + sc->sc_rec_blksize;
847 1.1 augustss sc->sc_rec_flip = 0;
848 1.1 augustss bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_REC_DMALEN, blksize - 1);
849 1.23 kent bus_space_write_4(sc->sc_iot, sc->sc_ioh, FM_REC_DMABUF1,
850 1.2 augustss sc->sc_rec_start);
851 1.23 kent bus_space_write_4(sc->sc_iot, sc->sc_ioh, FM_REC_DMABUF2,
852 1.2 augustss sc->sc_rec_nextblk);
853 1.23 kent bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_REC_CTL,
854 1.2 augustss FM_REC_START | FM_REC_STOPNOW | sc->sc_rec_reg);
855 1.1 augustss return 0;
856 1.1 augustss }
857