1/*	$OpenBSD: pluart_acpi.c,v 1.9 2022/06/11 05:29:24 anton Exp $	*/
2/*
3 * Copyright (c) 2018 Mark Kettenis
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18#include <sys/param.h>
19#include <sys/malloc.h>
20#include <sys/systm.h>
21#include <sys/tty.h>
22
23#include <dev/acpi/acpireg.h>
24#include <dev/acpi/acpivar.h>
25#include <dev/acpi/acpidev.h>
26#include <dev/acpi/amltypes.h>
27#include <dev/acpi/dsdt.h>
28
29#undef DEVNAME
30#include <dev/ic/pluartvar.h>
31#include <dev/cons.h>
32
33struct pluart_acpi_softc {
34	struct pluart_softc sc;
35	struct acpi_softc *sc_acpi;
36	struct aml_node *sc_node;
37	bus_addr_t	sc_addr;
38	void		*sc_ih;
39};
40
41int	pluart_acpi_match(struct device *, void *, void *);
42void	pluart_acpi_attach(struct device *, struct device *, void *);
43
44const struct cfattach pluart_acpi_ca = {
45	sizeof(struct pluart_acpi_softc), pluart_acpi_match, pluart_acpi_attach
46};
47
48const char *pluart_hids[] = {
49	"ARMH0011",
50	NULL
51};
52
53int	pluart_acpi_is_console(struct pluart_acpi_softc *);
54
55int
56pluart_acpi_match(struct device *parent, void *match, void *aux)
57{
58	struct acpi_attach_args *aaa = aux;
59	struct cfdata *cf = match;
60
61	if (aaa->aaa_naddr < 1 || aaa->aaa_nirq < 1)
62		return 0;
63	return acpi_matchhids(aaa, pluart_hids, cf->cf_driver->cd_name);
64}
65
66void
67pluart_acpi_attach(struct device *parent, struct device *self, void *aux)
68{
69	struct pluart_acpi_softc *sc = (struct pluart_acpi_softc *)self;
70	struct acpi_attach_args *aaa = aux;
71
72	sc->sc_acpi = (struct acpi_softc *)parent;
73	sc->sc_node = aaa->aaa_node;
74	printf(" %s", sc->sc_node->name);
75
76	printf(" addr 0x%llx/0x%llx", aaa->aaa_addr[0], aaa->aaa_size[0]);
77	printf(" irq %d", aaa->aaa_irq[0]);
78
79	sc->sc.sc_iot = aaa->aaa_bst[0];
80	sc->sc_addr = aaa->aaa_addr[0];
81	if (bus_space_map(sc->sc.sc_iot, aaa->aaa_addr[0], aaa->aaa_size[0],
82	    0, &sc->sc.sc_ioh)) {
83		printf(": can't map registers\n");
84		return;
85	}
86
87	sc->sc_ih = acpi_intr_establish(aaa->aaa_irq[0], aaa->aaa_irq_flags[0],
88	    IPL_TTY, pluart_intr, sc, sc->sc.sc_dev.dv_xname);
89	if (sc->sc_ih == NULL) {
90		printf(": can't establish interrupt\n");
91		return;
92	}
93
94	sc->sc.sc_hwflags |= COM_HW_SBSA;
95
96	pluart_attach_common(&sc->sc, pluart_acpi_is_console(sc));
97}
98
99int
100pluart_acpi_is_console(struct pluart_acpi_softc *sc)
101{
102	struct acpi_table_header *hdr;
103	struct acpi_spcr *spcr;
104	struct acpi_gas *base;
105	struct acpi_q *entry;
106
107	SIMPLEQ_FOREACH(entry, &sc->sc_acpi->sc_tables, q_next) {
108		hdr = entry->q_table;
109		if (strncmp(hdr->signature, SPCR_SIG,
110		    sizeof(hdr->signature)) == 0) {
111			spcr = entry->q_table;
112			base = &spcr->base_address;
113			if (base->address_space_id == GAS_SYSTEM_MEMORY &&
114			    base->address == sc->sc_addr)
115				return 1;
116		}
117	}
118
119	return 0;
120}
121