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