1 /*-
2 * Copyright (c) 2016 Oleksandr Tymoshenko <gonzo@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27 #include <sys/cdefs.h>
28 #include "opt_acpi.h"
29
30 #include <sys/param.h>
31 #include <sys/bus.h>
32 #include <sys/gpio.h>
33 #include <sys/kernel.h>
34 #include <sys/module.h>
35 #include <sys/proc.h>
36 #include <sys/rman.h>
37
38 #include <machine/bus.h>
39 #include <machine/resource.h>
40
41 #include <contrib/dev/acpica/include/acpi.h>
42 #include <contrib/dev/acpica/include/accommon.h>
43
44 #include <dev/acpica/acpivar.h>
45 #include <dev/gpio/gpiobusvar.h>
46
47 #include "gpio_if.h"
48
49 /**
50 * Macros for driver mutex locking
51 */
52 #define BYTGPIO_LOCK(_sc) mtx_lock_spin(&(_sc)->sc_mtx)
53 #define BYTGPIO_UNLOCK(_sc) mtx_unlock_spin(&(_sc)->sc_mtx)
54 #define BYTGPIO_LOCK_INIT(_sc) \
55 mtx_init(&_sc->sc_mtx, device_get_nameunit((_sc)->sc_dev), \
56 "bytgpio", MTX_SPIN)
57 #define BYTGPIO_LOCK_DESTROY(_sc) mtx_destroy(&(_sc)->sc_mtx)
58 #define BYTGPIO_ASSERT_LOCKED(_sc) mtx_assert(&(_sc)->sc_mtx, MA_OWNED)
59 #define BYTGPIO_ASSERT_UNLOCKED(_sc) mtx_assert(&(_sc)->sc_mtx, MA_NOTOWNED)
60
61 struct pinmap_info {
62 int reg;
63 int pad_func;
64 };
65
66 /* Ignore function check, no info is available at the moment */
67 #define PADCONF_FUNC_ANY -1
68
69 #define GPIO_PIN_MAP(r, f) { .reg = (r), .pad_func = (f) }
70
71 struct bytgpio_softc {
72 ACPI_HANDLE sc_handle;
73 device_t sc_dev;
74 device_t sc_busdev;
75 struct mtx sc_mtx;
76 int sc_mem_rid;
77 struct resource *sc_mem_res;
78 int sc_npins;
79 const char* sc_bank_prefix;
80 const struct pinmap_info *sc_pinpad_map;
81 /* List of current functions for pads shared by GPIO */
82 int *sc_pad_funcs;
83 };
84
85 static int bytgpio_probe(device_t dev);
86 static int bytgpio_attach(device_t dev);
87 static int bytgpio_detach(device_t dev);
88
89 #define SCORE_UID 1
90 #define SCORE_BANK_PREFIX "GPIO_S0_SC"
91 const struct pinmap_info bytgpio_score_pins[] = {
92 GPIO_PIN_MAP(85, 0),
93 GPIO_PIN_MAP(89, 0),
94 GPIO_PIN_MAP(93, 0),
95 GPIO_PIN_MAP(96, 0),
96 GPIO_PIN_MAP(99, 0),
97 GPIO_PIN_MAP(102, 0),
98 GPIO_PIN_MAP(98, 0),
99 GPIO_PIN_MAP(101, 0),
100 GPIO_PIN_MAP(34, 0),
101 GPIO_PIN_MAP(37, 0),
102 GPIO_PIN_MAP(36, 0),
103 GPIO_PIN_MAP(38, 0),
104 GPIO_PIN_MAP(39, 0),
105 GPIO_PIN_MAP(35, 0),
106 GPIO_PIN_MAP(40, 0),
107 GPIO_PIN_MAP(84, 0),
108 GPIO_PIN_MAP(62, 0),
109 GPIO_PIN_MAP(61, 0),
110 GPIO_PIN_MAP(64, 0),
111 GPIO_PIN_MAP(59, 0),
112 GPIO_PIN_MAP(54, 0),
113 GPIO_PIN_MAP(56, 0),
114 GPIO_PIN_MAP(60, 0),
115 GPIO_PIN_MAP(55, 0),
116 GPIO_PIN_MAP(63, 0),
117 GPIO_PIN_MAP(57, 0),
118 GPIO_PIN_MAP(51, 0),
119 GPIO_PIN_MAP(50, 0),
120 GPIO_PIN_MAP(53, 0),
121 GPIO_PIN_MAP(47, 0),
122 GPIO_PIN_MAP(52, 0),
123 GPIO_PIN_MAP(49, 0),
124 GPIO_PIN_MAP(48, 0),
125 GPIO_PIN_MAP(43, 0),
126 GPIO_PIN_MAP(46, 0),
127 GPIO_PIN_MAP(41, 0),
128 GPIO_PIN_MAP(45, 0),
129 GPIO_PIN_MAP(42, 0),
130 GPIO_PIN_MAP(58, 0),
131 GPIO_PIN_MAP(44, 0),
132 GPIO_PIN_MAP(95, 0),
133 GPIO_PIN_MAP(105, 0),
134 GPIO_PIN_MAP(70, 0),
135 GPIO_PIN_MAP(68, 0),
136 GPIO_PIN_MAP(67, 0),
137 GPIO_PIN_MAP(66, 0),
138 GPIO_PIN_MAP(69, 0),
139 GPIO_PIN_MAP(71, 0),
140 GPIO_PIN_MAP(65, 0),
141 GPIO_PIN_MAP(72, 0),
142 GPIO_PIN_MAP(86, 0),
143 GPIO_PIN_MAP(90, 0),
144 GPIO_PIN_MAP(88, 0),
145 GPIO_PIN_MAP(92, 0),
146 GPIO_PIN_MAP(103, 0),
147 GPIO_PIN_MAP(77, 0),
148 GPIO_PIN_MAP(79, 0),
149 GPIO_PIN_MAP(83, 0),
150 GPIO_PIN_MAP(78, 0),
151 GPIO_PIN_MAP(81, 0),
152 GPIO_PIN_MAP(80, 0),
153 GPIO_PIN_MAP(82, 0),
154 GPIO_PIN_MAP(13, 0),
155 GPIO_PIN_MAP(12, 0),
156 GPIO_PIN_MAP(15, 0),
157 GPIO_PIN_MAP(14, 0),
158 GPIO_PIN_MAP(17, 0),
159 GPIO_PIN_MAP(18, 0),
160 GPIO_PIN_MAP(19, 0),
161 GPIO_PIN_MAP(16, 0),
162 GPIO_PIN_MAP(2, 0),
163 GPIO_PIN_MAP(1, 0),
164 GPIO_PIN_MAP(0, 0),
165 GPIO_PIN_MAP(4, 0),
166 GPIO_PIN_MAP(6, 0),
167 GPIO_PIN_MAP(7, 0),
168 GPIO_PIN_MAP(9, 0),
169 GPIO_PIN_MAP(8, 0),
170 GPIO_PIN_MAP(33, 0),
171 GPIO_PIN_MAP(32, 0),
172 GPIO_PIN_MAP(31, 0),
173 GPIO_PIN_MAP(30, 0),
174 GPIO_PIN_MAP(29, 0),
175 GPIO_PIN_MAP(27, 0),
176 GPIO_PIN_MAP(25, 0),
177 GPIO_PIN_MAP(28, 0),
178 GPIO_PIN_MAP(26, 0),
179 GPIO_PIN_MAP(23, 0),
180 GPIO_PIN_MAP(21, 0),
181 GPIO_PIN_MAP(20, 0),
182 GPIO_PIN_MAP(24, 0),
183 GPIO_PIN_MAP(22, 0),
184 GPIO_PIN_MAP(5, 1),
185 GPIO_PIN_MAP(3, 1),
186 GPIO_PIN_MAP(10, 0),
187 GPIO_PIN_MAP(11, 0),
188 GPIO_PIN_MAP(106, 0),
189 GPIO_PIN_MAP(87, 0),
190 GPIO_PIN_MAP(91, 0),
191 GPIO_PIN_MAP(104, 0),
192 GPIO_PIN_MAP(97, 0),
193 GPIO_PIN_MAP(100, 0)
194 };
195
196 #define SCORE_PINS nitems(bytgpio_score_pins)
197
198 #define NCORE_UID 2
199 #define NCORE_BANK_PREFIX "GPIO_S0_NC"
200 const struct pinmap_info bytgpio_ncore_pins[] = {
201 GPIO_PIN_MAP(19, PADCONF_FUNC_ANY),
202 GPIO_PIN_MAP(18, PADCONF_FUNC_ANY),
203 GPIO_PIN_MAP(17, PADCONF_FUNC_ANY),
204 GPIO_PIN_MAP(20, PADCONF_FUNC_ANY),
205 GPIO_PIN_MAP(21, PADCONF_FUNC_ANY),
206 GPIO_PIN_MAP(22, PADCONF_FUNC_ANY),
207 GPIO_PIN_MAP(24, PADCONF_FUNC_ANY),
208 GPIO_PIN_MAP(25, PADCONF_FUNC_ANY),
209 GPIO_PIN_MAP(23, PADCONF_FUNC_ANY),
210 GPIO_PIN_MAP(16, PADCONF_FUNC_ANY),
211 GPIO_PIN_MAP(14, PADCONF_FUNC_ANY),
212 GPIO_PIN_MAP(15, PADCONF_FUNC_ANY),
213 GPIO_PIN_MAP(12, PADCONF_FUNC_ANY),
214 GPIO_PIN_MAP(26, PADCONF_FUNC_ANY),
215 GPIO_PIN_MAP(27, PADCONF_FUNC_ANY),
216 GPIO_PIN_MAP(1, PADCONF_FUNC_ANY),
217 GPIO_PIN_MAP(4, PADCONF_FUNC_ANY),
218 GPIO_PIN_MAP(8, PADCONF_FUNC_ANY),
219 GPIO_PIN_MAP(11, PADCONF_FUNC_ANY),
220 GPIO_PIN_MAP(0, PADCONF_FUNC_ANY),
221 GPIO_PIN_MAP(3, PADCONF_FUNC_ANY),
222 GPIO_PIN_MAP(6, PADCONF_FUNC_ANY),
223 GPIO_PIN_MAP(10, PADCONF_FUNC_ANY),
224 GPIO_PIN_MAP(13, PADCONF_FUNC_ANY),
225 GPIO_PIN_MAP(2, PADCONF_FUNC_ANY),
226 GPIO_PIN_MAP(5, PADCONF_FUNC_ANY),
227 GPIO_PIN_MAP(9, PADCONF_FUNC_ANY),
228 GPIO_PIN_MAP(7, PADCONF_FUNC_ANY)
229 };
230 #define NCORE_PINS nitems(bytgpio_ncore_pins)
231
232 #define SUS_UID 3
233 #define SUS_BANK_PREFIX "GPIO_S5_"
234 const struct pinmap_info bytgpio_sus_pins[] = {
235 GPIO_PIN_MAP(29, 0),
236 GPIO_PIN_MAP(33, 0),
237 GPIO_PIN_MAP(30, 0),
238 GPIO_PIN_MAP(31, 0),
239 GPIO_PIN_MAP(32, 0),
240 GPIO_PIN_MAP(34, 0),
241 GPIO_PIN_MAP(36, 0),
242 GPIO_PIN_MAP(35, 0),
243 GPIO_PIN_MAP(38, 0),
244 GPIO_PIN_MAP(37, 0),
245 GPIO_PIN_MAP(18, 0),
246 GPIO_PIN_MAP(7, 1),
247 GPIO_PIN_MAP(11, 1),
248 GPIO_PIN_MAP(20, 1),
249 GPIO_PIN_MAP(17, 1),
250 GPIO_PIN_MAP(1, 1),
251 GPIO_PIN_MAP(8, 1),
252 GPIO_PIN_MAP(10, 1),
253 GPIO_PIN_MAP(19, 1),
254 GPIO_PIN_MAP(12, 1),
255 GPIO_PIN_MAP(0, 1),
256 GPIO_PIN_MAP(2, 1),
257 GPIO_PIN_MAP(23, 0),
258 GPIO_PIN_MAP(39, 0),
259 GPIO_PIN_MAP(28, 0),
260 GPIO_PIN_MAP(27, 0),
261 GPIO_PIN_MAP(22, 0),
262 GPIO_PIN_MAP(21, 0),
263 GPIO_PIN_MAP(24, 0),
264 GPIO_PIN_MAP(25, 0),
265 GPIO_PIN_MAP(26, 0),
266 GPIO_PIN_MAP(51, 0),
267 GPIO_PIN_MAP(56, 0),
268 GPIO_PIN_MAP(54, 0),
269 GPIO_PIN_MAP(49, 0),
270 GPIO_PIN_MAP(55, 0),
271 GPIO_PIN_MAP(48, 0),
272 GPIO_PIN_MAP(57, 0),
273 GPIO_PIN_MAP(50, 0),
274 GPIO_PIN_MAP(58, 0),
275 GPIO_PIN_MAP(52, 0),
276 GPIO_PIN_MAP(53, 0),
277 GPIO_PIN_MAP(59, 0),
278 GPIO_PIN_MAP(40, 0)
279 };
280
281 #define SUS_PINS nitems(bytgpio_sus_pins)
282
283 #define BYGPIO_PIN_REGISTER(sc, pin, r) ((sc)->sc_pinpad_map[(pin)].reg * 16 + (r))
284 #define BYTGPIO_PCONF0 0x0000
285 #define BYTGPIO_PCONF0_FUNC_MASK 7
286 #define BYTGPIO_PAD_VAL 0x0008
287 #define BYTGPIO_PAD_VAL_LEVEL (1 << 0)
288 #define BYTGPIO_PAD_VAL_I_OUTPUT_ENABLED (1 << 1)
289 #define BYTGPIO_PAD_VAL_I_INPUT_ENABLED (1 << 2)
290 #define BYTGPIO_PAD_VAL_DIR_MASK (3 << 1)
291
292 static inline uint32_t
bytgpio_read_4(struct bytgpio_softc * sc,bus_size_t off)293 bytgpio_read_4(struct bytgpio_softc *sc, bus_size_t off)
294 {
295 return (bus_read_4(sc->sc_mem_res, off));
296 }
297
298 static inline void
bytgpio_write_4(struct bytgpio_softc * sc,bus_size_t off,uint32_t val)299 bytgpio_write_4(struct bytgpio_softc *sc, bus_size_t off,
300 uint32_t val)
301 {
302 bus_write_4(sc->sc_mem_res, off, val);
303 }
304
305 static device_t
bytgpio_get_bus(device_t dev)306 bytgpio_get_bus(device_t dev)
307 {
308 struct bytgpio_softc *sc;
309
310 sc = device_get_softc(dev);
311
312 return (sc->sc_busdev);
313 }
314
315 static int
bytgpio_pin_max(device_t dev,int * maxpin)316 bytgpio_pin_max(device_t dev, int *maxpin)
317 {
318 struct bytgpio_softc *sc;
319
320 sc = device_get_softc(dev);
321
322 *maxpin = sc->sc_npins - 1;
323
324 return (0);
325 }
326
327 static int
bytgpio_valid_pin(struct bytgpio_softc * sc,int pin)328 bytgpio_valid_pin(struct bytgpio_softc *sc, int pin)
329 {
330
331 if (pin >= sc->sc_npins || sc->sc_mem_res == NULL)
332 return (EINVAL);
333
334 return (0);
335 }
336
337 /*
338 * Returns true if pad configured to be used as GPIO
339 */
340 static bool
bytgpio_pad_is_gpio(struct bytgpio_softc * sc,int pin)341 bytgpio_pad_is_gpio(struct bytgpio_softc *sc, int pin)
342 {
343 if ((sc->sc_pinpad_map[pin].pad_func == PADCONF_FUNC_ANY) ||
344 (sc->sc_pad_funcs[pin] == sc->sc_pinpad_map[pin].pad_func))
345 return (true);
346 else
347 return (false);
348 }
349
350 static int
bytgpio_pin_getcaps(device_t dev,uint32_t pin,uint32_t * caps)351 bytgpio_pin_getcaps(device_t dev, uint32_t pin, uint32_t *caps)
352 {
353 struct bytgpio_softc *sc;
354
355 sc = device_get_softc(dev);
356 if (bytgpio_valid_pin(sc, pin) != 0)
357 return (EINVAL);
358
359 *caps = 0;
360 if (bytgpio_pad_is_gpio(sc, pin))
361 *caps = GPIO_PIN_INPUT | GPIO_PIN_OUTPUT;
362
363 return (0);
364 }
365
366 static int
bytgpio_pin_getflags(device_t dev,uint32_t pin,uint32_t * flags)367 bytgpio_pin_getflags(device_t dev, uint32_t pin, uint32_t *flags)
368 {
369 struct bytgpio_softc *sc;
370 uint32_t reg, val;
371
372 sc = device_get_softc(dev);
373 if (bytgpio_valid_pin(sc, pin) != 0)
374 return (EINVAL);
375
376 *flags = 0;
377 if (!bytgpio_pad_is_gpio(sc, pin))
378 return (0);
379
380 /* Get the current pin state */
381 BYTGPIO_LOCK(sc);
382 reg = BYGPIO_PIN_REGISTER(sc, pin, BYTGPIO_PAD_VAL);
383 val = bytgpio_read_4(sc, reg);
384 if ((val & BYTGPIO_PAD_VAL_I_OUTPUT_ENABLED) == 0)
385 *flags |= GPIO_PIN_OUTPUT;
386 /*
387 * this bit can be cleared to read current output value
388 * sou output bit takes precedense
389 */
390 else if ((val & BYTGPIO_PAD_VAL_I_INPUT_ENABLED) == 0)
391 *flags |= GPIO_PIN_INPUT;
392 BYTGPIO_UNLOCK(sc);
393
394 return (0);
395 }
396
397 static int
bytgpio_pin_setflags(device_t dev,uint32_t pin,uint32_t flags)398 bytgpio_pin_setflags(device_t dev, uint32_t pin, uint32_t flags)
399 {
400 struct bytgpio_softc *sc;
401 uint32_t reg, val;
402 uint32_t allowed;
403
404 sc = device_get_softc(dev);
405 if (bytgpio_valid_pin(sc, pin) != 0)
406 return (EINVAL);
407
408 if (bytgpio_pad_is_gpio(sc, pin))
409 allowed = GPIO_PIN_INPUT | GPIO_PIN_OUTPUT;
410 else
411 allowed = 0;
412
413 /*
414 * Only directtion flag allowed
415 */
416 if (flags & ~allowed)
417 return (EINVAL);
418
419 /*
420 * Not both directions simultaneously
421 */
422 if ((flags & allowed) == allowed)
423 return (EINVAL);
424
425 /* Set the GPIO mode and state */
426 BYTGPIO_LOCK(sc);
427 reg = BYGPIO_PIN_REGISTER(sc, pin, BYTGPIO_PAD_VAL);
428 val = bytgpio_read_4(sc, reg);
429 val = val | BYTGPIO_PAD_VAL_DIR_MASK;
430 if (flags & GPIO_PIN_INPUT)
431 val = val & ~BYTGPIO_PAD_VAL_I_INPUT_ENABLED;
432 if (flags & GPIO_PIN_OUTPUT)
433 val = val & ~BYTGPIO_PAD_VAL_I_OUTPUT_ENABLED;
434 bytgpio_write_4(sc, reg, val);
435 BYTGPIO_UNLOCK(sc);
436
437 return (0);
438 }
439
440 static int
bytgpio_pin_getname(device_t dev,uint32_t pin,char * name)441 bytgpio_pin_getname(device_t dev, uint32_t pin, char *name)
442 {
443 struct bytgpio_softc *sc;
444
445 sc = device_get_softc(dev);
446 if (bytgpio_valid_pin(sc, pin) != 0)
447 return (EINVAL);
448
449 /* Set a very simple name */
450 snprintf(name, GPIOMAXNAME, "%s%u", sc->sc_bank_prefix, pin);
451 name[GPIOMAXNAME - 1] = '\0';
452
453 return (0);
454 }
455
456 static int
bytgpio_pin_set(device_t dev,uint32_t pin,unsigned int value)457 bytgpio_pin_set(device_t dev, uint32_t pin, unsigned int value)
458 {
459 struct bytgpio_softc *sc;
460 uint32_t reg, val;
461
462 sc = device_get_softc(dev);
463 if (bytgpio_valid_pin(sc, pin) != 0)
464 return (EINVAL);
465
466 if (!bytgpio_pad_is_gpio(sc, pin))
467 return (EINVAL);
468
469 BYTGPIO_LOCK(sc);
470 reg = BYGPIO_PIN_REGISTER(sc, pin, BYTGPIO_PAD_VAL);
471 val = bytgpio_read_4(sc, reg);
472 if (value == GPIO_PIN_LOW)
473 val = val & ~BYTGPIO_PAD_VAL_LEVEL;
474 else
475 val = val | BYTGPIO_PAD_VAL_LEVEL;
476 bytgpio_write_4(sc, reg, val);
477 BYTGPIO_UNLOCK(sc);
478
479 return (0);
480 }
481
482 static int
bytgpio_pin_get(device_t dev,uint32_t pin,unsigned int * value)483 bytgpio_pin_get(device_t dev, uint32_t pin, unsigned int *value)
484 {
485 struct bytgpio_softc *sc;
486 uint32_t reg, val;
487
488 sc = device_get_softc(dev);
489 if (bytgpio_valid_pin(sc, pin) != 0)
490 return (EINVAL);
491 /*
492 * Report non-GPIO pads as pin LOW
493 */
494 if (!bytgpio_pad_is_gpio(sc, pin)) {
495 *value = GPIO_PIN_LOW;
496 return (0);
497 }
498
499 BYTGPIO_LOCK(sc);
500 reg = BYGPIO_PIN_REGISTER(sc, pin, BYTGPIO_PAD_VAL);
501 /*
502 * And read actual value
503 */
504 val = bytgpio_read_4(sc, reg);
505 if (val & BYTGPIO_PAD_VAL_LEVEL)
506 *value = GPIO_PIN_HIGH;
507 else
508 *value = GPIO_PIN_LOW;
509 BYTGPIO_UNLOCK(sc);
510
511 return (0);
512 }
513
514 static int
bytgpio_pin_toggle(device_t dev,uint32_t pin)515 bytgpio_pin_toggle(device_t dev, uint32_t pin)
516 {
517 struct bytgpio_softc *sc;
518 uint32_t reg, val;
519
520 sc = device_get_softc(dev);
521 if (bytgpio_valid_pin(sc, pin) != 0)
522 return (EINVAL);
523
524 if (!bytgpio_pad_is_gpio(sc, pin))
525 return (EINVAL);
526
527 /* Toggle the pin */
528 BYTGPIO_LOCK(sc);
529 reg = BYGPIO_PIN_REGISTER(sc, pin, BYTGPIO_PAD_VAL);
530 val = bytgpio_read_4(sc, reg);
531 val = val ^ BYTGPIO_PAD_VAL_LEVEL;
532 bytgpio_write_4(sc, reg, val);
533 BYTGPIO_UNLOCK(sc);
534
535 return (0);
536 }
537
538 static int
bytgpio_probe(device_t dev)539 bytgpio_probe(device_t dev)
540 {
541 static char *gpio_ids[] = { "INT33FC", NULL };
542 int rv;
543
544 if (acpi_disabled("gpio"))
545 return (ENXIO);
546 rv = ACPI_ID_PROBE(device_get_parent(dev), dev, gpio_ids, NULL);
547 if (rv <= 0)
548 device_set_desc(dev, "Intel Baytrail GPIO Controller");
549 return (rv);
550 }
551
552 static int
bytgpio_attach(device_t dev)553 bytgpio_attach(device_t dev)
554 {
555 struct bytgpio_softc *sc;
556 ACPI_STATUS status;
557 int uid;
558 int pin;
559 uint32_t reg, val;
560
561 sc = device_get_softc(dev);
562 sc->sc_dev = dev;
563 sc->sc_handle = acpi_get_handle(dev);
564 status = acpi_GetInteger(sc->sc_handle, "_UID", &uid);
565 if (ACPI_FAILURE(status)) {
566 device_printf(dev, "failed to read _UID\n");
567 return (ENXIO);
568 }
569
570 BYTGPIO_LOCK_INIT(sc);
571
572 switch (uid) {
573 case SCORE_UID:
574 sc->sc_npins = SCORE_PINS;
575 sc->sc_bank_prefix = SCORE_BANK_PREFIX;
576 sc->sc_pinpad_map = bytgpio_score_pins;
577 break;
578 case NCORE_UID:
579 sc->sc_npins = NCORE_PINS;
580 sc->sc_bank_prefix = NCORE_BANK_PREFIX;
581 sc->sc_pinpad_map = bytgpio_ncore_pins;
582 break;
583 case SUS_UID:
584 sc->sc_npins = SUS_PINS;
585 sc->sc_bank_prefix = SUS_BANK_PREFIX;
586 sc->sc_pinpad_map = bytgpio_sus_pins;
587 break;
588 default:
589 device_printf(dev, "invalid _UID value: %d\n", uid);
590 goto error;
591 }
592
593 sc->sc_pad_funcs = malloc(sizeof(int)*sc->sc_npins, M_DEVBUF,
594 M_WAITOK | M_ZERO);
595
596 sc->sc_mem_rid = 0;
597 sc->sc_mem_res = bus_alloc_resource_any(sc->sc_dev,
598 SYS_RES_MEMORY, &sc->sc_mem_rid, RF_ACTIVE);
599 if (sc->sc_mem_res == NULL) {
600 device_printf(dev, "can't allocate resource\n");
601 goto error;
602 }
603
604 for (pin = 0; pin < sc->sc_npins; pin++) {
605 reg = BYGPIO_PIN_REGISTER(sc, pin, BYTGPIO_PCONF0);
606 val = bytgpio_read_4(sc, reg);
607 sc->sc_pad_funcs[pin] = val & BYTGPIO_PCONF0_FUNC_MASK;
608 }
609
610 sc->sc_busdev = gpiobus_attach_bus(dev);
611 if (sc->sc_busdev == NULL) {
612 BYTGPIO_LOCK_DESTROY(sc);
613 bus_release_resource(dev, SYS_RES_MEMORY,
614 sc->sc_mem_rid, sc->sc_mem_res);
615 return (ENXIO);
616 }
617
618 return (0);
619
620 error:
621 BYTGPIO_LOCK_DESTROY(sc);
622
623 return (ENXIO);
624 }
625
626
627 static int
bytgpio_detach(device_t dev)628 bytgpio_detach(device_t dev)
629 {
630 struct bytgpio_softc *sc;
631
632 sc = device_get_softc(dev);
633
634 if (sc->sc_busdev)
635 gpiobus_detach_bus(dev);
636
637 BYTGPIO_LOCK_DESTROY(sc);
638
639 if (sc->sc_pad_funcs)
640 free(sc->sc_pad_funcs, M_DEVBUF);
641
642 if (sc->sc_mem_res != NULL)
643 bus_release_resource(dev, SYS_RES_MEMORY,
644 sc->sc_mem_rid, sc->sc_mem_res);
645
646 return (0);
647 }
648
649 static device_method_t bytgpio_methods[] = {
650 /* Device interface */
651 DEVMETHOD(device_probe, bytgpio_probe),
652 DEVMETHOD(device_attach, bytgpio_attach),
653 DEVMETHOD(device_detach, bytgpio_detach),
654
655 /* GPIO protocol */
656 DEVMETHOD(gpio_get_bus, bytgpio_get_bus),
657 DEVMETHOD(gpio_pin_max, bytgpio_pin_max),
658 DEVMETHOD(gpio_pin_getname, bytgpio_pin_getname),
659 DEVMETHOD(gpio_pin_getflags, bytgpio_pin_getflags),
660 DEVMETHOD(gpio_pin_getcaps, bytgpio_pin_getcaps),
661 DEVMETHOD(gpio_pin_setflags, bytgpio_pin_setflags),
662 DEVMETHOD(gpio_pin_get, bytgpio_pin_get),
663 DEVMETHOD(gpio_pin_set, bytgpio_pin_set),
664 DEVMETHOD(gpio_pin_toggle, bytgpio_pin_toggle),
665
666 DEVMETHOD_END
667 };
668
669 static driver_t bytgpio_driver = {
670 "gpio",
671 bytgpio_methods,
672 sizeof(struct bytgpio_softc),
673 };
674
675 DRIVER_MODULE(bytgpio, acpi, bytgpio_driver, 0, 0);
676 MODULE_DEPEND(bytgpio, acpi, 1, 1, 1);
677 MODULE_DEPEND(bytgpio, gpiobus, 1, 1, 1);
678