imx_gpio.c revision 1.1.2.3 1 1.1.2.3 thorpej /* $NetBSD: imx_gpio.c,v 1.1.2.3 2021/04/03 22:28:17 thorpej Exp $ */
2 1.1.2.2 thorpej /*-
3 1.1.2.2 thorpej * Copyright (c) 2019 Genetec Corporation. All rights reserved.
4 1.1.2.2 thorpej * Written by Hashimoto Kenichi for Genetec Corporation.
5 1.1.2.2 thorpej *
6 1.1.2.2 thorpej * Redistribution and use in source and binary forms, with or without
7 1.1.2.2 thorpej * modification, are permitted provided that the following conditions
8 1.1.2.2 thorpej * are met:
9 1.1.2.2 thorpej * 1. Redistributions of source code must retain the above copyright
10 1.1.2.2 thorpej * notice, this list of conditions and the following disclaimer.
11 1.1.2.2 thorpej * 2. Redistributions in binary form must reproduce the above copyright
12 1.1.2.2 thorpej * notice, this list of conditions and the following disclaimer in the
13 1.1.2.2 thorpej * documentation and/or other materials provided with the distribution.
14 1.1.2.2 thorpej *
15 1.1.2.2 thorpej * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 1.1.2.2 thorpej * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 1.1.2.2 thorpej * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 1.1.2.2 thorpej * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 1.1.2.2 thorpej * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
20 1.1.2.2 thorpej * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 1.1.2.2 thorpej * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
22 1.1.2.2 thorpej * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 1.1.2.2 thorpej * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 1.1.2.2 thorpej * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 1.1.2.2 thorpej * SUCH DAMAGE.
26 1.1.2.2 thorpej */
27 1.1.2.2 thorpej
28 1.1.2.2 thorpej #include <sys/cdefs.h>
29 1.1.2.3 thorpej __KERNEL_RCSID(0, "$NetBSD: imx_gpio.c,v 1.1.2.3 2021/04/03 22:28:17 thorpej Exp $");
30 1.1.2.2 thorpej
31 1.1.2.2 thorpej #include "opt_fdt.h"
32 1.1.2.2 thorpej #include "gpio.h"
33 1.1.2.2 thorpej
34 1.1.2.2 thorpej #define _INTR_PRIVATE
35 1.1.2.2 thorpej
36 1.1.2.2 thorpej #include <sys/param.h>
37 1.1.2.2 thorpej #include <sys/bus.h>
38 1.1.2.2 thorpej #include <sys/device.h>
39 1.1.2.2 thorpej #include <sys/intr.h>
40 1.1.2.2 thorpej #include <sys/systm.h>
41 1.1.2.2 thorpej #include <sys/kernel.h>
42 1.1.2.2 thorpej #include <sys/kmem.h>
43 1.1.2.2 thorpej #include <sys/gpio.h>
44 1.1.2.2 thorpej
45 1.1.2.2 thorpej #include <dev/gpio/gpiovar.h>
46 1.1.2.2 thorpej
47 1.1.2.2 thorpej #include <arm/pic/picvar.h>
48 1.1.2.2 thorpej #include <arm/nxp/imx6_reg.h>
49 1.1.2.2 thorpej
50 1.1.2.2 thorpej #include <arm/imx/imxgpioreg.h>
51 1.1.2.2 thorpej #include <arm/imx/imxgpiovar.h>
52 1.1.2.2 thorpej
53 1.1.2.2 thorpej #include <dev/fdt/fdtvar.h>
54 1.1.2.2 thorpej
55 1.1.2.2 thorpej static void *imx6_gpio_fdt_acquire(device_t, const void *, size_t, int);
56 1.1.2.2 thorpej static void imx6_gpio_fdt_release(device_t, void *);
57 1.1.2.2 thorpej static int imx6_gpio_fdt_read(device_t, void *, bool);
58 1.1.2.2 thorpej static void imx6_gpio_fdt_write(device_t, void *, int, bool);
59 1.1.2.2 thorpej
60 1.1.2.2 thorpej static void *imxgpio_establish(device_t, u_int *, int, int,
61 1.1.2.3 thorpej int (*)(void *), void *, const char *);
62 1.1.2.2 thorpej static void imxgpio_disestablish(device_t, void *);
63 1.1.2.2 thorpej static bool imxgpio_intrstr(device_t, u_int *, char *, size_t);
64 1.1.2.2 thorpej
65 1.1.2.2 thorpej static struct fdtbus_interrupt_controller_func imxgpio_funcs = {
66 1.1.2.2 thorpej .establish = imxgpio_establish,
67 1.1.2.2 thorpej .disestablish = imxgpio_disestablish,
68 1.1.2.2 thorpej .intrstr = imxgpio_intrstr
69 1.1.2.2 thorpej };
70 1.1.2.2 thorpej
71 1.1.2.2 thorpej static struct fdtbus_gpio_controller_func imx6_gpio_funcs = {
72 1.1.2.2 thorpej .acquire = imx6_gpio_fdt_acquire,
73 1.1.2.2 thorpej .release = imx6_gpio_fdt_release,
74 1.1.2.2 thorpej .read = imx6_gpio_fdt_read,
75 1.1.2.2 thorpej .write = imx6_gpio_fdt_write
76 1.1.2.2 thorpej };
77 1.1.2.2 thorpej
78 1.1.2.2 thorpej const int imxgpio_ngroups = GPIO_NGROUPS;
79 1.1.2.2 thorpej
80 1.1.2.3 thorpej static const struct device_compatible_entry compat_data[] = {
81 1.1.2.3 thorpej { .compat = "fsl,imx35-gpio" },
82 1.1.2.3 thorpej DEVICE_COMPAT_EOL
83 1.1.2.3 thorpej };
84 1.1.2.3 thorpej
85 1.1.2.2 thorpej int
86 1.1.2.2 thorpej imxgpio_match(device_t parent, cfdata_t cf, void *aux)
87 1.1.2.2 thorpej {
88 1.1.2.2 thorpej struct fdt_attach_args * const faa = aux;
89 1.1.2.2 thorpej
90 1.1.2.3 thorpej return of_compatible_match(faa->faa_phandle, compat_data);
91 1.1.2.2 thorpej }
92 1.1.2.2 thorpej
93 1.1.2.2 thorpej void
94 1.1.2.2 thorpej imxgpio_attach(device_t parent, device_t self, void *aux)
95 1.1.2.2 thorpej {
96 1.1.2.2 thorpej struct imxgpio_softc * const sc = device_private(self);
97 1.1.2.2 thorpej struct fdt_attach_args * const faa = aux;
98 1.1.2.2 thorpej char intrstr[128];
99 1.1.2.2 thorpej const int phandle = faa->faa_phandle;
100 1.1.2.2 thorpej bus_space_handle_t ioh;
101 1.1.2.2 thorpej bus_addr_t addr;
102 1.1.2.2 thorpej bus_size_t size;
103 1.1.2.2 thorpej int error;
104 1.1.2.2 thorpej
105 1.1.2.2 thorpej if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
106 1.1.2.2 thorpej aprint_error(": couldn't get registers\n");
107 1.1.2.2 thorpej return;
108 1.1.2.2 thorpej }
109 1.1.2.2 thorpej
110 1.1.2.2 thorpej error = bus_space_map(faa->faa_bst, addr, size, 0, &ioh);
111 1.1.2.2 thorpej if (error) {
112 1.1.2.2 thorpej aprint_error(": couldn't map %#" PRIxBUSADDR ": %d", addr, error);
113 1.1.2.2 thorpej return;
114 1.1.2.2 thorpej }
115 1.1.2.2 thorpej
116 1.1.2.2 thorpej aprint_naive("\n");
117 1.1.2.2 thorpej aprint_normal(": GPIO (%s)\n", fdtbus_get_string(phandle, "name"));
118 1.1.2.2 thorpej
119 1.1.2.2 thorpej sc->gpio_memt = faa->faa_bst;
120 1.1.2.2 thorpej sc->gpio_memh = ioh;
121 1.1.2.2 thorpej sc->gpio_unit = -1;
122 1.1.2.2 thorpej sc->gpio_irqbase = PIC_IRQBASE_ALLOC;
123 1.1.2.2 thorpej
124 1.1.2.2 thorpej if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
125 1.1.2.2 thorpej aprint_error_dev(self, "failed to decode interrupt\n");
126 1.1.2.2 thorpej return;
127 1.1.2.2 thorpej }
128 1.1.2.3 thorpej sc->gpio_is = fdtbus_intr_establish_xname(phandle, 0, IPL_HIGH, 0,
129 1.1.2.3 thorpej pic_handle_intr, &sc->gpio_pic, device_xname(self));
130 1.1.2.2 thorpej if (sc->gpio_is == NULL) {
131 1.1.2.2 thorpej aprint_error_dev(self, "couldn't establish interrupt on %s\n",
132 1.1.2.2 thorpej intrstr);
133 1.1.2.2 thorpej return;
134 1.1.2.2 thorpej }
135 1.1.2.2 thorpej aprint_normal_dev(self, "interrupting on %s\n", intrstr);
136 1.1.2.2 thorpej
137 1.1.2.2 thorpej if (!fdtbus_intr_str(phandle, 1, intrstr, sizeof(intrstr))) {
138 1.1.2.2 thorpej aprint_error_dev(self, "failed to decode interrupt\n");
139 1.1.2.2 thorpej return;
140 1.1.2.2 thorpej }
141 1.1.2.3 thorpej sc->gpio_is_high = fdtbus_intr_establish_xname(phandle, 1, IPL_HIGH, 0,
142 1.1.2.3 thorpej pic_handle_intr, &sc->gpio_pic, device_xname(self));
143 1.1.2.2 thorpej if (sc->gpio_is_high == NULL) {
144 1.1.2.2 thorpej aprint_error_dev(self, "couldn't establish interrupt on %s\n",
145 1.1.2.2 thorpej intrstr);
146 1.1.2.2 thorpej return;
147 1.1.2.2 thorpej }
148 1.1.2.2 thorpej aprint_normal_dev(self, "interrupting on %s\n", intrstr);
149 1.1.2.2 thorpej
150 1.1.2.2 thorpej fdtbus_register_gpio_controller(self, phandle, &imx6_gpio_funcs);
151 1.1.2.2 thorpej
152 1.1.2.2 thorpej error = fdtbus_register_interrupt_controller(self, phandle,
153 1.1.2.2 thorpej &imxgpio_funcs);
154 1.1.2.2 thorpej if (error) {
155 1.1.2.2 thorpej aprint_error(": couldn't register with fdtbus: %d\n", error);
156 1.1.2.2 thorpej return;
157 1.1.2.2 thorpej }
158 1.1.2.2 thorpej
159 1.1.2.2 thorpej imxgpio_attach_common(self);
160 1.1.2.2 thorpej }
161 1.1.2.2 thorpej
162 1.1.2.2 thorpej static void *
163 1.1.2.2 thorpej imx6_gpio_fdt_acquire(device_t dev, const void *data, size_t len, int flags)
164 1.1.2.2 thorpej {
165 1.1.2.2 thorpej struct imxgpio_softc * const sc = device_private(dev);
166 1.1.2.2 thorpej struct imxgpio_pin *gpin;
167 1.1.2.2 thorpej const u_int *gpio = data;
168 1.1.2.2 thorpej
169 1.1.2.2 thorpej if (len != 12)
170 1.1.2.2 thorpej return NULL;
171 1.1.2.2 thorpej
172 1.1.2.2 thorpej const u_int pin = be32toh(gpio[1]);
173 1.1.2.2 thorpej const bool actlo = be32toh(gpio[2]) & 1;
174 1.1.2.2 thorpej
175 1.1.2.2 thorpej gpin = kmem_zalloc(sizeof(*gpin), KM_SLEEP);
176 1.1.2.2 thorpej gpin->pin_no = pin;
177 1.1.2.2 thorpej gpin->pin_flags = flags;
178 1.1.2.2 thorpej gpin->pin_actlo = actlo;
179 1.1.2.2 thorpej
180 1.1.2.2 thorpej imxgpio_pin_ctl(sc, gpin->pin_no, gpin->pin_flags);
181 1.1.2.2 thorpej
182 1.1.2.2 thorpej return gpin;
183 1.1.2.2 thorpej }
184 1.1.2.2 thorpej
185 1.1.2.2 thorpej static void
186 1.1.2.2 thorpej imx6_gpio_fdt_release(device_t dev, void *priv)
187 1.1.2.2 thorpej {
188 1.1.2.2 thorpej struct imxgpio_softc * const sc = device_private(dev);
189 1.1.2.2 thorpej struct imxgpio_pin *gpin = priv;
190 1.1.2.2 thorpej
191 1.1.2.2 thorpej imxgpio_pin_ctl(sc, gpin->pin_no, GPIO_PIN_INPUT);
192 1.1.2.2 thorpej kmem_free(gpin, sizeof(*gpin));
193 1.1.2.2 thorpej }
194 1.1.2.2 thorpej
195 1.1.2.2 thorpej static int
196 1.1.2.2 thorpej imx6_gpio_fdt_read(device_t dev, void *priv, bool raw)
197 1.1.2.2 thorpej {
198 1.1.2.2 thorpej struct imxgpio_softc * const sc = device_private(dev);
199 1.1.2.2 thorpej struct imxgpio_pin *gpin = priv;
200 1.1.2.2 thorpej int val;
201 1.1.2.2 thorpej
202 1.1.2.2 thorpej val = imxgpio_pin_read(sc, gpin->pin_no);
203 1.1.2.2 thorpej
204 1.1.2.2 thorpej if (!raw && gpin->pin_actlo)
205 1.1.2.2 thorpej val = !val;
206 1.1.2.2 thorpej
207 1.1.2.2 thorpej return val;
208 1.1.2.2 thorpej }
209 1.1.2.2 thorpej
210 1.1.2.2 thorpej static void
211 1.1.2.2 thorpej imx6_gpio_fdt_write(device_t dev, void *priv, int val, bool raw)
212 1.1.2.2 thorpej {
213 1.1.2.2 thorpej struct imxgpio_softc * const sc = device_private(dev);
214 1.1.2.2 thorpej struct imxgpio_pin *gpin = priv;
215 1.1.2.2 thorpej
216 1.1.2.2 thorpej if (!raw && gpin->pin_actlo)
217 1.1.2.2 thorpej val = !val;
218 1.1.2.2 thorpej
219 1.1.2.2 thorpej imxgpio_pin_write(sc, gpin->pin_no, val);
220 1.1.2.2 thorpej }
221 1.1.2.2 thorpej
222 1.1.2.2 thorpej static void *
223 1.1.2.2 thorpej imxgpio_establish(device_t dev, u_int *specifier, int ipl, int flags,
224 1.1.2.3 thorpej int (*func)(void *), void *arg, const char *xname)
225 1.1.2.2 thorpej {
226 1.1.2.2 thorpej struct imxgpio_softc * const sc = device_private(dev);
227 1.1.2.2 thorpej
228 1.1.2.2 thorpej /* 1st cell is the interrupt number */
229 1.1.2.2 thorpej /* 2nd cell is flags */
230 1.1.2.2 thorpej
231 1.1.2.2 thorpej const u_int intr = be32toh(specifier[0]);
232 1.1.2.2 thorpej const u_int trig = be32toh(specifier[1]) & 0xf;
233 1.1.2.2 thorpej u_int level;
234 1.1.2.2 thorpej
235 1.1.2.2 thorpej if ((trig & 0x1) && (trig & 0x2))
236 1.1.2.2 thorpej level = IST_EDGE_BOTH;
237 1.1.2.2 thorpej else if (trig & 0x1)
238 1.1.2.2 thorpej level = IST_EDGE_RISING;
239 1.1.2.2 thorpej else if (trig & 0x2)
240 1.1.2.2 thorpej level = IST_EDGE_FALLING;
241 1.1.2.2 thorpej else if (trig & 0x4)
242 1.1.2.2 thorpej level = IST_LEVEL_HIGH;
243 1.1.2.2 thorpej else
244 1.1.2.2 thorpej level = IST_LEVEL_LOW;
245 1.1.2.2 thorpej
246 1.1.2.2 thorpej const u_int mpsafe = (flags & FDT_INTR_MPSAFE) ? IST_MPSAFE : 0;
247 1.1.2.2 thorpej
248 1.1.2.2 thorpej aprint_debug_dev(dev, "intr establish irq %d, level %d\n",
249 1.1.2.2 thorpej sc->gpio_irqbase + intr, level);
250 1.1.2.3 thorpej return intr_establish_xname(sc->gpio_irqbase + intr, ipl,
251 1.1.2.3 thorpej level | mpsafe, func, arg, xname);
252 1.1.2.2 thorpej }
253 1.1.2.2 thorpej
254 1.1.2.2 thorpej static void
255 1.1.2.2 thorpej imxgpio_disestablish(device_t dev, void *ih)
256 1.1.2.2 thorpej {
257 1.1.2.2 thorpej intr_disestablish(ih);
258 1.1.2.2 thorpej }
259 1.1.2.2 thorpej
260 1.1.2.2 thorpej static bool
261 1.1.2.2 thorpej imxgpio_intrstr(device_t dev, u_int *specifier, char *buf, size_t buflen)
262 1.1.2.2 thorpej {
263 1.1.2.2 thorpej struct imxgpio_softc * const sc = device_private(dev);
264 1.1.2.2 thorpej
265 1.1.2.2 thorpej /* 1st cell is the interrupt number */
266 1.1.2.2 thorpej /* 2nd cell is flags */
267 1.1.2.2 thorpej
268 1.1.2.2 thorpej if (!specifier)
269 1.1.2.2 thorpej return false;
270 1.1.2.2 thorpej
271 1.1.2.2 thorpej const u_int intr = be32toh(specifier[0]);
272 1.1.2.2 thorpej
273 1.1.2.2 thorpej snprintf(buf, buflen, "irq %d (gpio%d %d)",
274 1.1.2.2 thorpej sc->gpio_irqbase + intr, sc->gpio_unit, intr);
275 1.1.2.2 thorpej
276 1.1.2.2 thorpej return true;
277 1.1.2.2 thorpej }
278