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