xref: /netbsd-src/sys/dev/acpi/mpu_acpi.c (revision 3772555307d774424243e6d1cdc66aa160602c26)
1*37725553Sthorpej /* $NetBSD: mpu_acpi.c,v 1.16 2021/01/29 15:49:55 thorpej Exp $ */
2347378fcSxtraeme 
3347378fcSxtraeme /*
4347378fcSxtraeme  * Copyright (c) 2002 Jared D. McNeill <jmcneill@invisible.ca>
5347378fcSxtraeme  * All rights reserved.
6347378fcSxtraeme  *
7347378fcSxtraeme  * Redistribution and use in source and binary forms, with or without
8347378fcSxtraeme  * modification, are permitted provided that the following conditions
9347378fcSxtraeme  * are met:
10347378fcSxtraeme  * 1. Redistributions of source code must retain the above copyright
11347378fcSxtraeme  *    notice, this list of conditions and the following disclaimer.
12347378fcSxtraeme  * 2. The name of the author may not be used to endorse or promote products
13347378fcSxtraeme  *    derived from this software without specific prior written permission.
14347378fcSxtraeme  *
15347378fcSxtraeme  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16347378fcSxtraeme  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17347378fcSxtraeme  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18347378fcSxtraeme  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19347378fcSxtraeme  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
20347378fcSxtraeme  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21347378fcSxtraeme  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
22347378fcSxtraeme  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23347378fcSxtraeme  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24347378fcSxtraeme  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25347378fcSxtraeme  * SUCH DAMAGE.
26347378fcSxtraeme  */
27347378fcSxtraeme 
28347378fcSxtraeme /*
29347378fcSxtraeme  * ACPI mpu(4) attachment based in lpt_acpi.c by Jared D. McNeill.
30347378fcSxtraeme  */
31347378fcSxtraeme 
32347378fcSxtraeme #include <sys/cdefs.h>
33*37725553Sthorpej __KERNEL_RCSID(0, "$NetBSD: mpu_acpi.c,v 1.16 2021/01/29 15:49:55 thorpej Exp $");
34347378fcSxtraeme 
35347378fcSxtraeme #include <sys/param.h>
36347378fcSxtraeme #include <sys/device.h>
375a425210Sjruoho #include <sys/systm.h>
38347378fcSxtraeme 
39347378fcSxtraeme #include <dev/acpi/acpivar.h>
408d11c112Sjmcneill #include <dev/acpi/acpi_intr.h>
41347378fcSxtraeme 
42347378fcSxtraeme #include <dev/ic/mpuvar.h>
43347378fcSxtraeme 
445a425210Sjruoho #include <dev/isa/isadmavar.h>
455a425210Sjruoho 
460ab2da71Sxtraeme static int	mpu_acpi_match(device_t, cfdata_t, void *);
470ab2da71Sxtraeme static void	mpu_acpi_attach(device_t, device_t, void *);
48347378fcSxtraeme 
49347378fcSxtraeme struct mpu_acpi_softc {
50347378fcSxtraeme 	struct mpu_softc sc_mpu;
515249bbc5Smrg 	kmutex_t sc_lock;
52347378fcSxtraeme };
53347378fcSxtraeme 
540ab2da71Sxtraeme CFATTACH_DECL_NEW(mpu_acpi, sizeof(struct mpu_acpi_softc), mpu_acpi_match,
55347378fcSxtraeme     mpu_acpi_attach, NULL, NULL);
56347378fcSxtraeme 
57347378fcSxtraeme /*
58347378fcSxtraeme  * Supported device IDs
59347378fcSxtraeme  */
60347378fcSxtraeme 
61*37725553Sthorpej static const struct device_compatible_entry compat_data[] = {
62*37725553Sthorpej 	{ .compat = "PNPB006" }, /* Roland MPU-401 (compatible) MIDI UART */
63*37725553Sthorpej 	DEVICE_COMPAT_EOL
64347378fcSxtraeme };
65347378fcSxtraeme 
66347378fcSxtraeme /*
67347378fcSxtraeme  * mpu_acpi_match: autoconf(9) match routine
68347378fcSxtraeme  */
69347378fcSxtraeme static int
mpu_acpi_match(device_t parent,cfdata_t match,void * aux)700ab2da71Sxtraeme mpu_acpi_match(device_t parent, cfdata_t match, void *aux)
71347378fcSxtraeme {
72347378fcSxtraeme 	struct acpi_attach_args *aa = aux;
73347378fcSxtraeme 
74*37725553Sthorpej 	return acpi_compatible_match(aa, compat_data);
75347378fcSxtraeme }
76347378fcSxtraeme 
77347378fcSxtraeme /*
78347378fcSxtraeme  * mpu_acpi_attach: autoconf(9) attach routine
79347378fcSxtraeme  */
80347378fcSxtraeme static void
mpu_acpi_attach(device_t parent,device_t self,void * aux)810ab2da71Sxtraeme mpu_acpi_attach(device_t parent, device_t self, void *aux)
82347378fcSxtraeme {
830ab2da71Sxtraeme 	struct mpu_acpi_softc *asc = device_private(self);
84347378fcSxtraeme 	struct mpu_softc *sc = &asc->sc_mpu;
85347378fcSxtraeme 	struct acpi_attach_args *aa = aux;
86347378fcSxtraeme 	struct acpi_resources res;
87347378fcSxtraeme 	struct acpi_io *io;
88347378fcSxtraeme 	ACPI_STATUS rv;
89347378fcSxtraeme 
90347378fcSxtraeme 	/* parse resources */
910ab2da71Sxtraeme 	rv = acpi_resource_parse(self, aa->aa_node->ad_handle, "_CRS",
92347378fcSxtraeme 	    &res, &acpi_resource_parse_ops_default);
93347378fcSxtraeme 	if (ACPI_FAILURE(rv))
94347378fcSxtraeme 		return;
95347378fcSxtraeme 
96347378fcSxtraeme 	/* find our i/o registers */
97347378fcSxtraeme 	io = acpi_res_io(&res, 0);
98347378fcSxtraeme 	if (io == NULL) {
990ab2da71Sxtraeme 		aprint_error_dev(self,
1000ab2da71Sxtraeme 		    "unable to find i/o register resource\n");
101347378fcSxtraeme 		goto out;
102347378fcSxtraeme 	}
103347378fcSxtraeme 
104347378fcSxtraeme 	sc->iot = aa->aa_iot;
105347378fcSxtraeme 	if (bus_space_map(sc->iot, io->ar_base, io->ar_length, 0, &sc->ioh)) {
1060ab2da71Sxtraeme 		aprint_error_dev(self, "can't map i/o space\n");
107347378fcSxtraeme 		goto out;
108347378fcSxtraeme 	}
109347378fcSxtraeme 
110347378fcSxtraeme 	sc->model = "Roland MPU-401 MIDI UART";
1110ab2da71Sxtraeme 	sc->sc_dev = self;
1125249bbc5Smrg 	sc->lock = &asc->sc_lock;
1135249bbc5Smrg 	mutex_init(&asc->sc_lock, MUTEX_DEFAULT, IPL_AUDIO);
114347378fcSxtraeme 	mpu_attach(sc);
115347378fcSxtraeme 
116569b81e6Sjmcneill 	sc->arg = acpi_intr_establish(self,
117569b81e6Sjmcneill 	    (uint64_t)(uintptr_t)aa->aa_node->ad_handle,
1188d11c112Sjmcneill 	    IPL_AUDIO, true, mpu_intr, sc, device_xname(self));
1198d11c112Sjmcneill 	if (sc->arg == NULL) {
1208d11c112Sjmcneill 		aprint_error_dev(self, "unable to establish interrupt\n");
1218d11c112Sjmcneill 		goto out;
1228d11c112Sjmcneill 	}
123347378fcSxtraeme 
124347378fcSxtraeme  out:
125347378fcSxtraeme 	acpi_resource_cleanup(&res);
126347378fcSxtraeme }
127