xref: /netbsd-src/sys/dev/acpi/ym_acpi.c (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1 /* $NetBSD: ym_acpi.c,v 1.11 2010/03/05 14:00:17 jruoho Exp $ */
2 
3 /*
4  * Copyright (c) 2006 Jasper Wallace <jasper@pointless.net>
5  * All rights reserved.
6  *
7  * Copyright (c) 2002 Jared D. McNeill <jmcneill@invisible.ca>
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: ym_acpi.c,v 1.11 2010/03/05 14:00:17 jruoho Exp $");
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 
37 #include <dev/acpi/acpivar.h>
38 
39 #include <dev/audio_if.h>
40 
41 #include <dev/ic/ad1848reg.h>
42 #include <dev/ic/opl3sa3reg.h>
43 
44 #include <dev/isa/ad1848var.h>
45 #include <dev/isa/wssreg.h>
46 #include <dev/isa/ymvar.h>
47 
48 
49 static int	ym_acpi_match(device_t, cfdata_t, void *);
50 static void	ym_acpi_attach(device_t, device_t, void *);
51 
52 CFATTACH_DECL(ym_acpi, sizeof(struct ym_softc), ym_acpi_match,
53     ym_acpi_attach, NULL, NULL);
54 
55 /*
56  * ym_acpi_match: autoconf(9) match routine
57  */
58 static int
59 ym_acpi_match(device_t parent, cfdata_t match, void *aux)
60 {
61 	struct acpi_attach_args *aa = aux;
62 
63 	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
64 		return 0;
65 	if (!aa->aa_node->ad_devinfo->HardwareId.String)
66 		return 0;
67 	/* Yamaha OPL3-SA2 or OPL3-SA3 */
68 	if (strcmp("YMH0021", aa->aa_node->ad_devinfo->HardwareId.String))
69 		return 0;
70 
71 	return 1;
72 }
73 
74 /*
75  * ym_acpi_attach: autoconf(9) attach routine
76  */
77 static void
78 ym_acpi_attach(device_t parent, device_t self, void *aux)
79 {
80 	struct ym_softc *sc = (struct ym_softc *)self;
81 	struct acpi_attach_args *aa = aux;
82 	struct acpi_resources res;
83 	struct acpi_io *sb_io, *codec_io, *opl_io, *control_io;
84 #if NMPU_YM > 0
85 	struct acpi_io *mpu_io;
86 #endif
87 	struct acpi_irq *irq;
88 	struct acpi_drq *playdrq, *recdrq;
89 	struct ad1848_softc *ac = &sc->sc_ad1848.sc_ad1848;
90 	ACPI_STATUS rv;
91 
92 	/* Parse our resources */
93 	rv = acpi_resource_parse(&sc->sc_ad1848.sc_ad1848.sc_dev,
94 	    aa->aa_node->ad_handle, "_CRS", &res,
95 	    &acpi_resource_parse_ops_default);
96 	if (ACPI_FAILURE(rv))
97 		return;
98 
99 	/*
100 	 * sc_sb_ioh	 @ 0
101 	 * sc_ioh	 @ 1
102 	 * sc_opl_ioh	 @ 2
103 	 * sc_mpu_ioh	 @ 3
104 	 * sc_controlioh @ 4
105 	 */
106 
107 	/* Find and map our i/o registers */
108 	sc->sc_iot = aa->aa_iot;
109 	sb_io	 = acpi_res_io(&res, 0);
110 	codec_io = acpi_res_io(&res, 1);
111 	opl_io	 = acpi_res_io(&res, 2);
112 #if NMPU_YM > 0
113 	mpu_io	 = acpi_res_io(&res, 3);
114 #endif
115 	control_io = acpi_res_io(&res, 4);
116 
117 	if (sb_io == NULL || codec_io == NULL || opl_io == NULL ||
118 #if NMPU_YM > 0
119 	    mpu_io == NULL ||
120 #endif
121 	    control_io == NULL) {
122 		aprint_error_dev(self, "unable to find i/o registers resource\n");
123 		goto out;
124 	}
125 	if (bus_space_map(sc->sc_iot, sb_io->ar_base, sb_io->ar_length,
126 	    0, &sc->sc_sb_ioh) != 0) {
127 		aprint_error_dev(self, "unable to map i/o registers (sb)\n");
128 		goto out;
129 	}
130 	if (bus_space_map(sc->sc_iot, codec_io->ar_base, codec_io->ar_length,
131 	    0, &sc->sc_ioh) != 0) {
132 		aprint_error_dev(self, "unable to map i/o registers (codec)\n");
133 		goto out;
134 	}
135 	if (bus_space_map(sc->sc_iot, opl_io->ar_base, opl_io->ar_length,
136 	    0, &sc->sc_opl_ioh) != 0) {
137 		aprint_error_dev(self, "unable to map i/o registers (opl)\n");
138 		goto out;
139 	}
140 #if NMPU_YM > 0
141 	if (bus_space_map(sc->sc_iot, mpu_io->ar_base, mpu_io->ar_length,
142 	    0, &sc->sc_mpu_ioh) != 0) {
143 		aprint_error_dev(self, "unable to map i/o registers (mpu)\n");
144 		goto out;
145 	}
146 #endif
147 	if (bus_space_map(sc->sc_iot, control_io->ar_base,
148 	    control_io->ar_length, 0, &sc->sc_controlioh) != 0) {
149 		aprint_error_dev(self, "unable to map i/o registers (control)\n");
150 		goto out;
151 	}
152 
153 	sc->sc_ic = aa->aa_ic;
154 
155 	/* Find our IRQ */
156 	irq = acpi_res_irq(&res, 0);
157 	if (irq == NULL) {
158 		aprint_error_dev(self, "unable to find irq resource\n");
159 		/* XXX bus_space_unmap */
160 		goto out;
161 	}
162 	sc->ym_irq = irq->ar_irq;
163 
164 	/* Find our playback and record DRQs */
165 	playdrq = acpi_res_drq(&res, 0);
166 	recdrq = acpi_res_drq(&res, 1);
167 	if (playdrq == NULL) {
168 		aprint_error_dev(self, "unable to find drq resources\n");
169 		/* XXX bus_space_unmap */
170 		goto out;
171 	}
172 	if (recdrq == NULL) {
173 		/* half-duplex mode */
174 		sc->ym_recdrq = sc->ym_playdrq = playdrq->ar_drq;
175 	} else {
176 		sc->ym_playdrq = playdrq->ar_drq;
177 		sc->ym_recdrq = recdrq->ar_drq;
178 	}
179 
180 	ac->sc_iot = sc->sc_iot;
181 	if (bus_space_subregion(sc->sc_iot, sc->sc_ioh, WSS_CODEC,
182 	    AD1848_NPORT, &ac->sc_ioh)) {
183 		aprint_error_dev(self, "bus_space_subregion failed\n");
184 		/* XXX cleanup */
185 		goto out;
186 	}
187 
188 	aprint_normal_dev(self, "");
189 
190 	ac->mode = 2;
191 	ac->MCE_bit = MODE_CHANGE_ENABLE;
192 
193 	sc->sc_ad1848.sc_ic = sc->sc_ic;
194 
195 	/* Attach our ym device */
196 	ym_attach(sc);
197 
198  out:
199 	acpi_resource_cleanup(&res);
200 }
201