xref: /netbsd-src/sys/dev/isapnp/wss_isapnp.c (revision a5edd5414c8d7aa09f1beb4c011033b24983be3c)
1 /*	$NetBSD: wss_isapnp.c,v 1.31 2021/10/07 00:01:45 uwe Exp $	*/
2 
3 /*
4  * Copyright (c) 1997, 1999 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Lennart Augustsson (augustss@NetBSD.org).
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. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: wss_isapnp.c,v 1.31 2021/10/07 00:01:45 uwe Exp $");
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/device.h>
38 #include <sys/errno.h>
39 
40 #include <sys/bus.h>
41 
42 #include <sys/audioio.h>
43 #include <dev/audio/audio_if.h>
44 
45 #include <dev/isa/isavar.h>
46 #include <dev/isa/isadmavar.h>
47 
48 #include <dev/isapnp/isapnpreg.h>
49 #include <dev/isapnp/isapnpvar.h>
50 #include <dev/isapnp/isapnpdevs.h>
51 
52 #include <dev/isa/ad1848var.h>
53 #include <dev/isa/madreg.h>
54 #include <dev/isa/wssreg.h>
55 #include <dev/isa/wssvar.h>
56 #include <dev/isa/sbreg.h>
57 
58 int	wss_isapnp_match(device_t, cfdata_t, void *);
59 void	wss_isapnp_attach(device_t, device_t, void *);
60 
61 CFATTACH_DECL_NEW(wss_isapnp, sizeof(struct wss_softc),
62     wss_isapnp_match, wss_isapnp_attach, NULL, NULL);
63 
64 /*
65  * Probe / attach routines.
66  */
67 
68 /*
69  * Probe for the WSS hardware.
70  */
71 int
wss_isapnp_match(device_t parent,cfdata_t match,void * aux)72 wss_isapnp_match(device_t parent, cfdata_t match, void *aux)
73 {
74 	int pri, variant;
75 
76 	pri = isapnp_devmatch(aux, &isapnp_wss_devinfo, &variant);
77 	if (pri && variant > 1)
78 		pri = 0;
79 	return pri;
80 }
81 
82 /*
83  * Attach hardware to driver, attach hardware driver to audio
84  * pseudo-device driver.
85  */
86 void
wss_isapnp_attach(device_t parent,device_t self,void * aux)87 wss_isapnp_attach(device_t parent, device_t self, void *aux)
88 {
89 	struct wss_softc *sc;
90 	struct ad1848_softc *ac;
91 	struct isapnp_attach_args *ipa;
92 	int variant;
93 
94 	sc = device_private(self);
95 	ac = &sc->sc_ad1848.sc_ad1848;
96 	ac->sc_dev = self;
97 	ipa = aux;
98 	printf("\n");
99 
100 	if (!isapnp_devmatch(aux, &isapnp_wss_devinfo, &variant)) {
101 		aprint_error_dev(self, "match failed?\n");
102 		return;
103 	}
104 
105 	if (isapnp_config(ipa->ipa_iot, ipa->ipa_memt, ipa)) {
106 		aprint_error_dev(self, "error in region allocation\n");
107 		return;
108 	}
109 
110 	switch (variant) {
111 	case 1:
112 		/* We have to put the chip into `WSS mode'. */
113 		{
114 			bus_space_tag_t iot;
115 			bus_space_handle_t ioh;
116 
117 			iot = ipa->ipa_iot;
118 			ioh = ipa->ipa_io[0].h;
119 
120 			while (bus_space_read_1(iot, ioh,
121 			    SBP_DSP_WSTAT) & SB_DSP_BUSY);
122 			bus_space_write_1(iot, ioh, SBP_DSP_WRITE, 0x09);
123 			while (bus_space_read_1(iot, ioh,
124 			    SBP_DSP_WSTAT) & SB_DSP_BUSY);
125 			bus_space_write_1(iot, ioh, SBP_DSP_WRITE, 0x00);
126 			while (bus_space_read_1(iot, ioh,
127 			    SBP_DSP_WSTAT) & SB_DSP_BUSY);
128 
129 			delay(1000);
130 		}
131 
132 		sc->sc_iot = ipa->ipa_iot;
133 		sc->sc_ioh = ipa->ipa_io[2].h;
134 		break;
135 
136 	default:
137 		sc->sc_iot = ipa->ipa_iot;
138 		sc->sc_ioh = ipa->ipa_io[0].h;
139 		break;
140 	}
141 
142 	sc->mad_chip_type = MAD_NONE;
143 
144 	/* Set up AD1848 I/O handle. */
145 	ac->sc_iot = sc->sc_iot;
146 	ac->sc_ioh = sc->sc_ioh;
147 
148 	sc->sc_ad1848.sc_ic = ipa->ipa_ic;
149 
150 	sc->wss_ic = ipa->ipa_ic;
151 	sc->wss_irq = ipa->ipa_irq[0].num;
152 	sc->wss_playdrq = ipa->ipa_drq[0].num;
153 	sc->wss_recdrq =
154 	    ipa->ipa_ndrq > 1 ? ipa->ipa_drq[1].num : ipa->ipa_drq[0].num;
155 
156 	if (!ad1848_isa_probe(&sc->sc_ad1848)) {
157 		aprint_error_dev(self, "ad1848_probe failed\n");
158 		return;
159 	}
160 
161 	aprint_error_dev(self, "%s %s", ipa->ipa_devident,
162 	    ipa->ipa_devclass);
163 
164 	ac->mode = 2;
165 
166 	wssattach(sc);
167 
168 	/* set up OPL I/O handle for ISAPNP boards w/o MAD */
169 	if (ipa->ipa_nio > 1 && sc->mad_chip_type == MAD_NONE) {
170 		struct audio_attach_args arg;
171 
172 		sc->sc_opl_ioh = ipa->ipa_io[1].h;
173 
174 		arg.type = AUDIODEV_TYPE_OPL;
175 		arg.hwif = 0;
176 		arg.hdl = 0;
177 		config_found(self, &arg, audioprint, CFARGS(.iattr = "wss"));
178 	}
179 }
180