1*820c39ceSmlelstv /* $NetBSD: plcom_acpi.c,v 1.4 2023/01/24 06:56:40 mlelstv Exp $ */
297d57f80Sjmcneill
397d57f80Sjmcneill /*-
497d57f80Sjmcneill * Copyright (c) 2018 The NetBSD Foundation, Inc.
597d57f80Sjmcneill * All rights reserved.
697d57f80Sjmcneill *
797d57f80Sjmcneill * This code is derived from software contributed to The NetBSD Foundation
897d57f80Sjmcneill * by Jared McNeill <jmcneill@invisible.ca>.
997d57f80Sjmcneill *
1097d57f80Sjmcneill * Redistribution and use in source and binary forms, with or without
1197d57f80Sjmcneill * modification, are permitted provided that the following conditions
1297d57f80Sjmcneill * are met:
1397d57f80Sjmcneill * 1. Redistributions of source code must retain the above copyright
1497d57f80Sjmcneill * notice, this list of conditions and the following disclaimer.
1597d57f80Sjmcneill * 2. Redistributions in binary form must reproduce the above copyright
1697d57f80Sjmcneill * notice, this list of conditions and the following disclaimer in the
1797d57f80Sjmcneill * documentation and/or other materials provided with the distribution.
1897d57f80Sjmcneill *
1997d57f80Sjmcneill * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2097d57f80Sjmcneill * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2197d57f80Sjmcneill * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2297d57f80Sjmcneill * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2397d57f80Sjmcneill * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2497d57f80Sjmcneill * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2597d57f80Sjmcneill * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2697d57f80Sjmcneill * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2797d57f80Sjmcneill * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2897d57f80Sjmcneill * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2997d57f80Sjmcneill * POSSIBILITY OF SUCH DAMAGE.
3097d57f80Sjmcneill */
3197d57f80Sjmcneill
3297d57f80Sjmcneill #include <sys/cdefs.h>
33*820c39ceSmlelstv __KERNEL_RCSID(0, "$NetBSD: plcom_acpi.c,v 1.4 2023/01/24 06:56:40 mlelstv Exp $");
3497d57f80Sjmcneill
3597d57f80Sjmcneill #include <sys/param.h>
3697d57f80Sjmcneill #include <sys/bus.h>
3797d57f80Sjmcneill #include <sys/cpu.h>
3897d57f80Sjmcneill #include <sys/device.h>
3997d57f80Sjmcneill #include <sys/termios.h>
4097d57f80Sjmcneill
4197d57f80Sjmcneill #include <dev/acpi/acpireg.h>
4297d57f80Sjmcneill #include <dev/acpi/acpivar.h>
439156525dSjmcneill #include <dev/acpi/acpi_intr.h>
4497d57f80Sjmcneill
4597d57f80Sjmcneill #include <evbarm/dev/plcomreg.h>
4697d57f80Sjmcneill #include <evbarm/dev/plcomvar.h>
4797d57f80Sjmcneill
4897d57f80Sjmcneill static int plcom_acpi_match(device_t, cfdata_t, void *);
4997d57f80Sjmcneill static void plcom_acpi_attach(device_t, device_t, void *);
5097d57f80Sjmcneill
5197d57f80Sjmcneill CFATTACH_DECL_NEW(plcom_acpi, sizeof(struct plcom_softc), plcom_acpi_match, plcom_acpi_attach, NULL, NULL);
5297d57f80Sjmcneill
53*820c39ceSmlelstv enum plcom_acpi_variant {
54*820c39ceSmlelstv PLCOM_ACPI_GENERIC,
55*820c39ceSmlelstv PLCOM_ACPI_PL011,
56*820c39ceSmlelstv PLCOM_ACPI_BCM2837
57*820c39ceSmlelstv };
58*820c39ceSmlelstv
59*820c39ceSmlelstv static const struct device_compatible_entry compat_data[] = {
60*820c39ceSmlelstv { .compat = "BCM2837", .value = PLCOM_ACPI_BCM2837 },
61*820c39ceSmlelstv { .compat = "ARMH0011", .value = PLCOM_ACPI_PL011 },
62*820c39ceSmlelstv { .compat = "ARMHB000", .value = PLCOM_ACPI_GENERIC },
63*820c39ceSmlelstv DEVICE_COMPAT_EOL
6497d57f80Sjmcneill };
6597d57f80Sjmcneill
6697d57f80Sjmcneill static int
plcom_acpi_match(device_t parent,cfdata_t cf,void * aux)6797d57f80Sjmcneill plcom_acpi_match(device_t parent, cfdata_t cf, void *aux)
6897d57f80Sjmcneill {
6997d57f80Sjmcneill struct acpi_attach_args *aa = aux;
7097d57f80Sjmcneill
7197d57f80Sjmcneill if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
7297d57f80Sjmcneill return 0;
7397d57f80Sjmcneill
74*820c39ceSmlelstv return acpi_compatible_match(aa, compat_data);
7597d57f80Sjmcneill }
7697d57f80Sjmcneill
7797d57f80Sjmcneill static void
plcom_acpi_attach(device_t parent,device_t self,void * aux)7897d57f80Sjmcneill plcom_acpi_attach(device_t parent, device_t self, void *aux)
7997d57f80Sjmcneill {
8097d57f80Sjmcneill struct plcom_softc * const sc = device_private(self);
8197d57f80Sjmcneill struct acpi_attach_args *aa = aux;
8297d57f80Sjmcneill struct acpi_resources res;
8397d57f80Sjmcneill struct acpi_mem *mem;
8497d57f80Sjmcneill struct acpi_irq *irq;
8597d57f80Sjmcneill ACPI_STATUS rv;
8697d57f80Sjmcneill void *ih;
8797d57f80Sjmcneill
8897d57f80Sjmcneill sc->sc_dev = self;
8997d57f80Sjmcneill
9097d57f80Sjmcneill rv = acpi_resource_parse(sc->sc_dev, aa->aa_node->ad_handle, "_CRS",
9197d57f80Sjmcneill &res, &acpi_resource_parse_ops_default);
9297d57f80Sjmcneill if (ACPI_FAILURE(rv))
9397d57f80Sjmcneill return;
9497d57f80Sjmcneill
9597d57f80Sjmcneill mem = acpi_res_mem(&res, 0);
9697d57f80Sjmcneill if (mem == NULL) {
9797d57f80Sjmcneill aprint_error(": couldn't find mem resource\n");
9897d57f80Sjmcneill goto done;
9997d57f80Sjmcneill }
10097d57f80Sjmcneill
10197d57f80Sjmcneill irq = acpi_res_irq(&res, 0);
10297d57f80Sjmcneill if (irq == NULL) {
10397d57f80Sjmcneill aprint_error(": couldn't find irq resource\n");
10497d57f80Sjmcneill goto done;
10597d57f80Sjmcneill }
10697d57f80Sjmcneill
10747afb6f2Sjmcneill sc->sc_hwflags = 0;
10897d57f80Sjmcneill sc->sc_swflags = 0;
109*820c39ceSmlelstv sc->sc_fifolen = 0;
11097d57f80Sjmcneill sc->sc_pi.pi_type = PLCOM_TYPE_PL011;
11197d57f80Sjmcneill sc->sc_pi.pi_flags = PLC_FLAG_32BIT_ACCESS;
112*820c39ceSmlelstv
113*820c39ceSmlelstv switch (acpi_compatible_lookup(aa, compat_data)->value) {
114*820c39ceSmlelstv case PLCOM_ACPI_BCM2837:
115*820c39ceSmlelstv sc->sc_fifolen = 16;
116*820c39ceSmlelstv break;
117*820c39ceSmlelstv case PLCOM_ACPI_GENERIC:
118*820c39ceSmlelstv sc->sc_pi.pi_type = PLCOM_TYPE_GENERIC_UART;
119*820c39ceSmlelstv break;
120*820c39ceSmlelstv }
121*820c39ceSmlelstv
12297d57f80Sjmcneill sc->sc_pi.pi_iot = aa->aa_memt;
12397d57f80Sjmcneill sc->sc_pi.pi_iobase = mem->ar_base;
12497d57f80Sjmcneill if (bus_space_map(aa->aa_memt, mem->ar_base, mem->ar_length, 0, &sc->sc_pi.pi_ioh) != 0) {
12597d57f80Sjmcneill aprint_error(": couldn't map registers\n");
12697d57f80Sjmcneill goto done;
12797d57f80Sjmcneill }
12897d57f80Sjmcneill
12997d57f80Sjmcneill plcom_attach_subr(sc);
13097d57f80Sjmcneill
1319156525dSjmcneill ih = acpi_intr_establish(self, (uint64_t)aa->aa_node->ad_handle,
1329156525dSjmcneill IPL_SERIAL, true, plcomintr, sc, device_xname(self));
13397d57f80Sjmcneill if (ih == NULL) {
13497d57f80Sjmcneill aprint_error_dev(self, "couldn't install interrupt handler\n");
13597d57f80Sjmcneill return;
13697d57f80Sjmcneill }
13797d57f80Sjmcneill
13897d57f80Sjmcneill done:
13997d57f80Sjmcneill acpi_resource_cleanup(&res);
14097d57f80Sjmcneill }
141