Home | History | Annotate | Line # | Download | only in imx
      1  1.4  hkenken /*	$NetBSD: imx51_gpio.c,v 1.4 2019/07/24 12:33:18 hkenken Exp $ */
      2  1.1      bsh 
      3  1.1      bsh /* derived from imx31_gpio.c */
      4  1.1      bsh /*-
      5  1.1      bsh  * Copyright (c) 2007 The NetBSD Foundation, Inc.
      6  1.1      bsh  * All rights reserved.
      7  1.1      bsh  *
      8  1.1      bsh  * This code is derived from software contributed to The NetBSD Foundation
      9  1.1      bsh  * by Matt Thomas
     10  1.1      bsh  *
     11  1.1      bsh  * Redistribution and use in source and binary forms, with or without
     12  1.1      bsh  * modification, are permitted provided that the following conditions
     13  1.1      bsh  * are met:
     14  1.1      bsh  * 1. Redistributions of source code must retain the above copyright
     15  1.1      bsh  *    notice, this list of conditions and the following disclaimer.
     16  1.1      bsh  * 2. Redistributions in binary form must reproduce the above copyright
     17  1.1      bsh  *    notice, this list of conditions and the following disclaimer in the
     18  1.1      bsh  *    documentation and/or other materials provided with the distribution.
     19  1.1      bsh  *
     20  1.1      bsh  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21  1.1      bsh  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  1.1      bsh  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  1.1      bsh  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24  1.1      bsh  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25  1.1      bsh  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26  1.1      bsh  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27  1.1      bsh  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28  1.1      bsh  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29  1.1      bsh  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30  1.1      bsh  * POSSIBILITY OF SUCH DAMAGE.
     31  1.1      bsh  */
     32  1.1      bsh #include <sys/cdefs.h>
     33  1.4  hkenken __KERNEL_RCSID(0, "$NetBSD: imx51_gpio.c,v 1.4 2019/07/24 12:33:18 hkenken Exp $");
     34  1.3  hkenken 
     35  1.3  hkenken #include "opt_imx.h"
     36  1.1      bsh 
     37  1.1      bsh #include "locators.h"
     38  1.1      bsh #include "gpio.h"
     39  1.1      bsh 
     40  1.4  hkenken #define	_INTR_PRIVATE
     41  1.4  hkenken 
     42  1.1      bsh #include <sys/param.h>
     43  1.1      bsh #include <sys/evcnt.h>
     44  1.1      bsh #include <sys/atomic.h>
     45  1.1      bsh 
     46  1.1      bsh #include <machine/intr.h>
     47  1.1      bsh 
     48  1.1      bsh #include <arm/cpu.h>
     49  1.1      bsh #include <arm/armreg.h>
     50  1.1      bsh #include <arm/cpufunc.h>
     51  1.1      bsh 
     52  1.2   dyoung #include <sys/bus.h>
     53  1.1      bsh 
     54  1.1      bsh #include <arm/imx/imx51reg.h>
     55  1.1      bsh #include <arm/imx/imx51var.h>
     56  1.1      bsh #include <arm/pic/picvar.h>
     57  1.1      bsh 
     58  1.1      bsh #include <arm/imx/imxgpioreg.h>
     59  1.1      bsh #include <arm/imx/imxgpiovar.h>
     60  1.1      bsh 
     61  1.1      bsh #if NGPIO > 0
     62  1.1      bsh #include <sys/gpio.h>
     63  1.1      bsh #include <dev/gpio/gpiovar.h>
     64  1.1      bsh #endif
     65  1.1      bsh 
     66  1.1      bsh const int imxgpio_ngroups = GPIO_NGROUPS;
     67  1.1      bsh 
     68  1.1      bsh int
     69  1.1      bsh imxgpio_match(device_t parent, cfdata_t cfdata, void *aux)
     70  1.1      bsh {
     71  1.1      bsh 	struct axi_attach_args *aa = aux;
     72  1.1      bsh 
     73  1.1      bsh 	switch(aa->aa_addr) {
     74  1.1      bsh 	case GPIO1_BASE:
     75  1.1      bsh 	case GPIO2_BASE:
     76  1.1      bsh 	case GPIO3_BASE:
     77  1.1      bsh 	case GPIO4_BASE:
     78  1.3  hkenken #ifdef IMX50
     79  1.3  hkenken 	case GPIO5_BASE:
     80  1.3  hkenken 	case GPIO6_BASE:
     81  1.3  hkenken #endif
     82  1.1      bsh 		return 1;
     83  1.1      bsh 	}
     84  1.1      bsh 
     85  1.1      bsh 	return 0;
     86  1.1      bsh }
     87  1.1      bsh 
     88  1.1      bsh void
     89  1.1      bsh imxgpio_attach(device_t parent, device_t self, void *aux)
     90  1.1      bsh {
     91  1.4  hkenken 	struct imxgpio_softc * const gpio = device_private(self);
     92  1.1      bsh 	struct axi_attach_args * const aa = aux;
     93  1.1      bsh 	bus_space_handle_t ioh;
     94  1.1      bsh 	int error;
     95  1.1      bsh 
     96  1.1      bsh 	if (aa->aa_irq == AXICF_IRQ_DEFAULT &&
     97  1.1      bsh 	    aa->aa_irqbase != AXICF_IRQBASE_DEFAULT) {
     98  1.1      bsh 		aprint_error_dev(self, "missing intr in config\n");
     99  1.1      bsh 		return;
    100  1.1      bsh 	}
    101  1.1      bsh 	if (aa->aa_irq != AXICF_IRQ_DEFAULT &&
    102  1.1      bsh 	    aa->aa_irqbase == AXICF_IRQBASE_DEFAULT) {
    103  1.1      bsh 		aprint_error_dev(self, "missing irqbase in config\n");
    104  1.1      bsh 		return;
    105  1.1      bsh 	}
    106  1.4  hkenken 
    107  1.1      bsh 	if (aa->aa_size == AXICF_SIZE_DEFAULT)
    108  1.1      bsh 		aa->aa_size = GPIO_SIZE;
    109  1.1      bsh 
    110  1.1      bsh 	error = bus_space_map(aa->aa_iot, aa->aa_addr, aa->aa_size,
    111  1.1      bsh 	    0, &ioh);
    112  1.1      bsh 	if (error) {
    113  1.1      bsh 		aprint_error(": failed to map register %#lx@%#lx: %d\n",
    114  1.1      bsh 		    aa->aa_size, aa->aa_addr, error);
    115  1.1      bsh 		return;
    116  1.1      bsh 	}
    117  1.1      bsh 
    118  1.4  hkenken 	gpio->gpio_is = intr_establish(aa->aa_irq,
    119  1.4  hkenken 	    IPL_HIGH, IST_LEVEL, pic_handle_intr, &gpio->gpio_pic);
    120  1.4  hkenken 	KASSERT(gpio->gpio_is != NULL );
    121  1.4  hkenken 	gpio->gpio_is_high = intr_establish(aa->aa_irq + 1,
    122  1.4  hkenken 	    IPL_HIGH, IST_LEVEL, pic_handle_intr, &gpio->gpio_pic);
    123  1.4  hkenken 	KASSERT(gpio->gpio_is_high != NULL);
    124  1.4  hkenken 
    125  1.4  hkenken 	gpio->gpio_memt = aa->aa_iot;
    126  1.4  hkenken 	gpio->gpio_memh = ioh;
    127  1.4  hkenken 	gpio->gpio_unit = (aa->aa_addr - GPIO1_BASE) / 0x4000;
    128  1.4  hkenken 	gpio->gpio_irqbase = aa->aa_irqbase;
    129  1.4  hkenken 
    130  1.4  hkenken 	imxgpio_attach_common(self);
    131  1.1      bsh }
    132  1.1      bsh 
    133