Home | History | Annotate | Line # | Download | only in dev
gpio_opb.c revision 1.9
      1  1.9  thorpej /*	$NetBSD: gpio_opb.c,v 1.9 2021/04/24 23:36:46 thorpej Exp $	*/
      2  1.1    shige 
      3  1.1    shige /*
      4  1.1    shige  * Copyright (c) 2004 Shigeyuki Fukushima.
      5  1.1    shige  * All rights reserved.
      6  1.1    shige  *
      7  1.1    shige  * Redistribution and use in source and binary forms, with or without
      8  1.1    shige  * modification, are permitted provided that the following conditions
      9  1.1    shige  * are met:
     10  1.1    shige  * 1. Redistributions of source code must retain the above copyright
     11  1.1    shige  *    notice, this list of conditions and the following disclaimer.
     12  1.1    shige  * 2. Redistributions in binary form must reproduce the above
     13  1.1    shige  *    copyright notice, this list of conditions and the following
     14  1.1    shige  *    disclaimer in the documentation and/or other materials provided
     15  1.1    shige  *    with the distribution.
     16  1.1    shige  * 3. The name of the author may not be used to endorse or promote
     17  1.1    shige  *    products derived from this software without specific prior
     18  1.1    shige  *    written permission.
     19  1.1    shige  *
     20  1.1    shige  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     21  1.1    shige  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     22  1.1    shige  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23  1.1    shige  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
     24  1.1    shige  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     25  1.1    shige  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
     26  1.1    shige  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27  1.1    shige  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     28  1.1    shige  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     29  1.1    shige  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     30  1.1    shige  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  1.1    shige  */
     32  1.1    shige 
     33  1.1    shige #include "locators.h"
     34  1.1    shige 
     35  1.1    shige #include <sys/param.h>
     36  1.1    shige #include <sys/device.h>
     37  1.1    shige #include <sys/systm.h>
     38  1.1    shige 
     39  1.1    shige #include <machine/pio.h>
     40  1.1    shige 
     41  1.5    shige #include <sys/gpio.h>
     42  1.5    shige #include <dev/gpio/gpiovar.h>
     43  1.5    shige 
     44  1.1    shige #include <powerpc/ibm4xx/dev/opbvar.h>
     45  1.1    shige #include <powerpc/ibm4xx/dev/gpioreg.h>
     46  1.1    shige 
     47  1.5    shige struct gpio_opb_softc {
     48  1.8     matt 	device_t		sc_dev;		/* device generic */
     49  1.5    shige 	/* GPIO interface */
     50  1.5    shige 	bus_space_tag_t		sc_gpio_iot;
     51  1.5    shige 	bus_space_handle_t	sc_gpio_ioh;
     52  1.5    shige 	struct gpio_chipset_tag	sc_gpio_gc;
     53  1.5    shige 	gpio_pin_t		sc_gpio_pins[GPIO_NPINS];
     54  1.1    shige };
     55  1.1    shige 
     56  1.8     matt static int	gpio_opb_match(device_t, cfdata_t, void *);
     57  1.8     matt static void	gpio_opb_attach(device_t, device_t, void *);
     58  1.1    shige 
     59  1.8     matt CFATTACH_DECL_NEW(opbgpio, sizeof(struct gpio_opb_softc),
     60  1.5    shige 	gpio_opb_match, gpio_opb_attach, NULL, NULL);
     61  1.1    shige 
     62  1.5    shige static int	gpio_opb_pin_read(void *, int);
     63  1.5    shige static void	gpio_opb_pin_write(void *, int, int);
     64  1.5    shige static void	gpio_opb_pin_ctl(void *, int, int);
     65  1.1    shige 
     66  1.8     matt static inline uint32_t
     67  1.8     matt gpio_read(struct gpio_opb_softc *sc, bus_size_t o)
     68  1.8     matt {
     69  1.8     matt 	return bus_space_read_4(sc->sc_gpio_iot, sc->sc_gpio_ioh, o);
     70  1.8     matt }
     71  1.8     matt 
     72  1.8     matt static inline void
     73  1.8     matt gpio_write(struct gpio_opb_softc *sc, bus_size_t o, uint32_t v)
     74  1.8     matt {
     75  1.8     matt 	bus_space_write_4(sc->sc_gpio_iot, sc->sc_gpio_ioh, o, v);
     76  1.8     matt }
     77  1.8     matt 
     78  1.8     matt static inline void
     79  1.8     matt gpio_set(struct gpio_opb_softc *sc, bus_size_t o, uint32_t v)
     80  1.8     matt {
     81  1.8     matt 	gpio_write(sc, o, gpio_read(sc, o) | v);
     82  1.8     matt }
     83  1.8     matt 
     84  1.8     matt static inline void
     85  1.8     matt gpio_clear(struct gpio_opb_softc *sc, bus_size_t o, uint32_t v)
     86  1.8     matt {
     87  1.8     matt 	gpio_write(sc, o, gpio_read(sc, o) & ~v);
     88  1.8     matt }
     89  1.1    shige 
     90  1.1    shige static int
     91  1.8     matt gpio_opb_match(device_t parent, cfdata_t cf, void *aux)
     92  1.1    shige {
     93  1.8     matt 	struct opb_attach_args * const oaa = aux;
     94  1.1    shige 
     95  1.1    shige 	if (strcmp(oaa->opb_name, cf->cf_name) != 0)
     96  1.5    shige 		return 0;
     97  1.1    shige 
     98  1.5    shige 	return 1;
     99  1.1    shige }
    100  1.1    shige 
    101  1.1    shige static void
    102  1.8     matt gpio_opb_attach(device_t parent, device_t self, void *aux)
    103  1.1    shige {
    104  1.8     matt 	struct gpio_opb_softc * const sc = device_private(self);
    105  1.8     matt 	struct opb_attach_args * const oaa = aux;
    106  1.5    shige 	struct gpiobus_attach_args gba;
    107  1.8     matt 	uint32_t reg_ir, reg_tcr, reg_odr;
    108  1.1    shige 
    109  1.1    shige 	aprint_naive(": GPIO controller\n");
    110  1.1    shige 	aprint_normal(": On-Chip GPIO controller\n");
    111  1.1    shige 
    112  1.8     matt 	sc->sc_dev = self;
    113  1.8     matt 
    114  1.5    shige 	/* Map GPIO I/O space */
    115  1.5    shige 	sc->sc_gpio_iot = oaa->opb_bt;
    116  1.5    shige 	bus_space_map(sc->sc_gpio_iot, oaa->opb_addr,
    117  1.5    shige 		GPIO_NREG, 0, &sc->sc_gpio_ioh);
    118  1.5    shige 
    119  1.5    shige 	/* Read current register status */
    120  1.8     matt 	reg_ir  = gpio_read(sc, GPIO_IR);
    121  1.8     matt 	reg_tcr = gpio_read(sc, GPIO_TCR);
    122  1.8     matt 	reg_odr = gpio_read(sc, GPIO_ODR);
    123  1.5    shige 
    124  1.6   simonb 	/* Initialize pins array */
    125  1.8     matt 	gpio_pin_t *pin = sc->sc_gpio_pins;
    126  1.8     matt 	for (u_int i = 0 ; i < GPIO_NPINS ; i++, pin++) {
    127  1.8     matt 		const uint32_t pin_mask = 1 << GPIO_PIN_SHIFT(i + 1);
    128  1.8     matt 		pin->pin_num = i;
    129  1.8     matt 		pin->pin_caps = GPIO_PIN_INOUT
    130  1.8     matt 				 | GPIO_PIN_OPENDRAIN
    131  1.8     matt 				 | GPIO_PIN_TRISTATE;
    132  1.5    shige 
    133  1.5    shige 		/* current defaults */
    134  1.8     matt 		pin->pin_flags =
    135  1.8     matt 		    (reg_odr & pin_mask)
    136  1.5    shige 			? GPIO_PIN_OPENDRAIN
    137  1.8     matt 			: ((reg_tcr & pin_mask)
    138  1.8     matt 			    ? GPIO_PIN_INOUT
    139  1.8     matt 			    : GPIO_PIN_TRISTATE);
    140  1.8     matt 		pin->pin_state = (reg_ir & pin_mask) != 0;
    141  1.8     matt 		pin->pin_mapped = 0;
    142  1.5    shige 	}
    143  1.5    shige 
    144  1.5    shige 	/* Create controller tag */
    145  1.5    shige 	sc->sc_gpio_gc.gp_cookie = sc;
    146  1.5    shige 	sc->sc_gpio_gc.gp_pin_read = gpio_opb_pin_read;
    147  1.5    shige 	sc->sc_gpio_gc.gp_pin_write = gpio_opb_pin_write;
    148  1.5    shige 	sc->sc_gpio_gc.gp_pin_ctl = gpio_opb_pin_ctl;
    149  1.5    shige 
    150  1.5    shige 	gba.gba_gc = &sc->sc_gpio_gc;
    151  1.5    shige 	gba.gba_pins = sc->sc_gpio_pins;
    152  1.5    shige 	gba.gba_npins = GPIO_NPINS;
    153  1.5    shige 
    154  1.5    shige 	/* Attach GPIO framework */
    155  1.9  thorpej 	(void) config_found(self, &gba, gpiobus_print, CFARG_EOL);
    156  1.1    shige }
    157  1.1    shige 
    158  1.1    shige static int
    159  1.5    shige gpio_opb_pin_read(void *arg, int pin)
    160  1.5    shige {
    161  1.8     matt 	struct gpio_opb_softc * const sc = arg;
    162  1.8     matt 	const u_int p = (pin % GPIO_NPINS) + 1;
    163  1.8     matt 	uint32_t reg_ir = gpio_read(sc, GPIO_IR);
    164  1.1    shige 
    165  1.8     matt 	return (reg_ir >> GPIO_PIN_SHIFT(p)) & 0x01;
    166  1.1    shige }
    167  1.1    shige 
    168  1.1    shige static void
    169  1.5    shige gpio_opb_pin_write(void *arg, int pin, int value)
    170  1.1    shige {
    171  1.8     matt 	struct gpio_opb_softc * const sc = arg;
    172  1.8     matt 	const u_int p = (pin % GPIO_NPINS) + 1;
    173  1.8     matt 	const uint32_t pin_mask = 1 << GPIO_PIN_SHIFT(p);
    174  1.5    shige 
    175  1.5    shige 	if (value == 0) {
    176  1.8     matt 		gpio_clear(sc, GPIO_OR, pin_mask);
    177  1.5    shige 	} else if (value == 1) {
    178  1.8     matt 		gpio_set(sc, GPIO_OR, pin_mask);
    179  1.5    shige 	}
    180  1.1    shige }
    181  1.1    shige 
    182  1.1    shige static void
    183  1.5    shige gpio_opb_pin_ctl(void *arg, int pin, int flags)
    184  1.1    shige {
    185  1.8     matt 	struct gpio_opb_softc * const sc = arg;
    186  1.8     matt 	const u_int p = (pin % GPIO_NPINS) + 1;
    187  1.8     matt 	const uint32_t pin_mask = 1 << GPIO_PIN_SHIFT(p);
    188  1.5    shige 
    189  1.5    shige 	if (flags & GPIO_PIN_INOUT) {
    190  1.5    shige 		/* GPIOn_ODR register bit is 0 */
    191  1.8     matt 		gpio_clear(sc, GPIO_ODR, pin_mask);
    192  1.8     matt 
    193  1.5    shige 		/* GPIOn_TCR register bit is 1 */
    194  1.8     matt 		gpio_set(sc, GPIO_TCR, pin_mask);
    195  1.5    shige 	}
    196  1.5    shige 
    197  1.5    shige 	if (flags & GPIO_PIN_TRISTATE) {
    198  1.5    shige 		/* GPIOn_ODR register bit is 0 */
    199  1.8     matt 		gpio_clear(sc, GPIO_ODR, pin_mask);
    200  1.8     matt 
    201  1.5    shige 		/* GPIOn_TCR register bit is 0 */
    202  1.8     matt 		gpio_clear(sc, GPIO_TCR, pin_mask);
    203  1.5    shige 	}
    204  1.5    shige 
    205  1.5    shige 	if (flags & GPIO_PIN_OPENDRAIN) {
    206  1.5    shige 		/* GPIOn_ODR register bit is 1 */
    207  1.8     matt 		gpio_set(sc, GPIO_ODR, pin_mask);
    208  1.5    shige 	}
    209  1.1    shige }
    210