xref: /openbsd-src/sys/dev/acpi/pluart_acpi.c (revision c90a81c56dcebd6a1b73fe4aff9b03385b8e63b3)
1 /*	$OpenBSD: pluart_acpi.c,v 1.3 2018/08/25 09:39:20 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 
38 	bus_addr_t sc_addr;
39 	bus_size_t sc_size;
40 
41 	int sc_irq;
42 	int sc_irq_flags;
43 	void *sc_ih;
44 };
45 
46 int	pluart_acpi_match(struct device *, void *, void *);
47 void	pluart_acpi_attach(struct device *, struct device *, void *);
48 
49 struct cfattach pluart_acpi_ca = {
50 	sizeof(struct pluart_acpi_softc), pluart_acpi_match, pluart_acpi_attach
51 };
52 
53 const char *pluart_hids[] = {
54 	"ARMH0011",
55 	NULL
56 };
57 
58 int	pluart_acpi_parse_resources(int, union acpi_resource *, void *);
59 int	pluart_acpi_is_console(struct pluart_acpi_softc *);
60 
61 int
62 pluart_acpi_match(struct device *parent, void *match, void *aux)
63 {
64 	struct acpi_attach_args *aaa = aux;
65 	struct cfdata *cf = match;
66 
67 	return acpi_matchhids(aaa, pluart_hids, cf->cf_driver->cd_name);
68 }
69 
70 void
71 pluart_acpi_attach(struct device *parent, struct device *self, void *aux)
72 {
73 	struct acpi_attach_args *aaa = aux;
74 	struct pluart_acpi_softc *sc = (struct pluart_acpi_softc *)self;
75 	struct aml_value res;
76 
77 	sc->sc_acpi = (struct acpi_softc *)parent;
78 	sc->sc_node = aaa->aaa_node;
79 	printf(" %s", sc->sc_node->name);
80 
81 	if (aml_evalname(sc->sc_acpi, sc->sc_node, "_CRS", 0, NULL, &res)) {
82 		printf(": can't find registers\n");
83 		return;
84 	}
85 
86 	aml_parse_resource(&res, pluart_acpi_parse_resources, sc);
87 	printf(" addr 0x%lx/0x%lx", sc->sc_addr, sc->sc_size);
88 	if (sc->sc_addr == 0 || sc->sc_size == 0) {
89 		printf("\n");
90 		return;
91 	}
92 
93 	printf(" irq %d", sc->sc_irq);
94 
95 	sc->sc.sc_iot = aaa->aaa_memt;
96 	if (bus_space_map(sc->sc.sc_iot, sc->sc_addr, sc->sc_size, 0,
97 	    &sc->sc.sc_ioh)) {
98 		printf(": can't map registers\n");
99 		return;
100 	}
101 
102 	sc->sc_ih = acpi_intr_establish(sc->sc_irq, sc->sc_irq_flags, IPL_TTY,
103 	    pluart_intr, sc, sc->sc.sc_dev.dv_xname);
104 	if (sc->sc_ih == NULL) {
105 		printf(": can't establish interrupt\n");
106 		return;
107 	}
108 
109 	pluart_attach_common(&sc->sc, pluart_acpi_is_console(sc));
110 }
111 
112 int
113 pluart_acpi_parse_resources(int crsidx, union acpi_resource *crs, void *arg)
114 {
115 	struct pluart_acpi_softc *sc = arg;
116 	int type = AML_CRSTYPE(crs);
117 
118 	switch (type) {
119 	case LR_MEM32FIXED:
120 		sc->sc_addr = crs->lr_m32fixed._bas;
121 		sc->sc_size = crs->lr_m32fixed._len;
122 		break;
123 	case LR_EXTIRQ:
124 		sc->sc_irq = crs->lr_extirq.irq[0];
125 		sc->sc_irq_flags = crs->lr_extirq.flags;
126 		break;
127 	}
128 
129 	return 0;
130 }
131 
132 int
133 pluart_acpi_is_console(struct pluart_acpi_softc *sc)
134 {
135 	struct acpi_table_header *hdr;
136 	struct acpi_spcr *spcr;
137 	struct acpi_gas *base;
138 	struct acpi_q *entry;
139 
140 	SIMPLEQ_FOREACH(entry, &sc->sc_acpi->sc_tables, q_next) {
141 		hdr = entry->q_table;
142 		if (strncmp(hdr->signature, SPCR_SIG,
143 		    sizeof(hdr->signature)) == 0) {
144 			spcr = entry->q_table;
145 			base = &spcr->base_address;
146 			if (base->address_space_id == GAS_SYSTEM_MEMORY &&
147 			    base->address == sc->sc_addr)
148 				return 1;
149 		}
150 	}
151 
152 	return 0;
153 }
154