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
|
/* $NetBSD: pl061gpio_fdt.c,v 1.5 2021/01/27 03:10:21 thorpej Exp $ */
/*
* Copyright (c) 2018 Jonathan A. Kollasch
* 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, 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 COPYRIGHT HOLDERS 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 COPYRIGHT HOLDER 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.
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: pl061gpio_fdt.c,v 1.5 2021/01/27 03:10:21 thorpej Exp $");
#include <sys/param.h>
#include <sys/bus.h>
#include <sys/device.h>
#include <sys/intr.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/kmem.h>
#include <sys/gpio.h>
#include <dev/gpio/gpiovar.h>
#include <dev/ic/pl061reg.h>
#include <dev/ic/pl061var.h>
#include <dev/fdt/fdtvar.h>
static int plgpio_fdt_match(device_t, cfdata_t, void *);
static void plgpio_fdt_attach(device_t, device_t, void *);
static void * plgpio_fdt_acquire(device_t, const void *,
size_t, int);
static void plgpio_fdt_release(device_t, void *);
static int plgpio_fdt_read(device_t, void *, bool);
static void plgpio_fdt_write(device_t, void *, int, bool);
struct fdtbus_gpio_controller_func plgpio_fdt_funcs = {
.acquire = plgpio_fdt_acquire,
.release = plgpio_fdt_release,
.read = plgpio_fdt_read,
.write = plgpio_fdt_write
};
struct plgpio_fdt_pin {
struct plgpio_softc * pin_sc;
int pin_no;
u_int pin_flags;
bool pin_actlo;
};
CFATTACH_DECL_NEW(plgpio_fdt, sizeof(struct plgpio_softc),
plgpio_fdt_match, plgpio_fdt_attach, NULL, NULL);
static const struct device_compatible_entry compat_data[] = {
{ .compat = "arm,pl061" },
DEVICE_COMPAT_EOL
};
static int
plgpio_fdt_match(device_t parent, cfdata_t cf, void *aux)
{
struct fdt_attach_args * const faa = aux;
return of_compatible_match(faa->faa_phandle, compat_data);
}
static void
plgpio_fdt_attach(device_t parent, device_t self, void *aux)
{
struct plgpio_softc * const sc = device_private(self);
struct fdt_attach_args * const faa = aux;
bus_addr_t addr;
bus_size_t size;
int error;
if (fdtbus_get_reg(faa->faa_phandle, 0, &addr, &size) != 0) {
aprint_error(": couldn't get registers\n");
return;
}
sc->sc_dev = self;
sc->sc_bst = faa->faa_bst;
error = bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh);
if (error) {
aprint_error(": couldn't map %#"PRIx64": %d", (uint64_t)addr, error);
return;
}
aprint_naive("\n");
aprint_normal(": GPIO\n");
plgpio_attach(sc);
fdtbus_register_gpio_controller(self, faa->faa_phandle,
&plgpio_fdt_funcs);
}
static void *
plgpio_fdt_acquire(device_t dev, const void *data, size_t len, int flags)
{
struct plgpio_softc * const sc = device_private(dev);
struct plgpio_fdt_pin *gpin;
const u_int *gpio = data;
if (len != 12)
return NULL;
const u_int pin = be32toh(gpio[1]);
const bool actlo = be32toh(gpio[2]) & 1;
if (pin > 8)
return NULL;
const uint32_t cnf = PLGPIO_READ(sc, PL061_GPIOAFSEL_REG);
if ((cnf & __BIT(pin)) != 0)
PLGPIO_WRITE(sc, PL061_GPIOAFSEL_REG, cnf & ~__BIT(pin));
gpin = kmem_zalloc(sizeof(*gpin), KM_SLEEP);
gpin->pin_sc = sc;
gpin->pin_no = pin;
gpin->pin_flags = flags;
gpin->pin_actlo = actlo;
plgpio_pin_ctl(gpin->pin_sc, gpin->pin_no, gpin->pin_flags);
return gpin;
}
static void
plgpio_fdt_release(device_t dev, void *priv)
{
struct plgpio_fdt_pin * const gpin = priv;
plgpio_pin_ctl(gpin->pin_sc, gpin->pin_no, GPIO_PIN_INPUT);
kmem_free(gpin, sizeof(*gpin));
}
static int
plgpio_fdt_read(device_t dev, void *priv, bool raw)
{
struct plgpio_fdt_pin * const gpin = priv;
int val;
val = plgpio_pin_read(gpin->pin_sc, gpin->pin_no);
if (!raw && gpin->pin_actlo)
val = !val;
return val;
}
static void
plgpio_fdt_write(device_t dev, void *priv, int val, bool raw)
{
struct plgpio_fdt_pin * const gpin = priv;
if (!raw && gpin->pin_actlo)
val = !val;
plgpio_pin_write(gpin->pin_sc, gpin->pin_no, val);
}
|