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
|
/*-
* SPDX-License-Identifier: BSD-2-Clause
*
* Copyright (c) 2017 Tom Jones <tj@enoti.me>
* 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 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.
*
*/
/*
* Copyright (c) 2016 Mark Kettenis
*
* 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/systm.h>
#include <sys/bus.h>
#include <sys/gpio.h>
#include <sys/clock.h>
#include <sys/kernel.h>
#include <sys/module.h>
#include <sys/endian.h>
#include <sys/rman.h>
#include <sys/types.h>
#include <sys/malloc.h>
#include <machine/bus.h>
#include <machine/resource.h>
#include <contrib/dev/acpica/include/acpi.h>
#include <contrib/dev/acpica/include/accommon.h>
#include <dev/acpica/acpivar.h>
#include <dev/gpio/gpiobusvar.h>
#include "opt_platform.h"
#include "opt_acpi.h"
#include "gpio_if.h"
#include "chvgpio_reg.h"
/*
* Macros for driver mutex locking
*/
#define CHVGPIO_LOCK(_sc) mtx_lock_spin(&(_sc)->sc_mtx)
#define CHVGPIO_UNLOCK(_sc) mtx_unlock_spin(&(_sc)->sc_mtx)
#define CHVGPIO_LOCK_INIT(_sc) \
mtx_init(&_sc->sc_mtx, device_get_nameunit((_sc)->sc_dev), \
"chvgpio", MTX_SPIN)
#define CHVGPIO_LOCK_DESTROY(_sc) mtx_destroy(&(_sc)->sc_mtx)
#define CHVGPIO_ASSERT_LOCKED(_sc) mtx_assert(&(_sc)->sc_mtx, MA_OWNED)
#define CHVGPIO_ASSERT_UNLOCKED(_sc) mtx_assert(&(_sc)->sc_mtx, MA_NOTOWNED)
struct chvgpio_softc {
device_t sc_dev;
device_t sc_busdev;
struct mtx sc_mtx;
ACPI_HANDLE sc_handle;
int sc_mem_rid;
struct resource *sc_mem_res;
int sc_irq_rid;
struct resource *sc_irq_res;
void *intr_handle;
const char *sc_bank_prefix;
const int *sc_pins;
int sc_npins;
int sc_ngroups;
const char **sc_pin_names;
};
static void chvgpio_intr(void *);
static int chvgpio_probe(device_t);
static int chvgpio_attach(device_t);
static int chvgpio_detach(device_t);
static inline int
chvgpio_pad_cfg0_offset(int pin)
{
return (CHVGPIO_PAD_CFG0 + 1024 * (pin / 15) + 8 * (pin % 15));
}
static inline int
chvgpio_read_pad_cfg0(struct chvgpio_softc *sc, int pin)
{
return bus_read_4(sc->sc_mem_res, chvgpio_pad_cfg0_offset(pin));
}
static inline void
chvgpio_write_pad_cfg0(struct chvgpio_softc *sc, int pin, uint32_t val)
{
bus_write_4(sc->sc_mem_res, chvgpio_pad_cfg0_offset(pin), val);
}
static inline int
chvgpio_read_pad_cfg1(struct chvgpio_softc *sc, int pin)
{
return bus_read_4(sc->sc_mem_res, chvgpio_pad_cfg0_offset(pin) + 4);
}
static device_t
chvgpio_get_bus(device_t dev)
{
struct chvgpio_softc *sc;
sc = device_get_softc(dev);
return (sc->sc_busdev);
}
static int
chvgpio_pin_max(device_t dev, int *maxpin)
{
struct chvgpio_softc *sc;
sc = device_get_softc(dev);
*maxpin = sc->sc_npins - 1;
return (0);
}
static int
chvgpio_valid_pin(struct chvgpio_softc *sc, int pin)
{
if (pin < 0)
return EINVAL;
if ((pin / 15) >= sc->sc_ngroups)
return EINVAL;
if ((pin % 15) >= sc->sc_pins[pin / 15])
return EINVAL;
return (0);
}
static int
chvgpio_pin_getname(device_t dev, uint32_t pin, char *name)
{
struct chvgpio_softc *sc;
sc = device_get_softc(dev);
if (chvgpio_valid_pin(sc, pin) != 0)
return (EINVAL);
/* return pin name from datasheet */
snprintf(name, GPIOMAXNAME, "%s", sc->sc_pin_names[pin]);
name[GPIOMAXNAME - 1] = '\0';
return (0);
}
static int
chvgpio_pin_getcaps(device_t dev, uint32_t pin, uint32_t *caps)
{
struct chvgpio_softc *sc;
sc = device_get_softc(dev);
if (chvgpio_valid_pin(sc, pin) != 0)
return (EINVAL);
*caps = 0;
if (chvgpio_valid_pin(sc, pin))
*caps = GPIO_PIN_INPUT | GPIO_PIN_OUTPUT;
return (0);
}
static int
chvgpio_pin_getflags(device_t dev, uint32_t pin, uint32_t *flags)
{
struct chvgpio_softc *sc;
uint32_t val;
sc = device_get_softc(dev);
if (chvgpio_valid_pin(sc, pin) != 0)
return (EINVAL);
*flags = 0;
/* Get the current pin state */
CHVGPIO_LOCK(sc);
val = chvgpio_read_pad_cfg0(sc, pin);
if (val & CHVGPIO_PAD_CFG0_GPIOCFG_GPIO ||
val & CHVGPIO_PAD_CFG0_GPIOCFG_GPO)
*flags |= GPIO_PIN_OUTPUT;
if (val & CHVGPIO_PAD_CFG0_GPIOCFG_GPIO ||
val & CHVGPIO_PAD_CFG0_GPIOCFG_GPI)
*flags |= GPIO_PIN_INPUT;
val = chvgpio_read_pad_cfg1(sc, pin);
CHVGPIO_UNLOCK(sc);
return (0);
}
static int
chvgpio_pin_setflags(device_t dev, uint32_t pin, uint32_t flags)
{
struct chvgpio_softc *sc;
uint32_t val;
uint32_t allowed;
sc = device_get_softc(dev);
if (chvgpio_valid_pin(sc, pin) != 0)
return (EINVAL);
allowed = GPIO_PIN_INPUT | GPIO_PIN_OUTPUT;
/*
* Only direction flag allowed
*/
if (flags & ~allowed)
return (EINVAL);
/*
* Not both directions simultaneously
*/
if ((flags & allowed) == allowed)
return (EINVAL);
/* Set the GPIO mode and state */
CHVGPIO_LOCK(sc);
val = chvgpio_read_pad_cfg0(sc, pin);
if (flags & GPIO_PIN_INPUT)
val = val & CHVGPIO_PAD_CFG0_GPIOCFG_GPI;
if (flags & GPIO_PIN_OUTPUT)
val = val & CHVGPIO_PAD_CFG0_GPIOCFG_GPO;
chvgpio_write_pad_cfg0(sc, pin, val);
CHVGPIO_UNLOCK(sc);
return (0);
}
static int
chvgpio_pin_set(device_t dev, uint32_t pin, unsigned int value)
{
struct chvgpio_softc *sc;
uint32_t val;
sc = device_get_softc(dev);
if (chvgpio_valid_pin(sc, pin) != 0)
return (EINVAL);
CHVGPIO_LOCK(sc);
val = chvgpio_read_pad_cfg0(sc, pin);
if (value == GPIO_PIN_LOW)
val = val & ~CHVGPIO_PAD_CFG0_GPIOTXSTATE;
else
val = val | CHVGPIO_PAD_CFG0_GPIOTXSTATE;
chvgpio_write_pad_cfg0(sc, pin, val);
CHVGPIO_UNLOCK(sc);
return (0);
}
static int
chvgpio_pin_get(device_t dev, uint32_t pin, unsigned int *value)
{
struct chvgpio_softc *sc;
uint32_t val;
sc = device_get_softc(dev);
if (chvgpio_valid_pin(sc, pin) != 0)
return (EINVAL);
CHVGPIO_LOCK(sc);
/* Read pin value */
val = chvgpio_read_pad_cfg0(sc, pin);
if (val & CHVGPIO_PAD_CFG0_GPIORXSTATE)
*value = GPIO_PIN_HIGH;
else
*value = GPIO_PIN_LOW;
CHVGPIO_UNLOCK(sc);
return (0);
}
static int
chvgpio_pin_toggle(device_t dev, uint32_t pin)
{
struct chvgpio_softc *sc;
uint32_t val;
sc = device_get_softc(dev);
if (chvgpio_valid_pin(sc, pin) != 0)
return (EINVAL);
CHVGPIO_LOCK(sc);
/* Toggle the pin */
val = chvgpio_read_pad_cfg0(sc, pin);
val = val ^ CHVGPIO_PAD_CFG0_GPIOTXSTATE;
chvgpio_write_pad_cfg0(sc, pin, val);
CHVGPIO_UNLOCK(sc);
return (0);
}
static char *chvgpio_hids[] = {
"INT33FF",
NULL
};
static int
chvgpio_probe(device_t dev)
{
int rv;
if (acpi_disabled("chvgpio"))
return (ENXIO);
rv = ACPI_ID_PROBE(device_get_parent(dev), dev, chvgpio_hids, NULL);
if (rv <= 0)
device_set_desc(dev, "Intel Cherry View GPIO");
return (rv);
}
static int
chvgpio_attach(device_t dev)
{
struct chvgpio_softc *sc;
ACPI_STATUS status;
int uid;
int i;
int error;
sc = device_get_softc(dev);
sc->sc_dev = dev;
sc->sc_handle = acpi_get_handle(dev);
status = acpi_GetInteger(sc->sc_handle, "_UID", &uid);
if (ACPI_FAILURE(status)) {
device_printf(dev, "failed to read _UID\n");
return (ENXIO);
}
CHVGPIO_LOCK_INIT(sc);
switch (uid) {
case SW_UID:
sc->sc_bank_prefix = SW_BANK_PREFIX;
sc->sc_pins = chv_southwest_pins;
sc->sc_pin_names = chv_southwest_pin_names;
break;
case N_UID:
sc->sc_bank_prefix = N_BANK_PREFIX;
sc->sc_pins = chv_north_pins;
sc->sc_pin_names = chv_north_pin_names;
break;
case E_UID:
sc->sc_bank_prefix = E_BANK_PREFIX;
sc->sc_pins = chv_east_pins;
sc->sc_pin_names = chv_east_pin_names;
break;
case SE_UID:
sc->sc_bank_prefix = SE_BANK_PREFIX;
sc->sc_pins = chv_southeast_pins;
sc->sc_pin_names = chv_southeast_pin_names;
break;
default:
device_printf(dev, "invalid _UID value: %d\n", uid);
return (ENXIO);
}
for (i = 0; sc->sc_pins[i] >= 0; i++) {
sc->sc_npins += sc->sc_pins[i];
sc->sc_ngroups++;
}
sc->sc_mem_rid = 0;
sc->sc_mem_res = bus_alloc_resource_any(sc->sc_dev, SYS_RES_MEMORY,
&sc->sc_mem_rid, RF_ACTIVE);
if (sc->sc_mem_res == NULL) {
CHVGPIO_LOCK_DESTROY(sc);
device_printf(dev, "can't allocate memory resource\n");
return (ENOMEM);
}
sc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ,
&sc->sc_irq_rid, RF_ACTIVE);
if (!sc->sc_irq_res) {
CHVGPIO_LOCK_DESTROY(sc);
bus_release_resource(dev, SYS_RES_MEMORY,
sc->sc_mem_rid, sc->sc_mem_res);
device_printf(dev, "can't allocate irq resource\n");
return (ENOMEM);
}
error = bus_setup_intr(sc->sc_dev, sc->sc_irq_res, INTR_TYPE_MISC | INTR_MPSAFE,
NULL, chvgpio_intr, sc, &sc->intr_handle);
if (error) {
device_printf(sc->sc_dev, "unable to setup irq: error %d\n", error);
CHVGPIO_LOCK_DESTROY(sc);
bus_release_resource(dev, SYS_RES_MEMORY,
sc->sc_mem_rid, sc->sc_mem_res);
bus_release_resource(dev, SYS_RES_IRQ,
sc->sc_irq_rid, sc->sc_irq_res);
return (ENXIO);
}
/* Mask and ack all interrupts. */
bus_write_4(sc->sc_mem_res, CHVGPIO_INTERRUPT_MASK, 0);
bus_write_4(sc->sc_mem_res, CHVGPIO_INTERRUPT_STATUS, 0xffff);
sc->sc_busdev = gpiobus_add_bus(dev);
if (sc->sc_busdev == NULL) {
CHVGPIO_LOCK_DESTROY(sc);
bus_release_resource(dev, SYS_RES_MEMORY,
sc->sc_mem_rid, sc->sc_mem_res);
bus_release_resource(dev, SYS_RES_IRQ,
sc->sc_irq_rid, sc->sc_irq_res);
return (ENXIO);
}
bus_attach_children(dev);
return (0);
}
static void
chvgpio_intr(void *arg)
{
struct chvgpio_softc *sc = arg;
uint32_t reg;
int line;
reg = bus_read_4(sc->sc_mem_res, CHVGPIO_INTERRUPT_STATUS);
for (line = 0; line < 16; line++) {
if ((reg & (1 << line)) == 0)
continue;
bus_write_4(sc->sc_mem_res, CHVGPIO_INTERRUPT_STATUS, 1 << line);
}
}
static int
chvgpio_detach(device_t dev)
{
struct chvgpio_softc *sc;
sc = device_get_softc(dev);
if (sc->sc_busdev)
gpiobus_detach_bus(dev);
if (sc->intr_handle != NULL)
bus_teardown_intr(sc->sc_dev, sc->sc_irq_res, sc->intr_handle);
if (sc->sc_irq_res != NULL)
bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irq_rid, sc->sc_irq_res);
if (sc->sc_mem_res != NULL)
bus_release_resource(dev, SYS_RES_MEMORY, sc->sc_mem_rid, sc->sc_mem_res);
CHVGPIO_LOCK_DESTROY(sc);
return (0);
}
static device_method_t chvgpio_methods[] = {
DEVMETHOD(device_probe, chvgpio_probe),
DEVMETHOD(device_attach, chvgpio_attach),
DEVMETHOD(device_detach, chvgpio_detach),
/* GPIO protocol */
DEVMETHOD(gpio_get_bus, chvgpio_get_bus),
DEVMETHOD(gpio_pin_max, chvgpio_pin_max),
DEVMETHOD(gpio_pin_getname, chvgpio_pin_getname),
DEVMETHOD(gpio_pin_getflags, chvgpio_pin_getflags),
DEVMETHOD(gpio_pin_getcaps, chvgpio_pin_getcaps),
DEVMETHOD(gpio_pin_setflags, chvgpio_pin_setflags),
DEVMETHOD(gpio_pin_get, chvgpio_pin_get),
DEVMETHOD(gpio_pin_set, chvgpio_pin_set),
DEVMETHOD(gpio_pin_toggle, chvgpio_pin_toggle),
DEVMETHOD_END
};
static driver_t chvgpio_driver = {
.name = "gpio",
.methods = chvgpio_methods,
.size = sizeof(struct chvgpio_softc)
};
DRIVER_MODULE(chvgpio, acpi, chvgpio_driver, NULL, NULL);
MODULE_DEPEND(chvgpio, acpi, 1, 1, 1);
MODULE_DEPEND(chvgpio, gpiobus, 1, 1, 1);
MODULE_VERSION(chvgpio, 1);
|