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