imx23_pinctrl.c revision 1.6 1 1.6 thorpej /* $Id: imx23_pinctrl.c,v 1.6 2021/08/07 16:18:44 thorpej Exp $ */
2 1.1 matt
3 1.1 matt /*
4 1.1 matt * Copyright (c) 2013 The NetBSD Foundation, Inc.
5 1.1 matt * All rights reserved.
6 1.1 matt *
7 1.1 matt * This code is derived from software contributed to The NetBSD Foundation
8 1.1 matt * by Petri Laakso.
9 1.1 matt *
10 1.1 matt * Redistribution and use in source and binary forms, with or without
11 1.1 matt * modification, are permitted provided that the following conditions
12 1.1 matt * are met:
13 1.1 matt * 1. Redistributions of source code must retain the above copyright
14 1.1 matt * notice, this list of conditions and the following disclaimer.
15 1.1 matt * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 matt * notice, this list of conditions and the following disclaimer in the
17 1.1 matt * documentation and/or other materials provided with the distribution.
18 1.1 matt *
19 1.1 matt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 matt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 matt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 matt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 matt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 matt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 matt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 matt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 matt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 matt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 matt * POSSIBILITY OF SUCH DAMAGE.
30 1.1 matt */
31 1.1 matt
32 1.1 matt #include <sys/param.h>
33 1.1 matt #include <sys/types.h>
34 1.1 matt #include <sys/bus.h>
35 1.1 matt #include <sys/cdefs.h>
36 1.1 matt #include <sys/device.h>
37 1.1 matt #include <sys/errno.h>
38 1.1 matt #include <sys/gpio.h>
39 1.1 matt
40 1.1 matt #include <dev/gpio/gpiovar.h>
41 1.1 matt
42 1.1 matt #include <arm/imx/imx23_pinctrlreg.h>
43 1.1 matt #include <arm/imx/imx23_pinctrlvar.h>
44 1.1 matt #include <arm/imx/imx23var.h>
45 1.1 matt
46 1.1 matt #define GPIO_PINS 96
47 1.1 matt
48 1.4 skrll typedef struct imx23_pinctrl_softc {
49 1.1 matt device_t sc_dev;
50 1.1 matt bus_space_tag_t sc_iot;
51 1.1 matt bus_space_handle_t sc_hdl;
52 1.1 matt struct gpio_chipset_tag gc;
53 1.1 matt gpio_pin_t pins[GPIO_PINS];
54 1.4 skrll } *imx23_pinctrl_softc_t;
55 1.1 matt
56 1.4 skrll static int imx23_pinctrl_match(device_t, cfdata_t, void *);
57 1.4 skrll static void imx23_pinctrl_attach(device_t, device_t, void *);
58 1.4 skrll static int imx23_pinctrl_activate(device_t, enum devact);
59 1.1 matt
60 1.1 matt #if notyet
61 1.4 skrll static void imx23_pinctrl_reset(struct imx23_pinctrl_softc *);
62 1.1 matt #endif
63 1.4 skrll static void imx23_pinctrl_init(struct imx23_pinctrl_softc *);
64 1.1 matt
65 1.4 skrll static int imx23_pinctrl_gp_gc_open(void *, device_t);
66 1.4 skrll static void imx23_pinctrl_gp_gc_close(void *, device_t);
67 1.4 skrll static int imx23_pinctrl_gp_pin_read(void *, int);
68 1.4 skrll static void imx23_pinctrl_gp_pin_write(void *, int, int);
69 1.4 skrll static void imx23_pinctrl_gp_pin_ctl(void *, int, int);
70 1.4 skrll
71 1.4 skrll static imx23_pinctrl_softc_t _sc = NULL;
72 1.4 skrll
73 1.4 skrll CFATTACH_DECL3_NEW(imx23_pinctrl,
74 1.4 skrll sizeof(struct imx23_pinctrl_softc),
75 1.4 skrll imx23_pinctrl_match,
76 1.4 skrll imx23_pinctrl_attach,
77 1.1 matt NULL,
78 1.4 skrll imx23_pinctrl_activate,
79 1.1 matt NULL,
80 1.1 matt NULL,
81 1.1 matt 0
82 1.1 matt );
83 1.1 matt
84 1.1 matt #define GPIO_PIN_CAP (GPIO_PIN_INPUT | GPIO_PIN_OUTPUT | GPIO_PIN_INOUT | \
85 1.1 matt GPIO_PIN_PULLUP | GPIO_PIN_SET)
86 1.1 matt
87 1.1 matt /*
88 1.1 matt * Supported capabilities for each GPIO pin.
89 1.1 matt */
90 1.1 matt const static int pin_caps[GPIO_PINS] = {
91 1.1 matt /*
92 1.1 matt * HW_PINCTRL_MUXSEL0
93 1.1 matt */
94 1.1 matt /* PIN 0 */
95 1.1 matt GPIO_PIN_CAP,
96 1.1 matt /* PIN 1 */
97 1.1 matt GPIO_PIN_CAP,
98 1.1 matt /* PIN 2 */
99 1.1 matt GPIO_PIN_CAP,
100 1.1 matt /* PIN 3 */
101 1.1 matt GPIO_PIN_CAP,
102 1.1 matt /* PIN 4 */
103 1.1 matt GPIO_PIN_CAP,
104 1.1 matt /* PIN 5 */
105 1.1 matt GPIO_PIN_CAP,
106 1.1 matt /* PIN 6 */
107 1.1 matt GPIO_PIN_CAP,
108 1.1 matt /* PIN 7 */
109 1.1 matt GPIO_PIN_CAP,
110 1.1 matt /* PIN 8 */
111 1.1 matt GPIO_PIN_CAP,
112 1.1 matt /* PIN 9 */
113 1.1 matt GPIO_PIN_CAP,
114 1.1 matt /* PIN 10 */
115 1.1 matt GPIO_PIN_CAP,
116 1.1 matt /* PIN 11 */
117 1.1 matt GPIO_PIN_CAP,
118 1.1 matt /* PIN 12 */
119 1.1 matt GPIO_PIN_CAP,
120 1.1 matt /* PIN 13 */
121 1.1 matt GPIO_PIN_CAP,
122 1.1 matt /* PIN 14 */
123 1.1 matt GPIO_PIN_CAP,
124 1.1 matt /* PIN 15 */
125 1.1 matt GPIO_PIN_CAP,
126 1.1 matt /*
127 1.1 matt * HW_PINCTRL_MUXSEL1
128 1.1 matt */
129 1.1 matt /* PIN 16 */
130 1.1 matt GPIO_PIN_CAP,
131 1.1 matt /* PIN 17 */
132 1.1 matt 0, /* Reserved for powering OLinuXino MAXI/MINI USB hub. */
133 1.1 matt /* PIN 18 */
134 1.1 matt GPIO_PIN_CAP,
135 1.1 matt /* PIN 19 */
136 1.1 matt GPIO_PIN_CAP,
137 1.1 matt /* PIN 20 */
138 1.1 matt GPIO_PIN_CAP,
139 1.1 matt /* PIN 21 */
140 1.1 matt GPIO_PIN_CAP,
141 1.1 matt /* PIN 22 */
142 1.1 matt GPIO_PIN_CAP,
143 1.1 matt /* PIN 23 */
144 1.1 matt GPIO_PIN_CAP,
145 1.1 matt /* PIN 24 */
146 1.1 matt GPIO_PIN_CAP,
147 1.1 matt /* PIN 25 */
148 1.1 matt GPIO_PIN_CAP,
149 1.1 matt /* PIN 26 */
150 1.1 matt GPIO_PIN_CAP,
151 1.1 matt /* PIN 27 */
152 1.1 matt GPIO_PIN_CAP,
153 1.1 matt /* PIN 28 */
154 1.1 matt GPIO_PIN_CAP,
155 1.1 matt /* PIN 29 */
156 1.1 matt GPIO_PIN_CAP,
157 1.1 matt /* PIN 30 */
158 1.1 matt GPIO_PIN_CAP,
159 1.1 matt /* PIN 31 */
160 1.1 matt GPIO_PIN_CAP,
161 1.1 matt /*
162 1.1 matt * HW_PINCTRL_MUXSEL2
163 1.1 matt */
164 1.1 matt /* PIN 32 */
165 1.1 matt GPIO_PIN_CAP,
166 1.1 matt /* PIN 33 */
167 1.1 matt GPIO_PIN_CAP,
168 1.1 matt /* PIN 34 */
169 1.1 matt GPIO_PIN_CAP,
170 1.1 matt /* PIN 35 */
171 1.1 matt GPIO_PIN_CAP,
172 1.1 matt /* PIN 36 */
173 1.1 matt GPIO_PIN_CAP,
174 1.1 matt /* PIN 37 */
175 1.1 matt GPIO_PIN_CAP,
176 1.1 matt /* PIN 38 */
177 1.1 matt GPIO_PIN_CAP,
178 1.1 matt /* PIN 39 */
179 1.1 matt GPIO_PIN_CAP,
180 1.1 matt /* PIN 40 */
181 1.1 matt GPIO_PIN_CAP,
182 1.1 matt /* PIN 41 */
183 1.1 matt GPIO_PIN_CAP,
184 1.1 matt /* PIN 42 */
185 1.1 matt GPIO_PIN_CAP,
186 1.1 matt /* PIN 43 */
187 1.1 matt GPIO_PIN_CAP,
188 1.1 matt /* PIN 44 */
189 1.1 matt GPIO_PIN_CAP,
190 1.1 matt /* PIN 45 */
191 1.1 matt GPIO_PIN_CAP,
192 1.1 matt /* PIN 46 */
193 1.1 matt GPIO_PIN_CAP,
194 1.1 matt /* PIN 47 */
195 1.1 matt GPIO_PIN_CAP,
196 1.1 matt /*
197 1.1 matt * HW_PINCTRL_MUXSEL3
198 1.1 matt */
199 1.1 matt /* PIN 48 */
200 1.1 matt GPIO_PIN_CAP,
201 1.1 matt /* PIN 49 */
202 1.1 matt GPIO_PIN_CAP,
203 1.1 matt /* PIN 50 */
204 1.1 matt GPIO_PIN_CAP,
205 1.1 matt /* PIN 51 */
206 1.1 matt GPIO_PIN_CAP,
207 1.1 matt /* PIN 52 */
208 1.1 matt GPIO_PIN_CAP,
209 1.1 matt /* PIN 53 */
210 1.1 matt GPIO_PIN_CAP,
211 1.1 matt /* PIN 54 */
212 1.1 matt GPIO_PIN_CAP,
213 1.1 matt /* PIN 55 */
214 1.1 matt GPIO_PIN_CAP,
215 1.1 matt /* PIN 56 */
216 1.1 matt GPIO_PIN_CAP,
217 1.1 matt /* PIN 57 */
218 1.1 matt GPIO_PIN_CAP,
219 1.1 matt /* PIN 58 */
220 1.1 matt GPIO_PIN_CAP,
221 1.1 matt /* PIN 59 */
222 1.1 matt GPIO_PIN_CAP,
223 1.1 matt /* PIN 60 */
224 1.1 matt GPIO_PIN_CAP,
225 1.1 matt /* PIN 61 */
226 1.1 matt GPIO_PIN_CAP,
227 1.1 matt /* PIN 62 */
228 1.1 matt GPIO_PIN_CAP,
229 1.1 matt /* PIN 63 */
230 1.1 matt 0, /* Reserved. */
231 1.1 matt /*
232 1.1 matt * HW_PINCTRL_MUXSEL4
233 1.1 matt */
234 1.1 matt /* PIN 64 */
235 1.1 matt GPIO_PIN_CAP,
236 1.1 matt /* PIN 65 */
237 1.1 matt GPIO_PIN_CAP,
238 1.1 matt /* PIN 66 */
239 1.1 matt GPIO_PIN_CAP,
240 1.1 matt /* PIN 67 */
241 1.1 matt GPIO_PIN_CAP,
242 1.1 matt /* PIN 68 */
243 1.1 matt GPIO_PIN_CAP,
244 1.1 matt /* PIN 69 */
245 1.1 matt GPIO_PIN_CAP,
246 1.1 matt /* PIN 70 */
247 1.1 matt GPIO_PIN_CAP,
248 1.1 matt /* PIN 71 */
249 1.1 matt GPIO_PIN_CAP,
250 1.1 matt /* PIN 72 */
251 1.1 matt GPIO_PIN_CAP,
252 1.1 matt /* PIN 73 */
253 1.1 matt 0, /* From this on reserved for EMI (DRAM) pins. */
254 1.1 matt /* PIN 74 */
255 1.1 matt 0,
256 1.1 matt /* PIN 75 */
257 1.1 matt 0,
258 1.1 matt /* PIN 76 */
259 1.1 matt 0,
260 1.1 matt /* PIN 77 */
261 1.1 matt 0,
262 1.1 matt /* PIN 78 */
263 1.1 matt 0,
264 1.1 matt /* PIN 79 */
265 1.1 matt 0,
266 1.1 matt /*
267 1.1 matt * HW_PINCTRL_MUXSEL5
268 1.1 matt */
269 1.1 matt /* PIN 80 */
270 1.1 matt 0,
271 1.1 matt /* PIN 81 */
272 1.1 matt 0,
273 1.1 matt /* PIN 82 */
274 1.1 matt 0,
275 1.1 matt /* PIN 83 */
276 1.1 matt 0,
277 1.1 matt /* PIN 84 */
278 1.1 matt 0,
279 1.1 matt /* PIN 85 */
280 1.1 matt 0,
281 1.1 matt /* PIN 86 */
282 1.1 matt 0,
283 1.1 matt /* PIN 87 */
284 1.1 matt 0,
285 1.1 matt /* PIN 88 */
286 1.1 matt 0,
287 1.1 matt /* PIN 89 */
288 1.1 matt 0,
289 1.1 matt /* PIN 90 */
290 1.1 matt 0,
291 1.1 matt /* PIN 91 */
292 1.1 matt 0,
293 1.1 matt /* PIN 92 */
294 1.1 matt 0,
295 1.1 matt /* PIN 93 */
296 1.1 matt 0,
297 1.1 matt /* PIN 94 */
298 1.1 matt 0,
299 1.1 matt /* PIN 95 */
300 1.1 matt 0
301 1.1 matt };
302 1.1 matt
303 1.1 matt #define PINCTRL_RD(sc, reg) \
304 1.1 matt bus_space_read_4(sc->sc_iot, sc->sc_hdl, (reg))
305 1.1 matt #define PINCTRL_WR(sc, reg, val) \
306 1.1 matt bus_space_write_4(sc->sc_iot, sc->sc_hdl, (reg), (val))
307 1.1 matt
308 1.1 matt /*
309 1.1 matt * Macros to map pin numbers to registers and bit fields.
310 1.1 matt */
311 1.1 matt #define MUXSEL_REG_SIZE 0x10
312 1.1 matt #define PIN2MUXSEL_REG(pin) \
313 1.1 matt ((pin / 16) * MUXSEL_REG_SIZE + HW_PINCTRL_MUXSEL0)
314 1.1 matt #define PIN2MUXSEL_SET_REG(pin) \
315 1.1 matt ((pin / 16) * MUXSEL_REG_SIZE + HW_PINCTRL_MUXSEL0_SET)
316 1.1 matt #define PIN2MUXSEL_CLR_REG(pin) \
317 1.1 matt ((pin / 16) * MUXSEL_REG_SIZE + HW_PINCTRL_MUXSEL0_CLR)
318 1.1 matt #define PIN2MUXSEL_MASK(pin) (3<<(pin % 16 * 2))
319 1.1 matt
320 1.1 matt #define DRIVE_REG_SIZE 0x10
321 1.1 matt #define PIN2DRIVE_REG(pin) \
322 1.1 matt ((pin / 8) * DRIVE_REG_SIZE + HW_PINCTRL_DRIVE0)
323 1.1 matt #define PIN2DRIVE_SET_REG(pin) \
324 1.1 matt ((pin / 8) * DRIVE_REG_SIZE + HW_PINCTRL_DRIVE0_SET)
325 1.1 matt #define PIN2DRIVE_CLR_REG(pin) \
326 1.1 matt ((pin / 8) * DRIVE_REG_SIZE + HW_PINCTRL_DRIVE0_CLR)
327 1.1 matt #define PIN2DRIVE_MASK(pin) (3<<(pin % 8 * 4))
328 1.1 matt
329 1.1 matt #define PULL_REG_SIZE 0x10
330 1.1 matt #define PIN2PULL_REG(pin) \
331 1.1 matt ((pin / 32) * PULL_REG_SIZE + HW_PINCTRL_PULL0)
332 1.1 matt #define PIN2PULL_SET_REG(pin) \
333 1.1 matt ((pin / 32) * PULL_REG_SIZE + HW_PINCTRL_PULL0_SET)
334 1.1 matt #define PIN2PULL_CLR_REG(pin) \
335 1.1 matt ((pin / 32) * PULL_REG_SIZE + HW_PINCTRL_PULL0_CLR)
336 1.1 matt #define PIN2PULL_MASK(pin) (1<<(pin % 32))
337 1.1 matt
338 1.1 matt #define DOUT_REG_SIZE 0x10
339 1.1 matt #define PIN2DOUT_REG(pin) \
340 1.1 matt ((pin / 32) * DOUT_REG_SIZE + HW_PINCTRL_DOUT0)
341 1.1 matt #define PIN2DOUT_SET_REG(pin) \
342 1.1 matt ((pin / 32) * DOUT_REG_SIZE + HW_PINCTRL_DOUT0_SET)
343 1.1 matt #define PIN2DOUT_CLR_REG(pin) \
344 1.1 matt ((pin / 32) * DOUT_REG_SIZE + HW_PINCTRL_DOUT0_CLR)
345 1.1 matt #define PIN2DOUT_MASK(pin) (1<<(pin % 32))
346 1.1 matt
347 1.1 matt #define DIN_REG_SIZE 0x10
348 1.1 matt #define PIN2DIN_REG(pin) ((pin / 32) * DIN_REG_SIZE + HW_PINCTRL_DIN0)
349 1.1 matt #define PIN2DIN_MASK(pin) (1<<(pin % 32))
350 1.1 matt
351 1.1 matt #define DOE_REG_SIZE 0x10
352 1.1 matt #define PIN2DOE_REG(pin) \
353 1.1 matt ((pin / 32) * DOE_REG_SIZE + HW_PINCTRL_DOE0)
354 1.1 matt #define PIN2DOE_SET_REG(pin) \
355 1.1 matt ((pin / 32) * DOE_REG_SIZE + HW_PINCTRL_DOE0_SET)
356 1.1 matt #define PIN2DOE_CLR_REG(pin) \
357 1.1 matt ((pin / 32) * DOE_REG_SIZE + HW_PINCTRL_DOE0_CLR)
358 1.1 matt #define PIN2DOE_MASK(pin) (1<<(pin % 32))
359 1.1 matt
360 1.1 matt #define DRIVE_STRENGTH_4MA 0
361 1.1 matt #define DRIVE_STRENGTH_8MA 1
362 1.1 matt #define DRIVE_STRENGTH_12MA 2
363 1.1 matt
364 1.1 matt #define MUXEL_GPIO_MODE 3
365 1.1 matt
366 1.1 matt #define PINCTRL_SOFT_RST_LOOP 455 /* At least 1 us ... */
367 1.1 matt
368 1.1 matt static int
369 1.4 skrll imx23_pinctrl_match(device_t parent, cfdata_t match, void *aux)
370 1.1 matt {
371 1.1 matt struct apb_attach_args *aa = aux;
372 1.1 matt
373 1.1 matt if ((aa->aa_addr == HW_PINCTRL_BASE) &&
374 1.1 matt (aa->aa_size == HW_PINCTRL_SIZE))
375 1.1 matt return 1;
376 1.1 matt
377 1.1 matt return 0;
378 1.1 matt }
379 1.1 matt
380 1.1 matt static void
381 1.4 skrll imx23_pinctrl_attach(device_t parent, device_t self, void *aux)
382 1.1 matt {
383 1.4 skrll struct imx23_pinctrl_softc *sc = device_private(self);
384 1.1 matt struct apb_attach_args *aa = aux;
385 1.4 skrll static int imx23_pinctrl_attached = 0;
386 1.1 matt
387 1.1 matt sc->sc_dev = self;
388 1.1 matt sc->sc_iot = aa->aa_iot;
389 1.3 skrll
390 1.4 skrll if (imx23_pinctrl_attached) {
391 1.1 matt aprint_error_dev(sc->sc_dev, "already attached\n");
392 1.1 matt return;
393 1.1 matt }
394 1.1 matt
395 1.1 matt if (bus_space_map(sc->sc_iot, aa->aa_addr, aa->aa_size, 0,
396 1.4 skrll &sc->sc_hdl)) {
397 1.1 matt aprint_error_dev(sc->sc_dev, "Unable to map bus space\n");
398 1.1 matt return;
399 1.1 matt }
400 1.1 matt
401 1.1 matt #if notyet
402 1.4 skrll imx23_pinctrl_reset(sc);
403 1.1 matt #endif
404 1.1 matt
405 1.4 skrll imx23_pinctrl_init(sc);
406 1.1 matt
407 1.1 matt aprint_normal(": PIN MUX & GPIO\n");
408 1.1 matt
409 1.1 matt /* Set pin capabilities. */
410 1.1 matt int i;
411 1.1 matt for(i = 0; i < GPIO_PINS; i++) {
412 1.1 matt sc->pins[i].pin_caps = pin_caps[i];
413 1.1 matt }
414 1.1 matt
415 1.4 skrll imx23_pinctrl_attached = 1;
416 1.1 matt
417 1.1 matt sc->gc.gp_cookie = sc;
418 1.4 skrll sc->gc.gp_gc_open = imx23_pinctrl_gp_gc_open;
419 1.4 skrll sc->gc.gp_gc_close = imx23_pinctrl_gp_gc_close;
420 1.4 skrll sc->gc.gp_pin_read = imx23_pinctrl_gp_pin_read;
421 1.4 skrll sc->gc.gp_pin_write = imx23_pinctrl_gp_pin_write;
422 1.4 skrll sc->gc.gp_pin_ctl = imx23_pinctrl_gp_pin_ctl;
423 1.1 matt
424 1.1 matt struct gpiobus_attach_args gpiobus_aa;
425 1.1 matt gpiobus_aa.gba_gc = &sc->gc;
426 1.1 matt gpiobus_aa.gba_npins = GPIO_PINS;
427 1.1 matt gpiobus_aa.gba_pins = sc->pins;
428 1.1 matt
429 1.6 thorpej config_found(self, &gpiobus_aa, gpiobus_print, CFARGS_NONE);
430 1.3 skrll
431 1.1 matt return;
432 1.1 matt }
433 1.1 matt
434 1.1 matt static int
435 1.4 skrll imx23_pinctrl_activate(device_t self, enum devact act)
436 1.1 matt {
437 1.1 matt
438 1.1 matt return EOPNOTSUPP;
439 1.1 matt }
440 1.1 matt
441 1.3 skrll static void
442 1.4 skrll imx23_pinctrl_init(struct imx23_pinctrl_softc *sc)
443 1.1 matt {
444 1.1 matt _sc = sc;
445 1.1 matt return;
446 1.1 matt }
447 1.1 matt
448 1.1 matt #if notyet
449 1.1 matt /*
450 1.1 matt * Inspired by i.MX23 RM "39.3.10 Correct Way to Soft Reset a Block"
451 1.1 matt */
452 1.1 matt static void
453 1.4 skrll imx23_pinctrl_reset(struct imx23_pinctrl_softc *sc)
454 1.1 matt {
455 1.1 matt unsigned int loop;
456 1.1 matt
457 1.1 matt /* Prepare for soft-reset by making sure that SFTRST is not currently
458 1.1 matt * asserted. Also clear CLKGATE so we can wait for its assertion below.
459 1.1 matt */
460 1.1 matt PINCTRL_WR(sc, HW_PINCTRL_CTRL_CLR, HW_PINCTRL_CTRL_SFTRST);
461 1.1 matt
462 1.1 matt /* Wait at least a microsecond for SFTRST to deassert. */
463 1.1 matt loop = 0;
464 1.1 matt while ((PINCTRL_RD(sc, HW_PINCTRL_CTRL) & HW_PINCTRL_CTRL_SFTRST) ||
465 1.1 matt (loop < PINCTRL_SOFT_RST_LOOP))
466 1.1 matt loop++;
467 1.1 matt
468 1.1 matt /* Clear CLKGATE so we can wait for its assertion below. */
469 1.1 matt PINCTRL_WR(sc, HW_PINCTRL_CTRL_CLR, HW_PINCTRL_CTRL_CLKGATE);
470 1.1 matt
471 1.1 matt /* Soft-reset the block. */
472 1.1 matt PINCTRL_WR(sc, HW_PINCTRL_CTRL_SET, HW_PINCTRL_CTRL_SFTRST);
473 1.1 matt
474 1.1 matt /* Wait until clock is in the gated state. */
475 1.1 matt while (!(PINCTRL_RD(sc, HW_PINCTRL_CTRL) & HW_PINCTRL_CTRL_CLKGATE));
476 1.1 matt
477 1.1 matt /* Bring block out of reset. */
478 1.1 matt PINCTRL_WR(sc, HW_PINCTRL_CTRL_CLR, HW_PINCTRL_CTRL_SFTRST);
479 1.1 matt
480 1.1 matt loop = 0;
481 1.1 matt while ((PINCTRL_RD(sc, HW_PINCTRL_CTRL) & HW_PINCTRL_CTRL_SFTRST) ||
482 1.1 matt (loop < PINCTRL_SOFT_RST_LOOP))
483 1.1 matt loop++;
484 1.1 matt
485 1.1 matt PINCTRL_WR(sc, HW_PINCTRL_CTRL_CLR, HW_PINCTRL_CTRL_CLKGATE);
486 1.1 matt
487 1.1 matt /* Wait until clock is in the NON-gated state. */
488 1.1 matt while (PINCTRL_RD(sc, HW_PINCTRL_CTRL) & HW_PINCTRL_CTRL_CLKGATE);
489 1.1 matt
490 1.1 matt return;
491 1.1 matt }
492 1.1 matt #endif
493 1.1 matt
494 1.1 matt /*
495 1.1 matt * Enable external USB transceiver/HUB.
496 1.1 matt *
497 1.1 matt * PIN18/LCD_D17/USB_EN controls reset line of external USB chip on MINI and
498 1.1 matt * MAXI boards. We configure this pin to logic 1.
499 1.1 matt */
500 1.1 matt void
501 1.4 skrll imx23_pinctrl_en_usb(void)
502 1.1 matt {
503 1.4 skrll struct imx23_pinctrl_softc *sc = _sc;
504 1.1 matt
505 1.1 matt if (sc == NULL) {
506 1.4 skrll aprint_error("imx23_pinctrl is not initialized");
507 1.1 matt return;
508 1.1 matt }
509 1.1 matt
510 1.4 skrll imx23_pinctrl_gp_pin_ctl(sc, 17, GPIO_PIN_OUTPUT);
511 1.1 matt delay(1000);
512 1.4 skrll imx23_pinctrl_gp_pin_write(sc, 17, 1);
513 1.1 matt
514 1.1 matt return;
515 1.1 matt }
516 1.1 matt
517 1.1 matt static int
518 1.4 skrll imx23_pinctrl_gp_gc_open(void *cookie, device_t dev)
519 1.1 matt {
520 1.1 matt return 0;
521 1.1 matt }
522 1.1 matt
523 1.1 matt static void
524 1.4 skrll imx23_pinctrl_gp_gc_close(void *cookie, device_t dev)
525 1.1 matt {
526 1.1 matt return;
527 1.1 matt }
528 1.1 matt
529 1.1 matt static int
530 1.4 skrll imx23_pinctrl_gp_pin_read(void *cookie, int pin)
531 1.1 matt {
532 1.1 matt int value;
533 1.4 skrll imx23_pinctrl_softc_t sc = (imx23_pinctrl_softc_t) cookie;
534 1.1 matt
535 1.1 matt if (PINCTRL_RD(sc, PIN2DIN_REG(pin)) & PIN2DIN_MASK(pin))
536 1.1 matt value = 1;
537 1.1 matt else
538 1.1 matt value = 0;
539 1.3 skrll
540 1.1 matt return value;
541 1.1 matt }
542 1.1 matt
543 1.1 matt static void
544 1.4 skrll imx23_pinctrl_gp_pin_write(void *cookie, int pin, int value)
545 1.1 matt {
546 1.4 skrll imx23_pinctrl_softc_t sc = (imx23_pinctrl_softc_t) cookie;
547 1.1 matt
548 1.1 matt if (value)
549 1.1 matt PINCTRL_WR(sc, PIN2DOUT_SET_REG(pin), PIN2DOUT_MASK(pin));
550 1.1 matt else
551 1.1 matt PINCTRL_WR(sc, PIN2DOUT_CLR_REG(pin), PIN2DOUT_MASK(pin));
552 1.1 matt
553 1.1 matt return;
554 1.1 matt }
555 1.1 matt
556 1.1 matt /*
557 1.1 matt * Configure pin as requested in flags.
558 1.1 matt */
559 1.1 matt static void
560 1.4 skrll imx23_pinctrl_gp_pin_ctl(void *cookie, int pin, int flags)
561 1.1 matt {
562 1.4 skrll imx23_pinctrl_softc_t sc = (imx23_pinctrl_softc_t) cookie;
563 1.1 matt uint32_t tmpr;
564 1.1 matt
565 1.1 matt /* Enable GPIO pin. */
566 1.1 matt tmpr = PINCTRL_RD(sc, PIN2MUXSEL_REG(pin));
567 1.1 matt tmpr &= ~PIN2MUXSEL_MASK(pin);
568 1.1 matt tmpr |= __SHIFTIN(MUXEL_GPIO_MODE, PIN2MUXSEL_MASK(pin));
569 1.1 matt PINCTRL_WR(sc, PIN2MUXSEL_REG(pin), tmpr);
570 1.1 matt
571 1.1 matt /* Configure pin drive strength. */
572 1.1 matt tmpr = PINCTRL_RD(sc, PIN2DRIVE_REG(pin));
573 1.1 matt tmpr &= ~PIN2DRIVE_MASK(pin);
574 1.1 matt tmpr |= __SHIFTIN(DRIVE_STRENGTH_4MA, PIN2DRIVE_MASK(pin));
575 1.1 matt PINCTRL_WR(sc, PIN2DRIVE_REG(pin), tmpr);
576 1.1 matt
577 1.1 matt if ((flags & (GPIO_PIN_OUTPUT | GPIO_PIN_INOUT))) {
578 1.1 matt /* Configure pullup resistor or gate keeper. */
579 1.1 matt if (flags & GPIO_PIN_PULLUP)
580 1.1 matt PINCTRL_WR(sc, PIN2PULL_SET_REG(pin),
581 1.1 matt PIN2PULL_MASK(pin));
582 1.1 matt else
583 1.1 matt PINCTRL_WR(sc, PIN2PULL_CLR_REG(pin),
584 1.1 matt PIN2PULL_MASK(pin));
585 1.1 matt
586 1.1 matt /* Set initial pin value to logic zero. */
587 1.1 matt PINCTRL_WR(sc, PIN2DOUT_CLR_REG(pin), PIN2DOUT_MASK(pin));
588 1.1 matt
589 1.1 matt /* Enable pin output. */
590 1.1 matt PINCTRL_WR(sc, PIN2DOE_SET_REG(pin), PIN2DOE_MASK(pin));
591 1.1 matt }
592 1.1 matt
593 1.1 matt if (flags & GPIO_PIN_INPUT) {
594 1.1 matt /* Disable pin output. */
595 1.1 matt PINCTRL_WR(sc, PIN2DOE_CLR_REG(pin), PIN2DOE_MASK(pin));
596 1.1 matt
597 1.1 matt /* Configure pullup resistor or gate keeper. */
598 1.1 matt if (flags & GPIO_PIN_PULLUP)
599 1.1 matt PINCTRL_WR(sc, PIN2PULL_SET_REG(pin),
600 1.1 matt PIN2PULL_MASK(pin));
601 1.1 matt else
602 1.1 matt PINCTRL_WR(sc, PIN2PULL_CLR_REG(pin),
603 1.1 matt PIN2PULL_MASK(pin));
604 1.1 matt }
605 1.1 matt
606 1.1 matt return;
607 1.1 matt }
608