xref: /openbsd-src/sys/dev/isa/mpu_isa.c (revision a28daedfc357b214be5c701aa8ba8adb29a7f1c2)
1 /*	$OpenBSD: mpu_isa.c,v 1.4 2009/03/29 21:53:52 sthen Exp $	*/
2 
3 /*
4  * Copyright (c) 2002 Sergey Smitienko. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
21  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
27  * OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/errno.h>
33 #include <sys/ioctl.h>
34 #include <sys/syslog.h>
35 #include <sys/device.h>
36 #include <sys/proc.h>
37 
38 #include <machine/bus.h>
39 
40 #include <sys/audioio.h>
41 #include <dev/audio_if.h>
42 #include <dev/midi_if.h>
43 
44 #include <dev/isa/isavar.h>
45 #include <dev/isa/isadmavar.h>
46 
47 #include <dev/ic/mpuvar.h>
48 
49 int	mpu_isa_match(struct device *, void *, void *);
50 void	mpu_isa_attach(struct device *, struct device *, void *);
51 int	mpu_test(bus_space_tag_t, int);
52 
53 #ifdef	AUDIO_DEBUG
54 #define	DPRINTF(x)	if (mpu_debug) printf x
55 int	mpu_debug = 0;
56 #else
57 #define	DPRINTF(x)
58 #endif
59 
60 #define MPU_GETSTATUS(iot, ioh) (bus_space_read_1(iot, ioh, MPU_STATUS))
61 
62 struct mpu_isa_softc {
63 	struct device sc_dev;
64 
65 	struct mpu_softc sc_mpu;
66 };
67 
68 struct cfattach mpu_isa_ca = {
69 	sizeof(struct mpu_isa_softc), mpu_isa_match, mpu_isa_attach
70 };
71 
72 int
73 mpu_test (iot, iobase)
74 	bus_space_tag_t iot;
75 	int iobase;	/* base port number to try */
76 {
77 	bus_space_handle_t ioh;
78 	int	i, rc;
79 
80 	rc = 0;
81 	if (bus_space_map(iot, iobase, MPU401_NPORT, 0, &ioh)) {
82 		DPRINTF(("mpu_test: can't map: %x/2\n", iobase));
83 		return (0);
84 	}
85 
86 	DPRINTF(("mpu_test: trying: %x\n", iobase));
87 
88 	/*
89 	 * The following code is a shameless copy of mpu401.c
90 	 * it is here until a redesign of mpu_find() interface
91 	 */
92 
93 	if (MPU_GETSTATUS(iot, ioh) == 0xff)
94 		goto done;
95 
96 	for (i = 0; i < MPU_MAXWAIT; i++) {
97 		if (!(MPU_GETSTATUS(iot, ioh) & MPU_OUTPUT_BUSY)) {
98 			rc = 1;
99 			break;
100 		}
101 		delay (10);
102 	}
103 
104 	if (rc == 1) {
105 		bus_space_write_1(iot, ioh, MPU_COMMAND, MPU_RESET);
106 		rc = 0;
107 		for (i = 0; i < 2 * MPU_MAXWAIT; i++)
108 			if (!(MPU_GETSTATUS(iot, ioh) & MPU_INPUT_EMPTY) &&
109 			    bus_space_read_1(iot, ioh, MPU_DATA) == MPU_ACK) {
110 				rc = 1;
111 				break;
112 			}
113 	}
114 done:
115 	bus_space_unmap(iot, ioh, MPU401_NPORT);
116 
117 	return (rc);
118 }
119 
120 int
121 mpu_isa_match(parent, match, aux)
122 	struct device *parent;
123 	void *match, *aux;
124 {
125 	struct isa_attach_args *ia = aux;
126 
127         if (mpu_test(ia->ia_iot, ia->ia_iobase)) {
128 		ia->ia_iosize = MPU401_NPORT;
129 		return (1);
130 	}
131 
132 	return (0);
133 }
134 
135 void
136 mpu_isa_attach(parent, self, aux)
137 	struct device *parent, *self;
138 	void *aux;
139 {
140 	struct mpu_isa_softc *sc = (struct mpu_isa_softc *)self;
141 	struct isa_attach_args *ia = aux;
142 
143 	sc->sc_mpu.iot = ia->ia_iot;
144 
145 	if (bus_space_map (ia->ia_iot, ia->ia_iobase, MPU401_NPORT,
146 	    0, &sc->sc_mpu.ioh)) {
147 		printf(": can't map i/o space\n");
148 		return;
149 	}
150 
151 	if (!mpu_find(&sc->sc_mpu)) {
152 		printf(": find failed\n");
153 		return;
154 	}
155 
156 	printf(": generic MPU-401 compatible\n");
157 
158 	midi_attach_mi(&mpu_midi_hw_if, &sc->sc_mpu, &sc->sc_dev);
159 }
160