1 /*-
2 * Copyright (c) 2010-2011, Aleksandr Rybalko <ray@ddteam.net>
3 * Copyright (c) 2009, Oleksandr Tymoshenko <gonzo@FreeBSD.org>
4 * Copyright (c) 2009, Luiz Otavio O Souza.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions, and the following
12 * disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30 /*
31 * GPIO driver for RT305X SoC.
32 */
33
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/bus.h>
40
41 #include <sys/kernel.h>
42 #include <sys/module.h>
43 #include <sys/rman.h>
44 #include <sys/lock.h>
45 #include <sys/mutex.h>
46 #include <sys/gpio.h>
47
48 #include <machine/bus.h>
49 #include <machine/resource.h>
50 #include <mips/rt305x/rt305xreg.h>
51 #include <mips/rt305x/rt305x_gpio.h>
52 #include <mips/rt305x/rt305x_gpiovar.h>
53 #include <mips/rt305x/rt305x_sysctlvar.h>
54 #include <dev/gpio/gpiobusvar.h>
55
56 #include "gpio_if.h"
57
58 #ifdef notyet
59 #define DEFAULT_CAPS (GPIO_PIN_INPUT | GPIO_PIN_OUTPUT | GPIO_PIN_INVIN | \
60 GPIO_PIN_INVOUT | GPIO_PIN_REPORT )
61 #else
62 #define DEFAULT_CAPS (GPIO_PIN_INPUT | GPIO_PIN_OUTPUT | GPIO_PIN_INVIN | \
63 GPIO_PIN_INVOUT )
64 #endif
65
66 /*
67 * Helpers
68 */
69 static void rt305x_gpio_pin_configure(struct rt305x_gpio_softc *sc,
70 struct gpio_pin *pin, uint32_t flags);
71
72 /*
73 * Driver stuff
74 */
75 static int rt305x_gpio_probe(device_t dev);
76 static int rt305x_gpio_attach(device_t dev);
77 static int rt305x_gpio_detach(device_t dev);
78 static int rt305x_gpio_intr(void *arg);
79
80 int rt305x_get_int_mask (device_t);
81 void rt305x_set_int_mask (device_t, uint32_t);
82 int rt305x_get_int_status(device_t);
83 void rt305x_set_int_status(device_t, uint32_t);
84
85 /*
86 * GPIO interface
87 */
88 static device_t rt305x_gpio_get_bus(device_t);
89 static int rt305x_gpio_pin_max(device_t dev, int *maxpin);
90 static int rt305x_gpio_pin_getcaps(device_t dev, uint32_t pin, uint32_t *caps);
91 static int rt305x_gpio_pin_getflags(device_t dev, uint32_t pin, uint32_t
92 *flags);
93 static int rt305x_gpio_pin_getname(device_t dev, uint32_t pin, char *name);
94 static int rt305x_gpio_pin_setflags(device_t dev, uint32_t pin, uint32_t flags);
95 static int rt305x_gpio_pin_set(device_t dev, uint32_t pin, unsigned int value);
96 static int rt305x_gpio_pin_get(device_t dev, uint32_t pin, unsigned int *val);
97 static int rt305x_gpio_pin_toggle(device_t dev, uint32_t pin);
98
99 static void
100 rt305x_gpio_pin_configure(struct rt305x_gpio_softc *sc, struct gpio_pin *pin,
101 unsigned int flags)
102 {
103 GPIO_LOCK(sc);
104
105 /*
106 * Manage input/output
107 */
108 if (flags & (GPIO_PIN_INPUT|GPIO_PIN_OUTPUT)) {
109 pin->gp_flags &= ~(GPIO_PIN_INPUT|GPIO_PIN_OUTPUT);
110 if (flags & GPIO_PIN_OUTPUT) {
111 pin->gp_flags |= GPIO_PIN_OUTPUT;
112 GPIO_BIT_SET(sc, pin->gp_pin, DIR);
113 }
114 else {
115 pin->gp_flags |= GPIO_PIN_INPUT;
116 GPIO_BIT_CLR(sc, pin->gp_pin, DIR);
117 }
118 }
119
120 if (flags & GPIO_PIN_INVOUT) {
121 pin->gp_flags |= GPIO_PIN_INVOUT;
122 GPIO_BIT_SET(sc, pin->gp_pin, POL);
123 }
124 else {
125 pin->gp_flags &= ~GPIO_PIN_INVOUT;
126 GPIO_BIT_CLR(sc, pin->gp_pin, POL);
127 }
128
129 if (flags & GPIO_PIN_INVIN) {
130 pin->gp_flags |= GPIO_PIN_INVIN;
131 GPIO_BIT_SET(sc, pin->gp_pin, POL);
132 }
133 else {
134 pin->gp_flags &= ~GPIO_PIN_INVIN;
135 GPIO_BIT_CLR(sc, pin->gp_pin, POL);
136 }
137
138 #ifdef notyet
139 /* Enable interrupt bits for rising/falling transitions */
140 if (flags & GPIO_PIN_REPORT) {
141 pin->gp_flags |= GPIO_PIN_REPORT;
142 GPIO_BIT_SET(sc, pin->gp_pin, RENA);
143 GPIO_BIT_SET(sc, pin->gp_pin, FENA);
144 device_printf(sc->dev, "Will report interrupt on pin %d\n",
145 pin->gp_pin);
146
147 }
148 else {
149 pin->gp_flags &= ~GPIO_PIN_REPORT;
150 GPIO_BIT_CLR(sc, pin->gp_pin, RENA);
151 GPIO_BIT_CLR(sc, pin->gp_pin, FENA);
152 }
153 #else
154 /* Disable generating interrupts for now */
155 GPIO_BIT_CLR(sc, pin->gp_pin, RENA);
156 GPIO_BIT_CLR(sc, pin->gp_pin, FENA);
157 #endif
158
159 GPIO_UNLOCK(sc);
160 }
161
162 static device_t
163 rt305x_gpio_get_bus(device_t dev)
164 {
165 struct rt305x_gpio_softc *sc;
166
167 sc = device_get_softc(dev);
168
169 return (sc->busdev);
170 }
171
172 static int
173 rt305x_gpio_pin_max(device_t dev, int *maxpin)
174 {
175
176 *maxpin = NGPIO - 1;
177 return (0);
178 }
179
180 static int
181 rt305x_gpio_pin_getcaps(device_t dev, uint32_t pin, uint32_t *caps)
182 {
183 struct rt305x_gpio_softc *sc = device_get_softc(dev);
184 int i;
185
186 for (i = 0; i < sc->gpio_npins; i++) {
187 if (sc->gpio_pins[i].gp_pin == pin)
188 break;
189 }
190
191 if (i >= sc->gpio_npins)
192 return (EINVAL);
193
194 GPIO_LOCK(sc);
195 *caps = sc->gpio_pins[i].gp_caps;
196 GPIO_UNLOCK(sc);
197
198 return (0);
199 }
200
201 static int
202 rt305x_gpio_pin_getflags(device_t dev, uint32_t pin, uint32_t *flags)
203 {
204 struct rt305x_gpio_softc *sc = device_get_softc(dev);
205 int i;
206
207 for (i = 0; i < sc->gpio_npins; i++) {
208 if (sc->gpio_pins[i].gp_pin == pin)
209 break;
210 }
211
212 if (i >= sc->gpio_npins)
213 return (EINVAL);
214
215 GPIO_LOCK(sc);
216 *flags = sc->gpio_pins[i].gp_flags;
217 GPIO_UNLOCK(sc);
218
219 return (0);
220 }
221
222 static int
223 rt305x_gpio_pin_getname(device_t dev, uint32_t pin, char *name)
224 {
225 struct rt305x_gpio_softc *sc = device_get_softc(dev);
226 int i;
227
228 for (i = 0; i < sc->gpio_npins; i++) {
229 if (sc->gpio_pins[i].gp_pin == pin)
230 break;
231 }
232
233 if (i >= sc->gpio_npins)
234 return (EINVAL);
235
236 GPIO_LOCK(sc);
237 memcpy(name, sc->gpio_pins[i].gp_name, GPIOMAXNAME);
238 GPIO_UNLOCK(sc);
239
240 return (0);
241 }
242
243 static int
244 rt305x_gpio_pin_setflags(device_t dev, uint32_t pin, uint32_t flags)
245 {
246 int i;
247 struct rt305x_gpio_softc *sc = device_get_softc(dev);
248
249 for (i = 0; i < sc->gpio_npins; i++) {
250 if (sc->gpio_pins[i].gp_pin == pin)
251 break;
252 }
253
254 if (i >= sc->gpio_npins)
255 return (EINVAL);
256
257 rt305x_gpio_pin_configure(sc, &sc->gpio_pins[i], flags);
258
259 return (0);
260 }
261
262 static int
263 rt305x_gpio_pin_set(device_t dev, uint32_t pin, unsigned int value)
264 {
265 struct rt305x_gpio_softc *sc = device_get_softc(dev);
266 int i;
267
268 for (i = 0; i < sc->gpio_npins; i++) {
269 if (sc->gpio_pins[i].gp_pin == pin)
270 break;
271 }
272
273 if (i >= sc->gpio_npins)
274 return (EINVAL);
275
276
277 GPIO_LOCK(sc);
278 if (value) GPIO_BIT_SET(sc, i, DATA);
279 else GPIO_BIT_CLR(sc, i, DATA);
280 GPIO_UNLOCK(sc);
281
282 return (0);
283 }
284
285 static int
286 rt305x_gpio_pin_get(device_t dev, uint32_t pin, unsigned int *val)
287 {
288 struct rt305x_gpio_softc *sc = device_get_softc(dev);
289 int i;
290
291 for (i = 0; i < sc->gpio_npins; i++) {
292 if (sc->gpio_pins[i].gp_pin == pin)
293 break;
294 }
295
296 if (i >= sc->gpio_npins)
297 return (EINVAL);
298
299 GPIO_LOCK(sc);
300 *val = GPIO_BIT_GET(sc, i, DATA);
301 GPIO_UNLOCK(sc);
302
303 return (0);
304 }
305
306 static int
307 rt305x_gpio_pin_toggle(device_t dev, uint32_t pin)
308 {
309 int i;
310 struct rt305x_gpio_softc *sc = device_get_softc(dev);
311
312 for (i = 0; i < sc->gpio_npins; i++) {
313 if (sc->gpio_pins[i].gp_pin == pin)
314 break;
315 }
316
317 if (i >= sc->gpio_npins)
318 return (EINVAL);
319
320 GPIO_LOCK(sc);
321 GPIO_BIT_SET(sc, i, TOG);
322 GPIO_UNLOCK(sc);
323
324 return (0);
325 }
326
327 static int
328 rt305x_gpio_intr(void *arg)
329 {
330 struct rt305x_gpio_softc *sc = arg;
331 #ifdef notyet
332 uint32_t i;
333 #endif
334 uint64_t input, value;
335 #ifdef notyet
336 uint64_t reset_pin;
337 char notify[16];
338 char pinname[6];
339 #endif
340
341 /* Read all reported pins */
342 input = GPIO_READ_ALL(sc, INT);
343 /* Clear int status */
344 GPIO_WRITE_ALL(sc, INT, input);
345 /* Clear report for OUTs */
346 input &= ~GPIO_READ_ALL(sc, DIR);
347 value = input & GPIO_READ_ALL(sc, DATA);
348
349 if (!input) goto intr_done;
350
351 #ifdef notyet
352 /* if reset_gpio and this pin is input */
353 if (sc->reset_gpio >= 0 && (input & (1 << sc->reset_gpio))) {
354 /* get reset_gpio pin value */
355 reset_pin = (value & (1 << sc->reset_gpio))?1:0;
356 if ( sc->reset_gpio_last != reset_pin ) {
357 /*
358 * if now reset is high, check how long
359 * and do reset if less than 2 seconds
360 */
361 if ( reset_pin &&
362 (time_uptime - sc->reset_gpio_ontime) < 2 )
363 shutdown_nice(0);
364
365 sc->reset_gpio_last = reset_pin;
366 sc->reset_gpio_ontime = time_uptime;
367 }
368 }
369
370 for ( i = 0; i < NGPIO; i ++ )
371 {
372 /* Next if output pin */
373 if ( !(( input >> i) & 1) ) continue;
374
375 if ( (((value & input) >> i) & 1) != sc->gpio_pins[i].gp_last )
376 {
377 /* !system=GPIO subsystem=pin7 type=PIN_HIGH period=3 */
378 snprintf(notify , sizeof(notify ), "period=%d",
379 (uint32_t)time_uptime - sc->gpio_pins[i].gp_time);
380 snprintf(pinname, sizeof(pinname), "pin%02d", i);
381 devctl_notify("GPIO", pinname,
382 (((value & input) >> i) & 1)?"PIN_HIGH":"PIN_LOW",
383 notify);
384 printf("GPIO[%s] %s %s\n", pinname,
385 (((value & input) >> i) & 1)?"PIN_HIGH":"PIN_LOW",
386 notify);
387 sc->gpio_pins[i].gp_last = ((value & input) >> i) & 1;
388 sc->gpio_pins[i].gp_time = time_uptime;
389 }
390
391 }
392 #endif
393
394 intr_done:
395 return (FILTER_HANDLED);
396 }
397
398 static int
399 rt305x_gpio_probe(device_t dev)
400 {
401 device_set_desc(dev, "RT305X GPIO driver");
402 return (0);
403 }
404
405 static uint64_t
406 rt305x_gpio_init(device_t dev)
407 {
408 uint64_t avl = ~0ULL;
409 uint32_t gmode = rt305x_sysctl_get(SYSCTL_GPIOMODE);
410 if (!(gmode & SYSCTL_GPIOMODE_RGMII_GPIO_MODE))
411 avl &= ~RGMII_GPIO_MODE_MASK;
412 if (!(gmode & SYSCTL_GPIOMODE_SDRAM_GPIO_MODE))
413 avl &= ~SDRAM_GPIO_MODE_MASK;
414 if (!(gmode & SYSCTL_GPIOMODE_MDIO_GPIO_MODE))
415 avl &= ~MDIO_GPIO_MODE_MASK;
416 if (!(gmode & SYSCTL_GPIOMODE_JTAG_GPIO_MODE))
417 avl &= ~JTAG_GPIO_MODE_MASK;
418 if (!(gmode & SYSCTL_GPIOMODE_UARTL_GPIO_MODE))
419 avl &= ~UARTL_GPIO_MODE_MASK;
420 if (!(gmode & SYSCTL_GPIOMODE_SPI_GPIO_MODE))
421 avl &= ~SPI_GPIO_MODE_MASK;
422 if (!(gmode & SYSCTL_GPIOMODE_I2C_GPIO_MODE))
423 avl &= ~I2C_GPIO_MODE_MASK;
424 if ((gmode & SYSCTL_GPIOMODE_UARTF_SHARE_MODE_GPIO) !=
425 SYSCTL_GPIOMODE_UARTF_SHARE_MODE_GPIO)
426 avl &= ~I2C_GPIO_MODE_MASK;
427 /* D-Link DAP-1350 Board have
428 * MDIO_GPIO_MODE
429 * UARTF_GPIO_MODE
430 * SPI_GPIO_MODE
431 * I2C_GPIO_MODE
432 * So we have
433 * 00000001 10000000 01111111 11111110
434 */
435 return (avl);
436
437 }
438
439 #define DAP1350_RESET_GPIO 10
440
441 static int
442 rt305x_gpio_attach(device_t dev)
443 {
444 struct rt305x_gpio_softc *sc = device_get_softc(dev);
445 int i;
446 uint64_t avlpins = 0;
447 sc->reset_gpio = DAP1350_RESET_GPIO;
448
449 KASSERT((device_get_unit(dev) == 0),
450 ("rt305x_gpio_gpio: Only one gpio module supported"));
451
452 mtx_init(&sc->gpio_mtx, device_get_nameunit(dev), NULL, MTX_DEF);
453
454 /* Map control/status registers. */
455 sc->gpio_mem_rid = 0;
456 sc->gpio_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
457 &sc->gpio_mem_rid, RF_ACTIVE);
458
459 if (sc->gpio_mem_res == NULL) {
460 device_printf(dev, "couldn't map memory\n");
461 rt305x_gpio_detach(dev);
462 return (ENXIO);
463 }
464
465 if ((sc->gpio_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ,
466 &sc->gpio_irq_rid, RF_SHAREABLE | RF_ACTIVE)) == NULL) {
467 device_printf(dev, "unable to allocate IRQ resource\n");
468 rt305x_gpio_detach(dev);
469 return (ENXIO);
470 }
471
472 if ((bus_setup_intr(dev, sc->gpio_irq_res, INTR_TYPE_MISC,
473 /* rt305x_gpio_filter, */
474 rt305x_gpio_intr, NULL, sc, &sc->gpio_ih))) {
475 device_printf(dev,
476 "WARNING: unable to register interrupt handler\n");
477 rt305x_gpio_detach(dev);
478 return (ENXIO);
479 }
480
481 sc->dev = dev;
482 avlpins = rt305x_gpio_init(dev);
483
484 /* Configure all pins as input */
485 /* disable interrupts for all pins */
486 /* TODO */
487
488 sc->gpio_npins = NGPIO;
489 resource_int_value(device_get_name(dev), device_get_unit(dev),
490 "pins", &sc->gpio_npins);
491
492 for (i = 0; i < sc->gpio_npins; i++) {
493 sc->gpio_pins[i].gp_pin = i;
494 sc->gpio_pins[i].gp_caps = DEFAULT_CAPS;
495 sc->gpio_pins[i].gp_flags = 0;
496 }
497
498 /* Setup reset pin interrupt */
499 if (TUNABLE_INT_FETCH("reset_gpio", &sc->reset_gpio)) {
500 device_printf(dev, "\tHinted reset_gpio %d\n", sc->reset_gpio);
501 }
502 #ifdef notyet
503 if (sc->reset_gpio != -1) {
504 rt305x_gpio_pin_setflags(dev, sc->reset_gpio,
505 GPIO_PIN_INPUT|GPIO_PIN_INVOUT|
506 GPIO_PIN_INVOUT|GPIO_PIN_REPORT);
507 device_printf(dev, "\tUse reset_gpio %d\n", sc->reset_gpio);
508 }
509 #else
510 if (sc->reset_gpio != -1) {
511 rt305x_gpio_pin_setflags(dev, sc->reset_gpio,
512 GPIO_PIN_INPUT|GPIO_PIN_INVOUT);
513 device_printf(dev, "\tUse reset_gpio %d\n", sc->reset_gpio);
514 }
515 #endif
516 sc->busdev = gpiobus_attach_bus(dev);
517 if (sc->busdev == NULL) {
518 rt305x_gpio_detach(dev);
519 return (ENXIO);
520 }
521
522 return (0);
523 }
524
525 static int
526 rt305x_gpio_detach(device_t dev)
527 {
528 struct rt305x_gpio_softc *sc = device_get_softc(dev);
529
530 KASSERT(mtx_initialized(&sc->gpio_mtx), ("gpio mutex not initialized"));
531
532 gpiobus_detach_bus(dev);
533 if (sc->gpio_ih)
534 bus_teardown_intr(dev, sc->gpio_irq_res, sc->gpio_ih);
535 if (sc->gpio_irq_res)
536 bus_release_resource(dev, SYS_RES_IRQ, sc->gpio_irq_rid,
537 sc->gpio_irq_res);
538 if (sc->gpio_mem_res)
539 bus_release_resource(dev, SYS_RES_MEMORY, sc->gpio_mem_rid,
540 sc->gpio_mem_res);
541 mtx_destroy(&sc->gpio_mtx);
542
543 return(0);
544 }
545
546 #ifdef notyet
547 static struct resource *
548 rt305x_gpio_alloc_resource(device_t bus, device_t child, int type, int *rid,
549 rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
550 {
551 struct obio_softc *sc = device_get_softc(bus);
552 struct resource *rv;
553 struct rman *rm;
554
555 switch (type) {
556 case SYS_RES_GPIO:
557 rm = &sc->gpio_rman;
558 break;
559 default:
560 printf("%s: unknown resource type %d\n", __func__, type);
561 return (0);
562 }
563
564 rv = rman_reserve_resource(rm, start, end, count, flags, child);
565 if (rv == NULL) {
566 printf("%s: could not reserve resource\n", __func__);
567 return (0);
568 }
569
570 rman_set_rid(rv, *rid);
571
572 return (rv);
573 }
574
575 static int
576 rt305x_gpio_activate_resource(device_t bus, device_t child, int type, int rid,
577 struct resource *r)
578 {
579
580 return (rman_activate_resource(r));
581 }
582
583 static int
584 rt305x_gpio_deactivate_resource(device_t bus, device_t child, int type, int rid,
585 struct resource *r)
586 {
587
588 return (rman_deactivate_resource(r));
589 }
590
591 static int
592 rt305x_gpio_release_resource(device_t dev, device_t child, int type,
593 int rid, struct resource *r)
594 {
595 rman_release_resource(r);
596 return (0);
597 }
598 #endif
599
600 static device_method_t rt305x_gpio_methods[] = {
601 DEVMETHOD(device_probe, rt305x_gpio_probe),
602 DEVMETHOD(device_attach, rt305x_gpio_attach),
603 DEVMETHOD(device_detach, rt305x_gpio_detach),
604
605 /* GPIO protocol */
606 DEVMETHOD(gpio_get_bus, rt305x_gpio_get_bus),
607 DEVMETHOD(gpio_pin_max, rt305x_gpio_pin_max),
608 DEVMETHOD(gpio_pin_getname, rt305x_gpio_pin_getname),
609 DEVMETHOD(gpio_pin_getflags, rt305x_gpio_pin_getflags),
610 DEVMETHOD(gpio_pin_getcaps, rt305x_gpio_pin_getcaps),
611 DEVMETHOD(gpio_pin_setflags, rt305x_gpio_pin_setflags),
612 DEVMETHOD(gpio_pin_get, rt305x_gpio_pin_get),
613 DEVMETHOD(gpio_pin_set, rt305x_gpio_pin_set),
614 DEVMETHOD(gpio_pin_toggle, rt305x_gpio_pin_toggle),
615 {0, 0},
616 };
617
618 static driver_t rt305x_gpio_driver = {
619 "gpio",
620 rt305x_gpio_methods,
621 sizeof(struct rt305x_gpio_softc),
622 };
623 static devclass_t rt305x_gpio_devclass;
624
625 DRIVER_MODULE(rt305x_gpio, obio, rt305x_gpio_driver,
626 rt305x_gpio_devclass, 0, 0);
Cache object: d065cb927380b1a264750cfef1fd0333
|