imx_gpio.c revision 1.1.2.2 1 1.1.2.2 thorpej /* $NetBSD: imx_gpio.c,v 1.1.2.2 2021/01/03 16:34:52 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.2 thorpej __KERNEL_RCSID(0, "$NetBSD: imx_gpio.c,v 1.1.2.2 2021/01/03 16:34:52 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.2 thorpej int (*)(void *), void *);
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.2 thorpej int
81 1.1.2.2 thorpej imxgpio_match(device_t parent, cfdata_t cf, void *aux)
82 1.1.2.2 thorpej {
83 1.1.2.2 thorpej const char * const compatible[] = {
84 1.1.2.2 thorpej "fsl,imx35-gpio",
85 1.1.2.2 thorpej NULL
86 1.1.2.2 thorpej };
87 1.1.2.2 thorpej struct fdt_attach_args * const faa = aux;
88 1.1.2.2 thorpej
89 1.1.2.2 thorpej return of_match_compatible(faa->faa_phandle, compatible);
90 1.1.2.2 thorpej }
91 1.1.2.2 thorpej
92 1.1.2.2 thorpej void
93 1.1.2.2 thorpej imxgpio_attach(device_t parent, device_t self, void *aux)
94 1.1.2.2 thorpej {
95 1.1.2.2 thorpej struct imxgpio_softc * const sc = device_private(self);
96 1.1.2.2 thorpej struct fdt_attach_args * const faa = aux;
97 1.1.2.2 thorpej char intrstr[128];
98 1.1.2.2 thorpej const int phandle = faa->faa_phandle;
99 1.1.2.2 thorpej bus_space_handle_t ioh;
100 1.1.2.2 thorpej bus_addr_t addr;
101 1.1.2.2 thorpej bus_size_t size;
102 1.1.2.2 thorpej int error;
103 1.1.2.2 thorpej
104 1.1.2.2 thorpej if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
105 1.1.2.2 thorpej aprint_error(": couldn't get registers\n");
106 1.1.2.2 thorpej return;
107 1.1.2.2 thorpej }
108 1.1.2.2 thorpej
109 1.1.2.2 thorpej error = bus_space_map(faa->faa_bst, addr, size, 0, &ioh);
110 1.1.2.2 thorpej if (error) {
111 1.1.2.2 thorpej aprint_error(": couldn't map %#" PRIxBUSADDR ": %d", addr, error);
112 1.1.2.2 thorpej return;
113 1.1.2.2 thorpej }
114 1.1.2.2 thorpej
115 1.1.2.2 thorpej aprint_naive("\n");
116 1.1.2.2 thorpej aprint_normal(": GPIO (%s)\n", fdtbus_get_string(phandle, "name"));
117 1.1.2.2 thorpej
118 1.1.2.2 thorpej sc->gpio_memt = faa->faa_bst;
119 1.1.2.2 thorpej sc->gpio_memh = ioh;
120 1.1.2.2 thorpej sc->gpio_unit = -1;
121 1.1.2.2 thorpej sc->gpio_irqbase = PIC_IRQBASE_ALLOC;
122 1.1.2.2 thorpej
123 1.1.2.2 thorpej if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
124 1.1.2.2 thorpej aprint_error_dev(self, "failed to decode interrupt\n");
125 1.1.2.2 thorpej return;
126 1.1.2.2 thorpej }
127 1.1.2.2 thorpej sc->gpio_is = fdtbus_intr_establish(phandle, 0, IPL_HIGH, 0,
128 1.1.2.2 thorpej pic_handle_intr, &sc->gpio_pic);
129 1.1.2.2 thorpej if (sc->gpio_is == NULL) {
130 1.1.2.2 thorpej aprint_error_dev(self, "couldn't establish interrupt on %s\n",
131 1.1.2.2 thorpej intrstr);
132 1.1.2.2 thorpej return;
133 1.1.2.2 thorpej }
134 1.1.2.2 thorpej aprint_normal_dev(self, "interrupting on %s\n", intrstr);
135 1.1.2.2 thorpej
136 1.1.2.2 thorpej if (!fdtbus_intr_str(phandle, 1, intrstr, sizeof(intrstr))) {
137 1.1.2.2 thorpej aprint_error_dev(self, "failed to decode interrupt\n");
138 1.1.2.2 thorpej return;
139 1.1.2.2 thorpej }
140 1.1.2.2 thorpej sc->gpio_is_high = fdtbus_intr_establish(phandle, 1, IPL_HIGH, 0,
141 1.1.2.2 thorpej pic_handle_intr, &sc->gpio_pic);
142 1.1.2.2 thorpej if (sc->gpio_is_high == NULL) {
143 1.1.2.2 thorpej aprint_error_dev(self, "couldn't establish interrupt on %s\n",
144 1.1.2.2 thorpej intrstr);
145 1.1.2.2 thorpej return;
146 1.1.2.2 thorpej }
147 1.1.2.2 thorpej aprint_normal_dev(self, "interrupting on %s\n", intrstr);
148 1.1.2.2 thorpej
149 1.1.2.2 thorpej fdtbus_register_gpio_controller(self, phandle, &imx6_gpio_funcs);
150 1.1.2.2 thorpej
151 1.1.2.2 thorpej error = fdtbus_register_interrupt_controller(self, phandle,
152 1.1.2.2 thorpej &imxgpio_funcs);
153 1.1.2.2 thorpej if (error) {
154 1.1.2.2 thorpej aprint_error(": couldn't register with fdtbus: %d\n", error);
155 1.1.2.2 thorpej return;
156 1.1.2.2 thorpej }
157 1.1.2.2 thorpej
158 1.1.2.2 thorpej imxgpio_attach_common(self);
159 1.1.2.2 thorpej }
160 1.1.2.2 thorpej
161 1.1.2.2 thorpej static void *
162 1.1.2.2 thorpej imx6_gpio_fdt_acquire(device_t dev, const void *data, size_t len, int flags)
163 1.1.2.2 thorpej {
164 1.1.2.2 thorpej struct imxgpio_softc * const sc = device_private(dev);
165 1.1.2.2 thorpej struct imxgpio_pin *gpin;
166 1.1.2.2 thorpej const u_int *gpio = data;
167 1.1.2.2 thorpej
168 1.1.2.2 thorpej if (len != 12)
169 1.1.2.2 thorpej return NULL;
170 1.1.2.2 thorpej
171 1.1.2.2 thorpej const u_int pin = be32toh(gpio[1]);
172 1.1.2.2 thorpej const bool actlo = be32toh(gpio[2]) & 1;
173 1.1.2.2 thorpej
174 1.1.2.2 thorpej gpin = kmem_zalloc(sizeof(*gpin), KM_SLEEP);
175 1.1.2.2 thorpej gpin->pin_no = pin;
176 1.1.2.2 thorpej gpin->pin_flags = flags;
177 1.1.2.2 thorpej gpin->pin_actlo = actlo;
178 1.1.2.2 thorpej
179 1.1.2.2 thorpej imxgpio_pin_ctl(sc, gpin->pin_no, gpin->pin_flags);
180 1.1.2.2 thorpej
181 1.1.2.2 thorpej return gpin;
182 1.1.2.2 thorpej }
183 1.1.2.2 thorpej
184 1.1.2.2 thorpej static void
185 1.1.2.2 thorpej imx6_gpio_fdt_release(device_t dev, void *priv)
186 1.1.2.2 thorpej {
187 1.1.2.2 thorpej struct imxgpio_softc * const sc = device_private(dev);
188 1.1.2.2 thorpej struct imxgpio_pin *gpin = priv;
189 1.1.2.2 thorpej
190 1.1.2.2 thorpej imxgpio_pin_ctl(sc, gpin->pin_no, GPIO_PIN_INPUT);
191 1.1.2.2 thorpej kmem_free(gpin, sizeof(*gpin));
192 1.1.2.2 thorpej }
193 1.1.2.2 thorpej
194 1.1.2.2 thorpej static int
195 1.1.2.2 thorpej imx6_gpio_fdt_read(device_t dev, void *priv, bool raw)
196 1.1.2.2 thorpej {
197 1.1.2.2 thorpej struct imxgpio_softc * const sc = device_private(dev);
198 1.1.2.2 thorpej struct imxgpio_pin *gpin = priv;
199 1.1.2.2 thorpej int val;
200 1.1.2.2 thorpej
201 1.1.2.2 thorpej val = imxgpio_pin_read(sc, gpin->pin_no);
202 1.1.2.2 thorpej
203 1.1.2.2 thorpej if (!raw && gpin->pin_actlo)
204 1.1.2.2 thorpej val = !val;
205 1.1.2.2 thorpej
206 1.1.2.2 thorpej return val;
207 1.1.2.2 thorpej }
208 1.1.2.2 thorpej
209 1.1.2.2 thorpej static void
210 1.1.2.2 thorpej imx6_gpio_fdt_write(device_t dev, void *priv, int val, bool raw)
211 1.1.2.2 thorpej {
212 1.1.2.2 thorpej struct imxgpio_softc * const sc = device_private(dev);
213 1.1.2.2 thorpej struct imxgpio_pin *gpin = priv;
214 1.1.2.2 thorpej
215 1.1.2.2 thorpej if (!raw && gpin->pin_actlo)
216 1.1.2.2 thorpej val = !val;
217 1.1.2.2 thorpej
218 1.1.2.2 thorpej imxgpio_pin_write(sc, gpin->pin_no, val);
219 1.1.2.2 thorpej }
220 1.1.2.2 thorpej
221 1.1.2.2 thorpej static void *
222 1.1.2.2 thorpej imxgpio_establish(device_t dev, u_int *specifier, int ipl, int flags,
223 1.1.2.2 thorpej int (*func)(void *), void *arg)
224 1.1.2.2 thorpej {
225 1.1.2.2 thorpej struct imxgpio_softc * const sc = device_private(dev);
226 1.1.2.2 thorpej
227 1.1.2.2 thorpej /* 1st cell is the interrupt number */
228 1.1.2.2 thorpej /* 2nd cell is flags */
229 1.1.2.2 thorpej
230 1.1.2.2 thorpej const u_int intr = be32toh(specifier[0]);
231 1.1.2.2 thorpej const u_int trig = be32toh(specifier[1]) & 0xf;
232 1.1.2.2 thorpej u_int level;
233 1.1.2.2 thorpej
234 1.1.2.2 thorpej if ((trig & 0x1) && (trig & 0x2))
235 1.1.2.2 thorpej level = IST_EDGE_BOTH;
236 1.1.2.2 thorpej else if (trig & 0x1)
237 1.1.2.2 thorpej level = IST_EDGE_RISING;
238 1.1.2.2 thorpej else if (trig & 0x2)
239 1.1.2.2 thorpej level = IST_EDGE_FALLING;
240 1.1.2.2 thorpej else if (trig & 0x4)
241 1.1.2.2 thorpej level = IST_LEVEL_HIGH;
242 1.1.2.2 thorpej else
243 1.1.2.2 thorpej level = IST_LEVEL_LOW;
244 1.1.2.2 thorpej
245 1.1.2.2 thorpej const u_int mpsafe = (flags & FDT_INTR_MPSAFE) ? IST_MPSAFE : 0;
246 1.1.2.2 thorpej
247 1.1.2.2 thorpej aprint_debug_dev(dev, "intr establish irq %d, level %d\n",
248 1.1.2.2 thorpej sc->gpio_irqbase + intr, level);
249 1.1.2.2 thorpej return intr_establish(sc->gpio_irqbase + intr, ipl, level | mpsafe, func, arg);
250 1.1.2.2 thorpej }
251 1.1.2.2 thorpej
252 1.1.2.2 thorpej static void
253 1.1.2.2 thorpej imxgpio_disestablish(device_t dev, void *ih)
254 1.1.2.2 thorpej {
255 1.1.2.2 thorpej intr_disestablish(ih);
256 1.1.2.2 thorpej }
257 1.1.2.2 thorpej
258 1.1.2.2 thorpej static bool
259 1.1.2.2 thorpej imxgpio_intrstr(device_t dev, u_int *specifier, char *buf, size_t buflen)
260 1.1.2.2 thorpej {
261 1.1.2.2 thorpej struct imxgpio_softc * const sc = device_private(dev);
262 1.1.2.2 thorpej
263 1.1.2.2 thorpej /* 1st cell is the interrupt number */
264 1.1.2.2 thorpej /* 2nd cell is flags */
265 1.1.2.2 thorpej
266 1.1.2.2 thorpej if (!specifier)
267 1.1.2.2 thorpej return false;
268 1.1.2.2 thorpej
269 1.1.2.2 thorpej const u_int intr = be32toh(specifier[0]);
270 1.1.2.2 thorpej
271 1.1.2.2 thorpej snprintf(buf, buflen, "irq %d (gpio%d %d)",
272 1.1.2.2 thorpej sc->gpio_irqbase + intr, sc->gpio_unit, intr);
273 1.1.2.2 thorpej
274 1.1.2.2 thorpej return true;
275 1.1.2.2 thorpej }
276