xref: /openbsd-src/sys/dev/acpi/ccp_acpi.c (revision 99fd087599a8791921855f21bd7e36130f39aadc)
1 /*	$OpenBSD: ccp_acpi.c,v 1.1 2019/04/23 18:34:06 kettenis Exp $	*/
2 /*
3  * Copyright (c) 2019 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/systm.h>
20 
21 #include <dev/acpi/acpireg.h>
22 #include <dev/acpi/acpivar.h>
23 #include <dev/acpi/acpidev.h>
24 #include <dev/acpi/amltypes.h>
25 #include <dev/acpi/dsdt.h>
26 
27 #include <dev/ic/ccpvar.h>
28 
29 struct ccp_acpi_softc {
30 	struct ccp_softc sc;
31 	struct acpi_softc *sc_acpi;
32 	struct aml_node *sc_node;
33 
34 	bus_addr_t sc_addr;
35 	bus_size_t sc_size;
36 };
37 
38 int	ccp_acpi_match(struct device *, void *, void *);
39 void	ccp_acpi_attach(struct device *, struct device *, void *);
40 
41 struct cfattach ccp_acpi_ca = {
42 	sizeof(struct ccp_acpi_softc), ccp_acpi_match, ccp_acpi_attach
43 };
44 
45 const char *ccp_hids[] = {
46 	"AMDI0C00",
47 	NULL
48 };
49 
50 int	ccp_acpi_parse_resources(int, union acpi_resource *, void *);
51 
52 int
53 ccp_acpi_match(struct device *parent, void *match, void *aux)
54 {
55 	struct acpi_attach_args *aaa = aux;
56 	struct cfdata *cf = match;
57 
58 	return acpi_matchhids(aaa, ccp_hids, cf->cf_driver->cd_name);
59 }
60 
61 void
62 ccp_acpi_attach(struct device *parent, struct device *self, void *aux)
63 {
64 	struct acpi_attach_args *aaa = aux;
65 	struct ccp_acpi_softc *sc = (struct ccp_acpi_softc *)self;
66 	struct aml_value res;
67 
68 	sc->sc_acpi = (struct acpi_softc *)parent;
69 	sc->sc_node = aaa->aaa_node;
70 	printf(" %s", sc->sc_node->name);
71 
72 	if (aml_evalname(sc->sc_acpi, sc->sc_node, "_CRS", 0, NULL, &res)) {
73 		printf(": can't find registers\n");
74 		return;
75 	}
76 
77 	aml_parse_resource(&res, ccp_acpi_parse_resources, sc);
78 	printf(" addr 0x%lx/0x%lx", sc->sc_addr, sc->sc_size);
79 	if (sc->sc_addr == 0 || sc->sc_size == 0) {
80 		printf("\n");
81 		return;
82 	}
83 
84 	sc->sc.sc_iot = aaa->aaa_memt;
85 	if (bus_space_map(sc->sc.sc_iot, sc->sc_addr, sc->sc_size, 0,
86 	    &sc->sc.sc_ioh)) {
87 		printf(": can't map registers\n");
88 		return;
89 	}
90 
91 	ccp_attach(&sc->sc);
92 }
93 
94 int
95 ccp_acpi_parse_resources(int crsidx, union acpi_resource *crs, void *arg)
96 {
97 	struct ccp_acpi_softc *sc = arg;
98 	int type = AML_CRSTYPE(crs);
99 
100 	switch (type) {
101 	case LR_MEM32FIXED:
102 		sc->sc_addr = crs->lr_m32fixed._bas;
103 		sc->sc_size = crs->lr_m32fixed._len;
104 		break;
105 	}
106 
107 	return 0;
108 }
109