1228925Sgonzo/*-
2228925Sgonzo * Copyright (c) 2011, Oleksandr Tymoshenko <gonzo@FreeBSD.org>
3228925Sgonzo * All rights reserved.
4228925Sgonzo *
5228925Sgonzo * Redistribution and use in source and binary forms, with or without
6228925Sgonzo * modification, are permitted provided that the following conditions
7228925Sgonzo * are met:
8228925Sgonzo * 1. Redistributions of source code must retain the above copyright
9228925Sgonzo *    notice unmodified, this list of conditions, and the following
10228925Sgonzo *    disclaimer.
11228925Sgonzo * 2. Redistributions in binary form must reproduce the above copyright
12228925Sgonzo *    notice, this list of conditions and the following disclaimer in the
13228925Sgonzo *    documentation and/or other materials provided with the distribution.
14228925Sgonzo *
15228925Sgonzo * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16228925Sgonzo * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17228925Sgonzo * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18228925Sgonzo * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19228925Sgonzo * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20228925Sgonzo * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21228925Sgonzo * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22228925Sgonzo * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23228925Sgonzo * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24228925Sgonzo * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25228925Sgonzo * SUCH DAMAGE.
26228925Sgonzo */
27228925Sgonzo
28228925Sgonzo/*
29228925Sgonzo * GPIO driver for Cavium Octeon
30228925Sgonzo */
31228925Sgonzo
32228925Sgonzo#include <sys/cdefs.h>
33228925Sgonzo__FBSDID("$FreeBSD: releng/10.3/sys/mips/cavium/octeon_gpio.c 278786 2015-02-14 21:16:19Z loos $");
34228925Sgonzo
35228925Sgonzo#include <sys/param.h>
36228925Sgonzo#include <sys/systm.h>
37228925Sgonzo#include <sys/bus.h>
38228925Sgonzo
39228925Sgonzo#include <sys/kernel.h>
40228925Sgonzo#include <sys/module.h>
41228925Sgonzo#include <sys/rman.h>
42228925Sgonzo#include <sys/lock.h>
43228925Sgonzo#include <sys/mutex.h>
44228925Sgonzo#include <sys/gpio.h>
45228925Sgonzo
46228925Sgonzo#include <machine/bus.h>
47228925Sgonzo#include <machine/resource.h>
48228925Sgonzo
49228925Sgonzo#include <contrib/octeon-sdk/cvmx.h>
50228925Sgonzo#include <contrib/octeon-sdk/cvmx-gpio.h>
51232812Sjmallett#include <mips/cavium/octeon_irq.h>
52228925Sgonzo
53228925Sgonzo#include <mips/cavium/octeon_gpiovar.h>
54228925Sgonzo
55228925Sgonzo#include "gpio_if.h"
56228925Sgonzo
57228925Sgonzo#define	DEFAULT_CAPS	(GPIO_PIN_INPUT | GPIO_PIN_OUTPUT)
58228925Sgonzo
59228925Sgonzostruct octeon_gpio_pin {
60228925Sgonzo	const char *name;
61228925Sgonzo	int pin;
62228925Sgonzo	int flags;
63228925Sgonzo};
64228925Sgonzo
65228925Sgonzo/*
66228925Sgonzo * on CAP100 GPIO 7 is "Factory defaults" button
67228925Sgonzo *
68228925Sgonzo */
69228925Sgonzostatic struct octeon_gpio_pin octeon_gpio_pins[] = {
70228925Sgonzo	{ "F/D", 7,  GPIO_PIN_INPUT},
71228925Sgonzo	{ NULL, 0, 0},
72228925Sgonzo};
73228925Sgonzo
74228925Sgonzo/*
75228925Sgonzo * Helpers
76228925Sgonzo */
77228925Sgonzostatic void octeon_gpio_pin_configure(struct octeon_gpio_softc *sc,
78228925Sgonzo    struct gpio_pin *pin, uint32_t flags);
79228925Sgonzo
80228925Sgonzo/*
81228925Sgonzo * Driver stuff
82228925Sgonzo */
83228925Sgonzostatic void octeon_gpio_identify(driver_t *, device_t);
84228925Sgonzostatic int octeon_gpio_probe(device_t dev);
85228925Sgonzostatic int octeon_gpio_attach(device_t dev);
86228925Sgonzostatic int octeon_gpio_detach(device_t dev);
87228925Sgonzostatic int octeon_gpio_filter(void *arg);
88228925Sgonzostatic void octeon_gpio_intr(void *arg);
89228925Sgonzo
90228925Sgonzo/*
91228925Sgonzo * GPIO interface
92228925Sgonzo */
93228925Sgonzostatic int octeon_gpio_pin_max(device_t dev, int *maxpin);
94228925Sgonzostatic int octeon_gpio_pin_getcaps(device_t dev, uint32_t pin, uint32_t *caps);
95228925Sgonzostatic int octeon_gpio_pin_getflags(device_t dev, uint32_t pin, uint32_t
96228925Sgonzo    *flags);
97228925Sgonzostatic int octeon_gpio_pin_getname(device_t dev, uint32_t pin, char *name);
98228925Sgonzostatic int octeon_gpio_pin_setflags(device_t dev, uint32_t pin, uint32_t flags);
99228925Sgonzostatic int octeon_gpio_pin_set(device_t dev, uint32_t pin, unsigned int value);
100228925Sgonzostatic int octeon_gpio_pin_get(device_t dev, uint32_t pin, unsigned int *val);
101228925Sgonzostatic int octeon_gpio_pin_toggle(device_t dev, uint32_t pin);
102228925Sgonzo
103228925Sgonzostatic void
104228925Sgonzoocteon_gpio_pin_configure(struct octeon_gpio_softc *sc, struct gpio_pin *pin,
105228925Sgonzo    unsigned int flags)
106228925Sgonzo{
107228925Sgonzo	uint32_t mask;
108228925Sgonzo	cvmx_gpio_bit_cfgx_t gpio_cfgx;
109228925Sgonzo
110228925Sgonzo	mask = 1 << pin->gp_pin;
111228925Sgonzo	GPIO_LOCK(sc);
112228925Sgonzo
113228925Sgonzo	/*
114228925Sgonzo	 * Manage input/output
115228925Sgonzo	 */
116228925Sgonzo	if (flags & (GPIO_PIN_INPUT|GPIO_PIN_OUTPUT)) {
117228925Sgonzo		gpio_cfgx.u64 = cvmx_read_csr(CVMX_GPIO_BIT_CFGX(pin->gp_pin));
118228925Sgonzo		pin->gp_flags &= ~(GPIO_PIN_INPUT|GPIO_PIN_OUTPUT);
119228925Sgonzo		if (flags & GPIO_PIN_OUTPUT) {
120228925Sgonzo			pin->gp_flags |= GPIO_PIN_OUTPUT;
121228925Sgonzo			gpio_cfgx.s.tx_oe = 1;
122228925Sgonzo		}
123228925Sgonzo		else {
124228925Sgonzo			pin->gp_flags |= GPIO_PIN_INPUT;
125228925Sgonzo			gpio_cfgx.s.tx_oe = 0;
126228925Sgonzo		}
127228925Sgonzo		if (flags & GPIO_PIN_INVIN)
128228925Sgonzo			gpio_cfgx.s.rx_xor = 1;
129228925Sgonzo		else
130228925Sgonzo			gpio_cfgx.s.rx_xor = 0;
131228925Sgonzo		cvmx_write_csr(CVMX_GPIO_BIT_CFGX(pin->gp_pin), gpio_cfgx.u64);
132228925Sgonzo	}
133228925Sgonzo
134228925Sgonzo	GPIO_UNLOCK(sc);
135228925Sgonzo}
136228925Sgonzo
137228925Sgonzostatic int
138228925Sgonzoocteon_gpio_pin_max(device_t dev, int *maxpin)
139228925Sgonzo{
140228925Sgonzo
141228925Sgonzo	*maxpin = OCTEON_GPIO_PINS - 1;
142228925Sgonzo	return (0);
143228925Sgonzo}
144228925Sgonzo
145228925Sgonzostatic int
146228925Sgonzoocteon_gpio_pin_getcaps(device_t dev, uint32_t pin, uint32_t *caps)
147228925Sgonzo{
148228925Sgonzo	struct octeon_gpio_softc *sc = device_get_softc(dev);
149228925Sgonzo	int i;
150228925Sgonzo
151228925Sgonzo	for (i = 0; i < sc->gpio_npins; i++) {
152228925Sgonzo		if (sc->gpio_pins[i].gp_pin == pin)
153228925Sgonzo			break;
154228925Sgonzo	}
155228925Sgonzo
156228925Sgonzo	if (i >= sc->gpio_npins)
157228925Sgonzo		return (EINVAL);
158228925Sgonzo
159228925Sgonzo	GPIO_LOCK(sc);
160228925Sgonzo	*caps = sc->gpio_pins[i].gp_caps;
161228925Sgonzo	GPIO_UNLOCK(sc);
162228925Sgonzo
163228925Sgonzo	return (0);
164228925Sgonzo}
165228925Sgonzo
166228925Sgonzostatic int
167228925Sgonzoocteon_gpio_pin_getflags(device_t dev, uint32_t pin, uint32_t *flags)
168228925Sgonzo{
169228925Sgonzo	struct octeon_gpio_softc *sc = device_get_softc(dev);
170228925Sgonzo	int i;
171228925Sgonzo
172228925Sgonzo	for (i = 0; i < sc->gpio_npins; i++) {
173228925Sgonzo		if (sc->gpio_pins[i].gp_pin == pin)
174228925Sgonzo			break;
175228925Sgonzo	}
176228925Sgonzo
177228925Sgonzo	if (i >= sc->gpio_npins)
178228925Sgonzo		return (EINVAL);
179228925Sgonzo
180228925Sgonzo	GPIO_LOCK(sc);
181228925Sgonzo	*flags = sc->gpio_pins[i].gp_flags;
182228925Sgonzo	GPIO_UNLOCK(sc);
183228925Sgonzo
184228925Sgonzo	return (0);
185228925Sgonzo}
186228925Sgonzo
187228925Sgonzostatic int
188228925Sgonzoocteon_gpio_pin_getname(device_t dev, uint32_t pin, char *name)
189228925Sgonzo{
190228925Sgonzo	struct octeon_gpio_softc *sc = device_get_softc(dev);
191228925Sgonzo	int i;
192228925Sgonzo
193228925Sgonzo	for (i = 0; i < sc->gpio_npins; i++) {
194228925Sgonzo		if (sc->gpio_pins[i].gp_pin == pin)
195228925Sgonzo			break;
196228925Sgonzo	}
197228925Sgonzo
198228925Sgonzo	if (i >= sc->gpio_npins)
199228925Sgonzo		return (EINVAL);
200228925Sgonzo
201228925Sgonzo	GPIO_LOCK(sc);
202228925Sgonzo	memcpy(name, sc->gpio_pins[i].gp_name, GPIOMAXNAME);
203228925Sgonzo	GPIO_UNLOCK(sc);
204228925Sgonzo
205228925Sgonzo	return (0);
206228925Sgonzo}
207228925Sgonzo
208228925Sgonzostatic int
209228925Sgonzoocteon_gpio_pin_setflags(device_t dev, uint32_t pin, uint32_t flags)
210228925Sgonzo{
211228925Sgonzo	int i;
212228925Sgonzo	struct octeon_gpio_softc *sc = device_get_softc(dev);
213228925Sgonzo
214228925Sgonzo	for (i = 0; i < sc->gpio_npins; i++) {
215228925Sgonzo		if (sc->gpio_pins[i].gp_pin == pin)
216228925Sgonzo			break;
217228925Sgonzo	}
218228925Sgonzo
219228925Sgonzo	if (i >= sc->gpio_npins)
220228925Sgonzo		return (EINVAL);
221228925Sgonzo
222278786Sloos	octeon_gpio_pin_configure(sc, &sc->gpio_pins[i], flags);
223228925Sgonzo
224228925Sgonzo	return (0);
225228925Sgonzo}
226228925Sgonzo
227228925Sgonzostatic int
228228925Sgonzoocteon_gpio_pin_set(device_t dev, uint32_t pin, unsigned int value)
229228925Sgonzo{
230228925Sgonzo	struct octeon_gpio_softc *sc = device_get_softc(dev);
231228925Sgonzo	int i;
232228925Sgonzo
233228925Sgonzo	for (i = 0; i < sc->gpio_npins; i++) {
234228925Sgonzo		if (sc->gpio_pins[i].gp_pin == pin)
235228925Sgonzo			break;
236228925Sgonzo	}
237228925Sgonzo
238228925Sgonzo	if (i >= sc->gpio_npins)
239228925Sgonzo		return (EINVAL);
240228925Sgonzo
241228925Sgonzo	GPIO_LOCK(sc);
242228925Sgonzo	if (value)
243228925Sgonzo		cvmx_gpio_set(1 << pin);
244228925Sgonzo	else
245228925Sgonzo		cvmx_gpio_clear(1 << pin);
246228925Sgonzo	GPIO_UNLOCK(sc);
247228925Sgonzo
248228925Sgonzo	return (0);
249228925Sgonzo}
250228925Sgonzo
251228925Sgonzostatic int
252228925Sgonzoocteon_gpio_pin_get(device_t dev, uint32_t pin, unsigned int *val)
253228925Sgonzo{
254228925Sgonzo	struct octeon_gpio_softc *sc = device_get_softc(dev);
255228925Sgonzo	int i;
256228925Sgonzo	uint64_t state;
257228925Sgonzo
258228925Sgonzo	for (i = 0; i < sc->gpio_npins; i++) {
259228925Sgonzo		if (sc->gpio_pins[i].gp_pin == pin)
260228925Sgonzo			break;
261228925Sgonzo	}
262228925Sgonzo
263228925Sgonzo	if (i >= sc->gpio_npins)
264228925Sgonzo		return (EINVAL);
265228925Sgonzo
266228925Sgonzo	GPIO_LOCK(sc);
267228925Sgonzo	state = cvmx_gpio_read();
268228925Sgonzo	*val = (state & (1 << pin)) ? 1 : 0;
269228925Sgonzo	GPIO_UNLOCK(sc);
270228925Sgonzo
271228925Sgonzo	return (0);
272228925Sgonzo}
273228925Sgonzo
274228925Sgonzostatic int
275228925Sgonzoocteon_gpio_pin_toggle(device_t dev, uint32_t pin)
276228925Sgonzo{
277228925Sgonzo	int i;
278228925Sgonzo	uint64_t state;
279228925Sgonzo	struct octeon_gpio_softc *sc = device_get_softc(dev);
280228925Sgonzo
281228925Sgonzo	for (i = 0; i < sc->gpio_npins; i++) {
282228925Sgonzo		if (sc->gpio_pins[i].gp_pin == pin)
283228925Sgonzo			break;
284228925Sgonzo	}
285228925Sgonzo
286228925Sgonzo	if (i >= sc->gpio_npins)
287228925Sgonzo		return (EINVAL);
288228925Sgonzo
289228925Sgonzo	GPIO_LOCK(sc);
290228925Sgonzo	/*
291228925Sgonzo	 * XXX: Need to check if read returns actual state of output
292228925Sgonzo	 * pins or we need to keep this information by ourself
293228925Sgonzo	 */
294228925Sgonzo	state = cvmx_gpio_read();
295228925Sgonzo	if (state & (1 << pin))
296228925Sgonzo		cvmx_gpio_clear(1 << pin);
297228925Sgonzo	else
298228925Sgonzo		cvmx_gpio_set(1 << pin);
299228925Sgonzo	GPIO_UNLOCK(sc);
300228925Sgonzo
301228925Sgonzo	return (0);
302228925Sgonzo}
303228925Sgonzo
304228925Sgonzostatic int
305228925Sgonzoocteon_gpio_filter(void *arg)
306228925Sgonzo{
307228925Sgonzo	cvmx_gpio_bit_cfgx_t gpio_cfgx;
308228925Sgonzo	void **cookie = arg;
309228925Sgonzo	struct octeon_gpio_softc *sc = *cookie;
310228925Sgonzo	long int irq = (cookie - sc->gpio_intr_cookies);
311228925Sgonzo
312228925Sgonzo	if ((irq < 0) || (irq >= OCTEON_GPIO_IRQS))
313228925Sgonzo		return (FILTER_STRAY);
314228925Sgonzo
315228925Sgonzo	gpio_cfgx.u64 = cvmx_read_csr(CVMX_GPIO_BIT_CFGX(irq));
316228925Sgonzo	/* Clear rising edge detector */
317228925Sgonzo	if (gpio_cfgx.s.int_type == OCTEON_GPIO_IRQ_EDGE)
318228925Sgonzo		cvmx_gpio_interrupt_clear(1 << irq);
319228925Sgonzo	/* disable interrupt  */
320228925Sgonzo	gpio_cfgx.s.int_en = 0;
321228925Sgonzo	cvmx_write_csr(CVMX_GPIO_BIT_CFGX(irq), gpio_cfgx.u64);
322228925Sgonzo
323228925Sgonzo	return (FILTER_SCHEDULE_THREAD);
324228925Sgonzo}
325228925Sgonzo
326228925Sgonzostatic void
327228925Sgonzoocteon_gpio_intr(void *arg)
328228925Sgonzo{
329228925Sgonzo	cvmx_gpio_bit_cfgx_t gpio_cfgx;
330228925Sgonzo	void **cookie = arg;
331228925Sgonzo	struct octeon_gpio_softc *sc = *cookie;
332228925Sgonzo	long int irq = (cookie - sc->gpio_intr_cookies);
333228925Sgonzo
334228925Sgonzo	if ((irq < 0) || (irq >= OCTEON_GPIO_IRQS)) {
335228925Sgonzo		printf("%s: invalid GPIO IRQ: %ld\n",
336228925Sgonzo		    __func__, irq);
337228925Sgonzo		return;
338228925Sgonzo	}
339228925Sgonzo
340228925Sgonzo	GPIO_LOCK(sc);
341228925Sgonzo	gpio_cfgx.u64 = cvmx_read_csr(CVMX_GPIO_BIT_CFGX(irq));
342228925Sgonzo	/* disable interrupt  */
343228925Sgonzo	gpio_cfgx.s.int_en = 1;
344228925Sgonzo	cvmx_write_csr(CVMX_GPIO_BIT_CFGX(irq), gpio_cfgx.u64);
345228925Sgonzo
346228925Sgonzo	/* TODO: notify bus here or something */
347228925Sgonzo	printf("GPIO IRQ for pin %ld\n", irq);
348228925Sgonzo	GPIO_UNLOCK(sc);
349228925Sgonzo}
350228925Sgonzo
351228925Sgonzostatic void
352228925Sgonzoocteon_gpio_identify(driver_t *drv, device_t parent)
353228925Sgonzo{
354228925Sgonzo
355228925Sgonzo	BUS_ADD_CHILD(parent, 0, "gpio", 0);
356228925Sgonzo}
357228925Sgonzo
358228925Sgonzostatic int
359228925Sgonzoocteon_gpio_probe(device_t dev)
360228925Sgonzo{
361228925Sgonzo
362228925Sgonzo	device_set_desc(dev, "Cavium Octeon GPIO driver");
363228925Sgonzo	return (0);
364228925Sgonzo}
365228925Sgonzo
366228925Sgonzostatic int
367228925Sgonzoocteon_gpio_attach(device_t dev)
368228925Sgonzo{
369228925Sgonzo	struct octeon_gpio_softc *sc = device_get_softc(dev);
370228925Sgonzo	struct octeon_gpio_pin *pinp;
371228925Sgonzo	cvmx_gpio_bit_cfgx_t gpio_cfgx;
372228925Sgonzo
373228925Sgonzo	int i;
374228925Sgonzo
375228925Sgonzo	KASSERT((device_get_unit(dev) == 0),
376228925Sgonzo	    ("octeon_gpio: Only one gpio module supported"));
377228925Sgonzo
378239351Srpaulo	mtx_init(&sc->gpio_mtx, device_get_nameunit(dev), NULL, MTX_DEF);
379228925Sgonzo
380228925Sgonzo	for ( i = 0; i < OCTEON_GPIO_IRQS; i++) {
381228925Sgonzo		if ((sc->gpio_irq_res[i] = bus_alloc_resource(dev,
382228925Sgonzo		    SYS_RES_IRQ, &sc->gpio_irq_rid[i],
383232812Sjmallett		    OCTEON_IRQ_GPIO0 + i, OCTEON_IRQ_GPIO0 + i, 1,
384228925Sgonzo		    RF_SHAREABLE | RF_ACTIVE)) == NULL) {
385228925Sgonzo			device_printf(dev, "unable to allocate IRQ resource\n");
386228925Sgonzo			return (ENXIO);
387228925Sgonzo		}
388228925Sgonzo
389228925Sgonzo		sc->gpio_intr_cookies[i] = sc;
390228925Sgonzo		if ((bus_setup_intr(dev, sc->gpio_irq_res[i], INTR_TYPE_MISC,
391228925Sgonzo	    	    octeon_gpio_filter, octeon_gpio_intr,
392228925Sgonzo		    &(sc->gpio_intr_cookies[i]), &sc->gpio_ih[i]))) {
393228925Sgonzo			device_printf(dev,
394228925Sgonzo		    	"WARNING: unable to register interrupt handler\n");
395228925Sgonzo			return (ENXIO);
396228925Sgonzo		}
397228925Sgonzo	}
398228925Sgonzo
399228925Sgonzo	sc->dev = dev;
400228925Sgonzo	/* Configure all pins as input */
401228925Sgonzo	/* disable interrupts for all pins */
402228925Sgonzo	pinp = octeon_gpio_pins;
403228925Sgonzo	i = 0;
404228925Sgonzo	while (pinp->name) {
405228925Sgonzo		strncpy(sc->gpio_pins[i].gp_name, pinp->name, GPIOMAXNAME);
406228925Sgonzo		sc->gpio_pins[i].gp_pin = pinp->pin;
407228925Sgonzo		sc->gpio_pins[i].gp_caps = DEFAULT_CAPS;
408228925Sgonzo		sc->gpio_pins[i].gp_flags = 0;
409228925Sgonzo		octeon_gpio_pin_configure(sc, &sc->gpio_pins[i], pinp->flags);
410228925Sgonzo		pinp++;
411228925Sgonzo		i++;
412228925Sgonzo	}
413228925Sgonzo
414228925Sgonzo	sc->gpio_npins = i;
415228925Sgonzo
416228925Sgonzo#if 0
417228925Sgonzo	/*
418228925Sgonzo	 * Sample: how to enable edge-triggered interrupt
419228925Sgonzo	 * for GPIO pin
420228925Sgonzo	 */
421228925Sgonzo	gpio_cfgx.u64 = cvmx_read_csr(CVMX_GPIO_BIT_CFGX(7));
422228925Sgonzo	gpio_cfgx.s.int_en = 1;
423228925Sgonzo	gpio_cfgx.s.int_type = OCTEON_GPIO_IRQ_EDGE;
424228925Sgonzo	cvmx_write_csr(CVMX_GPIO_BIT_CFGX(7), gpio_cfgx.u64);
425228925Sgonzo#endif
426228925Sgonzo
427228925Sgonzo	if (bootverbose) {
428228925Sgonzo		for (i = 0; i < 16; i++) {
429228925Sgonzo			gpio_cfgx.u64 = cvmx_read_csr(CVMX_GPIO_BIT_CFGX(i));
430228925Sgonzo			device_printf(dev, "[pin%d] output=%d, invinput=%d, intr=%d, intr_type=%s\n",
431228925Sgonzo			    i, gpio_cfgx.s.tx_oe, gpio_cfgx.s.rx_xor,
432228925Sgonzo			    gpio_cfgx.s.int_en, gpio_cfgx.s.int_type ? "rising edge" : "level");
433228925Sgonzo		}
434228925Sgonzo	}
435228925Sgonzo
436278782Sloos	device_add_child(dev, "gpioc", -1);
437278782Sloos	device_add_child(dev, "gpiobus", -1);
438278782Sloos
439228925Sgonzo	return (bus_generic_attach(dev));
440228925Sgonzo}
441228925Sgonzo
442228925Sgonzostatic int
443228925Sgonzoocteon_gpio_detach(device_t dev)
444228925Sgonzo{
445228925Sgonzo	struct octeon_gpio_softc *sc = device_get_softc(dev);
446228925Sgonzo	int i;
447228925Sgonzo
448228925Sgonzo	KASSERT(mtx_initialized(&sc->gpio_mtx), ("gpio mutex not initialized"));
449228925Sgonzo
450228925Sgonzo	for ( i = 0; i < OCTEON_GPIO_IRQS; i++) {
451228925Sgonzo		bus_release_resource(dev, SYS_RES_IRQ,
452228925Sgonzo		    sc->gpio_irq_rid[i], sc->gpio_irq_res[i]);
453228925Sgonzo	}
454228925Sgonzo	bus_generic_detach(dev);
455228925Sgonzo
456228925Sgonzo	mtx_destroy(&sc->gpio_mtx);
457228925Sgonzo
458228925Sgonzo	return(0);
459228925Sgonzo}
460228925Sgonzo
461228925Sgonzostatic device_method_t octeon_gpio_methods[] = {
462228925Sgonzo	DEVMETHOD(device_identify, octeon_gpio_identify),
463228925Sgonzo	DEVMETHOD(device_probe, octeon_gpio_probe),
464228925Sgonzo	DEVMETHOD(device_attach, octeon_gpio_attach),
465228925Sgonzo	DEVMETHOD(device_detach, octeon_gpio_detach),
466228925Sgonzo
467228925Sgonzo	/* GPIO protocol */
468228925Sgonzo	DEVMETHOD(gpio_pin_max, octeon_gpio_pin_max),
469228925Sgonzo	DEVMETHOD(gpio_pin_getname, octeon_gpio_pin_getname),
470228925Sgonzo	DEVMETHOD(gpio_pin_getflags, octeon_gpio_pin_getflags),
471228925Sgonzo	DEVMETHOD(gpio_pin_getcaps, octeon_gpio_pin_getcaps),
472228925Sgonzo	DEVMETHOD(gpio_pin_setflags, octeon_gpio_pin_setflags),
473228925Sgonzo	DEVMETHOD(gpio_pin_get, octeon_gpio_pin_get),
474228925Sgonzo	DEVMETHOD(gpio_pin_set, octeon_gpio_pin_set),
475228925Sgonzo	DEVMETHOD(gpio_pin_toggle, octeon_gpio_pin_toggle),
476228925Sgonzo	{0, 0},
477228925Sgonzo};
478228925Sgonzo
479228925Sgonzostatic driver_t octeon_gpio_driver = {
480228925Sgonzo	"gpio",
481228925Sgonzo	octeon_gpio_methods,
482228925Sgonzo	sizeof(struct octeon_gpio_softc),
483228925Sgonzo};
484228925Sgonzostatic devclass_t octeon_gpio_devclass;
485228925Sgonzo
486228925SgonzoDRIVER_MODULE(octeon_gpio, ciu, octeon_gpio_driver, octeon_gpio_devclass, 0, 0);
487