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