xref: /netbsd-src/sys/arch/mips/atheros/dev/argpio.c (revision c7fb772b85b2b5d4cfb282f868f454b4701534fd)
1*c7fb772bSthorpej /* $NetBSD: argpio.c,v 1.10 2021/08/07 16:18:58 thorpej Exp $ */
2d266f72aSgdamore 
3d266f72aSgdamore /*-
4d266f72aSgdamore  * Copyright (c) 2006 Garrett D'Amore
5d266f72aSgdamore  * All rights reserved.
6d266f72aSgdamore  *
7d266f72aSgdamore  * Written by Garrett D'Amore.
8d266f72aSgdamore  *
9d266f72aSgdamore  * Redistribution and use in source and binary forms, with or without
10d266f72aSgdamore  * modification, are permitted provided that the following conditions
11d266f72aSgdamore  * are met:
12d266f72aSgdamore  * 1. Redistributions of source code must retain the above copyright
13d266f72aSgdamore  *    notice, this list of conditions and the following disclaimer.
14d266f72aSgdamore  * 2. Redistributions in binary form must reproduce the above copyright
15d266f72aSgdamore  *    notice, this list of conditions and the following disclaimer in the
16d266f72aSgdamore  *    documentation and/or other materials provided with the distribution.
17d266f72aSgdamore  * 3. The name of the author may not be used to endorse
18d266f72aSgdamore  *    or promote products derived from this software without specific
19d266f72aSgdamore  *    prior written permission.
20d266f72aSgdamore  *
21d266f72aSgdamore  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
22d266f72aSgdamore  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23d266f72aSgdamore  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24d266f72aSgdamore  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
25d266f72aSgdamore  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26d266f72aSgdamore  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27d266f72aSgdamore  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28d266f72aSgdamore  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29d266f72aSgdamore  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30d266f72aSgdamore  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31d266f72aSgdamore  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32d266f72aSgdamore  */
33d266f72aSgdamore 
34d266f72aSgdamore #include <sys/cdefs.h>
35*c7fb772bSthorpej __KERNEL_RCSID(0, "$NetBSD: argpio.c,v 1.10 2021/08/07 16:18:58 thorpej Exp $");
36d266f72aSgdamore 
37d266f72aSgdamore #include <sys/param.h>
38fa40faf6Smatt #include <sys/bus.h>
39d266f72aSgdamore #include <sys/conf.h>
40d266f72aSgdamore #include <sys/device.h>
41d266f72aSgdamore #include <sys/gpio.h>
42fa40faf6Smatt #include <sys/intr.h>
43fa40faf6Smatt #include <sys/kernel.h>
44fa40faf6Smatt #include <sys/types.h>
45d266f72aSgdamore 
46d266f72aSgdamore #include <mips/atheros/include/arbusvar.h>
47d266f72aSgdamore 
48d266f72aSgdamore #include <dev/gpio/gpiovar.h>
49d266f72aSgdamore #include <dev/sysmon/sysmonvar.h>
50d266f72aSgdamore #include <dev/sysmon/sysmon_taskq.h>
51d266f72aSgdamore 
52d266f72aSgdamore #include <mips/atheros/dev/argpioreg.h>
53d266f72aSgdamore 
54d266f72aSgdamore /*
55d266f72aSgdamore  * General Plan:
56d266f72aSgdamore  *
57d266f72aSgdamore  * Register GPIOs for all pins that are _not_ associated with the reset
5858d8753bSnia  * pin.  (Possibly also not the system LED.)
59d266f72aSgdamore  */
60d266f72aSgdamore 
61d266f72aSgdamore struct argpio_softc {
62fa40faf6Smatt 	device_t		sc_dev;
63d266f72aSgdamore 	struct gpio_chipset_tag	sc_gc;
64d266f72aSgdamore 	gpio_pin_t		sc_pins[ARGPIO_NPINS];
65d266f72aSgdamore 	int			sc_npins;
66d266f72aSgdamore 	bus_space_tag_t		sc_st;
67d266f72aSgdamore 	bus_space_handle_t	sc_sh;
68d266f72aSgdamore 	bus_size_t		sc_size;
69d266f72aSgdamore 	int			sc_caps;
70d266f72aSgdamore 	struct sysmon_pswitch	sc_resetbtn;
71d266f72aSgdamore 	void			*sc_ih;
72d266f72aSgdamore 	int			sc_rstpin;
731f585717Sgdamore 	int			sc_ledpin;
74d266f72aSgdamore };
75d266f72aSgdamore 
76fa40faf6Smatt static int argpio_match(device_t, cfdata_t, void *);
77fa40faf6Smatt static void argpio_attach(device_t, device_t, void *);
78d266f72aSgdamore static int argpio_intr(void *);
79d266f72aSgdamore static void argpio_reset_pressed(void *);
80d266f72aSgdamore static void argpio_ctl(void *, int, int);
81d266f72aSgdamore static void argpio_write(void *, int, int);
82d266f72aSgdamore static int argpio_read(void *, int);
83d266f72aSgdamore 
84fa40faf6Smatt CFATTACH_DECL_NEW(argpio, sizeof (struct argpio_softc), argpio_match,
85d266f72aSgdamore     argpio_attach, NULL, NULL);
86d266f72aSgdamore 
87d266f72aSgdamore #define	INPUT(pin)	(1 << (pin))		/* input bit */
88d266f72aSgdamore #define	INTR(pin)	(1 << ((pin) + 8))	/* interrupt bit */
89d266f72aSgdamore #define	SERIAL(pin)	(1 << ((pin) + 16))	/* serial mux bit */
90d266f72aSgdamore 
91d266f72aSgdamore #define	GETREG(sc, o)		bus_space_read_4(sc->sc_st, sc->sc_sh, o)
92d266f72aSgdamore #define	PUTREG(sc, o, v)	bus_space_write_4(sc->sc_st, sc->sc_sh, o, v)
93d266f72aSgdamore #define	FLUSH(sc)		bus_space_barrier(sc->sc_st, sc->sc_sh, \
94f52dcd49Sjdolecek 				0, 12, BUS_SPACE_BARRIER_READ|BUS_SPACE_BARRIER_WRITE)
95d266f72aSgdamore 
96d266f72aSgdamore int
argpio_match(device_t parent,cfdata_t match,void * aux)97fa40faf6Smatt argpio_match(device_t parent, cfdata_t match, void *aux)
98d266f72aSgdamore {
99d266f72aSgdamore 	struct arbus_attach_args *aa = aux;
100d266f72aSgdamore 
101d266f72aSgdamore 	return ((strcmp(aa->aa_name, "argpio") == 0) ? 1 : 0);
102d266f72aSgdamore }
103d266f72aSgdamore 
104d266f72aSgdamore void
argpio_attach(device_t parent,device_t self,void * aux)105fa40faf6Smatt argpio_attach(device_t parent, device_t self, void *aux)
106d266f72aSgdamore {
107fa40faf6Smatt 	struct argpio_softc *sc = device_private(self);
108d266f72aSgdamore 	struct arbus_attach_args *aa = aux;
109d266f72aSgdamore 	struct gpiobus_attach_args gba;
1101f585717Sgdamore 	prop_number_t	pn;
1111f585717Sgdamore 	int i;
112d266f72aSgdamore 	uint32_t reg;
113d266f72aSgdamore 
114fa40faf6Smatt 	sc->sc_dev = self;
115d266f72aSgdamore 	sc->sc_st = aa->aa_bst;
116d266f72aSgdamore 	sc->sc_npins = ARGPIO_NPINS;
117d266f72aSgdamore 	sc->sc_size = aa->aa_size;
1181f585717Sgdamore 	sc->sc_ledpin = -1;
1191f585717Sgdamore 	sc->sc_rstpin = -1;
120d266f72aSgdamore 
121d266f72aSgdamore 	if (bus_space_map(sc->sc_st, aa->aa_addr, sc->sc_size, 0,
122d266f72aSgdamore 		&sc->sc_sh) != 0) {
123d266f72aSgdamore 		printf(": unable to map registers!\n");
124d266f72aSgdamore 		return;
125d266f72aSgdamore 	}
126d266f72aSgdamore 
127d266f72aSgdamore 	sc->sc_gc.gp_cookie = sc;
128d266f72aSgdamore 	sc->sc_gc.gp_pin_read = argpio_read;
129d266f72aSgdamore 	sc->sc_gc.gp_pin_write = argpio_write;
130d266f72aSgdamore 	sc->sc_gc.gp_pin_ctl = argpio_ctl;
131d266f72aSgdamore 
132d266f72aSgdamore 	aprint_normal(": Atheros AR531X GPIO");
133fa40faf6Smatt 	pn = prop_dictionary_get(device_properties(sc->sc_dev), "reset-pin");
1341f585717Sgdamore 	if (pn != NULL) {
1351f585717Sgdamore 		KASSERT(prop_object_type(pn) == PROP_TYPE_NUMBER);
1361f585717Sgdamore 		sc->sc_rstpin = (int)prop_number_integer_value(pn);
1371f585717Sgdamore 		aprint_normal(", reset button pin %d", sc->sc_rstpin);
138d266f72aSgdamore 	}
139fa40faf6Smatt 	pn = prop_dictionary_get(device_properties(sc->sc_dev), "sysled-pin");
1401f585717Sgdamore 	if (pn != NULL) {
1411f585717Sgdamore 		KASSERT(prop_object_type(pn) == PROP_TYPE_NUMBER);
1421f585717Sgdamore 		sc->sc_ledpin = (int)prop_number_integer_value(pn);
1431f585717Sgdamore 		aprint_normal(", system led pin %d", sc->sc_ledpin);
144d266f72aSgdamore 	}
1451f585717Sgdamore 
146fa40faf6Smatt 	aprint_normal("\n");
147d266f72aSgdamore 
1481f585717Sgdamore 	if (sc->sc_ledpin) {
1491f585717Sgdamore 		sc->sc_ih = arbus_intr_establish(aa->aa_cirq, aa->aa_mirq,
1501f585717Sgdamore 		    argpio_intr, sc);
151d266f72aSgdamore 		if (sc->sc_ih == NULL) {
152fa40faf6Smatt 			aprint_error_dev(sc->sc_dev,
153fa40faf6Smatt 			    "couldn't establish interrupt\n");
154d266f72aSgdamore 		}
155d266f72aSgdamore 	}
156d266f72aSgdamore 
157d266f72aSgdamore 	if (sc->sc_ih) {
158d266f72aSgdamore 		sysmon_task_queue_init();
159d266f72aSgdamore 
160fa40faf6Smatt 		sc->sc_resetbtn.smpsw_name = device_xname(sc->sc_dev);
161d266f72aSgdamore 		sc->sc_resetbtn.smpsw_type = PSWITCH_TYPE_RESET;
162e72ff6f3Sdyoung 		if (sysmon_pswitch_register(&sc->sc_resetbtn) != 0) {
163e72ff6f3Sdyoung 			aprint_normal_dev(sc->sc_dev,
164e72ff6f3Sdyoung 			    "unable to register reset button\n");
165e72ff6f3Sdyoung 		}
166d266f72aSgdamore 	}
167d266f72aSgdamore 
168d266f72aSgdamore 	reg = GETREG(sc, GPIO_CR);
169d266f72aSgdamore 
170d266f72aSgdamore 	for (i = 0; i < sc->sc_npins; i++) {
171d266f72aSgdamore 		gpio_pin_t	*pp;
172d266f72aSgdamore 
173d266f72aSgdamore 		pp = &sc->sc_pins[i];
174d266f72aSgdamore 
1751f585717Sgdamore 		if (i == sc->sc_rstpin) {
176d266f72aSgdamore 			/* configure as interrupt for reset */
177d266f72aSgdamore 			pp->pin_caps = GPIO_PIN_INPUT;
178d266f72aSgdamore 			reg &= ~SERIAL(i);
179d266f72aSgdamore 			reg |= INPUT(i);
180d266f72aSgdamore 			/* only if we were able to set up the handler, tho' */
181d266f72aSgdamore 			if (sc->sc_ih != NULL)
182d266f72aSgdamore 				reg |= INTR(i);
183d266f72aSgdamore 
1841f585717Sgdamore 		} else if (i == sc->sc_ledpin) {
185d266f72aSgdamore 			/* configure as output for LED */
186d266f72aSgdamore 			pp->pin_caps = GPIO_PIN_OUTPUT;
187d266f72aSgdamore 			reg &= ~SERIAL(i);
188d266f72aSgdamore 			reg &= ~INPUT(i);
189d266f72aSgdamore 			reg &= ~INTR(i);
190d266f72aSgdamore 
191d266f72aSgdamore 		} else {
192d266f72aSgdamore 			if (reg & SERIAL(i)) {
193d266f72aSgdamore 				/* pin multiplexed with serial bit */
194d266f72aSgdamore 				pp->pin_caps = 0;
195d266f72aSgdamore 			} else {
196d266f72aSgdamore 				pp->pin_caps = GPIO_PIN_INPUT |
197d266f72aSgdamore 				    GPIO_PIN_OUTPUT;
198d266f72aSgdamore 			}
199d266f72aSgdamore 		}
200d266f72aSgdamore 	}
201d266f72aSgdamore 
202d266f72aSgdamore 	PUTREG(sc, GPIO_CR, reg);
203d266f72aSgdamore 	FLUSH(sc);
204d266f72aSgdamore 
205d266f72aSgdamore 	gba.gba_gc = &sc->sc_gc;
206d266f72aSgdamore 	gba.gba_pins = sc->sc_pins;
207d266f72aSgdamore 	gba.gba_npins = sc->sc_npins;
208*c7fb772bSthorpej 	config_found(sc->sc_dev, &gba, gpiobus_print, CFARGS_NONE);
209d266f72aSgdamore }
210d266f72aSgdamore 
211d266f72aSgdamore void
argpio_ctl(void * arg,int pin,int flags)212d266f72aSgdamore argpio_ctl(void *arg, int pin, int flags)
213d266f72aSgdamore {
214d266f72aSgdamore 	struct argpio_softc	*sc = arg;
215d266f72aSgdamore 	uint32_t		reg;
216d266f72aSgdamore 
217d266f72aSgdamore 	reg = GETREG(sc, GPIO_CR);
218d266f72aSgdamore 	if (reg & (SERIAL(pin) | INTR(pin))) {
219d266f72aSgdamore 		printf("pin %d cannot be changed!\n", pin);
220d266f72aSgdamore 		/* don't allow changes to these bits */
221d266f72aSgdamore 		return;
222d266f72aSgdamore 	}
223d266f72aSgdamore 	if (flags & GPIO_PIN_INPUT) {
224d266f72aSgdamore 		reg |= INPUT(pin);
225d266f72aSgdamore 	} else {
226d266f72aSgdamore 		reg &= ~INPUT(pin);
227d266f72aSgdamore 	}
228d266f72aSgdamore 
229d266f72aSgdamore 	PUTREG(sc, GPIO_CR, reg);
230d266f72aSgdamore 	FLUSH(sc);
231d266f72aSgdamore }
232d266f72aSgdamore 
233d266f72aSgdamore void
argpio_write(void * arg,int pin,int value)234d266f72aSgdamore argpio_write(void *arg, int pin, int value)
235d266f72aSgdamore {
236d266f72aSgdamore 	struct argpio_softc	*sc = arg;
237d266f72aSgdamore 	uint32_t		reg;
238d266f72aSgdamore 
239d266f72aSgdamore 	reg = GETREG(sc, GPIO_DO);
240d266f72aSgdamore 	if (value)
241d266f72aSgdamore 		reg &= ~(1 << pin);
242d266f72aSgdamore 	else
243d266f72aSgdamore 		reg |= (1 << pin);
244d266f72aSgdamore 	PUTREG(sc, GPIO_DO, reg);
245d266f72aSgdamore 	FLUSH(sc);
246d266f72aSgdamore }
247d266f72aSgdamore 
248d266f72aSgdamore int
argpio_read(void * arg,int pin)249d266f72aSgdamore argpio_read(void *arg, int pin)
250d266f72aSgdamore {
251d266f72aSgdamore 	struct argpio_softc	*sc = arg;
252d266f72aSgdamore 
253d266f72aSgdamore 	return ((GETREG(sc, GPIO_DI) & (1 << pin)) ?
254d266f72aSgdamore 	    GPIO_PIN_HIGH : GPIO_PIN_LOW);
255d266f72aSgdamore }
256d266f72aSgdamore 
257d266f72aSgdamore void
argpio_reset_pressed(void * arg)258d266f72aSgdamore argpio_reset_pressed(void *arg)
259d266f72aSgdamore {
260d266f72aSgdamore 	struct argpio_softc	*sc = arg;
261d266f72aSgdamore 	int			x;
262d266f72aSgdamore 
263d266f72aSgdamore 	sysmon_pswitch_event(&sc->sc_resetbtn, PSWITCH_EVENT_PRESSED);
264d266f72aSgdamore 
265d266f72aSgdamore 	/* reenable the interrupt */
266d266f72aSgdamore 	x = splhigh();
267d266f72aSgdamore 	PUTREG(sc, GPIO_CR,
268d266f72aSgdamore 	    GETREG(sc, GPIO_CR) | INTR(sc->sc_rstpin));
269d266f72aSgdamore 	splx(x);
270d266f72aSgdamore }
271d266f72aSgdamore 
272d266f72aSgdamore int
argpio_intr(void * arg)273d266f72aSgdamore argpio_intr(void  *arg)
274d266f72aSgdamore {
275d266f72aSgdamore 	struct argpio_softc	*sc = arg;
276d266f72aSgdamore 
277d266f72aSgdamore 	if (sc->sc_rstpin < 0)
278d266f72aSgdamore 		return 0;
279d266f72aSgdamore 
280d266f72aSgdamore 	/* this is an edge triggered interrupt, so disable it for now */
281d266f72aSgdamore 	PUTREG(sc, GPIO_CR, GETREG(sc, GPIO_CR) & ~INTR(sc->sc_rstpin));
282d266f72aSgdamore 
283d266f72aSgdamore 	/* no other interrupt on this, so we have to claim it */
284d266f72aSgdamore 	sysmon_task_queue_sched(0, argpio_reset_pressed, sc);
285d266f72aSgdamore 
286d266f72aSgdamore 	return 1;
287d266f72aSgdamore }
288