yds.c revision 1.35 1 /* $NetBSD: yds.c,v 1.35 2006/09/24 03:53:09 jmcneill Exp $ */
2
3 /*
4 * Copyright (c) 2000, 2001 Kazuki Sakamoto and Minoura Makoto.
5 * All rights reserved.
6 *
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.
15 *
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.
26 */
27
28 /*
29 * Yamaha YMF724[B-F]/740[B-C]/744/754
30 *
31 * Documentation links:
32 * - ftp://ftp.alsa-project.org/pub/manuals/yamaha/
33 * - ftp://ftp.alsa-project.org/pub/manuals/yamaha/pci/
34 *
35 * TODO:
36 * - FM synth volume (difficult: mixed before ac97)
37 * - Digital in/out (SPDIF) support
38 * - Effect??
39 */
40
41 #include <sys/cdefs.h>
42 __KERNEL_RCSID(0, "$NetBSD: yds.c,v 1.35 2006/09/24 03:53:09 jmcneill Exp $");
43
44 #include "mpu.h"
45
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>
53
54 #include <dev/pci/pcidevs.h>
55 #include <dev/pci/pcireg.h>
56 #include <dev/pci/pcivar.h>
57
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>
65
66 #include <machine/bus.h>
67 #include <machine/intr.h>
68
69 #include <dev/microcode/yds/yds_hwmcode.h>
70 #include <dev/pci/ydsreg.h>
71 #include <dev/pci/ydsvar.h>
72
73 /* Debug */
74 #undef YDS_USE_REC_SLOT
75 #define YDS_USE_P44
76
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
90
91 static int yds_match(struct device *, struct cfdata *, void *);
92 static void yds_attach(struct device *, struct device *, void *);
93 static int yds_intr(void *);
94
95 #define DMAADDR(p) ((p)->map->dm_segs[0].ds_addr)
96 #define KERNADDR(p) ((void *)((p)->addr))
97
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 *);
101
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)
111 {
112 DPRINTFN(5, (" YREAD2(0x%lX)\n", (unsigned long)r));
113 return bus_space_read_2(sc->memt, sc->memh, r);
114 }
115
116 static uint32_t YREAD4(struct yds_softc *sc, bus_size_t r)
117 {
118 DPRINTFN(5, (" YREAD4(0x%lX)\n", (unsigned long)r));
119 return bus_space_read_4(sc->memt, sc->memh, r);
120 }
121
122 #ifdef notdef
123 static void YWRITE1(struct yds_softc *sc, bus_size_t r, uint8_t x)
124 {
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);
128 }
129 #endif
130
131 static void YWRITE2(struct yds_softc *sc, bus_size_t r, uint16_t x)
132 {
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);
136 }
137
138 static void YWRITE4(struct yds_softc *sc, bus_size_t r, uint32_t x)
139 {
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);
143 }
144 #endif
145
146 #define YWRITEREGION4(sc, r, x, c) \
147 bus_space_write_region_4((sc)->memt, (sc)->memh, (r), (x), (c) / 4)
148
149 CFATTACH_DECL(yds, sizeof(struct yds_softc),
150 yds_match, yds_attach, NULL, NULL);
151
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 *);
176
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 *);
181
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(struct device *);
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 *);
193
194 static int yds_init(struct yds_softc *);
195 static void yds_powerhook(int, void *);
196
197 #ifdef AUDIO_DEBUG
198 static void yds_dump_play_slot(struct yds_softc *, int);
199 #define YDS_DUMP_PLAY_SLOT(n, sc, bank) \
200 if (ydsdebug > (n)) yds_dump_play_slot(sc, bank)
201 #else
202 #define YDS_DUMP_PLAY_SLOT(n, sc, bank)
203 #endif /* AUDIO_DEBUG */
204
205 static const struct audio_hw_if yds_hw_if = {
206 yds_open,
207 yds_close,
208 NULL,
209 yds_query_encoding,
210 yds_set_params,
211 yds_round_blocksize,
212 NULL,
213 NULL,
214 NULL,
215 NULL,
216 NULL,
217 yds_halt_output,
218 yds_halt_input,
219 NULL,
220 yds_getdev,
221 NULL,
222 yds_mixer_set_port,
223 yds_mixer_get_port,
224 yds_query_devinfo,
225 yds_malloc,
226 yds_free,
227 yds_round_buffersize,
228 yds_mappage,
229 yds_get_props,
230 yds_trigger_output,
231 yds_trigger_input,
232 NULL,
233 NULL, /* powerstate */
234 };
235
236 static const struct audio_device yds_device = {
237 "Yamaha DS-1",
238 "",
239 "yds"
240 };
241
242 static const struct {
243 uint id;
244 u_int flags;
245 #define YDS_CAP_MCODE_1 0x0001
246 #define YDS_CAP_MCODE_1E 0x0002
247 #define YDS_CAP_LEGACY_SELECTABLE 0x0004
248 #define YDS_CAP_LEGACY_FLEXIBLE 0x0008
249 #define YDS_CAP_HAS_P44 0x0010
250 } yds_chip_capabliity_list[] = {
251 { PCI_PRODUCT_YAMAHA_YMF724,
252 YDS_CAP_MCODE_1|YDS_CAP_LEGACY_SELECTABLE },
253 /* 740[C] has only 32 slots. But anyway we use only 2 */
254 { PCI_PRODUCT_YAMAHA_YMF740,
255 YDS_CAP_MCODE_1|YDS_CAP_LEGACY_SELECTABLE }, /* XXX NOT TESTED */
256 { PCI_PRODUCT_YAMAHA_YMF740C,
257 YDS_CAP_MCODE_1E|YDS_CAP_LEGACY_SELECTABLE },
258 { PCI_PRODUCT_YAMAHA_YMF724F,
259 YDS_CAP_MCODE_1E|YDS_CAP_LEGACY_SELECTABLE },
260 { PCI_PRODUCT_YAMAHA_YMF744B,
261 YDS_CAP_MCODE_1E|YDS_CAP_LEGACY_FLEXIBLE },
262 { PCI_PRODUCT_YAMAHA_YMF754,
263 YDS_CAP_MCODE_1E|YDS_CAP_LEGACY_FLEXIBLE|YDS_CAP_HAS_P44 },
264 { 0, 0 }
265 };
266 #ifdef AUDIO_DEBUG
267 #define YDS_CAP_BITS "\020\005P44\004LEGFLEX\003LEGSEL\002MCODE1E\001MCODE1"
268 #endif
269
270 static const struct audio_format yds_formats[] = {
271 {NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_SLINEAR_LE, 16, 16,
272 1, AUFMT_MONAURAL, 0, {4000, 48000}},
273 {NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_SLINEAR_LE, 16, 16,
274 2, AUFMT_STEREO, 0, {4000, 48000}},
275 {NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_ULINEAR_LE, 8, 8,
276 1, AUFMT_MONAURAL, 0, {4000, 48000}},
277 {NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_ULINEAR_LE, 8, 8,
278 2, AUFMT_STEREO, 0, {4000, 48000}},
279 };
280 #define YDS_NFORMATS (sizeof(yds_formats) / sizeof(struct audio_format))
281
282 #ifdef AUDIO_DEBUG
283 static void
284 yds_dump_play_slot(struct yds_softc *sc, int bank)
285 {
286 int i, j;
287 uint32_t *p;
288 uint32_t num;
289 char *pa;
290
291 for (i = 0; i < N_PLAY_SLOTS; i++) {
292 printf("pbankp[%d] = %p,", i*2, sc->pbankp[i*2]);
293 printf("pbankp[%d] = %p\n", i*2+1, sc->pbankp[i*2+1]);
294 }
295
296 pa = (char *)DMAADDR(&sc->sc_ctrldata) + sc->pbankoff;
297 p = (uint32_t *)sc->ptbl;
298 printf("ptbl + 0: %d\n", *p++);
299 for (i = 0; i < N_PLAY_SLOTS; i++) {
300 printf("ptbl + %d: 0x%x, should be %p\n",
301 i+1, *p,
302 pa + i * sizeof(struct play_slot_ctrl_bank) *
303 N_PLAY_SLOT_CTRL_BANK);
304 p++;
305 }
306
307 num = le32toh(*(uint32_t*)sc->ptbl);
308 printf("numofplay = %d\n", num);
309
310 for (i = 0; i < num; i++) {
311 p = (uint32_t *)sc->pbankp[i*2];
312
313 printf(" pbankp[%d], bank 0 : %p\n", i*2, p);
314 for (j = 0;
315 j < sizeof(struct play_slot_ctrl_bank) / sizeof(uint32_t);
316 j++) {
317 printf(" 0x%02x: 0x%08x\n",
318 (unsigned)(j * sizeof(uint32_t)),
319 (unsigned)*p++);
320 }
321
322 p = (uint32_t *)sc->pbankp[i*2 + 1];
323 printf(" pbankp[%d], bank 1 : %p\n", i*2 + 1, p);
324 for (j = 0;
325 j < sizeof(struct play_slot_ctrl_bank) / sizeof(uint32_t);
326 j++) {
327 printf(" 0x%02x: 0x%08x\n",
328 (unsigned)(j * sizeof(uint32_t)),
329 (unsigned)*p++);
330 }
331 }
332 }
333 #endif /* AUDIO_DEBUG */
334
335 static u_int
336 yds_get_dstype(int id)
337 {
338 int i;
339
340 for (i = 0; yds_chip_capabliity_list[i].id; i++) {
341 if (PCI_PRODUCT(id) == yds_chip_capabliity_list[i].id)
342 return yds_chip_capabliity_list[i].flags;
343 }
344
345 return -1;
346 }
347
348 static int
349 yds_download_mcode(struct yds_softc *sc)
350 {
351 static struct {
352 const uint32_t *mcode;
353 size_t size;
354 } ctrls[] = {
355 {yds_ds1_ctrl_mcode, sizeof(yds_ds1_ctrl_mcode)},
356 {yds_ds1e_ctrl_mcode, sizeof(yds_ds1e_ctrl_mcode)},
357 };
358 u_int ctrl;
359 const uint32_t *p;
360 size_t size;
361 int dstype;
362
363 if (sc->sc_flags & YDS_CAP_MCODE_1)
364 dstype = YDS_DS_1;
365 else if (sc->sc_flags & YDS_CAP_MCODE_1E)
366 dstype = YDS_DS_1E;
367 else
368 return 1; /* unknown */
369
370 if (yds_disable_dsp(sc))
371 return 1;
372
373 /* Software reset */
374 YWRITE4(sc, YDS_MODE, YDS_MODE_RESET);
375 YWRITE4(sc, YDS_MODE, 0);
376
377 YWRITE4(sc, YDS_MAPOF_REC, 0);
378 YWRITE4(sc, YDS_MAPOF_EFFECT, 0);
379 YWRITE4(sc, YDS_PLAY_CTRLBASE, 0);
380 YWRITE4(sc, YDS_REC_CTRLBASE, 0);
381 YWRITE4(sc, YDS_EFFECT_CTRLBASE, 0);
382 YWRITE4(sc, YDS_WORK_BASE, 0);
383
384 ctrl = YREAD2(sc, YDS_GLOBAL_CONTROL);
385 YWRITE2(sc, YDS_GLOBAL_CONTROL, ctrl & ~0x0007);
386
387 /* Download DSP microcode. */
388 p = yds_dsp_mcode;
389 size = sizeof(yds_dsp_mcode);
390 YWRITEREGION4(sc, YDS_DSP_INSTRAM, p, size);
391
392 /* Download CONTROL microcode. */
393 p = ctrls[dstype].mcode;
394 size = ctrls[dstype].size;
395 YWRITEREGION4(sc, YDS_CTRL_INSTRAM, p, size);
396
397 yds_enable_dsp(sc);
398 delay(10 * 1000); /* nessesary on my 724F (??) */
399
400 return 0;
401 }
402
403 static int
404 yds_allocate_slots(struct yds_softc *sc)
405 {
406 size_t pcs, rcs, ecs, ws, memsize;
407 void *mp;
408 uint32_t da; /* DMA address */
409 char *va; /* KVA */
410 off_t cb;
411 int i;
412 struct yds_dma *p;
413
414 /* Alloc DSP Control Data */
415 pcs = YREAD4(sc, YDS_PLAY_CTRLSIZE) * sizeof(uint32_t);
416 rcs = YREAD4(sc, YDS_REC_CTRLSIZE) * sizeof(uint32_t);
417 ecs = YREAD4(sc, YDS_EFFECT_CTRLSIZE) * sizeof(uint32_t);
418 ws = WORK_SIZE;
419 YWRITE4(sc, YDS_WORK_SIZE, ws / sizeof(uint32_t));
420
421 DPRINTF(("play control size : %d\n", (unsigned int)pcs));
422 DPRINTF(("rec control size : %d\n", (unsigned int)rcs));
423 DPRINTF(("eff control size : %d\n", (unsigned int)ecs));
424 DPRINTF(("work size : %d\n", (unsigned int)ws));
425 #ifdef DIAGNOSTIC
426 if (pcs != sizeof(struct play_slot_ctrl_bank)) {
427 printf("%s: invalid play slot ctrldata %d != %d\n",
428 sc->sc_dev.dv_xname, (unsigned int)pcs,
429 (unsigned int)sizeof(struct play_slot_ctrl_bank));
430 if (rcs != sizeof(struct rec_slot_ctrl_bank))
431 printf("%s: invalid rec slot ctrldata %d != %d\n",
432 sc->sc_dev.dv_xname, (unsigned int)rcs,
433 (unsigned int)sizeof(struct rec_slot_ctrl_bank));
434 }
435 #endif
436
437 memsize = N_PLAY_SLOTS*N_PLAY_SLOT_CTRL_BANK*pcs +
438 N_REC_SLOT_CTRL*N_REC_SLOT_CTRL_BANK*rcs + ws;
439 memsize += (N_PLAY_SLOTS+1)*sizeof(uint32_t);
440
441 p = &sc->sc_ctrldata;
442 if (KERNADDR(p) == NULL) {
443 i = yds_allocmem(sc, memsize, 16, p);
444 if (i) {
445 printf("%s: couldn't alloc/map DSP DMA buffer, reason %d\n",
446 sc->sc_dev.dv_xname, i);
447 free(p, M_DEVBUF);
448 return 1;
449 }
450 }
451 mp = KERNADDR(p);
452 da = DMAADDR(p);
453
454 DPRINTF(("mp:%p, DMA addr:%p\n",
455 mp, (void *)sc->sc_ctrldata.map->dm_segs[0].ds_addr));
456
457 memset(mp, 0, memsize);
458
459 /* Work space */
460 cb = 0;
461 va = (uint8_t *)mp;
462 YWRITE4(sc, YDS_WORK_BASE, da + cb);
463 cb += ws;
464
465 /* Play control data table */
466 sc->ptbl = (uint32_t *)(va + cb);
467 sc->ptbloff = cb;
468 YWRITE4(sc, YDS_PLAY_CTRLBASE, da + cb);
469 cb += (N_PLAY_SLOT_CTRL + 1) * sizeof(uint32_t);
470
471 /* Record slot control data */
472 sc->rbank = (struct rec_slot_ctrl_bank *)(va + cb);
473 YWRITE4(sc, YDS_REC_CTRLBASE, da + cb);
474 sc->rbankoff = cb;
475 cb += N_REC_SLOT_CTRL * N_REC_SLOT_CTRL_BANK * rcs;
476
477 #if 0
478 /* Effect slot control data -- unused */
479 YWRITE4(sc, YDS_EFFECT_CTRLBASE, da + cb);
480 cb += N_EFFECT_SLOT_CTRL * N_EFFECT_SLOT_CTRL_BANK * ecs;
481 #endif
482
483 /* Play slot control data */
484 sc->pbankoff = cb;
485 for (i=0; i < N_PLAY_SLOT_CTRL; i++) {
486 sc->pbankp[i*2] = (struct play_slot_ctrl_bank *)(va + cb);
487 *(sc->ptbl + i+1) = htole32(da + cb);
488 cb += pcs;
489
490 sc->pbankp[i*2+1] = (struct play_slot_ctrl_bank *)(va + cb);
491 cb += pcs;
492 }
493 /* Sync play control data table */
494 bus_dmamap_sync(sc->sc_dmatag, p->map,
495 sc->ptbloff, (N_PLAY_SLOT_CTRL+1) * sizeof(uint32_t),
496 BUS_DMASYNC_PREWRITE);
497
498 return 0;
499 }
500
501 static void
502 yds_enable_dsp(struct yds_softc *sc)
503 {
504
505 YWRITE4(sc, YDS_CONFIG, YDS_DSP_SETUP);
506 }
507
508 static int
509 yds_disable_dsp(struct yds_softc *sc)
510 {
511 int to;
512 uint32_t data;
513
514 data = YREAD4(sc, YDS_CONFIG);
515 if (data)
516 YWRITE4(sc, YDS_CONFIG, YDS_DSP_DISABLE);
517
518 for (to = 0; to < YDS_WORK_TIMEOUT; to++) {
519 if ((YREAD4(sc, YDS_STATUS) & YDS_STAT_WORK) == 0)
520 return 0;
521 delay(1);
522 }
523
524 return 1;
525 }
526
527 static int
528 yds_match(struct device *parent, struct cfdata *match, void *aux)
529 {
530 struct pci_attach_args *pa;
531
532 pa = (struct pci_attach_args *)aux;
533 switch (PCI_VENDOR(pa->pa_id)) {
534 case PCI_VENDOR_YAMAHA:
535 switch (PCI_PRODUCT(pa->pa_id)) {
536 case PCI_PRODUCT_YAMAHA_YMF724:
537 case PCI_PRODUCT_YAMAHA_YMF740:
538 case PCI_PRODUCT_YAMAHA_YMF740C:
539 case PCI_PRODUCT_YAMAHA_YMF724F:
540 case PCI_PRODUCT_YAMAHA_YMF744B:
541 case PCI_PRODUCT_YAMAHA_YMF754:
542 return 1;
543 }
544 break;
545 }
546
547 return 0;
548 }
549
550 /*
551 * This routine is called after all the ISA devices are configured,
552 * to avoid conflict.
553 */
554 static void
555 yds_configure_legacy(struct device *arg)
556 #define FLEXIBLE (sc->sc_flags & YDS_CAP_LEGACY_FLEXIBLE)
557 #define SELECTABLE (sc->sc_flags & YDS_CAP_LEGACY_SELECTABLE)
558 {
559 static const bus_addr_t opl_addrs[] = {0x388, 0x398, 0x3A0, 0x3A8};
560 static const bus_addr_t mpu_addrs[] = {0x330, 0x300, 0x332, 0x334};
561 struct yds_softc *sc;
562 pcireg_t reg;
563 struct device *dev;
564 int i;
565
566 sc = (struct yds_softc*) arg;
567 if (!FLEXIBLE && !SELECTABLE)
568 return;
569
570 reg = pci_conf_read(sc->sc_pc, sc->sc_pcitag, YDS_PCI_LEGACY);
571 reg &= ~0x8133c03f; /* these bits are out of interest */
572 reg |= ((YDS_PCI_EX_LEGACY_IMOD) |
573 (YDS_PCI_LEGACY_FMEN |
574 YDS_PCI_LEGACY_MEN /*| YDS_PCI_LEGACY_MIEN*/));
575 reg |= YDS_PCI_EX_LEGACY_SMOD_DISABLE;
576 if (FLEXIBLE) {
577 pci_conf_write(sc->sc_pc, sc->sc_pcitag, YDS_PCI_LEGACY, reg);
578 delay(100*1000);
579 }
580
581 /* Look for OPL */
582 dev = 0;
583 for (i = 0; i < sizeof(opl_addrs) / sizeof(bus_addr_t); i++) {
584 if (SELECTABLE) {
585 pci_conf_write(sc->sc_pc, sc->sc_pcitag,
586 YDS_PCI_LEGACY, reg | (i << (0+16)));
587 delay(100*1000); /* wait 100ms */
588 } else
589 pci_conf_write(sc->sc_pc, sc->sc_pcitag,
590 YDS_PCI_FM_BA, opl_addrs[i]);
591 if (bus_space_map(sc->sc_opl_iot,
592 opl_addrs[i], 4, 0, &sc->sc_opl_ioh) == 0) {
593 struct audio_attach_args aa;
594
595 aa.type = AUDIODEV_TYPE_OPL;
596 aa.hwif = aa.hdl = NULL;
597 dev = config_found(&sc->sc_dev, &aa, audioprint);
598 if (dev == 0)
599 bus_space_unmap(sc->sc_opl_iot,
600 sc->sc_opl_ioh, 4);
601 else {
602 if (SELECTABLE)
603 reg |= (i << (0+16));
604 break;
605 }
606 }
607 }
608 if (dev == 0) {
609 reg &= ~YDS_PCI_LEGACY_FMEN;
610 pci_conf_write(sc->sc_pc, sc->sc_pcitag,
611 YDS_PCI_LEGACY, reg);
612 } else {
613 /* Max. volume */
614 YWRITE4(sc, YDS_LEGACY_OUT_VOLUME, 0x3fff3fff);
615 YWRITE4(sc, YDS_LEGACY_REC_VOLUME, 0x3fff3fff);
616 }
617
618 /* Look for MPU */
619 dev = 0;
620 for (i = 0; i < sizeof(mpu_addrs) / sizeof(bus_addr_t); i++) {
621 if (SELECTABLE)
622 pci_conf_write(sc->sc_pc, sc->sc_pcitag,
623 YDS_PCI_LEGACY, reg | (i << (4+16)));
624 else
625 pci_conf_write(sc->sc_pc, sc->sc_pcitag,
626 YDS_PCI_MPU_BA, mpu_addrs[i]);
627 if (bus_space_map(sc->sc_mpu_iot,
628 mpu_addrs[i], 2, 0, &sc->sc_mpu_ioh) == 0) {
629 struct audio_attach_args aa;
630
631 aa.type = AUDIODEV_TYPE_MPU;
632 aa.hwif = aa.hdl = NULL;
633 dev = config_found(&sc->sc_dev, &aa, audioprint);
634 if (dev == 0)
635 bus_space_unmap(sc->sc_mpu_iot,
636 sc->sc_mpu_ioh, 2);
637 else {
638 if (SELECTABLE)
639 reg |= (i << (4+16));
640 break;
641 }
642 }
643 }
644 if (dev == 0) {
645 reg &= ~(YDS_PCI_LEGACY_MEN | YDS_PCI_LEGACY_MIEN);
646 pci_conf_write(sc->sc_pc, sc->sc_pcitag, YDS_PCI_LEGACY, reg);
647 }
648 sc->sc_mpu = dev;
649 }
650 #undef FLEXIBLE
651 #undef SELECTABLE
652
653 static int
654 yds_init(struct yds_softc *sc)
655 {
656 uint32_t reg;
657
658 DPRINTF(("yds_init()\n"));
659
660 /* Download microcode */
661 if (yds_download_mcode(sc)) {
662 printf("%s: download microcode failed\n", sc->sc_dev.dv_xname);
663 return 1;
664 }
665
666 /* Allocate DMA buffers */
667 if (yds_allocate_slots(sc)) {
668 printf("%s: could not allocate slots\n", sc->sc_dev.dv_xname);
669 return 1;
670 }
671
672 /* Warm reset */
673 reg = pci_conf_read(sc->sc_pc, sc->sc_pcitag, YDS_PCI_DSCTRL);
674 pci_conf_write(sc->sc_pc, sc->sc_pcitag, YDS_PCI_DSCTRL,
675 reg | YDS_DSCTRL_WRST);
676 delay(50000);
677
678 return 0;
679 }
680
681 static void
682 yds_powerhook(int why, void *addr)
683 {
684 struct yds_softc *sc;
685 pci_chipset_tag_t pc;
686 pcitag_t tag;
687 pcireg_t reg;
688 int s;
689
690 sc = (struct yds_softc *)addr;
691 pc = sc->sc_pc;
692 tag = sc->sc_pcitag;
693
694 s = splaudio();
695 switch (why) {
696 case PWR_SUSPEND:
697 pci_conf_capture(pc, tag, &sc->sc_pciconf);
698
699 sc->sc_dsctrl = pci_conf_read(pc, tag, YDS_PCI_DSCTRL);
700 sc->sc_legacy = pci_conf_read(pc, tag, YDS_PCI_LEGACY);
701 sc->sc_ba[0] = pci_conf_read(pc, tag, YDS_PCI_FM_BA);
702 sc->sc_ba[1] = pci_conf_read(pc, tag, YDS_PCI_MPU_BA);
703 break;
704 case PWR_RESUME:
705 pci_conf_restore(pc, tag, &sc->sc_pciconf);
706
707 /* Disable legacy mode */
708 reg = pci_conf_read(pc, tag, YDS_PCI_LEGACY);
709 pci_conf_write(pc, tag, YDS_PCI_LEGACY,
710 reg & YDS_PCI_LEGACY_LAD);
711
712 /* Enable the device. */
713 reg = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
714 reg |= (PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MEM_ENABLE |
715 PCI_COMMAND_MASTER_ENABLE);
716 pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG, reg);
717 reg = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
718 if (yds_init(sc)) {
719 printf("%s: reinitialize failed\n",
720 sc->sc_dev.dv_xname);
721 splx(s);
722 return;
723 }
724 pci_conf_write(pc, tag, YDS_PCI_DSCTRL, sc->sc_dsctrl);
725 sc->sc_codec[0].codec_if->vtbl->restore_ports(sc->sc_codec[0].codec_if);
726 break;
727 case PWR_SOFTRESUME:
728 pci_conf_write(pc, tag, YDS_PCI_LEGACY, sc->sc_legacy);
729 pci_conf_write(pc, tag, YDS_PCI_FM_BA, sc->sc_ba[0]);
730 pci_conf_write(pc, tag, YDS_PCI_MPU_BA, sc->sc_ba[1]);
731 #if notyet
732 yds_configure_legacy(addr);
733 #endif
734 break;
735 }
736 splx(s);
737
738 return;
739 }
740
741 static void
742 yds_attach(struct device *parent, struct device *self, void *aux)
743 {
744 struct yds_softc *sc;
745 struct pci_attach_args *pa;
746 pci_chipset_tag_t pc;
747 char const *intrstr;
748 pci_intr_handle_t ih;
749 pcireg_t reg;
750 struct yds_codec_softc *codec;
751 char devinfo[256];
752 int i, r, to;
753 int revision;
754 int ac97_id2;
755
756 sc = (struct yds_softc *)self;
757 pa = (struct pci_attach_args *)aux;
758 pc = pa->pa_pc;
759 pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
760 revision = PCI_REVISION(pa->pa_class);
761 printf(": %s (rev. 0x%02x)\n", devinfo, revision);
762
763 /* Map register to memory */
764 if (pci_mapreg_map(pa, YDS_PCI_MBA, PCI_MAPREG_TYPE_MEM, 0,
765 &sc->memt, &sc->memh, NULL, NULL)) {
766 printf("%s: can't map memory space\n", sc->sc_dev.dv_xname);
767 return;
768 }
769
770 /* Map and establish the interrupt. */
771 if (pci_intr_map(pa, &ih)) {
772 printf("%s: couldn't map interrupt\n", sc->sc_dev.dv_xname);
773 return;
774 }
775 intrstr = pci_intr_string(pc, ih);
776 sc->sc_ih = pci_intr_establish(pc, ih, IPL_AUDIO, yds_intr, sc);
777 if (sc->sc_ih == NULL) {
778 printf("%s: couldn't establish interrupt", sc->sc_dev.dv_xname);
779 if (intrstr != NULL)
780 printf(" at %s", intrstr);
781 printf("\n");
782 return;
783 }
784 printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
785
786 sc->sc_dmatag = pa->pa_dmat;
787 sc->sc_pc = pc;
788 sc->sc_pcitag = pa->pa_tag;
789 sc->sc_id = pa->pa_id;
790 sc->sc_revision = revision;
791 sc->sc_flags = yds_get_dstype(sc->sc_id);
792 #ifdef AUDIO_DEBUG
793 if (ydsdebug) {
794 char bits[80];
795
796 printf("%s: chip has %s\n", sc->sc_dev.dv_xname,
797 bitmask_snprintf(sc->sc_flags, YDS_CAP_BITS, bits,
798 sizeof(bits)));
799 }
800 #endif
801
802 /* Disable legacy mode */
803 reg = pci_conf_read(pc, pa->pa_tag, YDS_PCI_LEGACY);
804 pci_conf_write(pc, pa->pa_tag, YDS_PCI_LEGACY,
805 reg & YDS_PCI_LEGACY_LAD);
806
807 /* Enable the device. */
808 reg = pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
809 reg |= (PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MEM_ENABLE |
810 PCI_COMMAND_MASTER_ENABLE);
811 pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, reg);
812 reg = pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
813
814 /* Mute all volumes */
815 for (i = 0x80; i < 0xc0; i += 2)
816 YWRITE2(sc, i, 0);
817
818 /* Initialize the device */
819 if (yds_init(sc)) {
820 printf("%s: initialize failed\n", sc->sc_dev.dv_xname);
821 return;
822 }
823
824 /*
825 * Detect primary/secondary AC97
826 * YMF754 Hardware Specification Rev 1.01 page 24
827 */
828 reg = pci_conf_read(pc, pa->pa_tag, YDS_PCI_DSCTRL);
829 pci_conf_write(pc, pa->pa_tag, YDS_PCI_DSCTRL, reg & ~YDS_DSCTRL_CRST);
830 delay(400000); /* Needed for 740C. */
831
832 /* Primary */
833 for (to = 0; to < AC97_TIMEOUT; to++) {
834 if ((YREAD2(sc, AC97_STAT_ADDR1) & AC97_BUSY) == 0)
835 break;
836 delay(1);
837 }
838 if (to == AC97_TIMEOUT) {
839 printf("%s: no AC97 available\n", sc->sc_dev.dv_xname);
840 return;
841 }
842
843 /* Secondary */
844 /* Secondary AC97 is used for 4ch audio. Currently unused. */
845 ac97_id2 = -1;
846 if ((YREAD2(sc, YDS_ACTIVITY) & YDS_ACTIVITY_DOCKA) == 0)
847 goto detected;
848 #if 0 /* reset secondary... */
849 YWRITE2(sc, YDS_GPIO_OCTRL,
850 YREAD2(sc, YDS_GPIO_OCTRL) & ~YDS_GPIO_GPO2);
851 YWRITE2(sc, YDS_GPIO_FUNCE,
852 (YREAD2(sc, YDS_GPIO_FUNCE)&(~YDS_GPIO_GPC2))|YDS_GPIO_GPE2);
853 #endif
854 for (to = 0; to < AC97_TIMEOUT; to++) {
855 if ((YREAD2(sc, AC97_STAT_ADDR2) & AC97_BUSY) == 0)
856 break;
857 delay(1);
858 }
859 if (to < AC97_TIMEOUT) {
860 /* detect id */
861 for (ac97_id2 = 1; ac97_id2 < 4; ac97_id2++) {
862 YWRITE2(sc, AC97_CMD_ADDR,
863 AC97_CMD_READ | AC97_ID(ac97_id2) | 0x28);
864
865 for (to = 0; to < AC97_TIMEOUT; to++) {
866 if ((YREAD2(sc, AC97_STAT_ADDR2) & AC97_BUSY)
867 == 0)
868 goto detected;
869 delay(1);
870 }
871 }
872 if (ac97_id2 == 4)
873 ac97_id2 = -1;
874 detected:
875 ;
876 }
877
878 pci_conf_write(pc, pa->pa_tag, YDS_PCI_DSCTRL, reg | YDS_DSCTRL_CRST);
879 delay (20);
880 pci_conf_write(pc, pa->pa_tag, YDS_PCI_DSCTRL, reg & ~YDS_DSCTRL_CRST);
881 delay (400000);
882 for (to = 0; to < AC97_TIMEOUT; to++) {
883 if ((YREAD2(sc, AC97_STAT_ADDR1) & AC97_BUSY) == 0)
884 break;
885 delay(1);
886 }
887
888 /*
889 * Attach ac97 codec
890 */
891 for (i = 0; i < 2; i++) {
892 static struct {
893 int data;
894 int addr;
895 } statregs[] = {
896 {AC97_STAT_DATA1, AC97_STAT_ADDR1},
897 {AC97_STAT_DATA2, AC97_STAT_ADDR2},
898 };
899
900 if (i == 1 && ac97_id2 == -1)
901 break; /* secondary ac97 not available */
902
903 codec = &sc->sc_codec[i];
904 memcpy(&codec->sc_dev, &sc->sc_dev, sizeof(codec->sc_dev));
905 codec->sc = sc;
906 codec->id = i == 1 ? ac97_id2 : 0;
907 codec->status_data = statregs[i].data;
908 codec->status_addr = statregs[i].addr;
909 codec->host_if.arg = codec;
910 codec->host_if.attach = yds_attach_codec;
911 codec->host_if.read = yds_read_codec;
912 codec->host_if.write = yds_write_codec;
913 codec->host_if.reset = yds_reset_codec;
914
915 if ((r = ac97_attach(&codec->host_if, self)) != 0) {
916 printf("%s: can't attach codec (error 0x%X)\n",
917 sc->sc_dev.dv_xname, r);
918 return;
919 }
920 }
921
922 if (0 != auconv_create_encodings(yds_formats, YDS_NFORMATS,
923 &sc->sc_encodings))
924 return;
925
926 audio_attach_mi(&yds_hw_if, sc, &sc->sc_dev);
927
928 sc->sc_legacy_iot = pa->pa_iot;
929 config_defer((struct device*) sc, yds_configure_legacy);
930
931 powerhook_establish(sc->sc_dev.dv_xname, yds_powerhook, sc);
932 }
933
934 static int
935 yds_attach_codec(void *sc_, struct ac97_codec_if *codec_if)
936 {
937 struct yds_codec_softc *sc;
938
939 sc = sc_;
940 sc->codec_if = codec_if;
941 return 0;
942 }
943
944 static int
945 yds_ready_codec(struct yds_codec_softc *sc)
946 {
947 int to;
948
949 for (to = 0; to < AC97_TIMEOUT; to++) {
950 if ((YREAD2(sc->sc, sc->status_addr) & AC97_BUSY) == 0)
951 return 0;
952 delay(1);
953 }
954
955 return 1;
956 }
957
958 static int
959 yds_read_codec(void *sc_, uint8_t reg, uint16_t *data)
960 {
961 struct yds_codec_softc *sc;
962
963 sc = sc_;
964 YWRITE2(sc->sc, AC97_CMD_ADDR, AC97_CMD_READ | AC97_ID(sc->id) | reg);
965
966 if (yds_ready_codec(sc)) {
967 printf("%s: yds_read_codec timeout\n",
968 sc->sc->sc_dev.dv_xname);
969 return EIO;
970 }
971
972 if (PCI_PRODUCT(sc->sc->sc_id) == PCI_PRODUCT_YAMAHA_YMF744B &&
973 sc->sc->sc_revision < 2) {
974 int i;
975 for (i=0; i<600; i++)
976 (void)YREAD2(sc->sc, sc->status_data);
977 }
978
979 *data = YREAD2(sc->sc, sc->status_data);
980
981 return 0;
982 }
983
984 static int
985 yds_write_codec(void *sc_, uint8_t reg, uint16_t data)
986 {
987 struct yds_codec_softc *sc;
988
989 sc = sc_;
990 YWRITE2(sc->sc, AC97_CMD_ADDR, AC97_CMD_WRITE | AC97_ID(sc->id) | reg);
991 YWRITE2(sc->sc, AC97_CMD_DATA, data);
992
993 if (yds_ready_codec(sc)) {
994 printf("%s: yds_write_codec timeout\n",
995 sc->sc->sc_dev.dv_xname);
996 return EIO;
997 }
998
999 return 0;
1000 }
1001
1002 /*
1003 * XXX: Must handle the secondary differntly!!
1004 */
1005 static int
1006 yds_reset_codec(void *sc_)
1007 {
1008 struct yds_codec_softc *codec;
1009 struct yds_softc *sc;
1010 pcireg_t reg;
1011
1012 codec = sc_;
1013 sc = codec->sc;
1014 /* reset AC97 codec */
1015 reg = pci_conf_read(sc->sc_pc, sc->sc_pcitag, YDS_PCI_DSCTRL);
1016 if (reg & 0x03) {
1017 pci_conf_write(sc->sc_pc, sc->sc_pcitag,
1018 YDS_PCI_DSCTRL, reg & ~0x03);
1019 pci_conf_write(sc->sc_pc, sc->sc_pcitag,
1020 YDS_PCI_DSCTRL, reg | 0x03);
1021 pci_conf_write(sc->sc_pc, sc->sc_pcitag,
1022 YDS_PCI_DSCTRL, reg & ~0x03);
1023 delay(50000);
1024 }
1025
1026 yds_ready_codec(sc_);
1027 return 0;
1028 }
1029
1030 static int
1031 yds_intr(void *p)
1032 {
1033 struct yds_softc *sc;
1034 u_int status;
1035
1036 sc = p;
1037 status = YREAD4(sc, YDS_STATUS);
1038 DPRINTFN(1, ("yds_intr: status=%08x\n", status));
1039 if ((status & (YDS_STAT_INT|YDS_STAT_TINT)) == 0) {
1040 #if NMPU > 0
1041 if (sc->sc_mpu)
1042 return mpu_intr(sc->sc_mpu);
1043 #endif
1044 return 0;
1045 }
1046
1047 if (status & YDS_STAT_TINT) {
1048 YWRITE4(sc, YDS_STATUS, YDS_STAT_TINT);
1049 printf ("yds_intr: timeout!\n");
1050 }
1051
1052 if (status & YDS_STAT_INT) {
1053 int nbank;
1054
1055 nbank = (YREAD4(sc, YDS_CONTROL_SELECT) == 0);
1056 /* Clear interrupt flag */
1057 YWRITE4(sc, YDS_STATUS, YDS_STAT_INT);
1058
1059 /* Buffer for the next frame is always ready. */
1060 YWRITE4(sc, YDS_MODE, YREAD4(sc, YDS_MODE) | YDS_MODE_ACTV2);
1061
1062 if (sc->sc_play.intr) {
1063 u_int dma, ccpu, blk, len;
1064
1065 /* Sync play slot control data */
1066 bus_dmamap_sync(sc->sc_dmatag, sc->sc_ctrldata.map,
1067 sc->pbankoff,
1068 sizeof(struct play_slot_ctrl_bank)*
1069 le32toh(*sc->ptbl)*
1070 N_PLAY_SLOT_CTRL_BANK,
1071 BUS_DMASYNC_POSTWRITE|
1072 BUS_DMASYNC_POSTREAD);
1073 dma = le32toh(sc->pbankp[nbank]->pgstart) * sc->sc_play.factor;
1074 ccpu = sc->sc_play.offset;
1075 blk = sc->sc_play.blksize;
1076 len = sc->sc_play.length;
1077
1078 if (((dma > ccpu) && (dma - ccpu > blk * 2)) ||
1079 ((ccpu > dma) && (dma + len - ccpu > blk * 2))) {
1080 /* We can fill the next block */
1081 /* Sync ring buffer for previous write */
1082 bus_dmamap_sync(sc->sc_dmatag,
1083 sc->sc_play.dma->map,
1084 ccpu, blk,
1085 BUS_DMASYNC_POSTWRITE);
1086 sc->sc_play.intr(sc->sc_play.intr_arg);
1087 sc->sc_play.offset += blk;
1088 if (sc->sc_play.offset >= len) {
1089 sc->sc_play.offset -= len;
1090 #ifdef DIAGNOSTIC
1091 if (sc->sc_play.offset != 0)
1092 printf ("Audio ringbuffer botch\n");
1093 #endif
1094 }
1095 /* Sync ring buffer for next write */
1096 bus_dmamap_sync(sc->sc_dmatag,
1097 sc->sc_play.dma->map,
1098 ccpu, blk,
1099 BUS_DMASYNC_PREWRITE);
1100 }
1101 }
1102 if (sc->sc_rec.intr) {
1103 u_int dma, ccpu, blk, len;
1104
1105 /* Sync rec slot control data */
1106 bus_dmamap_sync(sc->sc_dmatag, sc->sc_ctrldata.map,
1107 sc->rbankoff,
1108 sizeof(struct rec_slot_ctrl_bank)*
1109 N_REC_SLOT_CTRL*
1110 N_REC_SLOT_CTRL_BANK,
1111 BUS_DMASYNC_POSTWRITE|
1112 BUS_DMASYNC_POSTREAD);
1113 dma = le32toh(sc->rbank[YDS_INPUT_SLOT*2 + nbank].pgstartadr);
1114 ccpu = sc->sc_rec.offset;
1115 blk = sc->sc_rec.blksize;
1116 len = sc->sc_rec.length;
1117
1118 if (((dma > ccpu) && (dma - ccpu > blk * 2)) ||
1119 ((ccpu > dma) && (dma + len - ccpu > blk * 2))) {
1120 /* We can drain the current block */
1121 /* Sync ring buffer first */
1122 bus_dmamap_sync(sc->sc_dmatag,
1123 sc->sc_rec.dma->map,
1124 ccpu, blk,
1125 BUS_DMASYNC_POSTREAD);
1126 sc->sc_rec.intr(sc->sc_rec.intr_arg);
1127 sc->sc_rec.offset += blk;
1128 if (sc->sc_rec.offset >= len) {
1129 sc->sc_rec.offset -= len;
1130 #ifdef DIAGNOSTIC
1131 if (sc->sc_rec.offset != 0)
1132 printf ("Audio ringbuffer botch\n");
1133 #endif
1134 }
1135 /* Sync ring buffer for next read */
1136 bus_dmamap_sync(sc->sc_dmatag,
1137 sc->sc_rec.dma->map,
1138 ccpu, blk,
1139 BUS_DMASYNC_PREREAD);
1140 }
1141 }
1142 }
1143
1144 return 1;
1145 }
1146
1147 static int
1148 yds_allocmem(struct yds_softc *sc, size_t size, size_t align, struct yds_dma *p)
1149 {
1150 int error;
1151
1152 p->size = size;
1153 error = bus_dmamem_alloc(sc->sc_dmatag, p->size, align, 0,
1154 p->segs, sizeof(p->segs)/sizeof(p->segs[0]),
1155 &p->nsegs, BUS_DMA_NOWAIT);
1156 if (error)
1157 return error;
1158
1159 error = bus_dmamem_map(sc->sc_dmatag, p->segs, p->nsegs, p->size,
1160 &p->addr, BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
1161 if (error)
1162 goto free;
1163
1164 error = bus_dmamap_create(sc->sc_dmatag, p->size, 1, p->size,
1165 0, BUS_DMA_NOWAIT, &p->map);
1166 if (error)
1167 goto unmap;
1168
1169 error = bus_dmamap_load(sc->sc_dmatag, p->map, p->addr, p->size, NULL,
1170 BUS_DMA_NOWAIT);
1171 if (error)
1172 goto destroy;
1173 return 0;
1174
1175 destroy:
1176 bus_dmamap_destroy(sc->sc_dmatag, p->map);
1177 unmap:
1178 bus_dmamem_unmap(sc->sc_dmatag, p->addr, p->size);
1179 free:
1180 bus_dmamem_free(sc->sc_dmatag, p->segs, p->nsegs);
1181 return error;
1182 }
1183
1184 static int
1185 yds_freemem(struct yds_softc *sc, struct yds_dma *p)
1186 {
1187
1188 bus_dmamap_unload(sc->sc_dmatag, p->map);
1189 bus_dmamap_destroy(sc->sc_dmatag, p->map);
1190 bus_dmamem_unmap(sc->sc_dmatag, p->addr, p->size);
1191 bus_dmamem_free(sc->sc_dmatag, p->segs, p->nsegs);
1192 return 0;
1193 }
1194
1195 static int
1196 yds_open(void *addr, int flags)
1197 {
1198 struct yds_softc *sc;
1199 uint32_t mode;
1200
1201 sc = addr;
1202 /* Select bank 0. */
1203 YWRITE4(sc, YDS_CONTROL_SELECT, 0);
1204
1205 /* Start the DSP operation. */
1206 mode = YREAD4(sc, YDS_MODE);
1207 mode |= YDS_MODE_ACTV;
1208 mode &= ~YDS_MODE_ACTV2;
1209 YWRITE4(sc, YDS_MODE, mode);
1210
1211 return 0;
1212 }
1213
1214 /*
1215 * Close function is called at splaudio().
1216 */
1217 static void
1218 yds_close(void *addr)
1219 {
1220
1221 yds_halt(addr);
1222 }
1223
1224 static int
1225 yds_query_encoding(void *addr, struct audio_encoding *fp)
1226 {
1227 struct yds_softc *sc;
1228
1229 sc = addr;
1230 return auconv_query_encoding(sc->sc_encodings, fp);
1231 }
1232
1233 static int
1234 yds_set_params(void *addr, int setmode, int usemode,
1235 audio_params_t *play, audio_params_t* rec,
1236 stream_filter_list_t *pfil, stream_filter_list_t *rfil)
1237 {
1238 if (setmode & AUMODE_RECORD) {
1239 if (auconv_set_converter(yds_formats, YDS_NFORMATS,
1240 AUMODE_RECORD, rec, FALSE, rfil) < 0)
1241 return EINVAL;
1242 }
1243 if (setmode & AUMODE_PLAY) {
1244 if (auconv_set_converter(yds_formats, YDS_NFORMATS,
1245 AUMODE_PLAY, play, FALSE, pfil) < 0)
1246 return EINVAL;
1247 }
1248 return 0;
1249 }
1250
1251 static int
1252 yds_round_blocksize(void *addr, int blk, int mode, const audio_params_t *param)
1253 {
1254
1255 /*
1256 * Block size must be bigger than a frame.
1257 * That is 1024bytes at most, i.e. for 48000Hz, 16bit, 2ch.
1258 */
1259 if (blk < 1024)
1260 blk = 1024;
1261
1262 return blk & ~4;
1263 }
1264
1265 static uint32_t
1266 yds_get_lpfq(u_int sample_rate)
1267 {
1268 int i;
1269 static struct lpfqt {
1270 u_int rate;
1271 uint32_t lpfq;
1272 } lpfqt[] = {
1273 {8000, 0x32020000},
1274 {11025, 0x31770000},
1275 {16000, 0x31390000},
1276 {22050, 0x31c90000},
1277 {32000, 0x33d00000},
1278 {48000, 0x40000000},
1279 {0, 0}
1280 };
1281
1282 if (sample_rate == 44100) /* for P44 slot? */
1283 return 0x370A0000;
1284
1285 for (i = 0; lpfqt[i].rate != 0; i++)
1286 if (sample_rate <= lpfqt[i].rate)
1287 break;
1288
1289 return lpfqt[i].lpfq;
1290 }
1291
1292 static uint32_t
1293 yds_get_lpfk(u_int sample_rate)
1294 {
1295 int i;
1296 static struct lpfkt {
1297 u_int rate;
1298 uint32_t lpfk;
1299 } lpfkt[] = {
1300 {8000, 0x18b20000},
1301 {11025, 0x20930000},
1302 {16000, 0x2b9a0000},
1303 {22050, 0x35a10000},
1304 {32000, 0x3eaa0000},
1305 {48000, 0x40000000},
1306 {0, 0}
1307 };
1308
1309 if (sample_rate == 44100) /* for P44 slot? */
1310 return 0x46460000;
1311
1312 for (i = 0; lpfkt[i].rate != 0; i++)
1313 if (sample_rate <= lpfkt[i].rate)
1314 break;
1315
1316 return lpfkt[i].lpfk;
1317 }
1318
1319 static int
1320 yds_trigger_output(void *addr, void *start, void *end, int blksize,
1321 void (*intr)(void *), void *arg, const audio_params_t *param)
1322 #define P44 (sc->sc_flags & YDS_CAP_HAS_P44)
1323 {
1324 struct yds_softc *sc;
1325 struct yds_dma *p;
1326 struct play_slot_ctrl_bank *psb;
1327 const u_int gain = 0x40000000;
1328 bus_addr_t s;
1329 size_t l;
1330 int i;
1331 int p44, channels;
1332 uint32_t format;
1333
1334 sc = addr;
1335 #ifdef DIAGNOSTIC
1336 if (sc->sc_play.intr)
1337 panic("yds_trigger_output: already running");
1338 #endif
1339
1340 sc->sc_play.intr = intr;
1341 sc->sc_play.intr_arg = arg;
1342 sc->sc_play.offset = 0;
1343 sc->sc_play.blksize = blksize;
1344
1345 DPRINTFN(1, ("yds_trigger_output: sc=%p start=%p end=%p "
1346 "blksize=%d intr=%p(%p)\n", addr, start, end, blksize, intr, arg));
1347
1348 p = yds_find_dma(sc, start);
1349 if (!p) {
1350 printf("yds_trigger_output: bad addr %p\n", start);
1351 return EINVAL;
1352 }
1353 sc->sc_play.dma = p;
1354
1355 #ifdef YDS_USE_P44
1356 /* The document says the P44 SRC supports only stereo, 16bit PCM. */
1357 if (P44)
1358 p44 = ((param->sample_rate == 44100) &&
1359 (param->channels == 2) &&
1360 (param->precision == 16));
1361 else
1362 #endif
1363 p44 = 0;
1364 channels = p44 ? 1 : param->channels;
1365
1366 s = DMAADDR(p);
1367 l = ((char *)end - (char *)start);
1368 sc->sc_play.length = l;
1369
1370 *sc->ptbl = htole32(channels); /* Num of play */
1371
1372 sc->sc_play.factor = 1;
1373 if (param->channels == 2)
1374 sc->sc_play.factor *= 2;
1375 if (param->precision != 8)
1376 sc->sc_play.factor *= 2;
1377 l /= sc->sc_play.factor;
1378
1379 format = ((channels == 2 ? PSLT_FORMAT_STEREO : 0) |
1380 (param->precision == 8 ? PSLT_FORMAT_8BIT : 0) |
1381 (p44 ? PSLT_FORMAT_SRC441 : 0));
1382
1383 psb = sc->pbankp[0];
1384 memset(psb, 0, sizeof(*psb));
1385 psb->format = htole32(format);
1386 psb->pgbase = htole32(s);
1387 psb->pgloopend = htole32(l);
1388 if (!p44) {
1389 psb->pgdeltaend = htole32((param->sample_rate * 65536 / 48000) << 12);
1390 psb->lpfkend = htole32(yds_get_lpfk(param->sample_rate));
1391 psb->eggainend = htole32(gain);
1392 psb->lpfq = htole32(yds_get_lpfq(param->sample_rate));
1393 psb->pgdelta = htole32(psb->pgdeltaend);
1394 psb->lpfk = htole32(yds_get_lpfk(param->sample_rate));
1395 psb->eggain = htole32(gain);
1396 }
1397
1398 for (i = 0; i < channels; i++) {
1399 /* i == 0: left or mono, i == 1: right */
1400 psb = sc->pbankp[i*2];
1401 if (i)
1402 /* copy from left */
1403 *psb = *(sc->pbankp[0]);
1404 if (channels == 2) {
1405 /* stereo */
1406 if (i == 0) {
1407 psb->lchgain = psb->lchgainend = htole32(gain);
1408 } else {
1409 psb->lchgain = psb->lchgainend = 0;
1410 psb->rchgain = psb->rchgainend = htole32(gain);
1411 psb->format |= htole32(PSLT_FORMAT_RCH);
1412 }
1413 } else if (!p44) {
1414 /* mono */
1415 psb->lchgain = psb->rchgain = htole32(gain);
1416 psb->lchgainend = psb->rchgainend = htole32(gain);
1417 }
1418 /* copy to the other bank */
1419 *(sc->pbankp[i*2+1]) = *psb;
1420 }
1421
1422 YDS_DUMP_PLAY_SLOT(5, sc, 0);
1423 YDS_DUMP_PLAY_SLOT(5, sc, 1);
1424
1425 if (p44)
1426 YWRITE4(sc, YDS_P44_OUT_VOLUME, 0x3fff3fff);
1427 else
1428 YWRITE4(sc, YDS_DAC_OUT_VOLUME, 0x3fff3fff);
1429
1430 /* Now the play slot for the next frame is set up!! */
1431 /* Sync play slot control data for both directions */
1432 bus_dmamap_sync(sc->sc_dmatag, sc->sc_ctrldata.map,
1433 sc->ptbloff,
1434 sizeof(struct play_slot_ctrl_bank) *
1435 channels * N_PLAY_SLOT_CTRL_BANK,
1436 BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
1437 /* Sync ring buffer */
1438 bus_dmamap_sync(sc->sc_dmatag, p->map, 0, blksize,
1439 BUS_DMASYNC_PREWRITE);
1440 /* HERE WE GO!! */
1441 YWRITE4(sc, YDS_MODE,
1442 YREAD4(sc, YDS_MODE) | YDS_MODE_ACTV | YDS_MODE_ACTV2);
1443
1444 return 0;
1445 }
1446 #undef P44
1447
1448 static int
1449 yds_trigger_input(void *addr, void *start, void *end, int blksize,
1450 void (*intr)(void *), void *arg, const audio_params_t *param)
1451 {
1452 struct yds_softc *sc;
1453 struct yds_dma *p;
1454 u_int srate, format;
1455 struct rec_slot_ctrl_bank *rsb;
1456 bus_addr_t s;
1457 size_t l;
1458
1459 sc = addr;
1460 #ifdef DIAGNOSTIC
1461 if (sc->sc_rec.intr)
1462 panic("yds_trigger_input: already running");
1463 #endif
1464 sc->sc_rec.intr = intr;
1465 sc->sc_rec.intr_arg = arg;
1466 sc->sc_rec.offset = 0;
1467 sc->sc_rec.blksize = blksize;
1468
1469 DPRINTFN(1, ("yds_trigger_input: "
1470 "sc=%p start=%p end=%p blksize=%d intr=%p(%p)\n",
1471 addr, start, end, blksize, intr, arg));
1472 DPRINTFN(1, (" parameters: rate=%u, precision=%u, channels=%u\n",
1473 param->sample_rate, param->precision, param->channels));
1474
1475 p = yds_find_dma(sc, start);
1476 if (!p) {
1477 printf("yds_trigger_input: bad addr %p\n", start);
1478 return EINVAL;
1479 }
1480 sc->sc_rec.dma = p;
1481
1482 s = DMAADDR(p);
1483 l = ((char *)end - (char *)start);
1484 sc->sc_rec.length = l;
1485
1486 sc->sc_rec.factor = 1;
1487 if (param->channels == 2)
1488 sc->sc_rec.factor *= 2;
1489 if (param->precision != 8)
1490 sc->sc_rec.factor *= 2;
1491
1492 rsb = &sc->rbank[0];
1493 memset(rsb, 0, sizeof(*rsb));
1494 rsb->pgbase = htole32(s);
1495 rsb->pgloopendadr = htole32(l);
1496 /* Seems all 4 banks must be set up... */
1497 sc->rbank[1] = *rsb;
1498 sc->rbank[2] = *rsb;
1499 sc->rbank[3] = *rsb;
1500
1501 YWRITE4(sc, YDS_ADC_IN_VOLUME, 0x3fff3fff);
1502 YWRITE4(sc, YDS_REC_IN_VOLUME, 0x3fff3fff);
1503 srate = 48000 * 4096 / param->sample_rate - 1;
1504 format = ((param->precision == 8 ? YDS_FORMAT_8BIT : 0) |
1505 (param->channels == 2 ? YDS_FORMAT_STEREO : 0));
1506 DPRINTF(("srate=%d, format=%08x\n", srate, format));
1507 #ifdef YDS_USE_REC_SLOT
1508 YWRITE4(sc, YDS_DAC_REC_VOLUME, 0x3fff3fff);
1509 YWRITE4(sc, YDS_P44_REC_VOLUME, 0x3fff3fff);
1510 YWRITE4(sc, YDS_MAPOF_REC, YDS_RECSLOT_VALID);
1511 YWRITE4(sc, YDS_REC_SAMPLE_RATE, srate);
1512 YWRITE4(sc, YDS_REC_FORMAT, format);
1513 #else
1514 YWRITE4(sc, YDS_MAPOF_REC, YDS_ADCSLOT_VALID);
1515 YWRITE4(sc, YDS_ADC_SAMPLE_RATE, srate);
1516 YWRITE4(sc, YDS_ADC_FORMAT, format);
1517 #endif
1518 /* Now the rec slot for the next frame is set up!! */
1519 /* Sync record slot control data */
1520 bus_dmamap_sync(sc->sc_dmatag, sc->sc_ctrldata.map,
1521 sc->rbankoff,
1522 sizeof(struct rec_slot_ctrl_bank)*
1523 N_REC_SLOT_CTRL*
1524 N_REC_SLOT_CTRL_BANK,
1525 BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
1526 /* Sync ring buffer */
1527 bus_dmamap_sync(sc->sc_dmatag, p->map, 0, blksize,
1528 BUS_DMASYNC_PREREAD);
1529 /* HERE WE GO!! */
1530 YWRITE4(sc, YDS_MODE,
1531 YREAD4(sc, YDS_MODE) | YDS_MODE_ACTV | YDS_MODE_ACTV2);
1532
1533 return 0;
1534 }
1535
1536 static int
1537 yds_halt(struct yds_softc *sc)
1538 {
1539 uint32_t mode;
1540
1541 /* Stop the DSP operation. */
1542 mode = YREAD4(sc, YDS_MODE);
1543 YWRITE4(sc, YDS_MODE, mode & ~(YDS_MODE_ACTV|YDS_MODE_ACTV2));
1544
1545 /* Paranoia... mute all */
1546 YWRITE4(sc, YDS_P44_OUT_VOLUME, 0);
1547 YWRITE4(sc, YDS_DAC_OUT_VOLUME, 0);
1548 YWRITE4(sc, YDS_ADC_IN_VOLUME, 0);
1549 YWRITE4(sc, YDS_REC_IN_VOLUME, 0);
1550 YWRITE4(sc, YDS_DAC_REC_VOLUME, 0);
1551 YWRITE4(sc, YDS_P44_REC_VOLUME, 0);
1552
1553 return 0;
1554 }
1555
1556 static int
1557 yds_halt_output(void *addr)
1558 {
1559 struct yds_softc *sc;
1560
1561 DPRINTF(("yds: yds_halt_output\n"));
1562 sc = addr;
1563 if (sc->sc_play.intr) {
1564 sc->sc_play.intr = 0;
1565 /* Sync play slot control data */
1566 bus_dmamap_sync(sc->sc_dmatag, sc->sc_ctrldata.map,
1567 sc->pbankoff,
1568 sizeof(struct play_slot_ctrl_bank)*
1569 (*sc->ptbl)*N_PLAY_SLOT_CTRL_BANK,
1570 BUS_DMASYNC_POSTWRITE|BUS_DMASYNC_POSTREAD);
1571 /* Stop the play slot operation */
1572 sc->pbankp[0]->status =
1573 sc->pbankp[1]->status =
1574 sc->pbankp[2]->status =
1575 sc->pbankp[3]->status = 1;
1576 /* Sync ring buffer */
1577 bus_dmamap_sync(sc->sc_dmatag, sc->sc_play.dma->map,
1578 0, sc->sc_play.length, BUS_DMASYNC_POSTWRITE);
1579 }
1580
1581 return 0;
1582 }
1583
1584 static int
1585 yds_halt_input(void *addr)
1586 {
1587 struct yds_softc *sc;
1588
1589 DPRINTF(("yds: yds_halt_input\n"));
1590 sc = addr;
1591 sc->sc_rec.intr = NULL;
1592 if (sc->sc_rec.intr) {
1593 /* Stop the rec slot operation */
1594 YWRITE4(sc, YDS_MAPOF_REC, 0);
1595 sc->sc_rec.intr = 0;
1596 /* Sync rec slot control data */
1597 bus_dmamap_sync(sc->sc_dmatag, sc->sc_ctrldata.map,
1598 sc->rbankoff,
1599 sizeof(struct rec_slot_ctrl_bank)*
1600 N_REC_SLOT_CTRL*N_REC_SLOT_CTRL_BANK,
1601 BUS_DMASYNC_POSTWRITE|BUS_DMASYNC_POSTREAD);
1602 /* Sync ring buffer */
1603 bus_dmamap_sync(sc->sc_dmatag, sc->sc_rec.dma->map,
1604 0, sc->sc_rec.length, BUS_DMASYNC_POSTREAD);
1605 }
1606
1607 return 0;
1608 }
1609
1610 static int
1611 yds_getdev(void *addr, struct audio_device *retp)
1612 {
1613
1614 *retp = yds_device;
1615 return 0;
1616 }
1617
1618 static int
1619 yds_mixer_set_port(void *addr, mixer_ctrl_t *cp)
1620 {
1621 struct yds_softc *sc;
1622
1623 sc = addr;
1624 return sc->sc_codec[0].codec_if->vtbl->mixer_set_port(
1625 sc->sc_codec[0].codec_if, cp);
1626 }
1627
1628 static int
1629 yds_mixer_get_port(void *addr, mixer_ctrl_t *cp)
1630 {
1631 struct yds_softc *sc;
1632
1633 sc = addr;
1634 return sc->sc_codec[0].codec_if->vtbl->mixer_get_port(
1635 sc->sc_codec[0].codec_if, cp);
1636 }
1637
1638 static int
1639 yds_query_devinfo(void *addr, mixer_devinfo_t *dip)
1640 {
1641 struct yds_softc *sc;
1642
1643 sc = addr;
1644 return sc->sc_codec[0].codec_if->vtbl->query_devinfo(
1645 sc->sc_codec[0].codec_if, dip);
1646 }
1647
1648 static void *
1649 yds_malloc(void *addr, int direction, size_t size,
1650 struct malloc_type *pool, int flags)
1651 {
1652 struct yds_softc *sc;
1653 struct yds_dma *p;
1654 int error;
1655
1656 p = malloc(sizeof(*p), pool, flags);
1657 if (p == NULL)
1658 return NULL;
1659 sc = addr;
1660 error = yds_allocmem(sc, size, 16, p);
1661 if (error) {
1662 free(p, pool);
1663 return NULL;
1664 }
1665 p->next = sc->sc_dmas;
1666 sc->sc_dmas = p;
1667 return KERNADDR(p);
1668 }
1669
1670 static void
1671 yds_free(void *addr, void *ptr, struct malloc_type *pool)
1672 {
1673 struct yds_softc *sc;
1674 struct yds_dma **pp, *p;
1675
1676 sc = addr;
1677 for (pp = &sc->sc_dmas; (p = *pp) != NULL; pp = &p->next) {
1678 if (KERNADDR(p) == ptr) {
1679 yds_freemem(sc, p);
1680 *pp = p->next;
1681 free(p, pool);
1682 return;
1683 }
1684 }
1685 }
1686
1687 static struct yds_dma *
1688 yds_find_dma(struct yds_softc *sc, void *addr)
1689 {
1690 struct yds_dma *p;
1691
1692 for (p = sc->sc_dmas; p && KERNADDR(p) != addr; p = p->next)
1693 continue;
1694
1695 return p;
1696 }
1697
1698 static size_t
1699 yds_round_buffersize(void *addr, int direction, size_t size)
1700 {
1701
1702 /*
1703 * Buffer size should be at least twice as bigger as a frame.
1704 */
1705 if (size < 1024 * 3)
1706 size = 1024 * 3;
1707 return size;
1708 }
1709
1710 static paddr_t
1711 yds_mappage(void *addr, void *mem, off_t off, int prot)
1712 {
1713 struct yds_softc *sc;
1714 struct yds_dma *p;
1715
1716 if (off < 0)
1717 return -1;
1718 sc = addr;
1719 p = yds_find_dma(sc, mem);
1720 if (p == NULL)
1721 return -1;
1722 return bus_dmamem_mmap(sc->sc_dmatag, p->segs, p->nsegs,
1723 off, prot, BUS_DMA_WAITOK);
1724 }
1725
1726 static int
1727 yds_get_props(void *addr)
1728 {
1729
1730 return AUDIO_PROP_MMAP | AUDIO_PROP_INDEPENDENT |
1731 AUDIO_PROP_FULLDUPLEX;
1732 }
1733