1/*	$OpenBSD: iosf_acpi.c,v 1.1 2023/04/23 00:20:26 dlg Exp $ */
2/*
3 * Copyright (c) 2023 David Gwynne <dlg@openbsd.org>
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#include <sys/device.h>
21
22#include <machine/bus.h>
23#include <machine/intr.h>
24
25#include <dev/acpi/acpireg.h>
26#include <dev/acpi/acpivar.h>
27#include <dev/acpi/acpidev.h>
28#include <dev/acpi/amltypes.h>
29#include <dev/acpi/dsdt.h>
30
31#include <dev/ic/iosfvar.h>
32
33struct iosf_acpi_softc {
34	struct device		 sc_dev;
35	struct acpi_softc	*sc_acpi;
36	struct aml_node		*sc_node;
37
38	bus_space_tag_t		 sc_iot;
39	bus_space_handle_t	 sc_ioh;
40	bus_size_t		 sc_ios;
41
42	struct iosf_mbi		 sc_mbi;
43};
44
45static int	iosf_acpi_match(struct device *, void *, void *);
46static void	iosf_acpi_attach(struct device *, struct device *, void *);
47
48static uint32_t	iosf_acpi_mbi_mdr_rd(struct iosf_mbi *, uint32_t, uint32_t);
49static void	iosf_acpi_mbi_mdr_wr(struct iosf_mbi *, uint32_t, uint32_t,
50		    uint32_t);
51
52const struct cfattach iosf_acpi_ca = {
53	sizeof(struct iosf_acpi_softc), iosf_acpi_match, iosf_acpi_attach
54};
55
56static const char *iosf_hids[] = {
57	"INT33BD",
58	NULL
59};
60
61static int
62iosf_acpi_match(struct device *parent, void *match, void *aux)
63{
64	struct acpi_attach_args *aaa = aux;
65	struct cfdata *cf = match;
66
67	if (aaa->aaa_naddr < 1)
68		return 0;
69
70	return (acpi_matchhids(aaa, iosf_hids, cf->cf_driver->cd_name));
71}
72
73static void
74iosf_acpi_attach(struct device *parent, struct device *self, void *aux)
75{
76	struct iosf_acpi_softc *sc = (struct iosf_acpi_softc *)self;
77	struct acpi_attach_args *aaa = aux;
78	struct cpu_info *ci;
79	int semaddr = -1;
80
81	sc->sc_acpi = (struct acpi_softc *)parent;
82	sc->sc_node = aaa->aaa_node;
83	printf(" %s", sc->sc_node->name);
84
85	sc->sc_iot = aaa->aaa_bst[0];
86	sc->sc_ios = aaa->aaa_size[0];
87
88	if (bus_space_map(sc->sc_iot, aaa->aaa_addr[0], aaa->aaa_size[0], 0,
89	    &sc->sc_ioh)) {
90		printf(": can't map registers\n");
91		return;
92	}
93
94	ci = curcpu();
95	if (strcmp(cpu_vendor, "GenuineIntel") == 0 &&
96	    ci->ci_family == 0x06 && ci->ci_model == 0x4c) {
97		/* cherry trail, braswell */
98		semaddr = 0x10e;
99	}
100
101	if (semaddr != -1) {
102		printf(": mbi");
103
104		sc->sc_mbi.mbi_dev = self;
105		sc->sc_mbi.mbi_prio = 1; /* lower prio than iosf_pci ops */
106		sc->sc_mbi.mbi_semaddr = semaddr;
107		sc->sc_mbi.mbi_mdr_rd = iosf_acpi_mbi_mdr_rd;
108		sc->sc_mbi.mbi_mdr_wr = iosf_acpi_mbi_mdr_wr;
109	}
110
111	printf("\n");
112}
113
114/*
115 * mbi mdr ACPI operations
116 */
117
118#define IOSF_ACPI_MBI_MCR	0x0
119#define IOSF_ACPI_MBI_MDR	0x4
120#define IOSF_ACPI_MBI_MCRX	0x8
121
122static uint32_t
123iosf_acpi_mbi_mdr_rd(struct iosf_mbi *mbi, uint32_t mcr, uint32_t mcrx)
124{
125	struct iosf_acpi_softc *sc = (struct iosf_acpi_softc *)mbi->mbi_dev;
126
127	if (mcrx != 0) {
128		bus_space_write_4(sc->sc_iot, sc->sc_ioh,
129		    IOSF_ACPI_MBI_MCRX, mcrx);
130	}
131	bus_space_write_4(sc->sc_iot, sc->sc_ioh, IOSF_ACPI_MBI_MCR, mcr);
132
133	return (bus_space_read_4(sc->sc_iot, sc->sc_ioh, IOSF_ACPI_MBI_MDR));
134}
135
136static void
137iosf_acpi_mbi_mdr_wr(struct iosf_mbi *mbi, uint32_t mcr, uint32_t mcrx,
138    uint32_t mdr)
139{
140	struct iosf_acpi_softc *sc = (struct iosf_acpi_softc *)mbi->mbi_dev;
141
142	bus_space_write_4(sc->sc_iot, sc->sc_ioh, IOSF_ACPI_MBI_MDR, mdr);
143	if (mcrx != 0) {
144		bus_space_write_4(sc->sc_iot, sc->sc_ioh,
145		    IOSF_ACPI_MBI_MCRX, mcrx);
146	}
147
148	bus_space_write_4(sc->sc_iot, sc->sc_ioh, IOSF_ACPI_MBI_MCR, mcr);
149}
150