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