xref: /netbsd-src/sys/arch/arm/acpi/plcom_acpi.c (revision 820c39cec25def994102cd51c1430bc4f78b2f31)
1 /* $NetBSD: plcom_acpi.c,v 1.4 2023/01/24 06:56:40 mlelstv Exp $ */
2 
3 /*-
4  * Copyright (c) 2018 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jared McNeill <jmcneill@invisible.ca>.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: plcom_acpi.c,v 1.4 2023/01/24 06:56:40 mlelstv Exp $");
34 
35 #include <sys/param.h>
36 #include <sys/bus.h>
37 #include <sys/cpu.h>
38 #include <sys/device.h>
39 #include <sys/termios.h>
40 
41 #include <dev/acpi/acpireg.h>
42 #include <dev/acpi/acpivar.h>
43 #include <dev/acpi/acpi_intr.h>
44 
45 #include <evbarm/dev/plcomreg.h>
46 #include <evbarm/dev/plcomvar.h>
47 
48 static int	plcom_acpi_match(device_t, cfdata_t, void *);
49 static void	plcom_acpi_attach(device_t, device_t, void *);
50 
51 CFATTACH_DECL_NEW(plcom_acpi, sizeof(struct plcom_softc), plcom_acpi_match, plcom_acpi_attach, NULL, NULL);
52 
53 enum plcom_acpi_variant {
54 	PLCOM_ACPI_GENERIC,
55 	PLCOM_ACPI_PL011,
56 	PLCOM_ACPI_BCM2837
57 };
58 
59 static const struct device_compatible_entry compat_data[] = {
60 	{ .compat = "BCM2837",		.value = PLCOM_ACPI_BCM2837 },
61 	{ .compat = "ARMH0011",		.value = PLCOM_ACPI_PL011 },
62 	{ .compat = "ARMHB000",		.value = PLCOM_ACPI_GENERIC },
63 	DEVICE_COMPAT_EOL
64 };
65 
66 static int
plcom_acpi_match(device_t parent,cfdata_t cf,void * aux)67 plcom_acpi_match(device_t parent, cfdata_t cf, void *aux)
68 {
69 	struct acpi_attach_args *aa = aux;
70 
71 	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
72 		return 0;
73 
74 	return acpi_compatible_match(aa, compat_data);
75 }
76 
77 static void
plcom_acpi_attach(device_t parent,device_t self,void * aux)78 plcom_acpi_attach(device_t parent, device_t self, void *aux)
79 {
80 	struct plcom_softc * const sc = device_private(self);
81 	struct acpi_attach_args *aa = aux;
82 	struct acpi_resources res;
83 	struct acpi_mem *mem;
84 	struct acpi_irq *irq;
85 	ACPI_STATUS rv;
86 	void *ih;
87 
88 	sc->sc_dev = self;
89 
90 	rv = acpi_resource_parse(sc->sc_dev, aa->aa_node->ad_handle, "_CRS",
91 	    &res, &acpi_resource_parse_ops_default);
92 	if (ACPI_FAILURE(rv))
93 		return;
94 
95 	mem = acpi_res_mem(&res, 0);
96 	if (mem == NULL) {
97 		aprint_error(": couldn't find mem resource\n");
98 		goto done;
99 	}
100 
101 	irq = acpi_res_irq(&res, 0);
102 	if (irq == NULL) {
103 		aprint_error(": couldn't find irq resource\n");
104 		goto done;
105 	}
106 
107 	sc->sc_hwflags = 0;
108 	sc->sc_swflags = 0;
109 	sc->sc_fifolen = 0;
110 	sc->sc_pi.pi_type = PLCOM_TYPE_PL011;
111 	sc->sc_pi.pi_flags = PLC_FLAG_32BIT_ACCESS;
112 
113 	switch (acpi_compatible_lookup(aa, compat_data)->value) {
114 	case PLCOM_ACPI_BCM2837:
115 		sc->sc_fifolen = 16;
116 		break;
117 	case PLCOM_ACPI_GENERIC:
118 		sc->sc_pi.pi_type = PLCOM_TYPE_GENERIC_UART;
119 		break;
120 	}
121 
122 	sc->sc_pi.pi_iot = aa->aa_memt;
123 	sc->sc_pi.pi_iobase = mem->ar_base;
124 	if (bus_space_map(aa->aa_memt, mem->ar_base, mem->ar_length, 0, &sc->sc_pi.pi_ioh) != 0) {
125 		aprint_error(": couldn't map registers\n");
126 		goto done;
127 	}
128 
129 	plcom_attach_subr(sc);
130 
131 	ih = acpi_intr_establish(self, (uint64_t)aa->aa_node->ad_handle,
132 	    IPL_SERIAL, true, plcomintr, sc, device_xname(self));
133 	if (ih == NULL) {
134 		aprint_error_dev(self, "couldn't install interrupt handler\n");
135 		return;
136 	}
137 
138 done:
139 	acpi_resource_cleanup(&res);
140 }
141