xref: /openbsd-src/sys/dev/isa/mpu_isa.c (revision 471aeecfc619bc9b69519928152daf993376c2a1)
1 /*	$OpenBSD: mpu_isa.c,v 1.8 2022/04/06 18:59:28 naddy 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 const struct cfattach mpu_isa_ca = {
67 	sizeof(struct mpu_isa_softc), mpu_isa_match, mpu_isa_attach
68 };
69 
70 int
mpu_test(bus_space_tag_t iot,int iobase)71 mpu_test(bus_space_tag_t iot, int iobase)	/* base port number to try */
72 {
73 	bus_space_handle_t ioh;
74 	int	i, rc;
75 
76 	rc = 0;
77 	if (bus_space_map(iot, iobase, MPU401_NPORT, 0, &ioh)) {
78 		DPRINTF(("mpu_test: can't map: %x/2\n", iobase));
79 		return (0);
80 	}
81 
82 	DPRINTF(("mpu_test: trying: %x\n", iobase));
83 
84 	/*
85 	 * The following code is a shameless copy of mpu401.c
86 	 * it is here until a redesign of mpu_find() interface
87 	 */
88 
89 	if (MPU_GETSTATUS(iot, ioh) == 0xff)
90 		goto done;
91 
92 	for (i = 0; i < MPU_MAXWAIT; i++) {
93 		if (!(MPU_GETSTATUS(iot, ioh) & MPU_OUTPUT_BUSY)) {
94 			rc = 1;
95 			break;
96 		}
97 		delay (10);
98 	}
99 
100 	if (rc == 1) {
101 		bus_space_write_1(iot, ioh, MPU_COMMAND, MPU_RESET);
102 		rc = 0;
103 		for (i = 0; i < 2 * MPU_MAXWAIT; i++)
104 			if (!(MPU_GETSTATUS(iot, ioh) & MPU_INPUT_EMPTY) &&
105 			    bus_space_read_1(iot, ioh, MPU_DATA) == MPU_ACK) {
106 				rc = 1;
107 				break;
108 			}
109 	}
110 done:
111 	bus_space_unmap(iot, ioh, MPU401_NPORT);
112 
113 	return (rc);
114 }
115 
116 int
mpu_isa_match(struct device * parent,void * match,void * aux)117 mpu_isa_match(struct device *parent, void *match, void *aux)
118 {
119 	struct isa_attach_args *ia = aux;
120 
121         if (mpu_test(ia->ia_iot, ia->ia_iobase)) {
122 		ia->ia_iosize = MPU401_NPORT;
123 		return (1);
124 	}
125 
126 	return (0);
127 }
128 
129 void
mpu_isa_attach(struct device * parent,struct device * self,void * aux)130 mpu_isa_attach(struct device *parent, struct device *self, void *aux)
131 {
132 	struct mpu_isa_softc *sc = (struct mpu_isa_softc *)self;
133 	struct isa_attach_args *ia = aux;
134 
135 	sc->sc_mpu.iot = ia->ia_iot;
136 
137 	if (bus_space_map (ia->ia_iot, ia->ia_iobase, MPU401_NPORT,
138 	    0, &sc->sc_mpu.ioh)) {
139 		printf(": can't map i/o space\n");
140 		return;
141 	}
142 
143 	if (!mpu_find(&sc->sc_mpu)) {
144 		printf(": find failed\n");
145 		return;
146 	}
147 
148 	printf(": generic MPU-401 compatible\n");
149 
150 	midi_attach_mi(&mpu_midi_hw_if, &sc->sc_mpu, &sc->sc_dev);
151 }
152