xref: /openbsd-src/sys/dev/acpi/pluart_acpi.c (revision 46035553bfdd96e63c94e32da0210227ec2e3cf1)
1 /*	$OpenBSD: pluart_acpi.c,v 1.4 2020/05/08 11:18:01 kettenis 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 
33 struct 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 
41 int	pluart_acpi_match(struct device *, void *, void *);
42 void	pluart_acpi_attach(struct device *, struct device *, void *);
43 
44 struct cfattach pluart_acpi_ca = {
45 	sizeof(struct pluart_acpi_softc), pluart_acpi_match, pluart_acpi_attach
46 };
47 
48 const char *pluart_hids[] = {
49 	"ARMH0011",
50 	NULL
51 };
52 
53 int	pluart_acpi_is_console(struct pluart_acpi_softc *);
54 
55 int
56 pluart_acpi_match(struct device *parent, void *match, void *aux)
57 {
58 	struct acpi_attach_args *aaa = aux;
59 	struct cfdata *cf = match;
60 
61 	return acpi_matchhids(aaa, pluart_hids, cf->cf_driver->cd_name);
62 }
63 
64 void
65 pluart_acpi_attach(struct device *parent, struct device *self, void *aux)
66 {
67 	struct pluart_acpi_softc *sc = (struct pluart_acpi_softc *)self;
68 	struct acpi_attach_args *aaa = aux;
69 
70 	sc->sc_acpi = (struct acpi_softc *)parent;
71 	sc->sc_node = aaa->aaa_node;
72 	printf(" %s", sc->sc_node->name);
73 
74 	if (aaa->aaa_naddr < 1) {
75 		printf(": no registers\n");
76 		return;
77 	}
78 
79 	if (aaa->aaa_nirq < 1) {
80 		printf(": no interrupt\n");
81 		return;
82 	}
83 
84 	printf(" addr 0x%llx/0x%llx", aaa->aaa_addr[0], aaa->aaa_size[0]);
85 	printf(" irq %d", aaa->aaa_irq[0]);
86 
87 	sc->sc.sc_iot = aaa->aaa_bst[0];
88 	sc->sc_addr = aaa->aaa_addr[0];
89 	if (bus_space_map(sc->sc.sc_iot, aaa->aaa_addr[0], aaa->aaa_size[0],
90 	    0, &sc->sc.sc_ioh)) {
91 		printf(": can't map registers\n");
92 		return;
93 	}
94 
95 	sc->sc_ih = acpi_intr_establish(aaa->aaa_irq[0], aaa->aaa_irq_flags[0],
96 	    IPL_TTY, pluart_intr, sc, sc->sc.sc_dev.dv_xname);
97 	if (sc->sc_ih == NULL) {
98 		printf(": can't establish interrupt\n");
99 		return;
100 	}
101 
102 	pluart_attach_common(&sc->sc, pluart_acpi_is_console(sc));
103 }
104 
105 int
106 pluart_acpi_is_console(struct pluart_acpi_softc *sc)
107 {
108 	struct acpi_table_header *hdr;
109 	struct acpi_spcr *spcr;
110 	struct acpi_gas *base;
111 	struct acpi_q *entry;
112 
113 	SIMPLEQ_FOREACH(entry, &sc->sc_acpi->sc_tables, q_next) {
114 		hdr = entry->q_table;
115 		if (strncmp(hdr->signature, SPCR_SIG,
116 		    sizeof(hdr->signature)) == 0) {
117 			spcr = entry->q_table;
118 			base = &spcr->base_address;
119 			if (base->address_space_id == GAS_SYSTEM_MEMORY &&
120 			    base->address == sc->sc_addr)
121 				return 1;
122 		}
123 	}
124 
125 	return 0;
126 }
127