xref: /netbsd-src/sys/dev/ofisa/sb_ofisa.c (revision 7c3f385475147b6e1c4753f2bee961630e2dfc40)
1 /*	$NetBSD: sb_ofisa.c,v 1.16 2008/03/15 21:09:02 cube Exp $	*/
2 
3 /*-
4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9  * NASA Ames Research Center.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *	This product includes software developed by the NetBSD
22  *	Foundation, Inc. and its contributors.
23  * 4. Neither the name of The NetBSD Foundation nor the names of its
24  *    contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  */
39 
40 #include <sys/cdefs.h>
41 __KERNEL_RCSID(0, "$NetBSD: sb_ofisa.c,v 1.16 2008/03/15 21:09:02 cube Exp $");
42 
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/device.h>
46 
47 #include <sys/bus.h>
48 #include <sys/intr.h>
49 
50 #include <sys/audioio.h>
51 #include <dev/audio_if.h>
52 #include <dev/midi_if.h>
53 #include <dev/mulaw.h>
54 
55 #include <dev/ofw/openfirm.h>
56 #include <dev/isa/isavar.h>
57 #include <dev/ofisa/ofisavar.h>
58 
59 #include <dev/isa/sbreg.h>
60 #include <dev/isa/sbvar.h>
61 #include <dev/isa/sbdspvar.h>
62 
63 int	sb_ofisa_match(device_t, cfdata_t, void *);
64 void	sb_ofisa_attach(device_t, device_t, void *);
65 
66 CFATTACH_DECL_NEW(sb_ofisa, sizeof(struct sbdsp_softc),
67     sb_ofisa_match, sb_ofisa_attach, NULL, NULL);
68 
69 int
70 sb_ofisa_match(device_t parent, cfdata_t cf, void *aux)
71 {
72 	struct ofisa_attach_args *aa = aux;
73 	static const char *const compatible_strings[] = {
74 		"pnpPNP,b000",			/* generic SB 1.5 */
75 		"pnpPNP,b001",			/* generic SB 2.0 */
76 		"pnpPNP,b002",			/* generic SB Pro */
77 		"pnpPNP,b003",			/* generic SB 16 */
78 		NULL,
79 	};
80 	int rv = 0;
81 
82 	if (of_compatible(aa->oba.oba_phandle, compatible_strings) != -1) {
83 		/*
84 		 * Use a low match priority so that a more specific driver
85 		 * can match, e.g. a native ESS driver.
86 		 */
87 		rv = 1;
88 	}
89 
90 	return (rv);
91 }
92 
93 void
94 sb_ofisa_attach(device_t parent, device_t self, void *aux)
95 {
96 	struct sbdsp_softc *sc = device_private(self);
97 	struct ofisa_attach_args *aa = aux;
98 	struct ofisa_reg_desc reg;
99 	struct ofisa_intr_desc intr;
100 	struct ofisa_dma_desc dma[2];
101 	int n, ndrq;
102 	char *model;
103 
104 	sc->sc_dev = self;
105 
106 	/*
107 	 * We're living on an OFW.  We have to ask the OFW what our
108 	 * registers and interrupts properties look like.
109 	 *
110 	 * We expect:
111 	 *
112 	 *	1 i/o register region
113 	 *	1 interrupt
114 	 *	1 or 2 DMA channels
115 	 */
116 
117 	n = ofisa_reg_get(aa->oba.oba_phandle, &reg, 1);
118 	if (n != 1) {
119 		printf(": error getting register data\n");
120 		return;
121 	}
122 	if (reg.type != OFISA_REG_TYPE_IO) {
123 		printf(": register type not i/o\n");
124 		return;
125 	}
126 	if (reg.len != SB_NPORT && reg.len != SBP_NPORT) {
127 		printf(": weird register size (%lu, expected %d or %d)\n",
128 		    (unsigned long)reg.len, SB_NPORT, SBP_NPORT);
129 		return;
130 	}
131 
132 	n = ofisa_intr_get(aa->oba.oba_phandle, &intr, 1);
133 	if (n != 1) {
134 		printf(": error getting interrupt data\n");
135 		return;
136 	}
137 
138 	ndrq = ofisa_dma_get(aa->oba.oba_phandle, dma, 2);
139 	if (ndrq != 1 && ndrq != 2) {
140 		printf(": error getting DMA data\n");
141 		return;
142 	}
143 
144 	sc->sc_ic = aa->ic;
145 
146 	sc->sc_iot = aa->iot;
147 	if (bus_space_map(sc->sc_iot, reg.addr, reg.len, 0, &sc->sc_ioh)) {
148 		aprint_error(": unable to map register space\n");
149 		return;
150 	}
151 
152 	/* XXX These are only for setting chip configuration registers. */
153 	sc->sc_iobase = reg.addr;
154 	sc->sc_irq = intr.irq;
155 
156 	sc->sc_drq8 = DRQUNK;
157 	sc->sc_drq16 = DRQUNK;
158 
159 	for (n = 0; n < ndrq; n++) {
160 		/* XXX check mode? */
161 		switch (dma[n].width) {
162 		case 8:
163 			if (sc->sc_drq8 == DRQUNK)
164 				sc->sc_drq8 = dma[n].drq;
165 			break;
166 		case 16:
167 			if (sc->sc_drq16 == DRQUNK)
168 				sc->sc_drq16 = dma[n].drq;
169 			break;
170 		default:
171 			aprint_error(": weird DMA width %d\n", dma[n].width);
172 			return;
173 		}
174 	}
175 
176 	if (sc->sc_drq8 == DRQUNK) {
177 		aprint_error(": no 8-bit DMA channel\n");
178 		return;
179 	}
180 
181 	if (sbmatch(sc) == 0) {
182 		aprint_error(": sbmatch failed\n");
183 		return;
184 	}
185 
186 	sc->sc_ih = isa_intr_establish(aa->ic, intr.irq, IST_EDGE, IPL_AUDIO,
187 	    sbdsp_intr, sc);
188 
189 	n = OF_getproplen(aa->oba.oba_phandle, "model");
190 	if (n > 0) {
191 		model = alloca(n);
192 		if (OF_getprop(aa->oba.oba_phandle, "model", model, n) == n)
193 			aprint_normal(": %s\n%s", model, device_xname(self));
194 	}
195 
196 	sbattach(sc);
197 }
198