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