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
|
/* $OpenBSD: qcgpio_fdt.c,v 1.2 2022/11/08 11:51:34 patrick Exp $ */
/*
* Copyright (c) 2022 Mark Kettenis <kettenis@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/param.h>
#include <sys/malloc.h>
#include <sys/systm.h>
#include <machine/fdt.h>
#include <dev/ofw/openfirm.h>
#include <dev/ofw/ofw_gpio.h>
#include <dev/ofw/fdt.h>
/* Registers. */
#define TLMM_GPIO_CFG(pin) (0x0000 + 0x1000 * (pin))
#define TLMM_GPIO_CFG_OUT_EN (1 << 9)
#define TLMM_GPIO_IN_OUT(pin) (0x0004 + 0x1000 * (pin))
#define TLMM_GPIO_IN_OUT_GPIO_IN (1 << 0)
#define TLMM_GPIO_IN_OUT_GPIO_OUT (1 << 1)
#define TLMM_GPIO_INTR_CFG(pin) (0x0008 + 0x1000 * (pin))
#define TLMM_GPIO_INTR_CFG_TARGET_PROC_MASK (0x7 << 5)
#define TLMM_GPIO_INTR_CFG_TARGET_PROC_RPM (0x3 << 5)
#define TLMM_GPIO_INTR_CFG_INTR_RAW_STATUS_EN (1 << 4)
#define TLMM_GPIO_INTR_CFG_INTR_DECT_CTL_MASK (0x3 << 2)
#define TLMM_GPIO_INTR_CFG_INTR_DECT_CTL_LEVEL (0x0 << 2)
#define TLMM_GPIO_INTR_CFG_INTR_DECT_CTL_EDGE_POS (0x1 << 2)
#define TLMM_GPIO_INTR_CFG_INTR_DECT_CTL_EDGE_NEG (0x2 << 2)
#define TLMM_GPIO_INTR_CFG_INTR_DECT_CTL_EDGE_BOTH (0x3 << 2)
#define TLMM_GPIO_INTR_CFG_INTR_POL_CTL (1 << 1)
#define TLMM_GPIO_INTR_CFG_INTR_ENABLE (1 << 0)
#define TLMM_GPIO_INTR_STATUS(pin) (0x000c + 0x1000 * (pin))
#define TLMM_GPIO_INTR_STATUS_INTR_STATUS (1 << 0)
#define HREAD4(sc, reg) \
(bus_space_read_4((sc)->sc_iot, (sc)->sc_ioh, (reg)))
#define HWRITE4(sc, reg, val) \
bus_space_write_4((sc)->sc_iot, (sc)->sc_ioh, (reg), (val))
#define HSET4(sc, reg, bits) \
HWRITE4((sc), (reg), HREAD4((sc), (reg)) | (bits))
#define HCLR4(sc, reg, bits) \
HWRITE4((sc), (reg), HREAD4((sc), (reg)) & ~(bits))
struct qcgpio_intrhand {
int (*ih_func)(void *);
void *ih_arg;
void *ih_sc;
int ih_pin;
};
struct qcgpio_softc {
struct device sc_dev;
bus_space_tag_t sc_iot;
bus_space_handle_t sc_ioh;
void *sc_ih;
uint32_t sc_npins;
struct qcgpio_intrhand *sc_pin_ih;
struct gpio_controller sc_gc;
struct interrupt_controller sc_ic;
};
int qcgpio_fdt_match(struct device *, void *, void *);
void qcgpio_fdt_attach(struct device *, struct device *, void *);
const struct cfattach qcgpio_fdt_ca = {
sizeof(struct qcgpio_softc), qcgpio_fdt_match, qcgpio_fdt_attach
};
void qcgpio_fdt_config_pin(void *, uint32_t *, int);
int qcgpio_fdt_get_pin(void *, uint32_t *);
void qcgpio_fdt_set_pin(void *, uint32_t *, int);
void *qcgpio_fdt_intr_establish(void *, int *, int, struct cpu_info *,
int (*)(void *), void *, char *);
void qcgpio_fdt_intr_disestablish(void *);
void qcgpio_fdt_intr_enable(void *);
void qcgpio_fdt_intr_disable(void *);
void qcgpio_fdt_intr_barrier(void *);
int qcgpio_fdt_pin_intr(struct qcgpio_softc *, int);
int qcgpio_fdt_intr(void *);
int
qcgpio_fdt_match(struct device *parent, void *match, void *aux)
{
struct fdt_attach_args *faa = aux;
return OF_is_compatible(faa->fa_node, "qcom,sc8280xp-tlmm");
}
void
qcgpio_fdt_attach(struct device *parent, struct device *self, void *aux)
{
struct fdt_attach_args *faa = aux;
struct qcgpio_softc *sc = (struct qcgpio_softc *)self;
sc->sc_iot = faa->fa_iot;
if (bus_space_map(sc->sc_iot, faa->fa_reg[0].addr, faa->fa_reg[0].size,
0, &sc->sc_ioh)) {
printf(": can't map registers\n");
return;
}
sc->sc_npins = 230;
sc->sc_pin_ih = mallocarray(sc->sc_npins, sizeof(*sc->sc_pin_ih),
M_DEVBUF, M_WAITOK | M_ZERO);
sc->sc_ih = fdt_intr_establish(faa->fa_node, IPL_BIO, qcgpio_fdt_intr,
sc, sc->sc_dev.dv_xname);
if (sc->sc_ih == NULL) {
printf(": can't establish interrupt\n");
goto unmap;
}
sc->sc_gc.gc_node = faa->fa_node;
sc->sc_gc.gc_cookie = sc;
sc->sc_gc.gc_config_pin = qcgpio_fdt_config_pin;
sc->sc_gc.gc_get_pin = qcgpio_fdt_get_pin;
sc->sc_gc.gc_set_pin = qcgpio_fdt_set_pin;
gpio_controller_register(&sc->sc_gc);
sc->sc_ic.ic_node = faa->fa_node;
sc->sc_ic.ic_cookie = sc;
sc->sc_ic.ic_establish = qcgpio_fdt_intr_establish;
sc->sc_ic.ic_disestablish = qcgpio_fdt_intr_disestablish;
sc->sc_ic.ic_enable = qcgpio_fdt_intr_enable;
sc->sc_ic.ic_disable = qcgpio_fdt_intr_disable;
sc->sc_ic.ic_barrier = qcgpio_fdt_intr_barrier;
fdt_intr_register(&sc->sc_ic);
printf("\n");
return;
unmap:
if (sc->sc_ih)
fdt_intr_disestablish(sc->sc_ih);
free(sc->sc_pin_ih, M_DEVBUF, sc->sc_npins * sizeof(*sc->sc_pin_ih));
bus_space_unmap(sc->sc_iot, sc->sc_ioh, faa->fa_reg[0].size);
}
void
qcgpio_fdt_config_pin(void *cookie, uint32_t *cells, int config)
{
struct qcgpio_softc *sc = cookie;
uint32_t pin = cells[0];
if (pin >= sc->sc_npins)
return;
if (config & GPIO_CONFIG_OUTPUT)
HSET4(sc, TLMM_GPIO_CFG(pin), TLMM_GPIO_CFG_OUT_EN);
else
HCLR4(sc, TLMM_GPIO_CFG(pin), TLMM_GPIO_CFG_OUT_EN);
}
int
qcgpio_fdt_get_pin(void *cookie, uint32_t *cells)
{
struct qcgpio_softc *sc = cookie;
uint32_t pin = cells[0];
uint32_t flags = cells[1];
uint32_t reg;
int val;
if (pin >= sc->sc_npins)
return 0;
reg = HREAD4(sc, TLMM_GPIO_IN_OUT(pin));
val = !!(reg & TLMM_GPIO_IN_OUT_GPIO_IN);
if (flags & GPIO_ACTIVE_LOW)
val = !val;
return val;
}
void
qcgpio_fdt_set_pin(void *cookie, uint32_t *cells, int val)
{
struct qcgpio_softc *sc = cookie;
uint32_t pin = cells[0];
uint32_t flags = cells[1];
if (pin >= sc->sc_npins)
return;
if (flags & GPIO_ACTIVE_LOW)
val = !val;
if (val) {
HSET4(sc, TLMM_GPIO_IN_OUT(pin),
TLMM_GPIO_IN_OUT_GPIO_OUT);
} else {
HCLR4(sc, TLMM_GPIO_IN_OUT(pin),
TLMM_GPIO_IN_OUT_GPIO_OUT);
}
}
void *
qcgpio_fdt_intr_establish(void *cookie, int *cells, int ipl,
struct cpu_info *ci, int (*func)(void *), void *arg, char *name)
{
struct qcgpio_softc *sc = cookie;
uint32_t reg;
int pin = cells[0];
int level = cells[1];
if (pin < 0 || pin >= sc->sc_npins)
return NULL;
sc->sc_pin_ih[pin].ih_func = func;
sc->sc_pin_ih[pin].ih_arg = arg;
sc->sc_pin_ih[pin].ih_pin = pin;
sc->sc_pin_ih[pin].ih_sc = sc;
reg = HREAD4(sc, TLMM_GPIO_INTR_CFG(pin));
reg &= ~TLMM_GPIO_INTR_CFG_INTR_DECT_CTL_MASK;
reg &= ~TLMM_GPIO_INTR_CFG_INTR_POL_CTL;
switch (level) {
case 1:
reg |= TLMM_GPIO_INTR_CFG_INTR_DECT_CTL_EDGE_POS |
TLMM_GPIO_INTR_CFG_INTR_POL_CTL;
break;
case 2:
reg |= TLMM_GPIO_INTR_CFG_INTR_DECT_CTL_EDGE_NEG |
TLMM_GPIO_INTR_CFG_INTR_POL_CTL;
break;
case 3:
reg |= TLMM_GPIO_INTR_CFG_INTR_DECT_CTL_EDGE_BOTH;
break;
case 4:
reg |= TLMM_GPIO_INTR_CFG_INTR_DECT_CTL_LEVEL |
TLMM_GPIO_INTR_CFG_INTR_POL_CTL;
break;
case 8:
reg |= TLMM_GPIO_INTR_CFG_INTR_DECT_CTL_LEVEL;
break;
default:
printf("%s: unsupported interrupt mode/polarity\n",
sc->sc_dev.dv_xname);
break;
}
reg &= ~TLMM_GPIO_INTR_CFG_TARGET_PROC_MASK;
reg |= TLMM_GPIO_INTR_CFG_TARGET_PROC_RPM;
reg |= TLMM_GPIO_INTR_CFG_INTR_RAW_STATUS_EN;
reg |= TLMM_GPIO_INTR_CFG_INTR_ENABLE;
HWRITE4(sc, TLMM_GPIO_INTR_CFG(pin), reg);
return &sc->sc_pin_ih[pin];
}
void
qcgpio_fdt_intr_disestablish(void *cookie)
{
struct qcgpio_intrhand *ih = cookie;
qcgpio_fdt_intr_disable(cookie);
ih->ih_func = NULL;
}
void
qcgpio_fdt_intr_enable(void *cookie)
{
struct qcgpio_intrhand *ih = cookie;
struct qcgpio_softc *sc = ih->ih_sc;
int pin = ih->ih_pin;
if (pin < 0 || pin >= sc->sc_npins)
return;
HSET4(sc, TLMM_GPIO_INTR_CFG(pin),
TLMM_GPIO_INTR_CFG_INTR_ENABLE);
}
void
qcgpio_fdt_intr_disable(void *cookie)
{
struct qcgpio_intrhand *ih = cookie;
struct qcgpio_softc *sc = ih->ih_sc;
int pin = ih->ih_pin;
if (pin < 0 || pin >= sc->sc_npins)
return;
HCLR4(sc, TLMM_GPIO_INTR_CFG(pin),
TLMM_GPIO_INTR_CFG_INTR_ENABLE);
}
void
qcgpio_fdt_intr_barrier(void *cookie)
{
struct qcgpio_intrhand *ih = cookie;
struct qcgpio_softc *sc = ih->ih_sc;
intr_barrier(sc->sc_ih);
}
int
qcgpio_fdt_intr(void *arg)
{
struct qcgpio_softc *sc = arg;
int pin, handled = 0;
uint32_t stat;
for (pin = 0; pin < sc->sc_npins; pin++) {
if (sc->sc_pin_ih[pin].ih_func == NULL)
continue;
stat = HREAD4(sc, TLMM_GPIO_INTR_STATUS(pin));
if (stat & TLMM_GPIO_INTR_STATUS_INTR_STATUS) {
sc->sc_pin_ih[pin].ih_func(sc->sc_pin_ih[pin].ih_arg);
handled = 1;
}
HWRITE4(sc, TLMM_GPIO_INTR_STATUS(pin),
stat & ~TLMM_GPIO_INTR_STATUS_INTR_STATUS);
}
return handled;
}
|