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