xref: /openbsd/sys/dev/isa/mpu_isa.c (revision 4cfece93)
1 /*	$OpenBSD: mpu_isa.c,v 1.6 2015/03/14 03:38:47 jsg 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 
37 #include <machine/bus.h>
38 
39 #include <sys/audioio.h>
40 #include <dev/midi_if.h>
41 
42 #include <dev/isa/isavar.h>
43 #include <dev/isa/isadmavar.h>
44 
45 #include <dev/ic/mpuvar.h>
46 
47 int	mpu_isa_match(struct device *, void *, void *);
48 void	mpu_isa_attach(struct device *, struct device *, void *);
49 int	mpu_test(bus_space_tag_t, int);
50 
51 #ifdef	AUDIO_DEBUG
52 #define	DPRINTF(x)	if (mpu_debug) printf x
53 int	mpu_debug = 0;
54 #else
55 #define	DPRINTF(x)
56 #endif
57 
58 #define MPU_GETSTATUS(iot, ioh) (bus_space_read_1(iot, ioh, MPU_STATUS))
59 
60 struct mpu_isa_softc {
61 	struct device sc_dev;
62 
63 	struct mpu_softc sc_mpu;
64 };
65 
66 struct cfattach mpu_isa_ca = {
67 	sizeof(struct mpu_isa_softc), mpu_isa_match, mpu_isa_attach
68 };
69 
70 int
71 mpu_test (iot, iobase)
72 	bus_space_tag_t iot;
73 	int iobase;	/* base port number to try */
74 {
75 	bus_space_handle_t ioh;
76 	int	i, rc;
77 
78 	rc = 0;
79 	if (bus_space_map(iot, iobase, MPU401_NPORT, 0, &ioh)) {
80 		DPRINTF(("mpu_test: can't map: %x/2\n", iobase));
81 		return (0);
82 	}
83 
84 	DPRINTF(("mpu_test: trying: %x\n", iobase));
85 
86 	/*
87 	 * The following code is a shameless copy of mpu401.c
88 	 * it is here until a redesign of mpu_find() interface
89 	 */
90 
91 	if (MPU_GETSTATUS(iot, ioh) == 0xff)
92 		goto done;
93 
94 	for (i = 0; i < MPU_MAXWAIT; i++) {
95 		if (!(MPU_GETSTATUS(iot, ioh) & MPU_OUTPUT_BUSY)) {
96 			rc = 1;
97 			break;
98 		}
99 		delay (10);
100 	}
101 
102 	if (rc == 1) {
103 		bus_space_write_1(iot, ioh, MPU_COMMAND, MPU_RESET);
104 		rc = 0;
105 		for (i = 0; i < 2 * MPU_MAXWAIT; i++)
106 			if (!(MPU_GETSTATUS(iot, ioh) & MPU_INPUT_EMPTY) &&
107 			    bus_space_read_1(iot, ioh, MPU_DATA) == MPU_ACK) {
108 				rc = 1;
109 				break;
110 			}
111 	}
112 done:
113 	bus_space_unmap(iot, ioh, MPU401_NPORT);
114 
115 	return (rc);
116 }
117 
118 int
119 mpu_isa_match(parent, match, aux)
120 	struct device *parent;
121 	void *match, *aux;
122 {
123 	struct isa_attach_args *ia = aux;
124 
125         if (mpu_test(ia->ia_iot, ia->ia_iobase)) {
126 		ia->ia_iosize = MPU401_NPORT;
127 		return (1);
128 	}
129 
130 	return (0);
131 }
132 
133 void
134 mpu_isa_attach(parent, self, aux)
135 	struct device *parent, *self;
136 	void *aux;
137 {
138 	struct mpu_isa_softc *sc = (struct mpu_isa_softc *)self;
139 	struct isa_attach_args *ia = aux;
140 
141 	sc->sc_mpu.iot = ia->ia_iot;
142 
143 	if (bus_space_map (ia->ia_iot, ia->ia_iobase, MPU401_NPORT,
144 	    0, &sc->sc_mpu.ioh)) {
145 		printf(": can't map i/o space\n");
146 		return;
147 	}
148 
149 	if (!mpu_find(&sc->sc_mpu)) {
150 		printf(": find failed\n");
151 		return;
152 	}
153 
154 	printf(": generic MPU-401 compatible\n");
155 
156 	midi_attach_mi(&mpu_midi_hw_if, &sc->sc_mpu, &sc->sc_dev);
157 }
158