xref: /netbsd/sys/arch/powerpc/ibm4xx/dev/gpio_opb.c (revision beecddb6)
1*beecddb6Sthorpej /*	$NetBSD: gpio_opb.c,v 1.10 2021/08/07 16:19:03 thorpej Exp $	*/
217dda527Sshige 
317dda527Sshige /*
417dda527Sshige  * Copyright (c) 2004 Shigeyuki Fukushima.
517dda527Sshige  * All rights reserved.
617dda527Sshige  *
717dda527Sshige  * Redistribution and use in source and binary forms, with or without
817dda527Sshige  * modification, are permitted provided that the following conditions
917dda527Sshige  * are met:
1017dda527Sshige  * 1. Redistributions of source code must retain the above copyright
1117dda527Sshige  *    notice, this list of conditions and the following disclaimer.
1217dda527Sshige  * 2. Redistributions in binary form must reproduce the above
1317dda527Sshige  *    copyright notice, this list of conditions and the following
1417dda527Sshige  *    disclaimer in the documentation and/or other materials provided
1517dda527Sshige  *    with the distribution.
1617dda527Sshige  * 3. The name of the author may not be used to endorse or promote
1717dda527Sshige  *    products derived from this software without specific prior
1817dda527Sshige  *    written permission.
1917dda527Sshige  *
2017dda527Sshige  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
2117dda527Sshige  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
2217dda527Sshige  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2317dda527Sshige  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
2417dda527Sshige  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2517dda527Sshige  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
2617dda527Sshige  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2717dda527Sshige  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
2817dda527Sshige  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
2917dda527Sshige  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
3017dda527Sshige  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3117dda527Sshige  */
3217dda527Sshige 
3317dda527Sshige #include "locators.h"
3417dda527Sshige 
3517dda527Sshige #include <sys/param.h>
3617dda527Sshige #include <sys/device.h>
3717dda527Sshige #include <sys/systm.h>
3817dda527Sshige 
3917dda527Sshige #include <machine/pio.h>
4017dda527Sshige 
4117629c72Sshige #include <sys/gpio.h>
4217629c72Sshige #include <dev/gpio/gpiovar.h>
4317629c72Sshige 
4417dda527Sshige #include <powerpc/ibm4xx/dev/opbvar.h>
4517dda527Sshige #include <powerpc/ibm4xx/dev/gpioreg.h>
4617dda527Sshige 
4717629c72Sshige struct gpio_opb_softc {
48d85eb0eaSmatt 	device_t		sc_dev;		/* device generic */
4917629c72Sshige 	/* GPIO interface */
5017629c72Sshige 	bus_space_tag_t		sc_gpio_iot;
5117629c72Sshige 	bus_space_handle_t	sc_gpio_ioh;
5217629c72Sshige 	struct gpio_chipset_tag	sc_gpio_gc;
5317629c72Sshige 	gpio_pin_t		sc_gpio_pins[GPIO_NPINS];
5417dda527Sshige };
5517dda527Sshige 
56d85eb0eaSmatt static int	gpio_opb_match(device_t, cfdata_t, void *);
57d85eb0eaSmatt static void	gpio_opb_attach(device_t, device_t, void *);
5817dda527Sshige 
59d85eb0eaSmatt CFATTACH_DECL_NEW(opbgpio, sizeof(struct gpio_opb_softc),
6017629c72Sshige 	gpio_opb_match, gpio_opb_attach, NULL, NULL);
6117dda527Sshige 
6217629c72Sshige static int	gpio_opb_pin_read(void *, int);
6317629c72Sshige static void	gpio_opb_pin_write(void *, int, int);
6417629c72Sshige static void	gpio_opb_pin_ctl(void *, int, int);
6517dda527Sshige 
66d85eb0eaSmatt static inline uint32_t
gpio_read(struct gpio_opb_softc * sc,bus_size_t o)67d85eb0eaSmatt gpio_read(struct gpio_opb_softc *sc, bus_size_t o)
68d85eb0eaSmatt {
69d85eb0eaSmatt 	return bus_space_read_4(sc->sc_gpio_iot, sc->sc_gpio_ioh, o);
70d85eb0eaSmatt }
71d85eb0eaSmatt 
72d85eb0eaSmatt static inline void
gpio_write(struct gpio_opb_softc * sc,bus_size_t o,uint32_t v)73d85eb0eaSmatt gpio_write(struct gpio_opb_softc *sc, bus_size_t o, uint32_t v)
74d85eb0eaSmatt {
75d85eb0eaSmatt 	bus_space_write_4(sc->sc_gpio_iot, sc->sc_gpio_ioh, o, v);
76d85eb0eaSmatt }
77d85eb0eaSmatt 
78d85eb0eaSmatt static inline void
gpio_set(struct gpio_opb_softc * sc,bus_size_t o,uint32_t v)79d85eb0eaSmatt gpio_set(struct gpio_opb_softc *sc, bus_size_t o, uint32_t v)
80d85eb0eaSmatt {
81d85eb0eaSmatt 	gpio_write(sc, o, gpio_read(sc, o) | v);
82d85eb0eaSmatt }
83d85eb0eaSmatt 
84d85eb0eaSmatt static inline void
gpio_clear(struct gpio_opb_softc * sc,bus_size_t o,uint32_t v)85d85eb0eaSmatt gpio_clear(struct gpio_opb_softc *sc, bus_size_t o, uint32_t v)
86d85eb0eaSmatt {
87d85eb0eaSmatt 	gpio_write(sc, o, gpio_read(sc, o) & ~v);
88d85eb0eaSmatt }
8917dda527Sshige 
9017dda527Sshige static int
gpio_opb_match(device_t parent,cfdata_t cf,void * aux)91d85eb0eaSmatt gpio_opb_match(device_t parent, cfdata_t cf, void *aux)
9217dda527Sshige {
93d85eb0eaSmatt 	struct opb_attach_args * const oaa = aux;
9417dda527Sshige 
9517dda527Sshige 	if (strcmp(oaa->opb_name, cf->cf_name) != 0)
9617629c72Sshige 		return 0;
9717dda527Sshige 
9817629c72Sshige 	return 1;
9917dda527Sshige }
10017dda527Sshige 
10117dda527Sshige static void
gpio_opb_attach(device_t parent,device_t self,void * aux)102d85eb0eaSmatt gpio_opb_attach(device_t parent, device_t self, void *aux)
10317dda527Sshige {
104d85eb0eaSmatt 	struct gpio_opb_softc * const sc = device_private(self);
105d85eb0eaSmatt 	struct opb_attach_args * const oaa = aux;
10617629c72Sshige 	struct gpiobus_attach_args gba;
107d85eb0eaSmatt 	uint32_t reg_ir, reg_tcr, reg_odr;
10817dda527Sshige 
10917dda527Sshige 	aprint_naive(": GPIO controller\n");
11017dda527Sshige 	aprint_normal(": On-Chip GPIO controller\n");
11117dda527Sshige 
112d85eb0eaSmatt 	sc->sc_dev = self;
113d85eb0eaSmatt 
11417629c72Sshige 	/* Map GPIO I/O space */
11517629c72Sshige 	sc->sc_gpio_iot = oaa->opb_bt;
11617629c72Sshige 	bus_space_map(sc->sc_gpio_iot, oaa->opb_addr,
11717629c72Sshige 		GPIO_NREG, 0, &sc->sc_gpio_ioh);
11817dda527Sshige 
11917629c72Sshige 	/* Read current register status */
120d85eb0eaSmatt 	reg_ir  = gpio_read(sc, GPIO_IR);
121d85eb0eaSmatt 	reg_tcr = gpio_read(sc, GPIO_TCR);
122d85eb0eaSmatt 	reg_odr = gpio_read(sc, GPIO_ODR);
12317629c72Sshige 
1241fc6e767Ssimonb 	/* Initialize pins array */
125d85eb0eaSmatt 	gpio_pin_t *pin = sc->sc_gpio_pins;
126d85eb0eaSmatt 	for (u_int i = 0 ; i < GPIO_NPINS ; i++, pin++) {
127d85eb0eaSmatt 		const uint32_t pin_mask = 1 << GPIO_PIN_SHIFT(i + 1);
128d85eb0eaSmatt 		pin->pin_num = i;
129d85eb0eaSmatt 		pin->pin_caps = GPIO_PIN_INOUT
13017629c72Sshige 				 | GPIO_PIN_OPENDRAIN
13117629c72Sshige 				 | GPIO_PIN_TRISTATE;
13217629c72Sshige 
13317629c72Sshige 		/* current defaults */
134d85eb0eaSmatt 		pin->pin_flags =
135d85eb0eaSmatt 		    (reg_odr & pin_mask)
13617629c72Sshige 			? GPIO_PIN_OPENDRAIN
137d85eb0eaSmatt 			: ((reg_tcr & pin_mask)
13817629c72Sshige 			    ? GPIO_PIN_INOUT
13917629c72Sshige 			    : GPIO_PIN_TRISTATE);
140d85eb0eaSmatt 		pin->pin_state = (reg_ir & pin_mask) != 0;
141d85eb0eaSmatt 		pin->pin_mapped = 0;
14217629c72Sshige 	}
14317629c72Sshige 
14417629c72Sshige 	/* Create controller tag */
14517629c72Sshige 	sc->sc_gpio_gc.gp_cookie = sc;
14617629c72Sshige 	sc->sc_gpio_gc.gp_pin_read = gpio_opb_pin_read;
14717629c72Sshige 	sc->sc_gpio_gc.gp_pin_write = gpio_opb_pin_write;
14817629c72Sshige 	sc->sc_gpio_gc.gp_pin_ctl = gpio_opb_pin_ctl;
14917629c72Sshige 
15017629c72Sshige 	gba.gba_gc = &sc->sc_gpio_gc;
15117629c72Sshige 	gba.gba_pins = sc->sc_gpio_pins;
15217629c72Sshige 	gba.gba_npins = GPIO_NPINS;
15317629c72Sshige 
15417629c72Sshige 	/* Attach GPIO framework */
155*beecddb6Sthorpej 	(void) config_found(self, &gba, gpiobus_print, CFARGS_NONE);
15617dda527Sshige }
15717dda527Sshige 
15817dda527Sshige static int
gpio_opb_pin_read(void * arg,int pin)15917629c72Sshige gpio_opb_pin_read(void *arg, int pin)
16017dda527Sshige {
161d85eb0eaSmatt 	struct gpio_opb_softc * const sc = arg;
162d85eb0eaSmatt 	const u_int p = (pin % GPIO_NPINS) + 1;
163d85eb0eaSmatt 	uint32_t reg_ir = gpio_read(sc, GPIO_IR);
16417dda527Sshige 
165d85eb0eaSmatt 	return (reg_ir >> GPIO_PIN_SHIFT(p)) & 0x01;
16617dda527Sshige }
16717dda527Sshige 
16817dda527Sshige static void
gpio_opb_pin_write(void * arg,int pin,int value)16917629c72Sshige gpio_opb_pin_write(void *arg, int pin, int value)
17017dda527Sshige {
171d85eb0eaSmatt 	struct gpio_opb_softc * const sc = arg;
172d85eb0eaSmatt 	const u_int p = (pin % GPIO_NPINS) + 1;
173d85eb0eaSmatt 	const uint32_t pin_mask = 1 << GPIO_PIN_SHIFT(p);
17417629c72Sshige 
17517629c72Sshige 	if (value == 0) {
176d85eb0eaSmatt 		gpio_clear(sc, GPIO_OR, pin_mask);
17717629c72Sshige 	} else if (value == 1) {
178d85eb0eaSmatt 		gpio_set(sc, GPIO_OR, pin_mask);
17917629c72Sshige 	}
18017dda527Sshige }
18117dda527Sshige 
18217dda527Sshige static void
gpio_opb_pin_ctl(void * arg,int pin,int flags)18317629c72Sshige gpio_opb_pin_ctl(void *arg, int pin, int flags)
18417dda527Sshige {
185d85eb0eaSmatt 	struct gpio_opb_softc * const sc = arg;
186d85eb0eaSmatt 	const u_int p = (pin % GPIO_NPINS) + 1;
187d85eb0eaSmatt 	const uint32_t pin_mask = 1 << GPIO_PIN_SHIFT(p);
18817629c72Sshige 
18917629c72Sshige 	if (flags & GPIO_PIN_INOUT) {
19017629c72Sshige 		/* GPIOn_ODR register bit is 0 */
191d85eb0eaSmatt 		gpio_clear(sc, GPIO_ODR, pin_mask);
192d85eb0eaSmatt 
19317629c72Sshige 		/* GPIOn_TCR register bit is 1 */
194d85eb0eaSmatt 		gpio_set(sc, GPIO_TCR, pin_mask);
19517dda527Sshige 	}
19617dda527Sshige 
19717629c72Sshige 	if (flags & GPIO_PIN_TRISTATE) {
19817629c72Sshige 		/* GPIOn_ODR register bit is 0 */
199d85eb0eaSmatt 		gpio_clear(sc, GPIO_ODR, pin_mask);
200d85eb0eaSmatt 
20117629c72Sshige 		/* GPIOn_TCR register bit is 0 */
202d85eb0eaSmatt 		gpio_clear(sc, GPIO_TCR, pin_mask);
20317dda527Sshige 	}
20417dda527Sshige 
20517629c72Sshige 	if (flags & GPIO_PIN_OPENDRAIN) {
20617629c72Sshige 		/* GPIOn_ODR register bit is 1 */
207d85eb0eaSmatt 		gpio_set(sc, GPIO_ODR, pin_mask);
20817dda527Sshige 	}
20917dda527Sshige }
210