qcomgpio.c revision 1.5 1 1.5 jmcneill /* $NetBSD: qcomgpio.c,v 1.5 2024/12/12 21:51:19 jmcneill Exp $ */
2 1.1 jmcneill
3 1.1 jmcneill /*-
4 1.1 jmcneill * Copyright (c) 2024 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.5 jmcneill __KERNEL_RCSID(0, "$NetBSD: qcomgpio.c,v 1.5 2024/12/12 21:51:19 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 #include <sys/queue.h>
41 1.1 jmcneill #include <sys/kmem.h>
42 1.1 jmcneill #include <sys/mutex.h>
43 1.1 jmcneill
44 1.1 jmcneill #include <dev/acpi/acpireg.h>
45 1.1 jmcneill #include <dev/acpi/acpivar.h>
46 1.1 jmcneill #include <dev/acpi/acpi_intr.h>
47 1.1 jmcneill #include <dev/acpi/acpi_event.h>
48 1.1 jmcneill #include <dev/acpi/acpi_gpio.h>
49 1.1 jmcneill #include <dev/acpi/qcomgpioreg.h>
50 1.1 jmcneill
51 1.1 jmcneill #include <dev/gpio/gpiovar.h>
52 1.1 jmcneill
53 1.1 jmcneill typedef enum {
54 1.1 jmcneill QCOMGPIO_X1E,
55 1.1 jmcneill } qcomgpio_type;
56 1.1 jmcneill
57 1.3 jmcneill struct qcomgpio_reserved {
58 1.3 jmcneill int start;
59 1.3 jmcneill int count;
60 1.3 jmcneill };
61 1.3 jmcneill
62 1.1 jmcneill struct qcomgpio_config {
63 1.3 jmcneill struct qcomgpio_reserved *reserved;
64 1.3 jmcneill u_int num_reserved;
65 1.5 jmcneill u_int *pdc_filter;
66 1.5 jmcneill u_int num_pdc_filter;
67 1.1 jmcneill };
68 1.1 jmcneill
69 1.1 jmcneill struct qcomgpio_intr_handler {
70 1.1 jmcneill int (*ih_func)(void *);
71 1.1 jmcneill void *ih_arg;
72 1.1 jmcneill int ih_pin;
73 1.2 jmcneill int ih_type;
74 1.1 jmcneill LIST_ENTRY(qcomgpio_intr_handler) ih_list;
75 1.1 jmcneill };
76 1.1 jmcneill
77 1.4 jmcneill struct qcomgpio_pdcmap {
78 1.4 jmcneill int pm_pin;
79 1.4 jmcneill u_int pm_irq;
80 1.4 jmcneill };
81 1.4 jmcneill
82 1.1 jmcneill struct qcomgpio_softc {
83 1.1 jmcneill device_t sc_dev;
84 1.1 jmcneill device_t sc_gpiodev;
85 1.1 jmcneill bus_space_handle_t sc_bsh;
86 1.1 jmcneill bus_space_tag_t sc_bst;
87 1.1 jmcneill const struct qcomgpio_config *sc_config;
88 1.1 jmcneill struct gpio_chipset_tag sc_gc;
89 1.1 jmcneill gpio_pin_t *sc_pins;
90 1.4 jmcneill u_int sc_npins;
91 1.1 jmcneill LIST_HEAD(, qcomgpio_intr_handler) sc_intrs;
92 1.1 jmcneill kmutex_t sc_lock;
93 1.4 jmcneill
94 1.4 jmcneill struct qcomgpio_pdcmap *sc_pdcmap;
95 1.4 jmcneill u_int sc_npdcmap;
96 1.1 jmcneill };
97 1.1 jmcneill
98 1.1 jmcneill #define RD4(sc, reg) \
99 1.1 jmcneill bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
100 1.1 jmcneill #define WR4(sc, reg, val) \
101 1.1 jmcneill bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
102 1.1 jmcneill
103 1.1 jmcneill static int qcomgpio_match(device_t, cfdata_t, void *);
104 1.1 jmcneill static void qcomgpio_attach(device_t, device_t, void *);
105 1.1 jmcneill
106 1.3 jmcneill static bool qcomgpio_pin_reserved(struct qcomgpio_softc *, int);
107 1.1 jmcneill static int qcomgpio_pin_read(void *, int);
108 1.1 jmcneill static void qcomgpio_pin_write(void *, int, int);
109 1.1 jmcneill static void qcomgpio_pin_ctl(void *, int, int);
110 1.1 jmcneill static void * qcomgpio_intr_establish(void *, int, int, int,
111 1.1 jmcneill int (*)(void *), void *);
112 1.1 jmcneill static void qcomgpio_intr_disestablish(void *, void *);
113 1.1 jmcneill static bool qcomgpio_intr_str(void *, int, int, char *, size_t);
114 1.1 jmcneill static void qcomgpio_intr_mask(void *, void *);
115 1.1 jmcneill static void qcomgpio_intr_unmask(void *, void *);
116 1.1 jmcneill
117 1.4 jmcneill static u_int qcomgpio_acpi_num_pins(device_t, ACPI_HANDLE);
118 1.4 jmcneill static void qcomgpio_acpi_fill_pdcmap(struct qcomgpio_softc *,
119 1.4 jmcneill ACPI_HANDLE);
120 1.2 jmcneill static int qcomgpio_acpi_translate(void *, ACPI_RESOURCE_GPIO *, void **);
121 1.1 jmcneill static void qcomgpio_register_event(void *, struct acpi_event *,
122 1.1 jmcneill ACPI_RESOURCE_GPIO *);
123 1.1 jmcneill static int qcomgpio_intr(void *);
124 1.1 jmcneill
125 1.1 jmcneill CFATTACH_DECL_NEW(qcomgpio, sizeof(struct qcomgpio_softc),
126 1.1 jmcneill qcomgpio_match, qcomgpio_attach, NULL, NULL);
127 1.1 jmcneill
128 1.4 jmcneill static UINT8 qcomgpio_gpio_dsm_uuid[ACPI_UUID_LENGTH] = {
129 1.4 jmcneill 0xa4, 0xb2, 0xb9, 0x98, 0x63, 0x16, 0x5f, 0x4a,
130 1.4 jmcneill 0x82, 0xf2, 0xc6, 0xc9, 0x9a, 0x39, 0x47, 0x26
131 1.4 jmcneill };
132 1.4 jmcneill #define QCOMGPIO_GPIO_DSM_REV 0
133 1.4 jmcneill #define QCOMGPIO_GPIO_DSM_FUNC_NUM_PINS 2
134 1.4 jmcneill
135 1.4 jmcneill static UINT8 qcomgpio_pdc_dsm_uuid[ACPI_UUID_LENGTH] = {
136 1.4 jmcneill 0xd4, 0x0f, 0x1b, 0x92, 0x7c, 0x56, 0xa0, 0x43,
137 1.4 jmcneill 0xbb, 0x14, 0x26, 0x48, 0xf7, 0xb2, 0xa1, 0x8c
138 1.4 jmcneill };
139 1.4 jmcneill #define QCOMGPIO_PDC_DSM_REV 0
140 1.4 jmcneill #define QCOMGPIO_PDC_DSM_FUNC_CIPR 2
141 1.2 jmcneill
142 1.3 jmcneill static struct qcomgpio_reserved qcomgpio_x1e_reserved[] = {
143 1.3 jmcneill { .start = 34, .count = 2 },
144 1.3 jmcneill { .start = 44, .count = 4 },
145 1.3 jmcneill { .start = 72, .count = 2 },
146 1.3 jmcneill { .start = 238, .count = 1 },
147 1.3 jmcneill };
148 1.3 jmcneill
149 1.5 jmcneill static int qcomgpio_x1e_pdc_filter[] = {
150 1.5 jmcneill 0x140, /* Interrupt storm due to missing SMI support. */
151 1.5 jmcneill };
152 1.5 jmcneill
153 1.1 jmcneill static struct qcomgpio_config qcomgpio_x1e_config = {
154 1.3 jmcneill .reserved = qcomgpio_x1e_reserved,
155 1.3 jmcneill .num_reserved = __arraycount(qcomgpio_x1e_reserved),
156 1.5 jmcneill .pdc_filter = qcomgpio_x1e_pdc_filter,
157 1.5 jmcneill .num_pdc_filter = __arraycount(qcomgpio_x1e_pdc_filter),
158 1.1 jmcneill };
159 1.1 jmcneill
160 1.1 jmcneill static const struct device_compatible_entry compat_data[] = {
161 1.1 jmcneill { .compat = "QCOM0C0C", .data = &qcomgpio_x1e_config },
162 1.1 jmcneill DEVICE_COMPAT_EOL
163 1.1 jmcneill };
164 1.1 jmcneill
165 1.1 jmcneill static int
166 1.1 jmcneill qcomgpio_match(device_t parent, cfdata_t cf, void *aux)
167 1.1 jmcneill {
168 1.1 jmcneill struct acpi_attach_args *aa = aux;
169 1.1 jmcneill
170 1.1 jmcneill return acpi_compatible_match(aa, compat_data);
171 1.1 jmcneill }
172 1.1 jmcneill
173 1.1 jmcneill static void
174 1.1 jmcneill qcomgpio_attach(device_t parent, device_t self, void *aux)
175 1.1 jmcneill {
176 1.1 jmcneill struct qcomgpio_softc * const sc = device_private(self);
177 1.1 jmcneill struct acpi_attach_args *aa = aux;
178 1.1 jmcneill struct gpiobus_attach_args gba;
179 1.1 jmcneill ACPI_HANDLE hdl = aa->aa_node->ad_handle;
180 1.1 jmcneill struct acpi_resources res;
181 1.1 jmcneill struct acpi_mem *mem;
182 1.1 jmcneill struct acpi_irq *irq;
183 1.1 jmcneill ACPI_STATUS rv;
184 1.4 jmcneill int error, pin, n;
185 1.1 jmcneill void *ih;
186 1.1 jmcneill
187 1.1 jmcneill sc->sc_dev = self;
188 1.1 jmcneill sc->sc_config = acpi_compatible_lookup(aa, compat_data)->data;
189 1.1 jmcneill sc->sc_bst = aa->aa_memt;
190 1.1 jmcneill KASSERT(sc->sc_config != NULL);
191 1.1 jmcneill LIST_INIT(&sc->sc_intrs);
192 1.1 jmcneill mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_VM);
193 1.1 jmcneill
194 1.1 jmcneill rv = acpi_resource_parse(sc->sc_dev, hdl, "_CRS",
195 1.1 jmcneill &res, &acpi_resource_parse_ops_default);
196 1.1 jmcneill if (ACPI_FAILURE(rv)) {
197 1.1 jmcneill return;
198 1.1 jmcneill }
199 1.1 jmcneill
200 1.1 jmcneill mem = acpi_res_mem(&res, 0);
201 1.1 jmcneill if (mem == NULL) {
202 1.1 jmcneill aprint_error_dev(self, "couldn't find mem resource\n");
203 1.1 jmcneill goto done;
204 1.1 jmcneill }
205 1.1 jmcneill
206 1.1 jmcneill irq = acpi_res_irq(&res, 0);
207 1.1 jmcneill if (irq == NULL) {
208 1.1 jmcneill aprint_error_dev(self, "couldn't find irq resource\n");
209 1.1 jmcneill goto done;
210 1.1 jmcneill }
211 1.1 jmcneill
212 1.1 jmcneill error = bus_space_map(sc->sc_bst, mem->ar_base, mem->ar_length, 0,
213 1.1 jmcneill &sc->sc_bsh);
214 1.1 jmcneill if (error) {
215 1.1 jmcneill aprint_error_dev(self, "couldn't map registers\n");
216 1.1 jmcneill goto done;
217 1.1 jmcneill }
218 1.1 jmcneill
219 1.4 jmcneill sc->sc_npdcmap = res.ar_nirq;
220 1.4 jmcneill sc->sc_pdcmap = kmem_zalloc(sizeof(*sc->sc_pdcmap) * sc->sc_npdcmap,
221 1.4 jmcneill KM_SLEEP);
222 1.4 jmcneill for (n = 0; n < sc->sc_npdcmap; n++) {
223 1.4 jmcneill sc->sc_pdcmap[n].pm_irq = acpi_res_irq(&res, n)->ar_irq;
224 1.4 jmcneill sc->sc_pdcmap[n].pm_pin = -1;
225 1.4 jmcneill aprint_debug_dev(self, "IRQ resource %u -> %#x\n",
226 1.4 jmcneill n, sc->sc_pdcmap[n].pm_irq);
227 1.4 jmcneill }
228 1.4 jmcneill qcomgpio_acpi_fill_pdcmap(sc, hdl);
229 1.4 jmcneill
230 1.4 jmcneill sc->sc_npins = qcomgpio_acpi_num_pins(self, hdl);
231 1.4 jmcneill if (sc->sc_npins == 0) {
232 1.4 jmcneill aprint_error_dev(self, "couldn't determine pin count!\n");
233 1.4 jmcneill goto done;
234 1.4 jmcneill }
235 1.4 jmcneill sc->sc_pins = kmem_zalloc(sizeof(*sc->sc_pins) * sc->sc_npins,
236 1.4 jmcneill KM_SLEEP);
237 1.4 jmcneill for (pin = 0; pin < sc->sc_npins; pin++) {
238 1.3 jmcneill sc->sc_pins[pin].pin_caps = qcomgpio_pin_reserved(sc, pin) ?
239 1.3 jmcneill 0 : (GPIO_PIN_INPUT | GPIO_PIN_OUTPUT);
240 1.1 jmcneill sc->sc_pins[pin].pin_num = pin;
241 1.1 jmcneill sc->sc_pins[pin].pin_intrcaps =
242 1.1 jmcneill GPIO_INTR_POS_EDGE | GPIO_INTR_NEG_EDGE |
243 1.1 jmcneill GPIO_INTR_DOUBLE_EDGE | GPIO_INTR_HIGH_LEVEL |
244 1.1 jmcneill GPIO_INTR_LOW_LEVEL | GPIO_INTR_MPSAFE;
245 1.1 jmcneill }
246 1.1 jmcneill
247 1.1 jmcneill sc->sc_gc.gp_cookie = sc;
248 1.1 jmcneill sc->sc_gc.gp_pin_read = qcomgpio_pin_read;
249 1.1 jmcneill sc->sc_gc.gp_pin_write = qcomgpio_pin_write;
250 1.1 jmcneill sc->sc_gc.gp_pin_ctl = qcomgpio_pin_ctl;
251 1.1 jmcneill sc->sc_gc.gp_intr_establish = qcomgpio_intr_establish;
252 1.1 jmcneill sc->sc_gc.gp_intr_disestablish = qcomgpio_intr_disestablish;
253 1.1 jmcneill sc->sc_gc.gp_intr_str = qcomgpio_intr_str;
254 1.1 jmcneill sc->sc_gc.gp_intr_mask = qcomgpio_intr_mask;
255 1.1 jmcneill sc->sc_gc.gp_intr_unmask = qcomgpio_intr_unmask;
256 1.1 jmcneill
257 1.1 jmcneill rv = acpi_event_create_gpio(self, hdl, qcomgpio_register_event, sc);
258 1.1 jmcneill if (ACPI_FAILURE(rv)) {
259 1.1 jmcneill if (rv != AE_NOT_FOUND) {
260 1.1 jmcneill aprint_error_dev(self, "failed to create events: %s\n",
261 1.1 jmcneill AcpiFormatException(rv));
262 1.1 jmcneill }
263 1.1 jmcneill goto done;
264 1.1 jmcneill }
265 1.1 jmcneill
266 1.1 jmcneill ih = acpi_intr_establish(self, (uint64_t)(uintptr_t)hdl,
267 1.1 jmcneill IPL_VM, false, qcomgpio_intr, sc, device_xname(self));
268 1.1 jmcneill if (ih == NULL) {
269 1.1 jmcneill aprint_error_dev(self, "couldn't establish interrupt\n");
270 1.1 jmcneill goto done;
271 1.1 jmcneill }
272 1.1 jmcneill
273 1.1 jmcneill memset(&gba, 0, sizeof(gba));
274 1.1 jmcneill gba.gba_gc = &sc->sc_gc;
275 1.1 jmcneill gba.gba_pins = sc->sc_pins;
276 1.4 jmcneill gba.gba_npins = sc->sc_npins;
277 1.1 jmcneill sc->sc_gpiodev = config_found(self, &gba, gpiobus_print,
278 1.1 jmcneill CFARGS(.iattr = "gpiobus"));
279 1.1 jmcneill if (sc->sc_gpiodev != NULL) {
280 1.1 jmcneill acpi_gpio_register(aa->aa_node, self,
281 1.1 jmcneill qcomgpio_acpi_translate, sc);
282 1.1 jmcneill }
283 1.1 jmcneill
284 1.1 jmcneill done:
285 1.1 jmcneill acpi_resource_cleanup(&res);
286 1.1 jmcneill }
287 1.1 jmcneill
288 1.4 jmcneill static u_int
289 1.4 jmcneill qcomgpio_acpi_num_pins(device_t dev, ACPI_HANDLE hdl)
290 1.4 jmcneill {
291 1.4 jmcneill ACPI_STATUS rv;
292 1.4 jmcneill ACPI_INTEGER npins;
293 1.4 jmcneill
294 1.4 jmcneill rv = acpi_dsm_integer(hdl, qcomgpio_gpio_dsm_uuid,
295 1.4 jmcneill QCOMGPIO_GPIO_DSM_REV, QCOMGPIO_GPIO_DSM_FUNC_NUM_PINS,
296 1.4 jmcneill NULL, &npins);
297 1.4 jmcneill if (ACPI_FAILURE(rv)) {
298 1.4 jmcneill aprint_error_dev(dev, "GPIO _DSM failed: %s\n",
299 1.4 jmcneill AcpiFormatException(rv));
300 1.4 jmcneill return 0;
301 1.4 jmcneill }
302 1.4 jmcneill
303 1.4 jmcneill aprint_debug_dev(dev, "GPIO pin count: %u\n", (u_int)npins);
304 1.4 jmcneill
305 1.4 jmcneill return (u_int)npins;
306 1.4 jmcneill }
307 1.4 jmcneill
308 1.4 jmcneill static void
309 1.4 jmcneill qcomgpio_acpi_fill_pdcmap(struct qcomgpio_softc *sc,
310 1.4 jmcneill ACPI_HANDLE hdl)
311 1.4 jmcneill {
312 1.4 jmcneill ACPI_STATUS rv;
313 1.4 jmcneill ACPI_OBJECT *obj;
314 1.5 jmcneill u_int n, filt;
315 1.4 jmcneill
316 1.4 jmcneill rv = acpi_dsm_typed(hdl, qcomgpio_pdc_dsm_uuid,
317 1.4 jmcneill QCOMGPIO_PDC_DSM_REV, QCOMGPIO_PDC_DSM_FUNC_CIPR,
318 1.4 jmcneill NULL, ACPI_TYPE_PACKAGE, &obj);
319 1.4 jmcneill if (ACPI_FAILURE(rv)) {
320 1.4 jmcneill aprint_error_dev(sc->sc_dev, "PDC _DSM failed: %s\n",
321 1.4 jmcneill AcpiFormatException(rv));
322 1.4 jmcneill return;
323 1.4 jmcneill }
324 1.4 jmcneill
325 1.4 jmcneill for (n = 0; n < obj->Package.Count; n++) {
326 1.4 jmcneill ACPI_OBJECT *map = &obj->Package.Elements[n];
327 1.5 jmcneill bool filter = false;
328 1.4 jmcneill u_int irq, pdc;
329 1.4 jmcneill int pin;
330 1.4 jmcneill
331 1.4 jmcneill if (map->Type != ACPI_TYPE_PACKAGE ||
332 1.4 jmcneill map->Package.Count < 3 ||
333 1.4 jmcneill map->Package.Elements[0].Type != ACPI_TYPE_INTEGER ||
334 1.4 jmcneill map->Package.Elements[1].Type != ACPI_TYPE_INTEGER ||
335 1.4 jmcneill map->Package.Elements[2].Type != ACPI_TYPE_INTEGER) {
336 1.4 jmcneill continue;
337 1.4 jmcneill }
338 1.4 jmcneill
339 1.4 jmcneill irq = (u_int)map->Package.Elements[2].Integer.Value;
340 1.4 jmcneill pin = (int)map->Package.Elements[1].Integer.Value;
341 1.4 jmcneill for (pdc = 0; pdc < sc->sc_npdcmap; pdc++) {
342 1.4 jmcneill if (sc->sc_pdcmap[pdc].pm_irq == irq) {
343 1.5 jmcneill for (filt = 0;
344 1.5 jmcneill filt < sc->sc_config->num_pdc_filter;
345 1.5 jmcneill filt++) {
346 1.5 jmcneill if (sc->sc_config->pdc_filter[filt] ==
347 1.5 jmcneill pdc * 64) {
348 1.5 jmcneill filter = true;
349 1.5 jmcneill break;
350 1.5 jmcneill }
351 1.5 jmcneill }
352 1.5 jmcneill
353 1.5 jmcneill if (!filter) {
354 1.5 jmcneill sc->sc_pdcmap[pdc].pm_pin = pin;
355 1.5 jmcneill }
356 1.4 jmcneill break;
357 1.4 jmcneill }
358 1.4 jmcneill }
359 1.5 jmcneill
360 1.4 jmcneill aprint_debug_dev(sc->sc_dev,
361 1.5 jmcneill "PDC irq %#x -> pin %d%s%s\n", irq, pin,
362 1.5 jmcneill filter ? " (filtered)" : "",
363 1.4 jmcneill pdc == sc->sc_npdcmap ? " (unused)" : "");
364 1.4 jmcneill }
365 1.4 jmcneill
366 1.4 jmcneill ACPI_FREE(obj);
367 1.4 jmcneill }
368 1.4 jmcneill
369 1.1 jmcneill static int
370 1.2 jmcneill qcomgpio_acpi_translate(void *priv, ACPI_RESOURCE_GPIO *gpio, void **gpiop)
371 1.1 jmcneill {
372 1.1 jmcneill struct qcomgpio_softc * const sc = priv;
373 1.4 jmcneill const ACPI_INTEGER vpin = gpio->PinTable[0];
374 1.4 jmcneill int pin = -1;
375 1.1 jmcneill
376 1.4 jmcneill if (vpin < sc->sc_npins) {
377 1.4 jmcneill /* Virtual pin number is 1:1 mapping with hardware. */
378 1.4 jmcneill pin = vpin;
379 1.4 jmcneill } else if (vpin / 64 < sc->sc_npdcmap) {
380 1.4 jmcneill /* Translate the virtual pin number to a hardware pin. */
381 1.4 jmcneill pin = sc->sc_pdcmap[vpin / 64].pm_pin;
382 1.4 jmcneill }
383 1.1 jmcneill
384 1.4 jmcneill aprint_debug_dev(sc->sc_dev, "translate %#lx -> %u\n", vpin, pin);
385 1.1 jmcneill
386 1.1 jmcneill if (gpiop != NULL) {
387 1.1 jmcneill if (sc->sc_gpiodev != NULL) {
388 1.1 jmcneill *gpiop = device_private(sc->sc_gpiodev);
389 1.1 jmcneill } else {
390 1.1 jmcneill device_printf(sc->sc_dev,
391 1.4 jmcneill "no gpiodev for pin %#lx -> %u\n", vpin, pin);
392 1.4 jmcneill pin = -1;
393 1.1 jmcneill }
394 1.1 jmcneill }
395 1.1 jmcneill
396 1.4 jmcneill return pin;
397 1.1 jmcneill }
398 1.1 jmcneill
399 1.1 jmcneill static int
400 1.1 jmcneill qcomgpio_acpi_event(void *priv)
401 1.1 jmcneill {
402 1.1 jmcneill struct acpi_event * const ev = priv;
403 1.1 jmcneill
404 1.1 jmcneill acpi_event_notify(ev);
405 1.1 jmcneill
406 1.1 jmcneill return 1;
407 1.1 jmcneill }
408 1.1 jmcneill
409 1.1 jmcneill static void
410 1.1 jmcneill qcomgpio_register_event(void *priv, struct acpi_event *ev,
411 1.1 jmcneill ACPI_RESOURCE_GPIO *gpio)
412 1.1 jmcneill {
413 1.1 jmcneill struct qcomgpio_softc * const sc = priv;
414 1.1 jmcneill int irqmode;
415 1.1 jmcneill void *ih;
416 1.1 jmcneill
417 1.2 jmcneill const int pin = qcomgpio_acpi_translate(sc, gpio, NULL);
418 1.1 jmcneill
419 1.1 jmcneill if (pin < 0) {
420 1.1 jmcneill aprint_error_dev(sc->sc_dev,
421 1.1 jmcneill "ignoring event for pin %#x (out of range)\n",
422 1.1 jmcneill gpio->PinTable[0]);
423 1.1 jmcneill return;
424 1.1 jmcneill }
425 1.1 jmcneill
426 1.1 jmcneill if (gpio->Triggering == ACPI_LEVEL_SENSITIVE) {
427 1.1 jmcneill irqmode = gpio->Polarity == ACPI_ACTIVE_HIGH ?
428 1.1 jmcneill GPIO_INTR_HIGH_LEVEL : GPIO_INTR_LOW_LEVEL;
429 1.1 jmcneill } else {
430 1.1 jmcneill KASSERT(gpio->Triggering == ACPI_EDGE_SENSITIVE);
431 1.1 jmcneill if (gpio->Polarity == ACPI_ACTIVE_LOW) {
432 1.1 jmcneill irqmode = GPIO_INTR_NEG_EDGE;
433 1.1 jmcneill } else if (gpio->Polarity == ACPI_ACTIVE_HIGH) {
434 1.1 jmcneill irqmode = GPIO_INTR_POS_EDGE;
435 1.1 jmcneill } else {
436 1.1 jmcneill KASSERT(gpio->Polarity == ACPI_ACTIVE_BOTH);
437 1.1 jmcneill irqmode = GPIO_INTR_DOUBLE_EDGE;
438 1.1 jmcneill }
439 1.1 jmcneill }
440 1.1 jmcneill
441 1.1 jmcneill ih = qcomgpio_intr_establish(sc, pin, IPL_VM, irqmode,
442 1.1 jmcneill qcomgpio_acpi_event, ev);
443 1.1 jmcneill if (ih == NULL) {
444 1.1 jmcneill aprint_error_dev(sc->sc_dev,
445 1.1 jmcneill "couldn't register event for pin %#x\n",
446 1.1 jmcneill gpio->PinTable[0]);
447 1.2 jmcneill return;
448 1.2 jmcneill }
449 1.2 jmcneill if (gpio->Triggering == ACPI_LEVEL_SENSITIVE) {
450 1.2 jmcneill acpi_event_set_intrcookie(ev, ih);
451 1.1 jmcneill }
452 1.1 jmcneill }
453 1.1 jmcneill
454 1.3 jmcneill static bool
455 1.3 jmcneill qcomgpio_pin_reserved(struct qcomgpio_softc *sc, int pin)
456 1.3 jmcneill {
457 1.3 jmcneill u_int n;
458 1.3 jmcneill
459 1.3 jmcneill for (n = 0; n < sc->sc_config->num_reserved; n++) {
460 1.3 jmcneill if (pin >= sc->sc_config->reserved[n].start &&
461 1.3 jmcneill pin < sc->sc_config->reserved[n].start +
462 1.3 jmcneill sc->sc_config->reserved[n].count) {
463 1.3 jmcneill return true;
464 1.3 jmcneill }
465 1.3 jmcneill }
466 1.3 jmcneill
467 1.3 jmcneill return false;
468 1.3 jmcneill }
469 1.3 jmcneill
470 1.1 jmcneill static int
471 1.1 jmcneill qcomgpio_pin_read(void *priv, int pin)
472 1.1 jmcneill {
473 1.1 jmcneill struct qcomgpio_softc * const sc = priv;
474 1.1 jmcneill uint32_t val;
475 1.1 jmcneill
476 1.4 jmcneill if (pin < 0 || pin >= sc->sc_npins) {
477 1.1 jmcneill return 0;
478 1.1 jmcneill }
479 1.2 jmcneill if ((sc->sc_pins[pin].pin_caps & GPIO_PIN_INPUT) == 0) {
480 1.2 jmcneill return 0;
481 1.2 jmcneill }
482 1.1 jmcneill
483 1.1 jmcneill val = RD4(sc, TLMM_GPIO_IN_OUT(pin));
484 1.1 jmcneill return (val & TLMM_GPIO_IN_OUT_GPIO_IN) != 0;
485 1.1 jmcneill }
486 1.1 jmcneill
487 1.1 jmcneill static void
488 1.1 jmcneill qcomgpio_pin_write(void *priv, int pin, int pinval)
489 1.1 jmcneill {
490 1.1 jmcneill struct qcomgpio_softc * const sc = priv;
491 1.1 jmcneill uint32_t val;
492 1.1 jmcneill
493 1.4 jmcneill if (pin < 0 || pin >= sc->sc_npins) {
494 1.1 jmcneill return;
495 1.1 jmcneill }
496 1.2 jmcneill if ((sc->sc_pins[pin].pin_caps & GPIO_PIN_OUTPUT) == 0) {
497 1.2 jmcneill return;
498 1.2 jmcneill }
499 1.1 jmcneill
500 1.1 jmcneill val = RD4(sc, TLMM_GPIO_IN_OUT(pin));
501 1.1 jmcneill if (pinval) {
502 1.1 jmcneill val |= TLMM_GPIO_IN_OUT_GPIO_OUT;
503 1.1 jmcneill } else {
504 1.1 jmcneill val &= ~TLMM_GPIO_IN_OUT_GPIO_OUT;
505 1.1 jmcneill }
506 1.1 jmcneill WR4(sc, TLMM_GPIO_IN_OUT(pin), val);
507 1.1 jmcneill }
508 1.1 jmcneill
509 1.1 jmcneill static void
510 1.1 jmcneill qcomgpio_pin_ctl(void *priv, int pin, int flags)
511 1.1 jmcneill {
512 1.1 jmcneill /* Nothing to do here, as firmware has already configured pins. */
513 1.1 jmcneill }
514 1.1 jmcneill
515 1.1 jmcneill static void *
516 1.1 jmcneill qcomgpio_intr_establish(void *priv, int pin, int ipl, int irqmode,
517 1.1 jmcneill int (*func)(void *), void *arg)
518 1.1 jmcneill {
519 1.1 jmcneill struct qcomgpio_softc * const sc = priv;
520 1.1 jmcneill struct qcomgpio_intr_handler *qih, *qihp;
521 1.1 jmcneill uint32_t dect, pol;
522 1.1 jmcneill uint32_t val;
523 1.1 jmcneill
524 1.4 jmcneill if (pin < 0 || pin >= sc->sc_npins) {
525 1.1 jmcneill return NULL;
526 1.1 jmcneill }
527 1.1 jmcneill if (ipl != IPL_VM) {
528 1.1 jmcneill device_printf(sc->sc_dev, "%s: only IPL_VM supported\n",
529 1.1 jmcneill __func__);
530 1.1 jmcneill return NULL;
531 1.1 jmcneill }
532 1.1 jmcneill
533 1.1 jmcneill qih = kmem_alloc(sizeof(*qih), KM_SLEEP);
534 1.1 jmcneill qih->ih_func = func;
535 1.1 jmcneill qih->ih_arg = arg;
536 1.1 jmcneill qih->ih_pin = pin;
537 1.2 jmcneill qih->ih_type = (irqmode & GPIO_INTR_LEVEL_MASK) != 0 ?
538 1.2 jmcneill IST_LEVEL : IST_EDGE;
539 1.1 jmcneill
540 1.1 jmcneill mutex_enter(&sc->sc_lock);
541 1.1 jmcneill
542 1.1 jmcneill LIST_FOREACH(qihp, &sc->sc_intrs, ih_list) {
543 1.1 jmcneill if (qihp->ih_pin == qih->ih_pin) {
544 1.1 jmcneill mutex_exit(&sc->sc_lock);
545 1.1 jmcneill kmem_free(qih, sizeof(*qih));
546 1.1 jmcneill device_printf(sc->sc_dev,
547 1.1 jmcneill "%s: pin %d already establish\n", __func__, pin);
548 1.1 jmcneill return NULL;
549 1.1 jmcneill }
550 1.1 jmcneill }
551 1.1 jmcneill
552 1.1 jmcneill LIST_INSERT_HEAD(&sc->sc_intrs, qih, ih_list);
553 1.1 jmcneill
554 1.1 jmcneill if ((irqmode & GPIO_INTR_LEVEL_MASK) != 0) {
555 1.1 jmcneill dect = TLMM_GPIO_INTR_CFG_INTR_DECT_CTL_LEVEL;
556 1.1 jmcneill pol = (irqmode & GPIO_INTR_HIGH_LEVEL) != 0 ?
557 1.1 jmcneill TLMM_GPIO_INTR_CFG_INTR_POL_CTL : 0;
558 1.1 jmcneill } else {
559 1.1 jmcneill KASSERT((irqmode & GPIO_INTR_EDGE_MASK) != 0);
560 1.1 jmcneill if ((irqmode & GPIO_INTR_NEG_EDGE) != 0) {
561 1.1 jmcneill dect = TLMM_GPIO_INTR_CFG_INTR_DECT_CTL_EDGE_NEG;
562 1.1 jmcneill pol = TLMM_GPIO_INTR_CFG_INTR_POL_CTL;
563 1.1 jmcneill } else if ((irqmode & GPIO_INTR_POS_EDGE) != 0) {
564 1.1 jmcneill dect = TLMM_GPIO_INTR_CFG_INTR_DECT_CTL_EDGE_POS;
565 1.1 jmcneill pol = TLMM_GPIO_INTR_CFG_INTR_POL_CTL;
566 1.1 jmcneill } else {
567 1.1 jmcneill KASSERT((irqmode & GPIO_INTR_DOUBLE_EDGE) != 0);
568 1.1 jmcneill dect = TLMM_GPIO_INTR_CFG_INTR_DECT_CTL_EDGE_BOTH;
569 1.1 jmcneill pol = 0;
570 1.1 jmcneill }
571 1.1 jmcneill }
572 1.1 jmcneill
573 1.1 jmcneill val = RD4(sc, TLMM_GPIO_INTR_CFG(pin));
574 1.1 jmcneill val &= ~TLMM_GPIO_INTR_CFG_INTR_DECT_CTL_MASK;
575 1.1 jmcneill val |= __SHIFTIN(dect, TLMM_GPIO_INTR_CFG_INTR_DECT_CTL_MASK);
576 1.1 jmcneill val &= ~TLMM_GPIO_INTR_CFG_INTR_POL_CTL;
577 1.1 jmcneill val |= pol;
578 1.1 jmcneill val &= ~TLMM_GPIO_INTR_CFG_TARGET_PROC_MASK;
579 1.1 jmcneill val |= __SHIFTIN(TLMM_GPIO_INTR_CFG_TARGET_PROC_RPM,
580 1.1 jmcneill TLMM_GPIO_INTR_CFG_TARGET_PROC_MASK);
581 1.1 jmcneill val |= TLMM_GPIO_INTR_CFG_INTR_RAW_STATUS_EN;
582 1.1 jmcneill val |= TLMM_GPIO_INTR_CFG_INTR_ENABLE;
583 1.1 jmcneill WR4(sc, TLMM_GPIO_INTR_CFG(pin), val);
584 1.1 jmcneill
585 1.1 jmcneill mutex_exit(&sc->sc_lock);
586 1.1 jmcneill
587 1.1 jmcneill return qih;
588 1.1 jmcneill }
589 1.1 jmcneill
590 1.1 jmcneill static void
591 1.1 jmcneill qcomgpio_intr_disestablish(void *priv, void *ih)
592 1.1 jmcneill {
593 1.1 jmcneill struct qcomgpio_softc * const sc = priv;
594 1.1 jmcneill struct qcomgpio_intr_handler *qih = ih;
595 1.1 jmcneill uint32_t val;
596 1.1 jmcneill
597 1.1 jmcneill mutex_enter(&sc->sc_lock);
598 1.1 jmcneill
599 1.1 jmcneill LIST_REMOVE(qih, ih_list);
600 1.1 jmcneill
601 1.1 jmcneill val = RD4(sc, TLMM_GPIO_INTR_CFG(qih->ih_pin));
602 1.1 jmcneill val &= ~TLMM_GPIO_INTR_CFG_INTR_ENABLE;
603 1.1 jmcneill WR4(sc, TLMM_GPIO_INTR_CFG(qih->ih_pin), val);
604 1.1 jmcneill
605 1.1 jmcneill mutex_exit(&sc->sc_lock);
606 1.1 jmcneill
607 1.1 jmcneill kmem_free(qih, sizeof(*qih));
608 1.1 jmcneill }
609 1.1 jmcneill
610 1.1 jmcneill static bool
611 1.1 jmcneill qcomgpio_intr_str(void *priv, int pin, int irqmode, char *buf, size_t buflen)
612 1.1 jmcneill {
613 1.1 jmcneill struct qcomgpio_softc * const sc = priv;
614 1.1 jmcneill int rv;
615 1.1 jmcneill
616 1.1 jmcneill rv = snprintf(buf, buflen, "%s pin %d", device_xname(sc->sc_dev), pin);
617 1.1 jmcneill
618 1.1 jmcneill return rv < buflen;
619 1.1 jmcneill }
620 1.1 jmcneill
621 1.1 jmcneill static void
622 1.1 jmcneill qcomgpio_intr_mask(void *priv, void *ih)
623 1.1 jmcneill {
624 1.1 jmcneill struct qcomgpio_softc * const sc = priv;
625 1.1 jmcneill struct qcomgpio_intr_handler *qih = ih;
626 1.1 jmcneill uint32_t val;
627 1.1 jmcneill
628 1.1 jmcneill val = RD4(sc, TLMM_GPIO_INTR_CFG(qih->ih_pin));
629 1.2 jmcneill if (qih->ih_type == IST_LEVEL) {
630 1.2 jmcneill val &= ~TLMM_GPIO_INTR_CFG_INTR_RAW_STATUS_EN;
631 1.2 jmcneill }
632 1.1 jmcneill val &= ~TLMM_GPIO_INTR_CFG_INTR_ENABLE;
633 1.1 jmcneill WR4(sc, TLMM_GPIO_INTR_CFG(qih->ih_pin), val);
634 1.1 jmcneill }
635 1.1 jmcneill
636 1.1 jmcneill static void
637 1.1 jmcneill qcomgpio_intr_unmask(void *priv, void *ih)
638 1.1 jmcneill {
639 1.1 jmcneill struct qcomgpio_softc * const sc = priv;
640 1.1 jmcneill struct qcomgpio_intr_handler *qih = ih;
641 1.1 jmcneill uint32_t val;
642 1.1 jmcneill
643 1.1 jmcneill val = RD4(sc, TLMM_GPIO_INTR_CFG(qih->ih_pin));
644 1.2 jmcneill if (qih->ih_type == IST_LEVEL) {
645 1.2 jmcneill val |= TLMM_GPIO_INTR_CFG_INTR_RAW_STATUS_EN;
646 1.2 jmcneill }
647 1.1 jmcneill val |= TLMM_GPIO_INTR_CFG_INTR_ENABLE;
648 1.1 jmcneill WR4(sc, TLMM_GPIO_INTR_CFG(qih->ih_pin), val);
649 1.1 jmcneill }
650 1.1 jmcneill
651 1.1 jmcneill static int
652 1.1 jmcneill qcomgpio_intr(void *priv)
653 1.1 jmcneill {
654 1.1 jmcneill struct qcomgpio_softc * const sc = priv;
655 1.1 jmcneill struct qcomgpio_intr_handler *qih;
656 1.1 jmcneill int rv = 0;
657 1.1 jmcneill
658 1.1 jmcneill mutex_enter(&sc->sc_lock);
659 1.1 jmcneill
660 1.1 jmcneill LIST_FOREACH(qih, &sc->sc_intrs, ih_list) {
661 1.1 jmcneill const int pin = qih->ih_pin;
662 1.1 jmcneill uint32_t val;
663 1.1 jmcneill
664 1.1 jmcneill val = RD4(sc, TLMM_GPIO_INTR_STATUS(pin));
665 1.1 jmcneill if ((val & TLMM_GPIO_INTR_STATUS_INTR_STATUS) != 0) {
666 1.1 jmcneill rv |= qih->ih_func(qih->ih_arg);
667 1.1 jmcneill
668 1.1 jmcneill val &= ~TLMM_GPIO_INTR_STATUS_INTR_STATUS;
669 1.1 jmcneill WR4(sc, TLMM_GPIO_INTR_STATUS(pin), val);
670 1.1 jmcneill }
671 1.1 jmcneill }
672 1.1 jmcneill
673 1.1 jmcneill mutex_exit(&sc->sc_lock);
674 1.1 jmcneill
675 1.1 jmcneill return rv;
676 1.1 jmcneill }
677