Sync usage with man page.
[netbsd-mini2440.git] / sys / dev / pci / yds.c
blobb892353871207ba2aab3401b4063b7888c0d581f
1 /* $NetBSD: yds.c,v 1.47 2009/11/26 15:17:10 njoly Exp $ */
3 /*
4 * Copyright (c) 2000, 2001 Kazuki Sakamoto and Minoura Makoto.
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * Yamaha YMF724[B-F]/740[B-C]/744/754
31 * Documentation links:
32 * - ftp://ftp.alsa-project.org/pub/manuals/yamaha/
33 * - ftp://ftp.alsa-project.org/pub/manuals/yamaha/pci/
35 * TODO:
36 * - FM synth volume (difficult: mixed before ac97)
37 * - Digital in/out (SPDIF) support
38 * - Effect??
41 #include <sys/cdefs.h>
42 __KERNEL_RCSID(0, "$NetBSD: yds.c,v 1.47 2009/11/26 15:17:10 njoly Exp $");
44 #include "mpu.h"
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
49 #include <sys/fcntl.h>
50 #include <sys/malloc.h>
51 #include <sys/device.h>
52 #include <sys/proc.h>
54 #include <dev/pci/pcidevs.h>
55 #include <dev/pci/pcireg.h>
56 #include <dev/pci/pcivar.h>
58 #include <sys/audioio.h>
59 #include <dev/audio_if.h>
60 #include <dev/mulaw.h>
61 #include <dev/auconv.h>
62 #include <dev/ic/ac97reg.h>
63 #include <dev/ic/ac97var.h>
64 #include <dev/ic/mpuvar.h>
66 #include <sys/bus.h>
67 #include <sys/intr.h>
69 #include <dev/microcode/yds/yds_hwmcode.h>
70 #include <dev/pci/ydsreg.h>
71 #include <dev/pci/ydsvar.h>
73 /* Debug */
74 #undef YDS_USE_REC_SLOT
75 #define YDS_USE_P44
77 #ifdef AUDIO_DEBUG
78 # define DPRINTF(x) if (ydsdebug) printf x
79 # define DPRINTFN(n,x) if (ydsdebug>(n)) printf x
80 int ydsdebug = 0;
81 #else
82 # define DPRINTF(x)
83 # define DPRINTFN(n,x)
84 #endif
85 #ifdef YDS_USE_REC_SLOT
86 # define YDS_INPUT_SLOT 0 /* REC slot = ADC + loopbacks */
87 #else
88 # define YDS_INPUT_SLOT 1 /* ADC slot */
89 #endif
91 static int yds_match(device_t, cfdata_t, void *);
92 static void yds_attach(device_t, device_t, void *);
93 static int yds_intr(void *);
95 #define DMAADDR(p) ((p)->map->dm_segs[0].ds_addr)
96 #define KERNADDR(p) ((void *)((p)->addr))
98 static int yds_allocmem(struct yds_softc *, size_t, size_t,
99 struct yds_dma *);
100 static int yds_freemem(struct yds_softc *, struct yds_dma *);
102 #ifndef AUDIO_DEBUG
103 #define YWRITE1(sc, r, x) bus_space_write_1((sc)->memt, (sc)->memh, (r), (x))
104 #define YWRITE2(sc, r, x) bus_space_write_2((sc)->memt, (sc)->memh, (r), (x))
105 #define YWRITE4(sc, r, x) bus_space_write_4((sc)->memt, (sc)->memh, (r), (x))
106 #define YREAD1(sc, r) bus_space_read_1((sc)->memt, (sc)->memh, (r))
107 #define YREAD2(sc, r) bus_space_read_2((sc)->memt, (sc)->memh, (r))
108 #define YREAD4(sc, r) bus_space_read_4((sc)->memt, (sc)->memh, (r))
109 #else
110 static uint16_t YREAD2(struct yds_softc *sc, bus_size_t r)
112 DPRINTFN(5, (" YREAD2(0x%lX)\n", (unsigned long)r));
113 return bus_space_read_2(sc->memt, sc->memh, r);
116 static uint32_t YREAD4(struct yds_softc *sc, bus_size_t r)
118 DPRINTFN(5, (" YREAD4(0x%lX)\n", (unsigned long)r));
119 return bus_space_read_4(sc->memt, sc->memh, r);
122 #ifdef notdef
123 static void YWRITE1(struct yds_softc *sc, bus_size_t r, uint8_t x)
125 DPRINTFN(5, (" YWRITE1(0x%lX,0x%lX)\n", (unsigned long)r,
126 (unsigned long)x));
127 bus_space_write_1(sc->memt, sc->memh, r, x);
129 #endif
131 static void YWRITE2(struct yds_softc *sc, bus_size_t r, uint16_t x)
133 DPRINTFN(5, (" YWRITE2(0x%lX,0x%lX)\n", (unsigned long)r,
134 (unsigned long)x));
135 bus_space_write_2(sc->memt, sc->memh, r, x);
138 static void YWRITE4(struct yds_softc *sc, bus_size_t r, uint32_t x)
140 DPRINTFN(5, (" YWRITE4(0x%lX,0x%lX)\n", (unsigned long)r,
141 (unsigned long)x));
142 bus_space_write_4(sc->memt, sc->memh, r, x);
144 #endif
146 #define YWRITEREGION4(sc, r, x, c) \
147 bus_space_write_region_4((sc)->memt, (sc)->memh, (r), (x), (c) / 4)
149 CFATTACH_DECL_NEW(yds, sizeof(struct yds_softc),
150 yds_match, yds_attach, NULL, NULL);
152 static int yds_open(void *, int);
153 static void yds_close(void *);
154 static int yds_query_encoding(void *, struct audio_encoding *);
155 static int yds_set_params(void *, int, int, audio_params_t *,
156 audio_params_t *, stream_filter_list_t *,
157 stream_filter_list_t *);
158 static int yds_round_blocksize(void *, int, int, const audio_params_t *);
159 static int yds_trigger_output(void *, void *, void *, int,
160 void (*)(void *), void *,
161 const audio_params_t *);
162 static int yds_trigger_input(void *, void *, void *, int,
163 void (*)(void *), void *,
164 const audio_params_t *);
165 static int yds_halt_output(void *);
166 static int yds_halt_input(void *);
167 static int yds_getdev(void *, struct audio_device *);
168 static int yds_mixer_set_port(void *, mixer_ctrl_t *);
169 static int yds_mixer_get_port(void *, mixer_ctrl_t *);
170 static void *yds_malloc(void *, int, size_t, struct malloc_type *, int);
171 static void yds_free(void *, void *, struct malloc_type *);
172 static size_t yds_round_buffersize(void *, int, size_t);
173 static paddr_t yds_mappage(void *, void *, off_t, int);
174 static int yds_get_props(void *);
175 static int yds_query_devinfo(void *, mixer_devinfo_t *);
177 static int yds_attach_codec(void *, struct ac97_codec_if *);
178 static int yds_read_codec(void *, uint8_t, uint16_t *);
179 static int yds_write_codec(void *, uint8_t, uint16_t);
180 static int yds_reset_codec(void *);
182 static u_int yds_get_dstype(int);
183 static int yds_download_mcode(struct yds_softc *);
184 static int yds_allocate_slots(struct yds_softc *);
185 static void yds_configure_legacy(device_t);
186 static void yds_enable_dsp(struct yds_softc *);
187 static int yds_disable_dsp(struct yds_softc *);
188 static int yds_ready_codec(struct yds_codec_softc *);
189 static int yds_halt(struct yds_softc *);
190 static uint32_t yds_get_lpfq(u_int);
191 static uint32_t yds_get_lpfk(u_int);
192 static struct yds_dma *yds_find_dma(struct yds_softc *, void *);
194 static int yds_init(struct yds_softc *);
196 #ifdef AUDIO_DEBUG
197 static void yds_dump_play_slot(struct yds_softc *, int);
198 #define YDS_DUMP_PLAY_SLOT(n, sc, bank) \
199 if (ydsdebug > (n)) yds_dump_play_slot(sc, bank)
200 #else
201 #define YDS_DUMP_PLAY_SLOT(n, sc, bank)
202 #endif /* AUDIO_DEBUG */
204 static const struct audio_hw_if yds_hw_if = {
205 yds_open,
206 yds_close,
207 NULL,
208 yds_query_encoding,
209 yds_set_params,
210 yds_round_blocksize,
211 NULL,
212 NULL,
213 NULL,
214 NULL,
215 NULL,
216 yds_halt_output,
217 yds_halt_input,
218 NULL,
219 yds_getdev,
220 NULL,
221 yds_mixer_set_port,
222 yds_mixer_get_port,
223 yds_query_devinfo,
224 yds_malloc,
225 yds_free,
226 yds_round_buffersize,
227 yds_mappage,
228 yds_get_props,
229 yds_trigger_output,
230 yds_trigger_input,
231 NULL,
232 NULL, /* powerstate */
235 static const struct audio_device yds_device = {
236 "Yamaha DS-1",
238 "yds"
241 static const struct {
242 uint id;
243 u_int flags;
244 #define YDS_CAP_MCODE_1 0x0001
245 #define YDS_CAP_MCODE_1E 0x0002
246 #define YDS_CAP_LEGACY_SELECTABLE 0x0004
247 #define YDS_CAP_LEGACY_FLEXIBLE 0x0008
248 #define YDS_CAP_HAS_P44 0x0010
249 } yds_chip_capabliity_list[] = {
250 { PCI_PRODUCT_YAMAHA_YMF724,
251 YDS_CAP_MCODE_1|YDS_CAP_LEGACY_SELECTABLE },
252 /* 740[C] has only 32 slots. But anyway we use only 2 */
253 { PCI_PRODUCT_YAMAHA_YMF740,
254 YDS_CAP_MCODE_1|YDS_CAP_LEGACY_SELECTABLE }, /* XXX NOT TESTED */
255 { PCI_PRODUCT_YAMAHA_YMF740C,
256 YDS_CAP_MCODE_1E|YDS_CAP_LEGACY_SELECTABLE },
257 { PCI_PRODUCT_YAMAHA_YMF724F,
258 YDS_CAP_MCODE_1E|YDS_CAP_LEGACY_SELECTABLE },
259 { PCI_PRODUCT_YAMAHA_YMF744B,
260 YDS_CAP_MCODE_1E|YDS_CAP_LEGACY_FLEXIBLE },
261 { PCI_PRODUCT_YAMAHA_YMF754,
262 YDS_CAP_MCODE_1E|YDS_CAP_LEGACY_FLEXIBLE|YDS_CAP_HAS_P44 },
263 { 0, 0 }
265 #ifdef AUDIO_DEBUG
266 #define YDS_CAP_BITS "\020\005P44\004LEGFLEX\003LEGSEL\002MCODE1E\001MCODE1"
267 #endif
269 static const struct audio_format yds_formats[] = {
270 {NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_SLINEAR_LE, 16, 16,
271 1, AUFMT_MONAURAL, 0, {4000, 48000}},
272 {NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_SLINEAR_LE, 16, 16,
273 2, AUFMT_STEREO, 0, {4000, 48000}},
274 {NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_ULINEAR_LE, 8, 8,
275 1, AUFMT_MONAURAL, 0, {4000, 48000}},
276 {NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_ULINEAR_LE, 8, 8,
277 2, AUFMT_STEREO, 0, {4000, 48000}},
279 #define YDS_NFORMATS (sizeof(yds_formats) / sizeof(struct audio_format))
281 #ifdef AUDIO_DEBUG
282 static void
283 yds_dump_play_slot(struct yds_softc *sc, int bank)
285 int i, j;
286 uint32_t *p;
287 uint32_t num;
288 char *pa;
290 for (i = 0; i < N_PLAY_SLOTS; i++) {
291 printf("pbankp[%d] = %p,", i*2, sc->pbankp[i*2]);
292 printf("pbankp[%d] = %p\n", i*2+1, sc->pbankp[i*2+1]);
295 pa = (char *)DMAADDR(&sc->sc_ctrldata) + sc->pbankoff;
296 p = (uint32_t *)sc->ptbl;
297 printf("ptbl + 0: %d\n", *p++);
298 for (i = 0; i < N_PLAY_SLOTS; i++) {
299 printf("ptbl + %d: 0x%x, should be %p\n",
300 i+1, *p,
301 pa + i * sizeof(struct play_slot_ctrl_bank) *
302 N_PLAY_SLOT_CTRL_BANK);
303 p++;
306 num = le32toh(*(uint32_t*)sc->ptbl);
307 printf("numofplay = %d\n", num);
309 for (i = 0; i < num; i++) {
310 p = (uint32_t *)sc->pbankp[i*2];
312 printf(" pbankp[%d], bank 0 : %p\n", i*2, p);
313 for (j = 0;
314 j < sizeof(struct play_slot_ctrl_bank) / sizeof(uint32_t);
315 j++) {
316 printf(" 0x%02x: 0x%08x\n",
317 (unsigned)(j * sizeof(uint32_t)),
318 (unsigned)*p++);
321 p = (uint32_t *)sc->pbankp[i*2 + 1];
322 printf(" pbankp[%d], bank 1 : %p\n", i*2 + 1, p);
323 for (j = 0;
324 j < sizeof(struct play_slot_ctrl_bank) / sizeof(uint32_t);
325 j++) {
326 printf(" 0x%02x: 0x%08x\n",
327 (unsigned)(j * sizeof(uint32_t)),
328 (unsigned)*p++);
332 #endif /* AUDIO_DEBUG */
334 static u_int
335 yds_get_dstype(int id)
337 int i;
339 for (i = 0; yds_chip_capabliity_list[i].id; i++) {
340 if (PCI_PRODUCT(id) == yds_chip_capabliity_list[i].id)
341 return yds_chip_capabliity_list[i].flags;
344 return -1;
347 static int
348 yds_download_mcode(struct yds_softc *sc)
350 static struct {
351 const uint32_t *mcode;
352 size_t size;
353 } ctrls[] = {
354 {yds_ds1_ctrl_mcode, sizeof(yds_ds1_ctrl_mcode)},
355 {yds_ds1e_ctrl_mcode, sizeof(yds_ds1e_ctrl_mcode)},
357 u_int ctrl;
358 const uint32_t *p;
359 size_t size;
360 int dstype;
362 if (sc->sc_flags & YDS_CAP_MCODE_1)
363 dstype = YDS_DS_1;
364 else if (sc->sc_flags & YDS_CAP_MCODE_1E)
365 dstype = YDS_DS_1E;
366 else
367 return 1; /* unknown */
369 if (yds_disable_dsp(sc))
370 return 1;
372 /* Software reset */
373 YWRITE4(sc, YDS_MODE, YDS_MODE_RESET);
374 YWRITE4(sc, YDS_MODE, 0);
376 YWRITE4(sc, YDS_MAPOF_REC, 0);
377 YWRITE4(sc, YDS_MAPOF_EFFECT, 0);
378 YWRITE4(sc, YDS_PLAY_CTRLBASE, 0);
379 YWRITE4(sc, YDS_REC_CTRLBASE, 0);
380 YWRITE4(sc, YDS_EFFECT_CTRLBASE, 0);
381 YWRITE4(sc, YDS_WORK_BASE, 0);
383 ctrl = YREAD2(sc, YDS_GLOBAL_CONTROL);
384 YWRITE2(sc, YDS_GLOBAL_CONTROL, ctrl & ~0x0007);
386 /* Download DSP microcode. */
387 p = yds_dsp_mcode;
388 size = sizeof(yds_dsp_mcode);
389 YWRITEREGION4(sc, YDS_DSP_INSTRAM, p, size);
391 /* Download CONTROL microcode. */
392 p = ctrls[dstype].mcode;
393 size = ctrls[dstype].size;
394 YWRITEREGION4(sc, YDS_CTRL_INSTRAM, p, size);
396 yds_enable_dsp(sc);
397 delay(10 * 1000); /* nessesary on my 724F (??) */
399 return 0;
402 static int
403 yds_allocate_slots(struct yds_softc *sc)
405 size_t pcs, rcs, ecs, ws, memsize;
406 void *mp;
407 uint32_t da; /* DMA address */
408 char *va; /* KVA */
409 off_t cb;
410 int i;
411 struct yds_dma *p;
413 /* Alloc DSP Control Data */
414 pcs = YREAD4(sc, YDS_PLAY_CTRLSIZE) * sizeof(uint32_t);
415 rcs = YREAD4(sc, YDS_REC_CTRLSIZE) * sizeof(uint32_t);
416 ecs = YREAD4(sc, YDS_EFFECT_CTRLSIZE) * sizeof(uint32_t);
417 ws = WORK_SIZE;
418 YWRITE4(sc, YDS_WORK_SIZE, ws / sizeof(uint32_t));
420 DPRINTF(("play control size : %d\n", (unsigned int)pcs));
421 DPRINTF(("rec control size : %d\n", (unsigned int)rcs));
422 DPRINTF(("eff control size : %d\n", (unsigned int)ecs));
423 DPRINTF(("work size : %d\n", (unsigned int)ws));
424 #ifdef DIAGNOSTIC
425 if (pcs != sizeof(struct play_slot_ctrl_bank)) {
426 aprint_error_dev(sc->sc_dev, "invalid play slot ctrldata %d != %d\n",
427 (unsigned int)pcs,
428 (unsigned int)sizeof(struct play_slot_ctrl_bank));
429 if (rcs != sizeof(struct rec_slot_ctrl_bank))
430 aprint_error_dev(sc->sc_dev, "invalid rec slot ctrldata %d != %d\n",
431 (unsigned int)rcs,
432 (unsigned int)sizeof(struct rec_slot_ctrl_bank));
434 #endif
436 memsize = N_PLAY_SLOTS*N_PLAY_SLOT_CTRL_BANK*pcs +
437 N_REC_SLOT_CTRL*N_REC_SLOT_CTRL_BANK*rcs + ws;
438 memsize += (N_PLAY_SLOTS+1)*sizeof(uint32_t);
440 p = &sc->sc_ctrldata;
441 if (KERNADDR(p) == NULL) {
442 i = yds_allocmem(sc, memsize, 16, p);
443 if (i) {
444 aprint_error_dev(sc->sc_dev, "couldn't alloc/map DSP DMA buffer, reason %d\n", i);
445 free(p, M_DEVBUF);
446 return 1;
449 mp = KERNADDR(p);
450 da = DMAADDR(p);
452 DPRINTF(("mp:%p, DMA addr:%p\n",
453 mp, (void *)sc->sc_ctrldata.map->dm_segs[0].ds_addr));
455 memset(mp, 0, memsize);
457 /* Work space */
458 cb = 0;
459 va = (uint8_t *)mp;
460 YWRITE4(sc, YDS_WORK_BASE, da + cb);
461 cb += ws;
463 /* Play control data table */
464 sc->ptbl = (uint32_t *)(va + cb);
465 sc->ptbloff = cb;
466 YWRITE4(sc, YDS_PLAY_CTRLBASE, da + cb);
467 cb += (N_PLAY_SLOT_CTRL + 1) * sizeof(uint32_t);
469 /* Record slot control data */
470 sc->rbank = (struct rec_slot_ctrl_bank *)(va + cb);
471 YWRITE4(sc, YDS_REC_CTRLBASE, da + cb);
472 sc->rbankoff = cb;
473 cb += N_REC_SLOT_CTRL * N_REC_SLOT_CTRL_BANK * rcs;
475 #if 0
476 /* Effect slot control data -- unused */
477 YWRITE4(sc, YDS_EFFECT_CTRLBASE, da + cb);
478 cb += N_EFFECT_SLOT_CTRL * N_EFFECT_SLOT_CTRL_BANK * ecs;
479 #endif
481 /* Play slot control data */
482 sc->pbankoff = cb;
483 for (i=0; i < N_PLAY_SLOT_CTRL; i++) {
484 sc->pbankp[i*2] = (struct play_slot_ctrl_bank *)(va + cb);
485 *(sc->ptbl + i+1) = htole32(da + cb);
486 cb += pcs;
488 sc->pbankp[i*2+1] = (struct play_slot_ctrl_bank *)(va + cb);
489 cb += pcs;
491 /* Sync play control data table */
492 bus_dmamap_sync(sc->sc_dmatag, p->map,
493 sc->ptbloff, (N_PLAY_SLOT_CTRL+1) * sizeof(uint32_t),
494 BUS_DMASYNC_PREWRITE);
496 return 0;
499 static void
500 yds_enable_dsp(struct yds_softc *sc)
503 YWRITE4(sc, YDS_CONFIG, YDS_DSP_SETUP);
506 static int
507 yds_disable_dsp(struct yds_softc *sc)
509 int to;
510 uint32_t data;
512 data = YREAD4(sc, YDS_CONFIG);
513 if (data)
514 YWRITE4(sc, YDS_CONFIG, YDS_DSP_DISABLE);
516 for (to = 0; to < YDS_WORK_TIMEOUT; to++) {
517 if ((YREAD4(sc, YDS_STATUS) & YDS_STAT_WORK) == 0)
518 return 0;
519 delay(1);
522 return 1;
525 static int
526 yds_match(device_t parent, cfdata_t match, void *aux)
528 struct pci_attach_args *pa;
530 pa = (struct pci_attach_args *)aux;
531 switch (PCI_VENDOR(pa->pa_id)) {
532 case PCI_VENDOR_YAMAHA:
533 switch (PCI_PRODUCT(pa->pa_id)) {
534 case PCI_PRODUCT_YAMAHA_YMF724:
535 case PCI_PRODUCT_YAMAHA_YMF740:
536 case PCI_PRODUCT_YAMAHA_YMF740C:
537 case PCI_PRODUCT_YAMAHA_YMF724F:
538 case PCI_PRODUCT_YAMAHA_YMF744B:
539 case PCI_PRODUCT_YAMAHA_YMF754:
540 return 1;
542 break;
545 return 0;
549 * This routine is called after all the ISA devices are configured,
550 * to avoid conflict.
552 static void
553 yds_configure_legacy(device_t self)
554 #define FLEXIBLE (sc->sc_flags & YDS_CAP_LEGACY_FLEXIBLE)
555 #define SELECTABLE (sc->sc_flags & YDS_CAP_LEGACY_SELECTABLE)
557 static const bus_addr_t opl_addrs[] = {0x388, 0x398, 0x3A0, 0x3A8};
558 static const bus_addr_t mpu_addrs[] = {0x330, 0x300, 0x332, 0x334};
559 struct yds_softc *sc;
560 pcireg_t reg;
561 device_t dev;
562 int i;
564 sc = device_private(self);
565 if (!FLEXIBLE && !SELECTABLE)
566 return;
568 reg = pci_conf_read(sc->sc_pc, sc->sc_pcitag, YDS_PCI_LEGACY);
569 reg &= ~0x8133c03f; /* these bits are out of interest */
570 reg |= ((YDS_PCI_EX_LEGACY_IMOD) |
571 (YDS_PCI_LEGACY_FMEN |
572 YDS_PCI_LEGACY_MEN /*| YDS_PCI_LEGACY_MIEN*/));
573 reg |= YDS_PCI_EX_LEGACY_SMOD_DISABLE;
574 if (FLEXIBLE) {
575 pci_conf_write(sc->sc_pc, sc->sc_pcitag, YDS_PCI_LEGACY, reg);
576 delay(100*1000);
579 /* Look for OPL */
580 dev = 0;
581 for (i = 0; i < sizeof(opl_addrs) / sizeof(bus_addr_t); i++) {
582 if (SELECTABLE) {
583 pci_conf_write(sc->sc_pc, sc->sc_pcitag,
584 YDS_PCI_LEGACY, reg | (i << (0+16)));
585 delay(100*1000); /* wait 100ms */
586 } else
587 pci_conf_write(sc->sc_pc, sc->sc_pcitag,
588 YDS_PCI_FM_BA, opl_addrs[i]);
589 if (bus_space_map(sc->sc_opl_iot,
590 opl_addrs[i], 4, 0, &sc->sc_opl_ioh) == 0) {
591 struct audio_attach_args aa;
593 aa.type = AUDIODEV_TYPE_OPL;
594 aa.hwif = aa.hdl = NULL;
595 dev = config_found(self, &aa, audioprint);
596 if (dev == 0)
597 bus_space_unmap(sc->sc_opl_iot,
598 sc->sc_opl_ioh, 4);
599 else {
600 if (SELECTABLE)
601 reg |= (i << (0+16));
602 break;
606 if (dev == 0) {
607 reg &= ~YDS_PCI_LEGACY_FMEN;
608 pci_conf_write(sc->sc_pc, sc->sc_pcitag,
609 YDS_PCI_LEGACY, reg);
610 } else {
611 /* Max. volume */
612 YWRITE4(sc, YDS_LEGACY_OUT_VOLUME, 0x3fff3fff);
613 YWRITE4(sc, YDS_LEGACY_REC_VOLUME, 0x3fff3fff);
616 /* Look for MPU */
617 dev = NULL;
618 for (i = 0; i < sizeof(mpu_addrs) / sizeof(bus_addr_t); i++) {
619 if (SELECTABLE)
620 pci_conf_write(sc->sc_pc, sc->sc_pcitag,
621 YDS_PCI_LEGACY, reg | (i << (4+16)));
622 else
623 pci_conf_write(sc->sc_pc, sc->sc_pcitag,
624 YDS_PCI_MPU_BA, mpu_addrs[i]);
625 if (bus_space_map(sc->sc_mpu_iot,
626 mpu_addrs[i], 2, 0, &sc->sc_mpu_ioh) == 0) {
627 struct audio_attach_args aa;
629 aa.type = AUDIODEV_TYPE_MPU;
630 aa.hwif = aa.hdl = NULL;
631 dev = config_found(self, &aa, audioprint);
632 if (dev == 0)
633 bus_space_unmap(sc->sc_mpu_iot,
634 sc->sc_mpu_ioh, 2);
635 else {
636 if (SELECTABLE)
637 reg |= (i << (4+16));
638 break;
642 if (dev == 0) {
643 reg &= ~(YDS_PCI_LEGACY_MEN | YDS_PCI_LEGACY_MIEN);
644 pci_conf_write(sc->sc_pc, sc->sc_pcitag, YDS_PCI_LEGACY, reg);
646 sc->sc_mpu = dev;
648 #undef FLEXIBLE
649 #undef SELECTABLE
651 static int
652 yds_init(struct yds_softc *sc)
654 uint32_t reg;
656 DPRINTF(("yds_init()\n"));
658 /* Download microcode */
659 if (yds_download_mcode(sc)) {
660 aprint_error_dev(sc->sc_dev, "download microcode failed\n");
661 return 1;
664 /* Allocate DMA buffers */
665 if (yds_allocate_slots(sc)) {
666 aprint_error_dev(sc->sc_dev, "could not allocate slots\n");
667 return 1;
670 /* Warm reset */
671 reg = pci_conf_read(sc->sc_pc, sc->sc_pcitag, YDS_PCI_DSCTRL);
672 pci_conf_write(sc->sc_pc, sc->sc_pcitag, YDS_PCI_DSCTRL,
673 reg | YDS_DSCTRL_WRST);
674 delay(50000);
676 return 0;
679 static bool
680 yds_suspend(device_t dv, pmf_qual_t qual)
682 struct yds_softc *sc = device_private(dv);
683 pci_chipset_tag_t pc = sc->sc_pc;
684 pcitag_t tag = sc->sc_pcitag;
686 sc->sc_dsctrl = pci_conf_read(pc, tag, YDS_PCI_DSCTRL);
687 sc->sc_legacy = pci_conf_read(pc, tag, YDS_PCI_LEGACY);
688 sc->sc_ba[0] = pci_conf_read(pc, tag, YDS_PCI_FM_BA);
689 sc->sc_ba[1] = pci_conf_read(pc, tag, YDS_PCI_MPU_BA);
691 return true;
694 static bool
695 yds_resume(device_t dv, pmf_qual_t qual)
697 struct yds_softc *sc = device_private(dv);
698 pci_chipset_tag_t pc = sc->sc_pc;
699 pcitag_t tag = sc->sc_pcitag;
700 pcireg_t reg;
702 /* Disable legacy mode */
703 reg = pci_conf_read(pc, tag, YDS_PCI_LEGACY);
704 pci_conf_write(pc, tag, YDS_PCI_LEGACY, reg & YDS_PCI_LEGACY_LAD);
706 /* Enable the device. */
707 reg = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
708 reg |= (PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MEM_ENABLE |
709 PCI_COMMAND_MASTER_ENABLE);
710 pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG, reg);
711 reg = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
712 if (yds_init(sc)) {
713 aprint_error_dev(dv, "reinitialize failed\n");
714 return false;
717 pci_conf_write(pc, tag, YDS_PCI_DSCTRL, sc->sc_dsctrl);
718 sc->sc_codec[0].codec_if->vtbl->restore_ports(sc->sc_codec[0].codec_if);
720 return true;
723 static void
724 yds_attach(device_t parent, device_t self, void *aux)
726 struct yds_softc *sc;
727 struct pci_attach_args *pa;
728 pci_chipset_tag_t pc;
729 char const *intrstr;
730 pci_intr_handle_t ih;
731 pcireg_t reg;
732 struct yds_codec_softc *codec;
733 char devinfo[256];
734 int i, r, to;
735 int revision;
736 int ac97_id2;
738 sc = device_private(self);
739 sc->sc_dev = self;
740 pa = (struct pci_attach_args *)aux;
741 pc = pa->pa_pc;
742 pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
743 revision = PCI_REVISION(pa->pa_class);
744 printf(": %s (rev. 0x%02x)\n", devinfo, revision);
746 /* Map register to memory */
747 if (pci_mapreg_map(pa, YDS_PCI_MBA, PCI_MAPREG_TYPE_MEM, 0,
748 &sc->memt, &sc->memh, NULL, NULL)) {
749 aprint_error_dev(self, "can't map memory space\n");
750 return;
753 /* Map and establish the interrupt. */
754 if (pci_intr_map(pa, &ih)) {
755 aprint_error_dev(self, "couldn't map interrupt\n");
756 return;
758 intrstr = pci_intr_string(pc, ih);
759 sc->sc_ih = pci_intr_establish(pc, ih, IPL_AUDIO, yds_intr, sc);
760 if (sc->sc_ih == NULL) {
761 aprint_error_dev(self, "couldn't establish interrupt");
762 if (intrstr != NULL)
763 aprint_error(" at %s", intrstr);
764 aprint_error("\n");
765 return;
767 aprint_normal_dev(self, "interrupting at %s\n", intrstr);
769 sc->sc_dmatag = pa->pa_dmat;
770 sc->sc_pc = pc;
771 sc->sc_pcitag = pa->pa_tag;
772 sc->sc_id = pa->pa_id;
773 sc->sc_revision = revision;
774 sc->sc_flags = yds_get_dstype(sc->sc_id);
775 #ifdef AUDIO_DEBUG
776 if (ydsdebug) {
777 char bits[80];
779 snprintb(bits, sizeof(bits), YDS_CAP_BITS, sc->sc_flags);
780 printf("%s: chip has %s\n", device_xname(self), bits);
782 #endif
784 /* Disable legacy mode */
785 reg = pci_conf_read(pc, pa->pa_tag, YDS_PCI_LEGACY);
786 pci_conf_write(pc, pa->pa_tag, YDS_PCI_LEGACY,
787 reg & YDS_PCI_LEGACY_LAD);
789 /* Enable the device. */
790 reg = pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
791 reg |= (PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MEM_ENABLE |
792 PCI_COMMAND_MASTER_ENABLE);
793 pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, reg);
794 reg = pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
796 /* Mute all volumes */
797 for (i = 0x80; i < 0xc0; i += 2)
798 YWRITE2(sc, i, 0);
800 /* Initialize the device */
801 if (yds_init(sc)) {
802 aprint_error_dev(self, "initialize failed\n");
803 return;
807 * Detect primary/secondary AC97
808 * YMF754 Hardware Specification Rev 1.01 page 24
810 reg = pci_conf_read(pc, pa->pa_tag, YDS_PCI_DSCTRL);
811 pci_conf_write(pc, pa->pa_tag, YDS_PCI_DSCTRL, reg & ~YDS_DSCTRL_CRST);
812 delay(400000); /* Needed for 740C. */
814 /* Primary */
815 for (to = 0; to < AC97_TIMEOUT; to++) {
816 if ((YREAD2(sc, AC97_STAT_ADDR1) & AC97_BUSY) == 0)
817 break;
818 delay(1);
820 if (to == AC97_TIMEOUT) {
821 aprint_error_dev(self, "no AC97 available\n");
822 return;
825 /* Secondary */
826 /* Secondary AC97 is used for 4ch audio. Currently unused. */
827 ac97_id2 = -1;
828 if ((YREAD2(sc, YDS_ACTIVITY) & YDS_ACTIVITY_DOCKA) == 0)
829 goto detected;
830 #if 0 /* reset secondary... */
831 YWRITE2(sc, YDS_GPIO_OCTRL,
832 YREAD2(sc, YDS_GPIO_OCTRL) & ~YDS_GPIO_GPO2);
833 YWRITE2(sc, YDS_GPIO_FUNCE,
834 (YREAD2(sc, YDS_GPIO_FUNCE)&(~YDS_GPIO_GPC2))|YDS_GPIO_GPE2);
835 #endif
836 for (to = 0; to < AC97_TIMEOUT; to++) {
837 if ((YREAD2(sc, AC97_STAT_ADDR2) & AC97_BUSY) == 0)
838 break;
839 delay(1);
841 if (to < AC97_TIMEOUT) {
842 /* detect id */
843 for (ac97_id2 = 1; ac97_id2 < 4; ac97_id2++) {
844 YWRITE2(sc, AC97_CMD_ADDR,
845 AC97_CMD_READ | AC97_ID(ac97_id2) | 0x28);
847 for (to = 0; to < AC97_TIMEOUT; to++) {
848 if ((YREAD2(sc, AC97_STAT_ADDR2) & AC97_BUSY)
849 == 0)
850 goto detected;
851 delay(1);
854 if (ac97_id2 == 4)
855 ac97_id2 = -1;
856 detected:
860 pci_conf_write(pc, pa->pa_tag, YDS_PCI_DSCTRL, reg | YDS_DSCTRL_CRST);
861 delay (20);
862 pci_conf_write(pc, pa->pa_tag, YDS_PCI_DSCTRL, reg & ~YDS_DSCTRL_CRST);
863 delay (400000);
864 for (to = 0; to < AC97_TIMEOUT; to++) {
865 if ((YREAD2(sc, AC97_STAT_ADDR1) & AC97_BUSY) == 0)
866 break;
867 delay(1);
871 * Attach ac97 codec
873 for (i = 0; i < 2; i++) {
874 static struct {
875 int data;
876 int addr;
877 } statregs[] = {
878 {AC97_STAT_DATA1, AC97_STAT_ADDR1},
879 {AC97_STAT_DATA2, AC97_STAT_ADDR2},
882 if (i == 1 && ac97_id2 == -1)
883 break; /* secondary ac97 not available */
885 codec = &sc->sc_codec[i];
886 codec->sc = sc;
887 codec->id = i == 1 ? ac97_id2 : 0;
888 codec->status_data = statregs[i].data;
889 codec->status_addr = statregs[i].addr;
890 codec->host_if.arg = codec;
891 codec->host_if.attach = yds_attach_codec;
892 codec->host_if.read = yds_read_codec;
893 codec->host_if.write = yds_write_codec;
894 codec->host_if.reset = yds_reset_codec;
896 if ((r = ac97_attach(&codec->host_if, self)) != 0) {
897 aprint_error_dev(self, "can't attach codec (error 0x%X)\n", r);
898 return;
902 if (0 != auconv_create_encodings(yds_formats, YDS_NFORMATS,
903 &sc->sc_encodings))
904 return;
906 audio_attach_mi(&yds_hw_if, sc, self);
908 sc->sc_legacy_iot = pa->pa_iot;
909 config_defer(self, yds_configure_legacy);
911 if (!pmf_device_register(self, yds_suspend, yds_resume))
912 aprint_error_dev(self, "couldn't establish power handler\n");
915 static int
916 yds_attach_codec(void *sc_, struct ac97_codec_if *codec_if)
918 struct yds_codec_softc *sc;
920 sc = sc_;
921 sc->codec_if = codec_if;
922 return 0;
925 static int
926 yds_ready_codec(struct yds_codec_softc *sc)
928 int to;
930 for (to = 0; to < AC97_TIMEOUT; to++) {
931 if ((YREAD2(sc->sc, sc->status_addr) & AC97_BUSY) == 0)
932 return 0;
933 delay(1);
936 return 1;
939 static int
940 yds_read_codec(void *sc_, uint8_t reg, uint16_t *data)
942 struct yds_codec_softc *sc;
944 sc = sc_;
945 YWRITE2(sc->sc, AC97_CMD_ADDR, AC97_CMD_READ | AC97_ID(sc->id) | reg);
947 if (yds_ready_codec(sc)) {
948 aprint_error_dev(sc->sc->sc_dev, "yds_read_codec timeout\n");
949 return EIO;
952 if (PCI_PRODUCT(sc->sc->sc_id) == PCI_PRODUCT_YAMAHA_YMF744B &&
953 sc->sc->sc_revision < 2) {
954 int i;
955 for (i=0; i<600; i++)
956 (void)YREAD2(sc->sc, sc->status_data);
959 *data = YREAD2(sc->sc, sc->status_data);
961 return 0;
964 static int
965 yds_write_codec(void *sc_, uint8_t reg, uint16_t data)
967 struct yds_codec_softc *sc;
969 sc = sc_;
970 YWRITE2(sc->sc, AC97_CMD_ADDR, AC97_CMD_WRITE | AC97_ID(sc->id) | reg);
971 YWRITE2(sc->sc, AC97_CMD_DATA, data);
973 if (yds_ready_codec(sc)) {
974 aprint_error_dev(sc->sc->sc_dev, "yds_write_codec timeout\n");
975 return EIO;
978 return 0;
982 * XXX: Must handle the secondary differntly!!
984 static int
985 yds_reset_codec(void *sc_)
987 struct yds_codec_softc *codec;
988 struct yds_softc *sc;
989 pcireg_t reg;
991 codec = sc_;
992 sc = codec->sc;
993 /* reset AC97 codec */
994 reg = pci_conf_read(sc->sc_pc, sc->sc_pcitag, YDS_PCI_DSCTRL);
995 if (reg & 0x03) {
996 pci_conf_write(sc->sc_pc, sc->sc_pcitag,
997 YDS_PCI_DSCTRL, reg & ~0x03);
998 pci_conf_write(sc->sc_pc, sc->sc_pcitag,
999 YDS_PCI_DSCTRL, reg | 0x03);
1000 pci_conf_write(sc->sc_pc, sc->sc_pcitag,
1001 YDS_PCI_DSCTRL, reg & ~0x03);
1002 delay(50000);
1005 yds_ready_codec(sc_);
1006 return 0;
1009 static int
1010 yds_intr(void *p)
1012 struct yds_softc *sc = p;
1013 #if NMPU > 0
1014 struct mpu_softc *sc_mpu = device_private(sc->sc_mpu);
1015 #endif
1016 u_int status;
1018 status = YREAD4(sc, YDS_STATUS);
1019 DPRINTFN(1, ("yds_intr: status=%08x\n", status));
1020 if ((status & (YDS_STAT_INT|YDS_STAT_TINT)) == 0) {
1021 #if NMPU > 0
1022 if (sc_mpu)
1023 return mpu_intr(sc_mpu);
1024 #endif
1025 return 0;
1028 if (status & YDS_STAT_TINT) {
1029 YWRITE4(sc, YDS_STATUS, YDS_STAT_TINT);
1030 printf ("yds_intr: timeout!\n");
1033 if (status & YDS_STAT_INT) {
1034 int nbank;
1036 nbank = (YREAD4(sc, YDS_CONTROL_SELECT) == 0);
1037 /* Clear interrupt flag */
1038 YWRITE4(sc, YDS_STATUS, YDS_STAT_INT);
1040 /* Buffer for the next frame is always ready. */
1041 YWRITE4(sc, YDS_MODE, YREAD4(sc, YDS_MODE) | YDS_MODE_ACTV2);
1043 if (sc->sc_play.intr) {
1044 u_int dma, ccpu, blk, len;
1046 /* Sync play slot control data */
1047 bus_dmamap_sync(sc->sc_dmatag, sc->sc_ctrldata.map,
1048 sc->pbankoff,
1049 sizeof(struct play_slot_ctrl_bank)*
1050 le32toh(*sc->ptbl)*
1051 N_PLAY_SLOT_CTRL_BANK,
1052 BUS_DMASYNC_POSTWRITE|
1053 BUS_DMASYNC_POSTREAD);
1054 dma = le32toh(sc->pbankp[nbank]->pgstart) * sc->sc_play.factor;
1055 ccpu = sc->sc_play.offset;
1056 blk = sc->sc_play.blksize;
1057 len = sc->sc_play.length;
1059 if (((dma > ccpu) && (dma - ccpu > blk * 2)) ||
1060 ((ccpu > dma) && (dma + len - ccpu > blk * 2))) {
1061 /* We can fill the next block */
1062 /* Sync ring buffer for previous write */
1063 bus_dmamap_sync(sc->sc_dmatag,
1064 sc->sc_play.dma->map,
1065 ccpu, blk,
1066 BUS_DMASYNC_POSTWRITE);
1067 sc->sc_play.intr(sc->sc_play.intr_arg);
1068 sc->sc_play.offset += blk;
1069 if (sc->sc_play.offset >= len) {
1070 sc->sc_play.offset -= len;
1071 #ifdef DIAGNOSTIC
1072 if (sc->sc_play.offset != 0)
1073 printf ("Audio ringbuffer botch\n");
1074 #endif
1076 /* Sync ring buffer for next write */
1077 bus_dmamap_sync(sc->sc_dmatag,
1078 sc->sc_play.dma->map,
1079 ccpu, blk,
1080 BUS_DMASYNC_PREWRITE);
1083 if (sc->sc_rec.intr) {
1084 u_int dma, ccpu, blk, len;
1086 /* Sync rec slot control data */
1087 bus_dmamap_sync(sc->sc_dmatag, sc->sc_ctrldata.map,
1088 sc->rbankoff,
1089 sizeof(struct rec_slot_ctrl_bank)*
1090 N_REC_SLOT_CTRL*
1091 N_REC_SLOT_CTRL_BANK,
1092 BUS_DMASYNC_POSTWRITE|
1093 BUS_DMASYNC_POSTREAD);
1094 dma = le32toh(sc->rbank[YDS_INPUT_SLOT*2 + nbank].pgstartadr);
1095 ccpu = sc->sc_rec.offset;
1096 blk = sc->sc_rec.blksize;
1097 len = sc->sc_rec.length;
1099 if (((dma > ccpu) && (dma - ccpu > blk * 2)) ||
1100 ((ccpu > dma) && (dma + len - ccpu > blk * 2))) {
1101 /* We can drain the current block */
1102 /* Sync ring buffer first */
1103 bus_dmamap_sync(sc->sc_dmatag,
1104 sc->sc_rec.dma->map,
1105 ccpu, blk,
1106 BUS_DMASYNC_POSTREAD);
1107 sc->sc_rec.intr(sc->sc_rec.intr_arg);
1108 sc->sc_rec.offset += blk;
1109 if (sc->sc_rec.offset >= len) {
1110 sc->sc_rec.offset -= len;
1111 #ifdef DIAGNOSTIC
1112 if (sc->sc_rec.offset != 0)
1113 printf ("Audio ringbuffer botch\n");
1114 #endif
1116 /* Sync ring buffer for next read */
1117 bus_dmamap_sync(sc->sc_dmatag,
1118 sc->sc_rec.dma->map,
1119 ccpu, blk,
1120 BUS_DMASYNC_PREREAD);
1125 return 1;
1128 static int
1129 yds_allocmem(struct yds_softc *sc, size_t size, size_t align, struct yds_dma *p)
1131 int error;
1133 p->size = size;
1134 error = bus_dmamem_alloc(sc->sc_dmatag, p->size, align, 0,
1135 p->segs, sizeof(p->segs)/sizeof(p->segs[0]),
1136 &p->nsegs, BUS_DMA_NOWAIT);
1137 if (error)
1138 return error;
1140 error = bus_dmamem_map(sc->sc_dmatag, p->segs, p->nsegs, p->size,
1141 &p->addr, BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
1142 if (error)
1143 goto free;
1145 error = bus_dmamap_create(sc->sc_dmatag, p->size, 1, p->size,
1146 0, BUS_DMA_NOWAIT, &p->map);
1147 if (error)
1148 goto unmap;
1150 error = bus_dmamap_load(sc->sc_dmatag, p->map, p->addr, p->size, NULL,
1151 BUS_DMA_NOWAIT);
1152 if (error)
1153 goto destroy;
1154 return 0;
1156 destroy:
1157 bus_dmamap_destroy(sc->sc_dmatag, p->map);
1158 unmap:
1159 bus_dmamem_unmap(sc->sc_dmatag, p->addr, p->size);
1160 free:
1161 bus_dmamem_free(sc->sc_dmatag, p->segs, p->nsegs);
1162 return error;
1165 static int
1166 yds_freemem(struct yds_softc *sc, struct yds_dma *p)
1169 bus_dmamap_unload(sc->sc_dmatag, p->map);
1170 bus_dmamap_destroy(sc->sc_dmatag, p->map);
1171 bus_dmamem_unmap(sc->sc_dmatag, p->addr, p->size);
1172 bus_dmamem_free(sc->sc_dmatag, p->segs, p->nsegs);
1173 return 0;
1176 static int
1177 yds_open(void *addr, int flags)
1179 struct yds_softc *sc;
1180 uint32_t mode;
1182 sc = addr;
1183 /* Select bank 0. */
1184 YWRITE4(sc, YDS_CONTROL_SELECT, 0);
1186 /* Start the DSP operation. */
1187 mode = YREAD4(sc, YDS_MODE);
1188 mode |= YDS_MODE_ACTV;
1189 mode &= ~YDS_MODE_ACTV2;
1190 YWRITE4(sc, YDS_MODE, mode);
1192 return 0;
1196 * Close function is called at splaudio().
1198 static void
1199 yds_close(void *addr)
1202 yds_halt(addr);
1205 static int
1206 yds_query_encoding(void *addr, struct audio_encoding *fp)
1208 struct yds_softc *sc;
1210 sc = addr;
1211 return auconv_query_encoding(sc->sc_encodings, fp);
1214 static int
1215 yds_set_params(void *addr, int setmode, int usemode,
1216 audio_params_t *play, audio_params_t* rec,
1217 stream_filter_list_t *pfil, stream_filter_list_t *rfil)
1219 if (setmode & AUMODE_RECORD) {
1220 if (auconv_set_converter(yds_formats, YDS_NFORMATS,
1221 AUMODE_RECORD, rec, FALSE, rfil) < 0)
1222 return EINVAL;
1224 if (setmode & AUMODE_PLAY) {
1225 if (auconv_set_converter(yds_formats, YDS_NFORMATS,
1226 AUMODE_PLAY, play, FALSE, pfil) < 0)
1227 return EINVAL;
1229 return 0;
1232 static int
1233 yds_round_blocksize(void *addr, int blk, int mode,
1234 const audio_params_t *param)
1238 * Block size must be bigger than a frame.
1239 * That is 1024bytes at most, i.e. for 48000Hz, 16bit, 2ch.
1241 if (blk < 1024)
1242 blk = 1024;
1244 return blk & ~4;
1247 static uint32_t
1248 yds_get_lpfq(u_int sample_rate)
1250 int i;
1251 static struct lpfqt {
1252 u_int rate;
1253 uint32_t lpfq;
1254 } lpfqt[] = {
1255 {8000, 0x32020000},
1256 {11025, 0x31770000},
1257 {16000, 0x31390000},
1258 {22050, 0x31c90000},
1259 {32000, 0x33d00000},
1260 {48000, 0x40000000},
1261 {0, 0}
1264 if (sample_rate == 44100) /* for P44 slot? */
1265 return 0x370A0000;
1267 for (i = 0; lpfqt[i].rate != 0; i++)
1268 if (sample_rate <= lpfqt[i].rate)
1269 break;
1271 return lpfqt[i].lpfq;
1274 static uint32_t
1275 yds_get_lpfk(u_int sample_rate)
1277 int i;
1278 static struct lpfkt {
1279 u_int rate;
1280 uint32_t lpfk;
1281 } lpfkt[] = {
1282 {8000, 0x18b20000},
1283 {11025, 0x20930000},
1284 {16000, 0x2b9a0000},
1285 {22050, 0x35a10000},
1286 {32000, 0x3eaa0000},
1287 {48000, 0x40000000},
1288 {0, 0}
1291 if (sample_rate == 44100) /* for P44 slot? */
1292 return 0x46460000;
1294 for (i = 0; lpfkt[i].rate != 0; i++)
1295 if (sample_rate <= lpfkt[i].rate)
1296 break;
1298 return lpfkt[i].lpfk;
1301 static int
1302 yds_trigger_output(void *addr, void *start, void *end, int blksize,
1303 void (*intr)(void *), void *arg, const audio_params_t *param)
1304 #define P44 (sc->sc_flags & YDS_CAP_HAS_P44)
1306 struct yds_softc *sc;
1307 struct yds_dma *p;
1308 struct play_slot_ctrl_bank *psb;
1309 const u_int gain = 0x40000000;
1310 bus_addr_t s;
1311 size_t l;
1312 int i;
1313 int p44, channels;
1314 uint32_t format;
1316 sc = addr;
1317 #ifdef DIAGNOSTIC
1318 if (sc->sc_play.intr)
1319 panic("yds_trigger_output: already running");
1320 #endif
1322 sc->sc_play.intr = intr;
1323 sc->sc_play.intr_arg = arg;
1324 sc->sc_play.offset = 0;
1325 sc->sc_play.blksize = blksize;
1327 DPRINTFN(1, ("yds_trigger_output: sc=%p start=%p end=%p "
1328 "blksize=%d intr=%p(%p)\n", addr, start, end, blksize, intr, arg));
1330 p = yds_find_dma(sc, start);
1331 if (!p) {
1332 printf("yds_trigger_output: bad addr %p\n", start);
1333 return EINVAL;
1335 sc->sc_play.dma = p;
1337 #ifdef YDS_USE_P44
1338 /* The document says the P44 SRC supports only stereo, 16bit PCM. */
1339 if (P44)
1340 p44 = ((param->sample_rate == 44100) &&
1341 (param->channels == 2) &&
1342 (param->precision == 16));
1343 else
1344 #endif
1345 p44 = 0;
1346 channels = p44 ? 1 : param->channels;
1348 s = DMAADDR(p);
1349 l = ((char *)end - (char *)start);
1350 sc->sc_play.length = l;
1352 *sc->ptbl = htole32(channels); /* Num of play */
1354 sc->sc_play.factor = 1;
1355 if (param->channels == 2)
1356 sc->sc_play.factor *= 2;
1357 if (param->precision != 8)
1358 sc->sc_play.factor *= 2;
1359 l /= sc->sc_play.factor;
1361 format = ((channels == 2 ? PSLT_FORMAT_STEREO : 0) |
1362 (param->precision == 8 ? PSLT_FORMAT_8BIT : 0) |
1363 (p44 ? PSLT_FORMAT_SRC441 : 0));
1365 psb = sc->pbankp[0];
1366 memset(psb, 0, sizeof(*psb));
1367 psb->format = htole32(format);
1368 psb->pgbase = htole32(s);
1369 psb->pgloopend = htole32(l);
1370 if (!p44) {
1371 psb->pgdeltaend = htole32((param->sample_rate * 65536 / 48000) << 12);
1372 psb->lpfkend = htole32(yds_get_lpfk(param->sample_rate));
1373 psb->eggainend = htole32(gain);
1374 psb->lpfq = htole32(yds_get_lpfq(param->sample_rate));
1375 psb->pgdelta = htole32(psb->pgdeltaend);
1376 psb->lpfk = htole32(yds_get_lpfk(param->sample_rate));
1377 psb->eggain = htole32(gain);
1380 for (i = 0; i < channels; i++) {
1381 /* i == 0: left or mono, i == 1: right */
1382 psb = sc->pbankp[i*2];
1383 if (i)
1384 /* copy from left */
1385 *psb = *(sc->pbankp[0]);
1386 if (channels == 2) {
1387 /* stereo */
1388 if (i == 0) {
1389 psb->lchgain = psb->lchgainend = htole32(gain);
1390 } else {
1391 psb->lchgain = psb->lchgainend = 0;
1392 psb->rchgain = psb->rchgainend = htole32(gain);
1393 psb->format |= htole32(PSLT_FORMAT_RCH);
1395 } else if (!p44) {
1396 /* mono */
1397 psb->lchgain = psb->rchgain = htole32(gain);
1398 psb->lchgainend = psb->rchgainend = htole32(gain);
1400 /* copy to the other bank */
1401 *(sc->pbankp[i*2+1]) = *psb;
1404 YDS_DUMP_PLAY_SLOT(5, sc, 0);
1405 YDS_DUMP_PLAY_SLOT(5, sc, 1);
1407 if (p44)
1408 YWRITE4(sc, YDS_P44_OUT_VOLUME, 0x3fff3fff);
1409 else
1410 YWRITE4(sc, YDS_DAC_OUT_VOLUME, 0x3fff3fff);
1412 /* Now the play slot for the next frame is set up!! */
1413 /* Sync play slot control data for both directions */
1414 bus_dmamap_sync(sc->sc_dmatag, sc->sc_ctrldata.map,
1415 sc->ptbloff,
1416 sizeof(struct play_slot_ctrl_bank) *
1417 channels * N_PLAY_SLOT_CTRL_BANK,
1418 BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
1419 /* Sync ring buffer */
1420 bus_dmamap_sync(sc->sc_dmatag, p->map, 0, blksize,
1421 BUS_DMASYNC_PREWRITE);
1422 /* HERE WE GO!! */
1423 YWRITE4(sc, YDS_MODE,
1424 YREAD4(sc, YDS_MODE) | YDS_MODE_ACTV | YDS_MODE_ACTV2);
1426 return 0;
1428 #undef P44
1430 static int
1431 yds_trigger_input(void *addr, void *start, void *end, int blksize,
1432 void (*intr)(void *), void *arg, const audio_params_t *param)
1434 struct yds_softc *sc;
1435 struct yds_dma *p;
1436 u_int srate, format;
1437 struct rec_slot_ctrl_bank *rsb;
1438 bus_addr_t s;
1439 size_t l;
1441 sc = addr;
1442 #ifdef DIAGNOSTIC
1443 if (sc->sc_rec.intr)
1444 panic("yds_trigger_input: already running");
1445 #endif
1446 sc->sc_rec.intr = intr;
1447 sc->sc_rec.intr_arg = arg;
1448 sc->sc_rec.offset = 0;
1449 sc->sc_rec.blksize = blksize;
1451 DPRINTFN(1, ("yds_trigger_input: "
1452 "sc=%p start=%p end=%p blksize=%d intr=%p(%p)\n",
1453 addr, start, end, blksize, intr, arg));
1454 DPRINTFN(1, (" parameters: rate=%u, precision=%u, channels=%u\n",
1455 param->sample_rate, param->precision, param->channels));
1457 p = yds_find_dma(sc, start);
1458 if (!p) {
1459 printf("yds_trigger_input: bad addr %p\n", start);
1460 return EINVAL;
1462 sc->sc_rec.dma = p;
1464 s = DMAADDR(p);
1465 l = ((char *)end - (char *)start);
1466 sc->sc_rec.length = l;
1468 sc->sc_rec.factor = 1;
1469 if (param->channels == 2)
1470 sc->sc_rec.factor *= 2;
1471 if (param->precision != 8)
1472 sc->sc_rec.factor *= 2;
1474 rsb = &sc->rbank[0];
1475 memset(rsb, 0, sizeof(*rsb));
1476 rsb->pgbase = htole32(s);
1477 rsb->pgloopendadr = htole32(l);
1478 /* Seems all 4 banks must be set up... */
1479 sc->rbank[1] = *rsb;
1480 sc->rbank[2] = *rsb;
1481 sc->rbank[3] = *rsb;
1483 YWRITE4(sc, YDS_ADC_IN_VOLUME, 0x3fff3fff);
1484 YWRITE4(sc, YDS_REC_IN_VOLUME, 0x3fff3fff);
1485 srate = 48000 * 4096 / param->sample_rate - 1;
1486 format = ((param->precision == 8 ? YDS_FORMAT_8BIT : 0) |
1487 (param->channels == 2 ? YDS_FORMAT_STEREO : 0));
1488 DPRINTF(("srate=%d, format=%08x\n", srate, format));
1489 #ifdef YDS_USE_REC_SLOT
1490 YWRITE4(sc, YDS_DAC_REC_VOLUME, 0x3fff3fff);
1491 YWRITE4(sc, YDS_P44_REC_VOLUME, 0x3fff3fff);
1492 YWRITE4(sc, YDS_MAPOF_REC, YDS_RECSLOT_VALID);
1493 YWRITE4(sc, YDS_REC_SAMPLE_RATE, srate);
1494 YWRITE4(sc, YDS_REC_FORMAT, format);
1495 #else
1496 YWRITE4(sc, YDS_MAPOF_REC, YDS_ADCSLOT_VALID);
1497 YWRITE4(sc, YDS_ADC_SAMPLE_RATE, srate);
1498 YWRITE4(sc, YDS_ADC_FORMAT, format);
1499 #endif
1500 /* Now the rec slot for the next frame is set up!! */
1501 /* Sync record slot control data */
1502 bus_dmamap_sync(sc->sc_dmatag, sc->sc_ctrldata.map,
1503 sc->rbankoff,
1504 sizeof(struct rec_slot_ctrl_bank)*
1505 N_REC_SLOT_CTRL*
1506 N_REC_SLOT_CTRL_BANK,
1507 BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
1508 /* Sync ring buffer */
1509 bus_dmamap_sync(sc->sc_dmatag, p->map, 0, blksize,
1510 BUS_DMASYNC_PREREAD);
1511 /* HERE WE GO!! */
1512 YWRITE4(sc, YDS_MODE,
1513 YREAD4(sc, YDS_MODE) | YDS_MODE_ACTV | YDS_MODE_ACTV2);
1515 return 0;
1518 static int
1519 yds_halt(struct yds_softc *sc)
1521 uint32_t mode;
1523 /* Stop the DSP operation. */
1524 mode = YREAD4(sc, YDS_MODE);
1525 YWRITE4(sc, YDS_MODE, mode & ~(YDS_MODE_ACTV|YDS_MODE_ACTV2));
1527 /* Paranoia... mute all */
1528 YWRITE4(sc, YDS_P44_OUT_VOLUME, 0);
1529 YWRITE4(sc, YDS_DAC_OUT_VOLUME, 0);
1530 YWRITE4(sc, YDS_ADC_IN_VOLUME, 0);
1531 YWRITE4(sc, YDS_REC_IN_VOLUME, 0);
1532 YWRITE4(sc, YDS_DAC_REC_VOLUME, 0);
1533 YWRITE4(sc, YDS_P44_REC_VOLUME, 0);
1535 return 0;
1538 static int
1539 yds_halt_output(void *addr)
1541 struct yds_softc *sc;
1543 DPRINTF(("yds: yds_halt_output\n"));
1544 sc = addr;
1545 if (sc->sc_play.intr) {
1546 sc->sc_play.intr = 0;
1547 /* Sync play slot control data */
1548 bus_dmamap_sync(sc->sc_dmatag, sc->sc_ctrldata.map,
1549 sc->pbankoff,
1550 sizeof(struct play_slot_ctrl_bank)*
1551 (*sc->ptbl)*N_PLAY_SLOT_CTRL_BANK,
1552 BUS_DMASYNC_POSTWRITE|BUS_DMASYNC_POSTREAD);
1553 /* Stop the play slot operation */
1554 sc->pbankp[0]->status =
1555 sc->pbankp[1]->status =
1556 sc->pbankp[2]->status =
1557 sc->pbankp[3]->status = 1;
1558 /* Sync ring buffer */
1559 bus_dmamap_sync(sc->sc_dmatag, sc->sc_play.dma->map,
1560 0, sc->sc_play.length, BUS_DMASYNC_POSTWRITE);
1563 return 0;
1566 static int
1567 yds_halt_input(void *addr)
1569 struct yds_softc *sc;
1571 DPRINTF(("yds: yds_halt_input\n"));
1572 sc = addr;
1573 sc->sc_rec.intr = NULL;
1574 if (sc->sc_rec.intr) {
1575 /* Stop the rec slot operation */
1576 YWRITE4(sc, YDS_MAPOF_REC, 0);
1577 sc->sc_rec.intr = 0;
1578 /* Sync rec slot control data */
1579 bus_dmamap_sync(sc->sc_dmatag, sc->sc_ctrldata.map,
1580 sc->rbankoff,
1581 sizeof(struct rec_slot_ctrl_bank)*
1582 N_REC_SLOT_CTRL*N_REC_SLOT_CTRL_BANK,
1583 BUS_DMASYNC_POSTWRITE|BUS_DMASYNC_POSTREAD);
1584 /* Sync ring buffer */
1585 bus_dmamap_sync(sc->sc_dmatag, sc->sc_rec.dma->map,
1586 0, sc->sc_rec.length, BUS_DMASYNC_POSTREAD);
1589 return 0;
1592 static int
1593 yds_getdev(void *addr, struct audio_device *retp)
1596 *retp = yds_device;
1597 return 0;
1600 static int
1601 yds_mixer_set_port(void *addr, mixer_ctrl_t *cp)
1603 struct yds_softc *sc;
1605 sc = addr;
1606 return sc->sc_codec[0].codec_if->vtbl->mixer_set_port(
1607 sc->sc_codec[0].codec_if, cp);
1610 static int
1611 yds_mixer_get_port(void *addr, mixer_ctrl_t *cp)
1613 struct yds_softc *sc;
1615 sc = addr;
1616 return sc->sc_codec[0].codec_if->vtbl->mixer_get_port(
1617 sc->sc_codec[0].codec_if, cp);
1620 static int
1621 yds_query_devinfo(void *addr, mixer_devinfo_t *dip)
1623 struct yds_softc *sc;
1625 sc = addr;
1626 return sc->sc_codec[0].codec_if->vtbl->query_devinfo(
1627 sc->sc_codec[0].codec_if, dip);
1630 static void *
1631 yds_malloc(void *addr, int direction, size_t size,
1632 struct malloc_type *pool, int flags)
1634 struct yds_softc *sc;
1635 struct yds_dma *p;
1636 int error;
1638 p = malloc(sizeof(*p), pool, flags);
1639 if (p == NULL)
1640 return NULL;
1641 sc = addr;
1642 error = yds_allocmem(sc, size, 16, p);
1643 if (error) {
1644 free(p, pool);
1645 return NULL;
1647 p->next = sc->sc_dmas;
1648 sc->sc_dmas = p;
1649 return KERNADDR(p);
1652 static void
1653 yds_free(void *addr, void *ptr, struct malloc_type *pool)
1655 struct yds_softc *sc;
1656 struct yds_dma **pp, *p;
1658 sc = addr;
1659 for (pp = &sc->sc_dmas; (p = *pp) != NULL; pp = &p->next) {
1660 if (KERNADDR(p) == ptr) {
1661 yds_freemem(sc, p);
1662 *pp = p->next;
1663 free(p, pool);
1664 return;
1669 static struct yds_dma *
1670 yds_find_dma(struct yds_softc *sc, void *addr)
1672 struct yds_dma *p;
1674 for (p = sc->sc_dmas; p && KERNADDR(p) != addr; p = p->next)
1675 continue;
1677 return p;
1680 static size_t
1681 yds_round_buffersize(void *addr, int direction, size_t size)
1685 * Buffer size should be at least twice as bigger as a frame.
1687 if (size < 1024 * 3)
1688 size = 1024 * 3;
1689 return size;
1692 static paddr_t
1693 yds_mappage(void *addr, void *mem, off_t off, int prot)
1695 struct yds_softc *sc;
1696 struct yds_dma *p;
1698 if (off < 0)
1699 return -1;
1700 sc = addr;
1701 p = yds_find_dma(sc, mem);
1702 if (p == NULL)
1703 return -1;
1704 return bus_dmamem_mmap(sc->sc_dmatag, p->segs, p->nsegs,
1705 off, prot, BUS_DMA_WAITOK);
1708 static int
1709 yds_get_props(void *addr)
1712 return AUDIO_PROP_MMAP | AUDIO_PROP_INDEPENDENT |
1713 AUDIO_PROP_FULLDUPLEX;