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