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