plgpio_acpi.c revision 1.6 1 1.6 jmcneill /* $NetBSD: plgpio_acpi.c,v 1.6 2020/12/07 10:02:51 jmcneill Exp $ */
2 1.1 jmcneill
3 1.1 jmcneill /*-
4 1.1 jmcneill * Copyright (c) 2018 The NetBSD Foundation, Inc.
5 1.1 jmcneill * All rights reserved.
6 1.1 jmcneill *
7 1.1 jmcneill * This code is derived from software contributed to The NetBSD Foundation
8 1.1 jmcneill * by Jared McNeill <jmcneill (at) invisible.ca>.
9 1.1 jmcneill *
10 1.1 jmcneill * Redistribution and use in source and binary forms, with or without
11 1.1 jmcneill * modification, are permitted provided that the following conditions
12 1.1 jmcneill * are met:
13 1.1 jmcneill * 1. Redistributions of source code must retain the above copyright
14 1.1 jmcneill * notice, this list of conditions and the following disclaimer.
15 1.1 jmcneill * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 jmcneill * notice, this list of conditions and the following disclaimer in the
17 1.1 jmcneill * documentation and/or other materials provided with the distribution.
18 1.1 jmcneill *
19 1.1 jmcneill * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 jmcneill * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 jmcneill * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 jmcneill * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 jmcneill * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 jmcneill * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 jmcneill * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 jmcneill * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 jmcneill * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 jmcneill * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 jmcneill * POSSIBILITY OF SUCH DAMAGE.
30 1.1 jmcneill */
31 1.1 jmcneill
32 1.1 jmcneill #include <sys/cdefs.h>
33 1.6 jmcneill __KERNEL_RCSID(0, "$NetBSD: plgpio_acpi.c,v 1.6 2020/12/07 10:02:51 jmcneill Exp $");
34 1.1 jmcneill
35 1.1 jmcneill #include <sys/param.h>
36 1.1 jmcneill #include <sys/bus.h>
37 1.1 jmcneill #include <sys/cpu.h>
38 1.1 jmcneill #include <sys/device.h>
39 1.1 jmcneill #include <sys/gpio.h>
40 1.1 jmcneill
41 1.1 jmcneill #include <dev/acpi/acpireg.h>
42 1.1 jmcneill #include <dev/acpi/acpivar.h>
43 1.5 jmcneill #include <dev/acpi/acpi_intr.h>
44 1.3 jmcneill #include <dev/acpi/acpi_event.h>
45 1.1 jmcneill
46 1.1 jmcneill #include <dev/gpio/gpiovar.h>
47 1.1 jmcneill #include <dev/ic/pl061var.h>
48 1.2 jmcneill #include <dev/ic/pl061reg.h>
49 1.2 jmcneill
50 1.2 jmcneill struct plgpio_acpi_softc;
51 1.2 jmcneill
52 1.2 jmcneill struct plgpio_acpi_softc {
53 1.2 jmcneill struct plgpio_softc sc_base;
54 1.2 jmcneill
55 1.2 jmcneill ACPI_HANDLE sc_handle;
56 1.2 jmcneill
57 1.3 jmcneill struct acpi_event * sc_event[8];
58 1.2 jmcneill };
59 1.1 jmcneill
60 1.1 jmcneill static int plgpio_acpi_match(device_t, cfdata_t, void *);
61 1.1 jmcneill static void plgpio_acpi_attach(device_t, device_t, void *);
62 1.1 jmcneill
63 1.3 jmcneill static void plgpio_acpi_register_event(void *, struct acpi_event *, ACPI_RESOURCE_GPIO *);
64 1.2 jmcneill static int plgpio_acpi_intr(void *);
65 1.2 jmcneill
66 1.2 jmcneill CFATTACH_DECL_NEW(plgpio_acpi, sizeof(struct plgpio_acpi_softc), plgpio_acpi_match, plgpio_acpi_attach, NULL, NULL);
67 1.1 jmcneill
68 1.1 jmcneill static const char * const compatible[] = {
69 1.1 jmcneill "ARMH0061",
70 1.1 jmcneill NULL
71 1.1 jmcneill };
72 1.1 jmcneill
73 1.1 jmcneill static int
74 1.1 jmcneill plgpio_acpi_match(device_t parent, cfdata_t cf, void *aux)
75 1.1 jmcneill {
76 1.1 jmcneill struct acpi_attach_args *aa = aux;
77 1.1 jmcneill
78 1.1 jmcneill if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
79 1.1 jmcneill return 0;
80 1.1 jmcneill
81 1.1 jmcneill return acpi_match_hid(aa->aa_node->ad_devinfo, compatible);
82 1.1 jmcneill }
83 1.1 jmcneill
84 1.1 jmcneill static void
85 1.1 jmcneill plgpio_acpi_attach(device_t parent, device_t self, void *aux)
86 1.1 jmcneill {
87 1.2 jmcneill struct plgpio_acpi_softc * const asc = device_private(self);
88 1.2 jmcneill struct plgpio_softc * const sc = &asc->sc_base;
89 1.1 jmcneill struct acpi_attach_args *aa = aux;
90 1.1 jmcneill struct acpi_resources res;
91 1.1 jmcneill struct acpi_mem *mem;
92 1.2 jmcneill struct acpi_irq *irq;
93 1.1 jmcneill ACPI_STATUS rv;
94 1.1 jmcneill int error;
95 1.2 jmcneill void *ih;
96 1.1 jmcneill
97 1.1 jmcneill sc->sc_dev = self;
98 1.2 jmcneill asc->sc_handle = aa->aa_node->ad_handle;
99 1.1 jmcneill
100 1.1 jmcneill rv = acpi_resource_parse(sc->sc_dev, aa->aa_node->ad_handle, "_CRS",
101 1.1 jmcneill &res, &acpi_resource_parse_ops_default);
102 1.1 jmcneill if (ACPI_FAILURE(rv))
103 1.1 jmcneill return;
104 1.1 jmcneill
105 1.1 jmcneill mem = acpi_res_mem(&res, 0);
106 1.1 jmcneill if (mem == NULL) {
107 1.1 jmcneill aprint_error_dev(self, "couldn't find mem resource\n");
108 1.1 jmcneill goto done;
109 1.1 jmcneill }
110 1.1 jmcneill
111 1.2 jmcneill irq = acpi_res_irq(&res, 0);
112 1.5 jmcneill if (irq == NULL) {
113 1.2 jmcneill aprint_error_dev(self, "couldn't find irq resource\n");
114 1.2 jmcneill goto done;
115 1.2 jmcneill }
116 1.2 jmcneill
117 1.1 jmcneill sc->sc_dev = self;
118 1.1 jmcneill sc->sc_bst = aa->aa_memt;
119 1.1 jmcneill error = bus_space_map(sc->sc_bst, mem->ar_base, mem->ar_length, 0, &sc->sc_bsh);
120 1.1 jmcneill if (error) {
121 1.1 jmcneill aprint_error_dev(self, "couldn't map registers\n");
122 1.1 jmcneill return;
123 1.1 jmcneill }
124 1.1 jmcneill
125 1.1 jmcneill plgpio_attach(sc);
126 1.1 jmcneill
127 1.4 jmcneill rv = acpi_event_create_gpio(self, asc->sc_handle, plgpio_acpi_register_event, asc);
128 1.4 jmcneill if (ACPI_FAILURE(rv)) {
129 1.4 jmcneill if (rv != AE_NOT_FOUND)
130 1.4 jmcneill aprint_error_dev(self, "failed to create events: %s\n", AcpiFormatException(rv));
131 1.3 jmcneill goto done;
132 1.3 jmcneill }
133 1.3 jmcneill
134 1.6 jmcneill ih = acpi_intr_establish(self,
135 1.6 jmcneill (uint64_t)(uintptr_t)asc->sc_handle,
136 1.5 jmcneill IPL_VM, false, plgpio_acpi_intr, asc, device_xname(self));
137 1.2 jmcneill if (ih == NULL)
138 1.2 jmcneill aprint_error_dev(self, "couldn't establish interrupt\n");
139 1.2 jmcneill
140 1.1 jmcneill done:
141 1.1 jmcneill acpi_resource_cleanup(&res);
142 1.1 jmcneill }
143 1.2 jmcneill
144 1.2 jmcneill static void
145 1.3 jmcneill plgpio_acpi_register_event(void *priv, struct acpi_event *ev, ACPI_RESOURCE_GPIO *gpio)
146 1.2 jmcneill {
147 1.3 jmcneill struct plgpio_acpi_softc * const asc = priv;
148 1.2 jmcneill struct plgpio_softc * const sc = &asc->sc_base;
149 1.2 jmcneill uint32_t ibe, iev, is, ie;
150 1.2 jmcneill
151 1.3 jmcneill const int pin = gpio->PinTable[0];
152 1.3 jmcneill
153 1.3 jmcneill if (pin >= __arraycount(asc->sc_event)) {
154 1.3 jmcneill aprint_error_dev(asc->sc_base.sc_dev,
155 1.3 jmcneill "ignoring event for pin %u (out of range)\n", pin);
156 1.2 jmcneill return;
157 1.2 jmcneill }
158 1.3 jmcneill if (asc->sc_event[pin] != NULL) {
159 1.3 jmcneill aprint_error_dev(asc->sc_base.sc_dev,
160 1.3 jmcneill "ignoring duplicate pin %u\n", pin);
161 1.2 jmcneill return;
162 1.3 jmcneill }
163 1.2 jmcneill
164 1.3 jmcneill asc->sc_event[pin] = ev;
165 1.3 jmcneill asc->sc_base.sc_reserved_mask |= __BIT(pin);
166 1.2 jmcneill
167 1.2 jmcneill /*
168 1.3 jmcneill * Configure and enable interrupts for this pin.
169 1.2 jmcneill */
170 1.2 jmcneill
171 1.3 jmcneill ibe = PLGPIO_READ(sc, PL061_GPIOIBE_REG);
172 1.3 jmcneill iev = PLGPIO_READ(sc, PL061_GPIOIEV_REG);
173 1.3 jmcneill switch (gpio->Polarity) {
174 1.3 jmcneill case ACPI_ACTIVE_HIGH:
175 1.3 jmcneill ibe &= ~__BIT(pin);
176 1.3 jmcneill iev |= __BIT(pin);
177 1.3 jmcneill break;
178 1.3 jmcneill case ACPI_ACTIVE_LOW:
179 1.3 jmcneill ibe &= ~__BIT(pin);
180 1.3 jmcneill iev &= ~__BIT(pin);
181 1.3 jmcneill break;
182 1.3 jmcneill case ACPI_ACTIVE_BOTH:
183 1.3 jmcneill ibe |= __BIT(pin);
184 1.3 jmcneill break;
185 1.3 jmcneill }
186 1.3 jmcneill PLGPIO_WRITE(sc, PL061_GPIOIBE_REG, ibe);
187 1.3 jmcneill PLGPIO_WRITE(sc, PL061_GPIOIEV_REG, iev);
188 1.3 jmcneill
189 1.3 jmcneill is = PLGPIO_READ(sc, PL061_GPIOIS_REG);
190 1.3 jmcneill switch (gpio->Triggering) {
191 1.3 jmcneill case ACPI_LEVEL_SENSITIVE:
192 1.3 jmcneill is |= __BIT(pin);
193 1.3 jmcneill break;
194 1.3 jmcneill case ACPI_EDGE_SENSITIVE:
195 1.3 jmcneill is &= ~__BIT(pin);
196 1.3 jmcneill break;
197 1.3 jmcneill }
198 1.3 jmcneill PLGPIO_WRITE(sc, PL061_GPIOIS_REG, is);
199 1.3 jmcneill
200 1.3 jmcneill delay(20);
201 1.3 jmcneill
202 1.3 jmcneill PLGPIO_WRITE(sc, PL061_GPIOIC_REG, __BIT(pin));
203 1.3 jmcneill
204 1.3 jmcneill ie = PLGPIO_READ(sc, PL061_GPIOIE_REG);
205 1.3 jmcneill ie |= __BIT(pin);
206 1.3 jmcneill PLGPIO_WRITE(sc, PL061_GPIOIE_REG, ie);
207 1.2 jmcneill }
208 1.2 jmcneill
209 1.2 jmcneill static int
210 1.2 jmcneill plgpio_acpi_intr(void *priv)
211 1.2 jmcneill {
212 1.2 jmcneill struct plgpio_acpi_softc * const asc = priv;
213 1.2 jmcneill struct plgpio_softc * const sc = &asc->sc_base;
214 1.2 jmcneill uint32_t mis;
215 1.2 jmcneill int bit;
216 1.2 jmcneill
217 1.2 jmcneill mis = PLGPIO_READ(sc, PL061_GPIOMIS_REG);
218 1.2 jmcneill PLGPIO_WRITE(sc, PL061_GPIOIC_REG, mis);
219 1.2 jmcneill
220 1.2 jmcneill while ((bit = __builtin_ffs(mis)) != 0) {
221 1.2 jmcneill const int pin = bit - 1;
222 1.3 jmcneill struct acpi_event * const ev = asc->sc_event[pin];
223 1.3 jmcneill KASSERT(ev != NULL);
224 1.2 jmcneill
225 1.3 jmcneill acpi_event_notify(ev);
226 1.2 jmcneill
227 1.2 jmcneill mis &= ~__BIT(pin);
228 1.2 jmcneill }
229 1.2 jmcneill
230 1.2 jmcneill return 1;
231 1.2 jmcneill }
232