1 /* $NetBSD: mpu_acpi.c,v 1.16 2021/01/29 15:49:55 thorpej Exp $ */
2 
3 /*
4  * Copyright (c) 2002 Jared D. McNeill <jmcneill@invisible.ca>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. The name of the author may not be used to endorse or promote products
13  *    derived from this software without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
20  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
22  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 /*
29  * ACPI mpu(4) attachment based in lpt_acpi.c by Jared D. McNeill.
30  */
31 
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: mpu_acpi.c,v 1.16 2021/01/29 15:49:55 thorpej Exp $");
34 
35 #include <sys/param.h>
36 #include <sys/device.h>
37 #include <sys/systm.h>
38 
39 #include <dev/acpi/acpivar.h>
40 #include <dev/acpi/acpi_intr.h>
41 
42 #include <dev/ic/mpuvar.h>
43 
44 #include <dev/isa/isadmavar.h>
45 
46 static int          mpu_acpi_match(device_t, cfdata_t, void *);
47 static void         mpu_acpi_attach(device_t, device_t, void *);
48 
49 struct mpu_acpi_softc {
50           struct mpu_softc sc_mpu;
51           kmutex_t sc_lock;
52 };
53 
54 CFATTACH_DECL_NEW(mpu_acpi, sizeof(struct mpu_acpi_softc), mpu_acpi_match,
55     mpu_acpi_attach, NULL, NULL);
56 
57 /*
58  * Supported device IDs
59  */
60 
61 static const struct device_compatible_entry compat_data[] = {
62           { .compat = "PNPB006" }, /* Roland MPU-401 (compatible) MIDI UART */
63           DEVICE_COMPAT_EOL
64 };
65 
66 /*
67  * mpu_acpi_match: autoconf(9) match routine
68  */
69 static int
mpu_acpi_match(device_t parent,cfdata_t match,void * aux)70 mpu_acpi_match(device_t parent, cfdata_t match, void *aux)
71 {
72           struct acpi_attach_args *aa = aux;
73 
74           return acpi_compatible_match(aa, compat_data);
75 }
76 
77 /*
78  * mpu_acpi_attach: autoconf(9) attach routine
79  */
80 static void
mpu_acpi_attach(device_t parent,device_t self,void * aux)81 mpu_acpi_attach(device_t parent, device_t self, void *aux)
82 {
83           struct mpu_acpi_softc *asc = device_private(self);
84           struct mpu_softc *sc = &asc->sc_mpu;
85           struct acpi_attach_args *aa = aux;
86           struct acpi_resources res;
87           struct acpi_io *io;
88           ACPI_STATUS rv;
89 
90           /* parse resources */
91           rv = acpi_resource_parse(self, aa->aa_node->ad_handle, "_CRS",
92               &res, &acpi_resource_parse_ops_default);
93           if (ACPI_FAILURE(rv))
94                     return;
95 
96           /* find our i/o registers */
97           io = acpi_res_io(&res, 0);
98           if (io == NULL) {
99                     aprint_error_dev(self,
100                         "unable to find i/o register resource\n");
101                     goto out;
102           }
103 
104           sc->iot = aa->aa_iot;
105           if (bus_space_map(sc->iot, io->ar_base, io->ar_length, 0, &sc->ioh)) {
106                     aprint_error_dev(self, "can't map i/o space\n");
107                     goto out;
108           }
109 
110           sc->model = "Roland MPU-401 MIDI UART";
111           sc->sc_dev = self;
112           sc->lock = &asc->sc_lock;
113           mutex_init(&asc->sc_lock, MUTEX_DEFAULT, IPL_AUDIO);
114           mpu_attach(sc);
115 
116           sc->arg = acpi_intr_establish(self,
117               (uint64_t)(uintptr_t)aa->aa_node->ad_handle,
118               IPL_AUDIO, true, mpu_intr, sc, device_xname(self));
119           if (sc->arg == NULL) {
120                     aprint_error_dev(self, "unable to establish interrupt\n");
121                     goto out;
122           }
123 
124  out:
125           acpi_resource_cleanup(&res);
126 }
127