yds.c revision 1.41 1 /* $NetBSD: yds.c,v 1.41 2008/04/01 13:35:39 xtraeme 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.41 2008/04/01 13:35:39 xtraeme 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 <sys/bus.h>
67 #include <sys/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
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 */
203
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 */
233 };
234
235 static const struct audio_device yds_device = {
236 "Yamaha DS-1",
237 "",
238 "yds"
239 };
240
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 }
264 };
265 #ifdef AUDIO_DEBUG
266 #define YDS_CAP_BITS "\020\005P44\004LEGFLEX\003LEGSEL\002MCODE1E\001MCODE1"
267 #endif
268
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}},
278 };
279 #define YDS_NFORMATS (sizeof(yds_formats) / sizeof(struct audio_format))
280
281 #ifdef AUDIO_DEBUG
282 static void
283 yds_dump_play_slot(struct yds_softc *sc, int bank)
284 {
285 int i, j;
286 uint32_t *p;
287 uint32_t num;
288 char *pa;
289
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]);
293 }
294
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++;
304 }
305
306 num = le32toh(*(uint32_t*)sc->ptbl);
307 printf("numofplay = %d\n", num);
308
309 for (i = 0; i < num; i++) {
310 p = (uint32_t *)sc->pbankp[i*2];
311
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++);
319 }
320
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++);
329 }
330 }
331 }
332 #endif /* AUDIO_DEBUG */
333
334 static u_int
335 yds_get_dstype(int id)
336 {
337 int i;
338
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;
342 }
343
344 return -1;
345 }
346
347 static int
348 yds_download_mcode(struct yds_softc *sc)
349 {
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)},
356 };
357 u_int ctrl;
358 const uint32_t *p;
359 size_t size;
360 int dstype;
361
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 */
368
369 if (yds_disable_dsp(sc))
370 return 1;
371
372 /* Software reset */
373 YWRITE4(sc, YDS_MODE, YDS_MODE_RESET);
374 YWRITE4(sc, YDS_MODE, 0);
375
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);
382
383 ctrl = YREAD2(sc, YDS_GLOBAL_CONTROL);
384 YWRITE2(sc, YDS_GLOBAL_CONTROL, ctrl & ~0x0007);
385
386 /* Download DSP microcode. */
387 p = yds_dsp_mcode;
388 size = sizeof(yds_dsp_mcode);
389 YWRITEREGION4(sc, YDS_DSP_INSTRAM, p, size);
390
391 /* Download CONTROL microcode. */
392 p = ctrls[dstype].mcode;
393 size = ctrls[dstype].size;
394 YWRITEREGION4(sc, YDS_CTRL_INSTRAM, p, size);
395
396 yds_enable_dsp(sc);
397 delay(10 * 1000); /* nessesary on my 724F (??) */
398
399 return 0;
400 }
401
402 static int
403 yds_allocate_slots(struct yds_softc *sc)
404 {
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;
412
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));
419
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 printf("%s: invalid play slot ctrldata %d != %d\n",
427 sc->sc_dev.dv_xname, (unsigned int)pcs,
428 (unsigned int)sizeof(struct play_slot_ctrl_bank));
429 if (rcs != sizeof(struct rec_slot_ctrl_bank))
430 printf("%s: invalid rec slot ctrldata %d != %d\n",
431 sc->sc_dev.dv_xname, (unsigned int)rcs,
432 (unsigned int)sizeof(struct rec_slot_ctrl_bank));
433 }
434 #endif
435
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);
439
440 p = &sc->sc_ctrldata;
441 if (KERNADDR(p) == NULL) {
442 i = yds_allocmem(sc, memsize, 16, p);
443 if (i) {
444 printf("%s: couldn't alloc/map DSP DMA buffer, reason %d\n",
445 sc->sc_dev.dv_xname, i);
446 free(p, M_DEVBUF);
447 return 1;
448 }
449 }
450 mp = KERNADDR(p);
451 da = DMAADDR(p);
452
453 DPRINTF(("mp:%p, DMA addr:%p\n",
454 mp, (void *)sc->sc_ctrldata.map->dm_segs[0].ds_addr));
455
456 memset(mp, 0, memsize);
457
458 /* Work space */
459 cb = 0;
460 va = (uint8_t *)mp;
461 YWRITE4(sc, YDS_WORK_BASE, da + cb);
462 cb += ws;
463
464 /* Play control data table */
465 sc->ptbl = (uint32_t *)(va + cb);
466 sc->ptbloff = cb;
467 YWRITE4(sc, YDS_PLAY_CTRLBASE, da + cb);
468 cb += (N_PLAY_SLOT_CTRL + 1) * sizeof(uint32_t);
469
470 /* Record slot control data */
471 sc->rbank = (struct rec_slot_ctrl_bank *)(va + cb);
472 YWRITE4(sc, YDS_REC_CTRLBASE, da + cb);
473 sc->rbankoff = cb;
474 cb += N_REC_SLOT_CTRL * N_REC_SLOT_CTRL_BANK * rcs;
475
476 #if 0
477 /* Effect slot control data -- unused */
478 YWRITE4(sc, YDS_EFFECT_CTRLBASE, da + cb);
479 cb += N_EFFECT_SLOT_CTRL * N_EFFECT_SLOT_CTRL_BANK * ecs;
480 #endif
481
482 /* Play slot control data */
483 sc->pbankoff = cb;
484 for (i=0; i < N_PLAY_SLOT_CTRL; i++) {
485 sc->pbankp[i*2] = (struct play_slot_ctrl_bank *)(va + cb);
486 *(sc->ptbl + i+1) = htole32(da + cb);
487 cb += pcs;
488
489 sc->pbankp[i*2+1] = (struct play_slot_ctrl_bank *)(va + cb);
490 cb += pcs;
491 }
492 /* Sync play control data table */
493 bus_dmamap_sync(sc->sc_dmatag, p->map,
494 sc->ptbloff, (N_PLAY_SLOT_CTRL+1) * sizeof(uint32_t),
495 BUS_DMASYNC_PREWRITE);
496
497 return 0;
498 }
499
500 static void
501 yds_enable_dsp(struct yds_softc *sc)
502 {
503
504 YWRITE4(sc, YDS_CONFIG, YDS_DSP_SETUP);
505 }
506
507 static int
508 yds_disable_dsp(struct yds_softc *sc)
509 {
510 int to;
511 uint32_t data;
512
513 data = YREAD4(sc, YDS_CONFIG);
514 if (data)
515 YWRITE4(sc, YDS_CONFIG, YDS_DSP_DISABLE);
516
517 for (to = 0; to < YDS_WORK_TIMEOUT; to++) {
518 if ((YREAD4(sc, YDS_STATUS) & YDS_STAT_WORK) == 0)
519 return 0;
520 delay(1);
521 }
522
523 return 1;
524 }
525
526 static int
527 yds_match(struct device *parent, struct cfdata *match,
528 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 bool
682 yds_suspend(device_t dv PMF_FN_ARGS)
683 {
684 struct yds_softc *sc = device_private(dv);
685 pci_chipset_tag_t pc = sc->sc_pc;
686 pcitag_t tag = sc->sc_pcitag;
687
688 sc->sc_dsctrl = pci_conf_read(pc, tag, YDS_PCI_DSCTRL);
689 sc->sc_legacy = pci_conf_read(pc, tag, YDS_PCI_LEGACY);
690 sc->sc_ba[0] = pci_conf_read(pc, tag, YDS_PCI_FM_BA);
691 sc->sc_ba[1] = pci_conf_read(pc, tag, YDS_PCI_MPU_BA);
692
693 return true;
694 }
695
696 static bool
697 yds_resume(device_t dv PMF_FN_ARGS)
698 {
699 struct yds_softc *sc = device_private(dv);
700 pci_chipset_tag_t pc = sc->sc_pc;
701 pcitag_t tag = sc->sc_pcitag;
702 pcireg_t reg;
703
704 /* Disable legacy mode */
705 reg = pci_conf_read(pc, tag, YDS_PCI_LEGACY);
706 pci_conf_write(pc, tag, YDS_PCI_LEGACY, reg & YDS_PCI_LEGACY_LAD);
707
708 /* Enable the device. */
709 reg = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
710 reg |= (PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MEM_ENABLE |
711 PCI_COMMAND_MASTER_ENABLE);
712 pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG, reg);
713 reg = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
714 if (yds_init(sc)) {
715 aprint_error_dev(dv, "reinitialize failed\n");
716 return false;
717 }
718
719 pci_conf_write(pc, tag, YDS_PCI_DSCTRL, sc->sc_dsctrl);
720 sc->sc_codec[0].codec_if->vtbl->restore_ports(sc->sc_codec[0].codec_if);
721
722 return true;
723 }
724
725 static void
726 yds_attach(struct device *parent, struct device *self, void *aux)
727 {
728 struct yds_softc *sc;
729 struct pci_attach_args *pa;
730 pci_chipset_tag_t pc;
731 char const *intrstr;
732 pci_intr_handle_t ih;
733 pcireg_t reg;
734 struct yds_codec_softc *codec;
735 char devinfo[256];
736 int i, r, to;
737 int revision;
738 int ac97_id2;
739
740 sc = (struct yds_softc *)self;
741 pa = (struct pci_attach_args *)aux;
742 pc = pa->pa_pc;
743 pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
744 revision = PCI_REVISION(pa->pa_class);
745 printf(": %s (rev. 0x%02x)\n", devinfo, revision);
746
747 /* Map register to memory */
748 if (pci_mapreg_map(pa, YDS_PCI_MBA, PCI_MAPREG_TYPE_MEM, 0,
749 &sc->memt, &sc->memh, NULL, NULL)) {
750 printf("%s: can't map memory space\n", sc->sc_dev.dv_xname);
751 return;
752 }
753
754 /* Map and establish the interrupt. */
755 if (pci_intr_map(pa, &ih)) {
756 printf("%s: couldn't map interrupt\n", sc->sc_dev.dv_xname);
757 return;
758 }
759 intrstr = pci_intr_string(pc, ih);
760 sc->sc_ih = pci_intr_establish(pc, ih, IPL_AUDIO, yds_intr, sc);
761 if (sc->sc_ih == NULL) {
762 printf("%s: couldn't establish interrupt", sc->sc_dev.dv_xname);
763 if (intrstr != NULL)
764 printf(" at %s", intrstr);
765 printf("\n");
766 return;
767 }
768 printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
769
770 sc->sc_dmatag = pa->pa_dmat;
771 sc->sc_pc = pc;
772 sc->sc_pcitag = pa->pa_tag;
773 sc->sc_id = pa->pa_id;
774 sc->sc_revision = revision;
775 sc->sc_flags = yds_get_dstype(sc->sc_id);
776 #ifdef AUDIO_DEBUG
777 if (ydsdebug) {
778 char bits[80];
779
780 printf("%s: chip has %s\n", sc->sc_dev.dv_xname,
781 bitmask_snprintf(sc->sc_flags, YDS_CAP_BITS, bits,
782 sizeof(bits)));
783 }
784 #endif
785
786 /* Disable legacy mode */
787 reg = pci_conf_read(pc, pa->pa_tag, YDS_PCI_LEGACY);
788 pci_conf_write(pc, pa->pa_tag, YDS_PCI_LEGACY,
789 reg & YDS_PCI_LEGACY_LAD);
790
791 /* Enable the device. */
792 reg = pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
793 reg |= (PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MEM_ENABLE |
794 PCI_COMMAND_MASTER_ENABLE);
795 pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, reg);
796 reg = pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
797
798 /* Mute all volumes */
799 for (i = 0x80; i < 0xc0; i += 2)
800 YWRITE2(sc, i, 0);
801
802 /* Initialize the device */
803 if (yds_init(sc)) {
804 printf("%s: initialize failed\n", sc->sc_dev.dv_xname);
805 return;
806 }
807
808 /*
809 * Detect primary/secondary AC97
810 * YMF754 Hardware Specification Rev 1.01 page 24
811 */
812 reg = pci_conf_read(pc, pa->pa_tag, YDS_PCI_DSCTRL);
813 pci_conf_write(pc, pa->pa_tag, YDS_PCI_DSCTRL, reg & ~YDS_DSCTRL_CRST);
814 delay(400000); /* Needed for 740C. */
815
816 /* Primary */
817 for (to = 0; to < AC97_TIMEOUT; to++) {
818 if ((YREAD2(sc, AC97_STAT_ADDR1) & AC97_BUSY) == 0)
819 break;
820 delay(1);
821 }
822 if (to == AC97_TIMEOUT) {
823 printf("%s: no AC97 available\n", sc->sc_dev.dv_xname);
824 return;
825 }
826
827 /* Secondary */
828 /* Secondary AC97 is used for 4ch audio. Currently unused. */
829 ac97_id2 = -1;
830 if ((YREAD2(sc, YDS_ACTIVITY) & YDS_ACTIVITY_DOCKA) == 0)
831 goto detected;
832 #if 0 /* reset secondary... */
833 YWRITE2(sc, YDS_GPIO_OCTRL,
834 YREAD2(sc, YDS_GPIO_OCTRL) & ~YDS_GPIO_GPO2);
835 YWRITE2(sc, YDS_GPIO_FUNCE,
836 (YREAD2(sc, YDS_GPIO_FUNCE)&(~YDS_GPIO_GPC2))|YDS_GPIO_GPE2);
837 #endif
838 for (to = 0; to < AC97_TIMEOUT; to++) {
839 if ((YREAD2(sc, AC97_STAT_ADDR2) & AC97_BUSY) == 0)
840 break;
841 delay(1);
842 }
843 if (to < AC97_TIMEOUT) {
844 /* detect id */
845 for (ac97_id2 = 1; ac97_id2 < 4; ac97_id2++) {
846 YWRITE2(sc, AC97_CMD_ADDR,
847 AC97_CMD_READ | AC97_ID(ac97_id2) | 0x28);
848
849 for (to = 0; to < AC97_TIMEOUT; to++) {
850 if ((YREAD2(sc, AC97_STAT_ADDR2) & AC97_BUSY)
851 == 0)
852 goto detected;
853 delay(1);
854 }
855 }
856 if (ac97_id2 == 4)
857 ac97_id2 = -1;
858 detected:
859 ;
860 }
861
862 pci_conf_write(pc, pa->pa_tag, YDS_PCI_DSCTRL, reg | YDS_DSCTRL_CRST);
863 delay (20);
864 pci_conf_write(pc, pa->pa_tag, YDS_PCI_DSCTRL, reg & ~YDS_DSCTRL_CRST);
865 delay (400000);
866 for (to = 0; to < AC97_TIMEOUT; to++) {
867 if ((YREAD2(sc, AC97_STAT_ADDR1) & AC97_BUSY) == 0)
868 break;
869 delay(1);
870 }
871
872 /*
873 * Attach ac97 codec
874 */
875 for (i = 0; i < 2; i++) {
876 static struct {
877 int data;
878 int addr;
879 } statregs[] = {
880 {AC97_STAT_DATA1, AC97_STAT_ADDR1},
881 {AC97_STAT_DATA2, AC97_STAT_ADDR2},
882 };
883
884 if (i == 1 && ac97_id2 == -1)
885 break; /* secondary ac97 not available */
886
887 codec = &sc->sc_codec[i];
888 memcpy(&codec->sc_dev, &sc->sc_dev, sizeof(codec->sc_dev));
889 codec->sc = sc;
890 codec->id = i == 1 ? ac97_id2 : 0;
891 codec->status_data = statregs[i].data;
892 codec->status_addr = statregs[i].addr;
893 codec->host_if.arg = codec;
894 codec->host_if.attach = yds_attach_codec;
895 codec->host_if.read = yds_read_codec;
896 codec->host_if.write = yds_write_codec;
897 codec->host_if.reset = yds_reset_codec;
898
899 if ((r = ac97_attach(&codec->host_if, self)) != 0) {
900 printf("%s: can't attach codec (error 0x%X)\n",
901 sc->sc_dev.dv_xname, r);
902 return;
903 }
904 }
905
906 if (0 != auconv_create_encodings(yds_formats, YDS_NFORMATS,
907 &sc->sc_encodings))
908 return;
909
910 audio_attach_mi(&yds_hw_if, sc, &sc->sc_dev);
911
912 sc->sc_legacy_iot = pa->pa_iot;
913 config_defer((struct device*) sc, yds_configure_legacy);
914
915 if (!pmf_device_register(self, yds_suspend, yds_resume))
916 aprint_error_dev(self, "couldn't establish power handler\n");
917 }
918
919 static int
920 yds_attach_codec(void *sc_, struct ac97_codec_if *codec_if)
921 {
922 struct yds_codec_softc *sc;
923
924 sc = sc_;
925 sc->codec_if = codec_if;
926 return 0;
927 }
928
929 static int
930 yds_ready_codec(struct yds_codec_softc *sc)
931 {
932 int to;
933
934 for (to = 0; to < AC97_TIMEOUT; to++) {
935 if ((YREAD2(sc->sc, sc->status_addr) & AC97_BUSY) == 0)
936 return 0;
937 delay(1);
938 }
939
940 return 1;
941 }
942
943 static int
944 yds_read_codec(void *sc_, uint8_t reg, uint16_t *data)
945 {
946 struct yds_codec_softc *sc;
947
948 sc = sc_;
949 YWRITE2(sc->sc, AC97_CMD_ADDR, AC97_CMD_READ | AC97_ID(sc->id) | reg);
950
951 if (yds_ready_codec(sc)) {
952 printf("%s: yds_read_codec timeout\n",
953 sc->sc->sc_dev.dv_xname);
954 return EIO;
955 }
956
957 if (PCI_PRODUCT(sc->sc->sc_id) == PCI_PRODUCT_YAMAHA_YMF744B &&
958 sc->sc->sc_revision < 2) {
959 int i;
960 for (i=0; i<600; i++)
961 (void)YREAD2(sc->sc, sc->status_data);
962 }
963
964 *data = YREAD2(sc->sc, sc->status_data);
965
966 return 0;
967 }
968
969 static int
970 yds_write_codec(void *sc_, uint8_t reg, uint16_t data)
971 {
972 struct yds_codec_softc *sc;
973
974 sc = sc_;
975 YWRITE2(sc->sc, AC97_CMD_ADDR, AC97_CMD_WRITE | AC97_ID(sc->id) | reg);
976 YWRITE2(sc->sc, AC97_CMD_DATA, data);
977
978 if (yds_ready_codec(sc)) {
979 printf("%s: yds_write_codec timeout\n",
980 sc->sc->sc_dev.dv_xname);
981 return EIO;
982 }
983
984 return 0;
985 }
986
987 /*
988 * XXX: Must handle the secondary differntly!!
989 */
990 static int
991 yds_reset_codec(void *sc_)
992 {
993 struct yds_codec_softc *codec;
994 struct yds_softc *sc;
995 pcireg_t reg;
996
997 codec = sc_;
998 sc = codec->sc;
999 /* reset AC97 codec */
1000 reg = pci_conf_read(sc->sc_pc, sc->sc_pcitag, YDS_PCI_DSCTRL);
1001 if (reg & 0x03) {
1002 pci_conf_write(sc->sc_pc, sc->sc_pcitag,
1003 YDS_PCI_DSCTRL, reg & ~0x03);
1004 pci_conf_write(sc->sc_pc, sc->sc_pcitag,
1005 YDS_PCI_DSCTRL, reg | 0x03);
1006 pci_conf_write(sc->sc_pc, sc->sc_pcitag,
1007 YDS_PCI_DSCTRL, reg & ~0x03);
1008 delay(50000);
1009 }
1010
1011 yds_ready_codec(sc_);
1012 return 0;
1013 }
1014
1015 static int
1016 yds_intr(void *p)
1017 {
1018 struct yds_softc *sc = p;
1019 #if NMPU > 0
1020 struct mpu_softc *sc_mpu = device_private(sc->sc_mpu);
1021 #endif
1022 u_int status;
1023
1024 status = YREAD4(sc, YDS_STATUS);
1025 DPRINTFN(1, ("yds_intr: status=%08x\n", status));
1026 if ((status & (YDS_STAT_INT|YDS_STAT_TINT)) == 0) {
1027 #if NMPU > 0
1028 if (sc_mpu)
1029 return mpu_intr(sc_mpu);
1030 #endif
1031 return 0;
1032 }
1033
1034 if (status & YDS_STAT_TINT) {
1035 YWRITE4(sc, YDS_STATUS, YDS_STAT_TINT);
1036 printf ("yds_intr: timeout!\n");
1037 }
1038
1039 if (status & YDS_STAT_INT) {
1040 int nbank;
1041
1042 nbank = (YREAD4(sc, YDS_CONTROL_SELECT) == 0);
1043 /* Clear interrupt flag */
1044 YWRITE4(sc, YDS_STATUS, YDS_STAT_INT);
1045
1046 /* Buffer for the next frame is always ready. */
1047 YWRITE4(sc, YDS_MODE, YREAD4(sc, YDS_MODE) | YDS_MODE_ACTV2);
1048
1049 if (sc->sc_play.intr) {
1050 u_int dma, ccpu, blk, len;
1051
1052 /* Sync play slot control data */
1053 bus_dmamap_sync(sc->sc_dmatag, sc->sc_ctrldata.map,
1054 sc->pbankoff,
1055 sizeof(struct play_slot_ctrl_bank)*
1056 le32toh(*sc->ptbl)*
1057 N_PLAY_SLOT_CTRL_BANK,
1058 BUS_DMASYNC_POSTWRITE|
1059 BUS_DMASYNC_POSTREAD);
1060 dma = le32toh(sc->pbankp[nbank]->pgstart) * sc->sc_play.factor;
1061 ccpu = sc->sc_play.offset;
1062 blk = sc->sc_play.blksize;
1063 len = sc->sc_play.length;
1064
1065 if (((dma > ccpu) && (dma - ccpu > blk * 2)) ||
1066 ((ccpu > dma) && (dma + len - ccpu > blk * 2))) {
1067 /* We can fill the next block */
1068 /* Sync ring buffer for previous write */
1069 bus_dmamap_sync(sc->sc_dmatag,
1070 sc->sc_play.dma->map,
1071 ccpu, blk,
1072 BUS_DMASYNC_POSTWRITE);
1073 sc->sc_play.intr(sc->sc_play.intr_arg);
1074 sc->sc_play.offset += blk;
1075 if (sc->sc_play.offset >= len) {
1076 sc->sc_play.offset -= len;
1077 #ifdef DIAGNOSTIC
1078 if (sc->sc_play.offset != 0)
1079 printf ("Audio ringbuffer botch\n");
1080 #endif
1081 }
1082 /* Sync ring buffer for next write */
1083 bus_dmamap_sync(sc->sc_dmatag,
1084 sc->sc_play.dma->map,
1085 ccpu, blk,
1086 BUS_DMASYNC_PREWRITE);
1087 }
1088 }
1089 if (sc->sc_rec.intr) {
1090 u_int dma, ccpu, blk, len;
1091
1092 /* Sync rec slot control data */
1093 bus_dmamap_sync(sc->sc_dmatag, sc->sc_ctrldata.map,
1094 sc->rbankoff,
1095 sizeof(struct rec_slot_ctrl_bank)*
1096 N_REC_SLOT_CTRL*
1097 N_REC_SLOT_CTRL_BANK,
1098 BUS_DMASYNC_POSTWRITE|
1099 BUS_DMASYNC_POSTREAD);
1100 dma = le32toh(sc->rbank[YDS_INPUT_SLOT*2 + nbank].pgstartadr);
1101 ccpu = sc->sc_rec.offset;
1102 blk = sc->sc_rec.blksize;
1103 len = sc->sc_rec.length;
1104
1105 if (((dma > ccpu) && (dma - ccpu > blk * 2)) ||
1106 ((ccpu > dma) && (dma + len - ccpu > blk * 2))) {
1107 /* We can drain the current block */
1108 /* Sync ring buffer first */
1109 bus_dmamap_sync(sc->sc_dmatag,
1110 sc->sc_rec.dma->map,
1111 ccpu, blk,
1112 BUS_DMASYNC_POSTREAD);
1113 sc->sc_rec.intr(sc->sc_rec.intr_arg);
1114 sc->sc_rec.offset += blk;
1115 if (sc->sc_rec.offset >= len) {
1116 sc->sc_rec.offset -= len;
1117 #ifdef DIAGNOSTIC
1118 if (sc->sc_rec.offset != 0)
1119 printf ("Audio ringbuffer botch\n");
1120 #endif
1121 }
1122 /* Sync ring buffer for next read */
1123 bus_dmamap_sync(sc->sc_dmatag,
1124 sc->sc_rec.dma->map,
1125 ccpu, blk,
1126 BUS_DMASYNC_PREREAD);
1127 }
1128 }
1129 }
1130
1131 return 1;
1132 }
1133
1134 static int
1135 yds_allocmem(struct yds_softc *sc, size_t size, size_t align, struct yds_dma *p)
1136 {
1137 int error;
1138
1139 p->size = size;
1140 error = bus_dmamem_alloc(sc->sc_dmatag, p->size, align, 0,
1141 p->segs, sizeof(p->segs)/sizeof(p->segs[0]),
1142 &p->nsegs, BUS_DMA_NOWAIT);
1143 if (error)
1144 return error;
1145
1146 error = bus_dmamem_map(sc->sc_dmatag, p->segs, p->nsegs, p->size,
1147 &p->addr, BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
1148 if (error)
1149 goto free;
1150
1151 error = bus_dmamap_create(sc->sc_dmatag, p->size, 1, p->size,
1152 0, BUS_DMA_NOWAIT, &p->map);
1153 if (error)
1154 goto unmap;
1155
1156 error = bus_dmamap_load(sc->sc_dmatag, p->map, p->addr, p->size, NULL,
1157 BUS_DMA_NOWAIT);
1158 if (error)
1159 goto destroy;
1160 return 0;
1161
1162 destroy:
1163 bus_dmamap_destroy(sc->sc_dmatag, p->map);
1164 unmap:
1165 bus_dmamem_unmap(sc->sc_dmatag, p->addr, p->size);
1166 free:
1167 bus_dmamem_free(sc->sc_dmatag, p->segs, p->nsegs);
1168 return error;
1169 }
1170
1171 static int
1172 yds_freemem(struct yds_softc *sc, struct yds_dma *p)
1173 {
1174
1175 bus_dmamap_unload(sc->sc_dmatag, p->map);
1176 bus_dmamap_destroy(sc->sc_dmatag, p->map);
1177 bus_dmamem_unmap(sc->sc_dmatag, p->addr, p->size);
1178 bus_dmamem_free(sc->sc_dmatag, p->segs, p->nsegs);
1179 return 0;
1180 }
1181
1182 static int
1183 yds_open(void *addr, int flags)
1184 {
1185 struct yds_softc *sc;
1186 uint32_t mode;
1187
1188 sc = addr;
1189 /* Select bank 0. */
1190 YWRITE4(sc, YDS_CONTROL_SELECT, 0);
1191
1192 /* Start the DSP operation. */
1193 mode = YREAD4(sc, YDS_MODE);
1194 mode |= YDS_MODE_ACTV;
1195 mode &= ~YDS_MODE_ACTV2;
1196 YWRITE4(sc, YDS_MODE, mode);
1197
1198 return 0;
1199 }
1200
1201 /*
1202 * Close function is called at splaudio().
1203 */
1204 static void
1205 yds_close(void *addr)
1206 {
1207
1208 yds_halt(addr);
1209 }
1210
1211 static int
1212 yds_query_encoding(void *addr, struct audio_encoding *fp)
1213 {
1214 struct yds_softc *sc;
1215
1216 sc = addr;
1217 return auconv_query_encoding(sc->sc_encodings, fp);
1218 }
1219
1220 static int
1221 yds_set_params(void *addr, int setmode, int usemode,
1222 audio_params_t *play, audio_params_t* rec,
1223 stream_filter_list_t *pfil, stream_filter_list_t *rfil)
1224 {
1225 if (setmode & AUMODE_RECORD) {
1226 if (auconv_set_converter(yds_formats, YDS_NFORMATS,
1227 AUMODE_RECORD, rec, FALSE, rfil) < 0)
1228 return EINVAL;
1229 }
1230 if (setmode & AUMODE_PLAY) {
1231 if (auconv_set_converter(yds_formats, YDS_NFORMATS,
1232 AUMODE_PLAY, play, FALSE, pfil) < 0)
1233 return EINVAL;
1234 }
1235 return 0;
1236 }
1237
1238 static int
1239 yds_round_blocksize(void *addr, int blk, int mode,
1240 const audio_params_t *param)
1241 {
1242
1243 /*
1244 * Block size must be bigger than a frame.
1245 * That is 1024bytes at most, i.e. for 48000Hz, 16bit, 2ch.
1246 */
1247 if (blk < 1024)
1248 blk = 1024;
1249
1250 return blk & ~4;
1251 }
1252
1253 static uint32_t
1254 yds_get_lpfq(u_int sample_rate)
1255 {
1256 int i;
1257 static struct lpfqt {
1258 u_int rate;
1259 uint32_t lpfq;
1260 } lpfqt[] = {
1261 {8000, 0x32020000},
1262 {11025, 0x31770000},
1263 {16000, 0x31390000},
1264 {22050, 0x31c90000},
1265 {32000, 0x33d00000},
1266 {48000, 0x40000000},
1267 {0, 0}
1268 };
1269
1270 if (sample_rate == 44100) /* for P44 slot? */
1271 return 0x370A0000;
1272
1273 for (i = 0; lpfqt[i].rate != 0; i++)
1274 if (sample_rate <= lpfqt[i].rate)
1275 break;
1276
1277 return lpfqt[i].lpfq;
1278 }
1279
1280 static uint32_t
1281 yds_get_lpfk(u_int sample_rate)
1282 {
1283 int i;
1284 static struct lpfkt {
1285 u_int rate;
1286 uint32_t lpfk;
1287 } lpfkt[] = {
1288 {8000, 0x18b20000},
1289 {11025, 0x20930000},
1290 {16000, 0x2b9a0000},
1291 {22050, 0x35a10000},
1292 {32000, 0x3eaa0000},
1293 {48000, 0x40000000},
1294 {0, 0}
1295 };
1296
1297 if (sample_rate == 44100) /* for P44 slot? */
1298 return 0x46460000;
1299
1300 for (i = 0; lpfkt[i].rate != 0; i++)
1301 if (sample_rate <= lpfkt[i].rate)
1302 break;
1303
1304 return lpfkt[i].lpfk;
1305 }
1306
1307 static int
1308 yds_trigger_output(void *addr, void *start, void *end, int blksize,
1309 void (*intr)(void *), void *arg, const audio_params_t *param)
1310 #define P44 (sc->sc_flags & YDS_CAP_HAS_P44)
1311 {
1312 struct yds_softc *sc;
1313 struct yds_dma *p;
1314 struct play_slot_ctrl_bank *psb;
1315 const u_int gain = 0x40000000;
1316 bus_addr_t s;
1317 size_t l;
1318 int i;
1319 int p44, channels;
1320 uint32_t format;
1321
1322 sc = addr;
1323 #ifdef DIAGNOSTIC
1324 if (sc->sc_play.intr)
1325 panic("yds_trigger_output: already running");
1326 #endif
1327
1328 sc->sc_play.intr = intr;
1329 sc->sc_play.intr_arg = arg;
1330 sc->sc_play.offset = 0;
1331 sc->sc_play.blksize = blksize;
1332
1333 DPRINTFN(1, ("yds_trigger_output: sc=%p start=%p end=%p "
1334 "blksize=%d intr=%p(%p)\n", addr, start, end, blksize, intr, arg));
1335
1336 p = yds_find_dma(sc, start);
1337 if (!p) {
1338 printf("yds_trigger_output: bad addr %p\n", start);
1339 return EINVAL;
1340 }
1341 sc->sc_play.dma = p;
1342
1343 #ifdef YDS_USE_P44
1344 /* The document says the P44 SRC supports only stereo, 16bit PCM. */
1345 if (P44)
1346 p44 = ((param->sample_rate == 44100) &&
1347 (param->channels == 2) &&
1348 (param->precision == 16));
1349 else
1350 #endif
1351 p44 = 0;
1352 channels = p44 ? 1 : param->channels;
1353
1354 s = DMAADDR(p);
1355 l = ((char *)end - (char *)start);
1356 sc->sc_play.length = l;
1357
1358 *sc->ptbl = htole32(channels); /* Num of play */
1359
1360 sc->sc_play.factor = 1;
1361 if (param->channels == 2)
1362 sc->sc_play.factor *= 2;
1363 if (param->precision != 8)
1364 sc->sc_play.factor *= 2;
1365 l /= sc->sc_play.factor;
1366
1367 format = ((channels == 2 ? PSLT_FORMAT_STEREO : 0) |
1368 (param->precision == 8 ? PSLT_FORMAT_8BIT : 0) |
1369 (p44 ? PSLT_FORMAT_SRC441 : 0));
1370
1371 psb = sc->pbankp[0];
1372 memset(psb, 0, sizeof(*psb));
1373 psb->format = htole32(format);
1374 psb->pgbase = htole32(s);
1375 psb->pgloopend = htole32(l);
1376 if (!p44) {
1377 psb->pgdeltaend = htole32((param->sample_rate * 65536 / 48000) << 12);
1378 psb->lpfkend = htole32(yds_get_lpfk(param->sample_rate));
1379 psb->eggainend = htole32(gain);
1380 psb->lpfq = htole32(yds_get_lpfq(param->sample_rate));
1381 psb->pgdelta = htole32(psb->pgdeltaend);
1382 psb->lpfk = htole32(yds_get_lpfk(param->sample_rate));
1383 psb->eggain = htole32(gain);
1384 }
1385
1386 for (i = 0; i < channels; i++) {
1387 /* i == 0: left or mono, i == 1: right */
1388 psb = sc->pbankp[i*2];
1389 if (i)
1390 /* copy from left */
1391 *psb = *(sc->pbankp[0]);
1392 if (channels == 2) {
1393 /* stereo */
1394 if (i == 0) {
1395 psb->lchgain = psb->lchgainend = htole32(gain);
1396 } else {
1397 psb->lchgain = psb->lchgainend = 0;
1398 psb->rchgain = psb->rchgainend = htole32(gain);
1399 psb->format |= htole32(PSLT_FORMAT_RCH);
1400 }
1401 } else if (!p44) {
1402 /* mono */
1403 psb->lchgain = psb->rchgain = htole32(gain);
1404 psb->lchgainend = psb->rchgainend = htole32(gain);
1405 }
1406 /* copy to the other bank */
1407 *(sc->pbankp[i*2+1]) = *psb;
1408 }
1409
1410 YDS_DUMP_PLAY_SLOT(5, sc, 0);
1411 YDS_DUMP_PLAY_SLOT(5, sc, 1);
1412
1413 if (p44)
1414 YWRITE4(sc, YDS_P44_OUT_VOLUME, 0x3fff3fff);
1415 else
1416 YWRITE4(sc, YDS_DAC_OUT_VOLUME, 0x3fff3fff);
1417
1418 /* Now the play slot for the next frame is set up!! */
1419 /* Sync play slot control data for both directions */
1420 bus_dmamap_sync(sc->sc_dmatag, sc->sc_ctrldata.map,
1421 sc->ptbloff,
1422 sizeof(struct play_slot_ctrl_bank) *
1423 channels * N_PLAY_SLOT_CTRL_BANK,
1424 BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
1425 /* Sync ring buffer */
1426 bus_dmamap_sync(sc->sc_dmatag, p->map, 0, blksize,
1427 BUS_DMASYNC_PREWRITE);
1428 /* HERE WE GO!! */
1429 YWRITE4(sc, YDS_MODE,
1430 YREAD4(sc, YDS_MODE) | YDS_MODE_ACTV | YDS_MODE_ACTV2);
1431
1432 return 0;
1433 }
1434 #undef P44
1435
1436 static int
1437 yds_trigger_input(void *addr, void *start, void *end, int blksize,
1438 void (*intr)(void *), void *arg, const audio_params_t *param)
1439 {
1440 struct yds_softc *sc;
1441 struct yds_dma *p;
1442 u_int srate, format;
1443 struct rec_slot_ctrl_bank *rsb;
1444 bus_addr_t s;
1445 size_t l;
1446
1447 sc = addr;
1448 #ifdef DIAGNOSTIC
1449 if (sc->sc_rec.intr)
1450 panic("yds_trigger_input: already running");
1451 #endif
1452 sc->sc_rec.intr = intr;
1453 sc->sc_rec.intr_arg = arg;
1454 sc->sc_rec.offset = 0;
1455 sc->sc_rec.blksize = blksize;
1456
1457 DPRINTFN(1, ("yds_trigger_input: "
1458 "sc=%p start=%p end=%p blksize=%d intr=%p(%p)\n",
1459 addr, start, end, blksize, intr, arg));
1460 DPRINTFN(1, (" parameters: rate=%u, precision=%u, channels=%u\n",
1461 param->sample_rate, param->precision, param->channels));
1462
1463 p = yds_find_dma(sc, start);
1464 if (!p) {
1465 printf("yds_trigger_input: bad addr %p\n", start);
1466 return EINVAL;
1467 }
1468 sc->sc_rec.dma = p;
1469
1470 s = DMAADDR(p);
1471 l = ((char *)end - (char *)start);
1472 sc->sc_rec.length = l;
1473
1474 sc->sc_rec.factor = 1;
1475 if (param->channels == 2)
1476 sc->sc_rec.factor *= 2;
1477 if (param->precision != 8)
1478 sc->sc_rec.factor *= 2;
1479
1480 rsb = &sc->rbank[0];
1481 memset(rsb, 0, sizeof(*rsb));
1482 rsb->pgbase = htole32(s);
1483 rsb->pgloopendadr = htole32(l);
1484 /* Seems all 4 banks must be set up... */
1485 sc->rbank[1] = *rsb;
1486 sc->rbank[2] = *rsb;
1487 sc->rbank[3] = *rsb;
1488
1489 YWRITE4(sc, YDS_ADC_IN_VOLUME, 0x3fff3fff);
1490 YWRITE4(sc, YDS_REC_IN_VOLUME, 0x3fff3fff);
1491 srate = 48000 * 4096 / param->sample_rate - 1;
1492 format = ((param->precision == 8 ? YDS_FORMAT_8BIT : 0) |
1493 (param->channels == 2 ? YDS_FORMAT_STEREO : 0));
1494 DPRINTF(("srate=%d, format=%08x\n", srate, format));
1495 #ifdef YDS_USE_REC_SLOT
1496 YWRITE4(sc, YDS_DAC_REC_VOLUME, 0x3fff3fff);
1497 YWRITE4(sc, YDS_P44_REC_VOLUME, 0x3fff3fff);
1498 YWRITE4(sc, YDS_MAPOF_REC, YDS_RECSLOT_VALID);
1499 YWRITE4(sc, YDS_REC_SAMPLE_RATE, srate);
1500 YWRITE4(sc, YDS_REC_FORMAT, format);
1501 #else
1502 YWRITE4(sc, YDS_MAPOF_REC, YDS_ADCSLOT_VALID);
1503 YWRITE4(sc, YDS_ADC_SAMPLE_RATE, srate);
1504 YWRITE4(sc, YDS_ADC_FORMAT, format);
1505 #endif
1506 /* Now the rec slot for the next frame is set up!! */
1507 /* Sync record slot control data */
1508 bus_dmamap_sync(sc->sc_dmatag, sc->sc_ctrldata.map,
1509 sc->rbankoff,
1510 sizeof(struct rec_slot_ctrl_bank)*
1511 N_REC_SLOT_CTRL*
1512 N_REC_SLOT_CTRL_BANK,
1513 BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
1514 /* Sync ring buffer */
1515 bus_dmamap_sync(sc->sc_dmatag, p->map, 0, blksize,
1516 BUS_DMASYNC_PREREAD);
1517 /* HERE WE GO!! */
1518 YWRITE4(sc, YDS_MODE,
1519 YREAD4(sc, YDS_MODE) | YDS_MODE_ACTV | YDS_MODE_ACTV2);
1520
1521 return 0;
1522 }
1523
1524 static int
1525 yds_halt(struct yds_softc *sc)
1526 {
1527 uint32_t mode;
1528
1529 /* Stop the DSP operation. */
1530 mode = YREAD4(sc, YDS_MODE);
1531 YWRITE4(sc, YDS_MODE, mode & ~(YDS_MODE_ACTV|YDS_MODE_ACTV2));
1532
1533 /* Paranoia... mute all */
1534 YWRITE4(sc, YDS_P44_OUT_VOLUME, 0);
1535 YWRITE4(sc, YDS_DAC_OUT_VOLUME, 0);
1536 YWRITE4(sc, YDS_ADC_IN_VOLUME, 0);
1537 YWRITE4(sc, YDS_REC_IN_VOLUME, 0);
1538 YWRITE4(sc, YDS_DAC_REC_VOLUME, 0);
1539 YWRITE4(sc, YDS_P44_REC_VOLUME, 0);
1540
1541 return 0;
1542 }
1543
1544 static int
1545 yds_halt_output(void *addr)
1546 {
1547 struct yds_softc *sc;
1548
1549 DPRINTF(("yds: yds_halt_output\n"));
1550 sc = addr;
1551 if (sc->sc_play.intr) {
1552 sc->sc_play.intr = 0;
1553 /* Sync play slot control data */
1554 bus_dmamap_sync(sc->sc_dmatag, sc->sc_ctrldata.map,
1555 sc->pbankoff,
1556 sizeof(struct play_slot_ctrl_bank)*
1557 (*sc->ptbl)*N_PLAY_SLOT_CTRL_BANK,
1558 BUS_DMASYNC_POSTWRITE|BUS_DMASYNC_POSTREAD);
1559 /* Stop the play slot operation */
1560 sc->pbankp[0]->status =
1561 sc->pbankp[1]->status =
1562 sc->pbankp[2]->status =
1563 sc->pbankp[3]->status = 1;
1564 /* Sync ring buffer */
1565 bus_dmamap_sync(sc->sc_dmatag, sc->sc_play.dma->map,
1566 0, sc->sc_play.length, BUS_DMASYNC_POSTWRITE);
1567 }
1568
1569 return 0;
1570 }
1571
1572 static int
1573 yds_halt_input(void *addr)
1574 {
1575 struct yds_softc *sc;
1576
1577 DPRINTF(("yds: yds_halt_input\n"));
1578 sc = addr;
1579 sc->sc_rec.intr = NULL;
1580 if (sc->sc_rec.intr) {
1581 /* Stop the rec slot operation */
1582 YWRITE4(sc, YDS_MAPOF_REC, 0);
1583 sc->sc_rec.intr = 0;
1584 /* Sync rec slot control data */
1585 bus_dmamap_sync(sc->sc_dmatag, sc->sc_ctrldata.map,
1586 sc->rbankoff,
1587 sizeof(struct rec_slot_ctrl_bank)*
1588 N_REC_SLOT_CTRL*N_REC_SLOT_CTRL_BANK,
1589 BUS_DMASYNC_POSTWRITE|BUS_DMASYNC_POSTREAD);
1590 /* Sync ring buffer */
1591 bus_dmamap_sync(sc->sc_dmatag, sc->sc_rec.dma->map,
1592 0, sc->sc_rec.length, BUS_DMASYNC_POSTREAD);
1593 }
1594
1595 return 0;
1596 }
1597
1598 static int
1599 yds_getdev(void *addr, struct audio_device *retp)
1600 {
1601
1602 *retp = yds_device;
1603 return 0;
1604 }
1605
1606 static int
1607 yds_mixer_set_port(void *addr, mixer_ctrl_t *cp)
1608 {
1609 struct yds_softc *sc;
1610
1611 sc = addr;
1612 return sc->sc_codec[0].codec_if->vtbl->mixer_set_port(
1613 sc->sc_codec[0].codec_if, cp);
1614 }
1615
1616 static int
1617 yds_mixer_get_port(void *addr, mixer_ctrl_t *cp)
1618 {
1619 struct yds_softc *sc;
1620
1621 sc = addr;
1622 return sc->sc_codec[0].codec_if->vtbl->mixer_get_port(
1623 sc->sc_codec[0].codec_if, cp);
1624 }
1625
1626 static int
1627 yds_query_devinfo(void *addr, mixer_devinfo_t *dip)
1628 {
1629 struct yds_softc *sc;
1630
1631 sc = addr;
1632 return sc->sc_codec[0].codec_if->vtbl->query_devinfo(
1633 sc->sc_codec[0].codec_if, dip);
1634 }
1635
1636 static void *
1637 yds_malloc(void *addr, int direction, size_t size,
1638 struct malloc_type *pool, int flags)
1639 {
1640 struct yds_softc *sc;
1641 struct yds_dma *p;
1642 int error;
1643
1644 p = malloc(sizeof(*p), pool, flags);
1645 if (p == NULL)
1646 return NULL;
1647 sc = addr;
1648 error = yds_allocmem(sc, size, 16, p);
1649 if (error) {
1650 free(p, pool);
1651 return NULL;
1652 }
1653 p->next = sc->sc_dmas;
1654 sc->sc_dmas = p;
1655 return KERNADDR(p);
1656 }
1657
1658 static void
1659 yds_free(void *addr, void *ptr, struct malloc_type *pool)
1660 {
1661 struct yds_softc *sc;
1662 struct yds_dma **pp, *p;
1663
1664 sc = addr;
1665 for (pp = &sc->sc_dmas; (p = *pp) != NULL; pp = &p->next) {
1666 if (KERNADDR(p) == ptr) {
1667 yds_freemem(sc, p);
1668 *pp = p->next;
1669 free(p, pool);
1670 return;
1671 }
1672 }
1673 }
1674
1675 static struct yds_dma *
1676 yds_find_dma(struct yds_softc *sc, void *addr)
1677 {
1678 struct yds_dma *p;
1679
1680 for (p = sc->sc_dmas; p && KERNADDR(p) != addr; p = p->next)
1681 continue;
1682
1683 return p;
1684 }
1685
1686 static size_t
1687 yds_round_buffersize(void *addr, int direction, size_t size)
1688 {
1689
1690 /*
1691 * Buffer size should be at least twice as bigger as a frame.
1692 */
1693 if (size < 1024 * 3)
1694 size = 1024 * 3;
1695 return size;
1696 }
1697
1698 static paddr_t
1699 yds_mappage(void *addr, void *mem, off_t off, int prot)
1700 {
1701 struct yds_softc *sc;
1702 struct yds_dma *p;
1703
1704 if (off < 0)
1705 return -1;
1706 sc = addr;
1707 p = yds_find_dma(sc, mem);
1708 if (p == NULL)
1709 return -1;
1710 return bus_dmamem_mmap(sc->sc_dmatag, p->segs, p->nsegs,
1711 off, prot, BUS_DMA_WAITOK);
1712 }
1713
1714 static int
1715 yds_get_props(void *addr)
1716 {
1717
1718 return AUDIO_PROP_MMAP | AUDIO_PROP_INDEPENDENT |
1719 AUDIO_PROP_FULLDUPLEX;
1720 }
1721