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