Home | History | Annotate | Line # | Download | only in imx
imx31_ahb.c revision 1.5
      1 /*	$NetBSD: imx31_ahb.c,v 1.5 2011/07/01 20:27:50 dyoung Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2002, 2005  Genetec Corporation.  All rights reserved.
      5  * Written by Hiroyuki Bessho for Genetec Corporation.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed for the NetBSD Project by
     18  *	Genetec Corporation.
     19  * 4. The name of Genetec Corporation may not be used to endorse or
     20  *    promote products derived from this software without specific prior
     21  *    written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY GENETEC CORPORATION ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     25  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     26  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL GENETEC CORPORATION
     27  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     33  * POSSIBILITY OF SUCH DAMAGE.
     34  *
     35  *
     36  * Autoconfiguration support for the Intel PXA2[15]0 application
     37  * processor. This code is derived from arm/sa11x0/sa11x0.c
     38  */
     39 
     40 /*-
     41  * Copyright (c) 2001, The NetBSD Foundation, Inc.  All rights reserved.
     42  *
     43  * This code is derived from software contributed to The NetBSD Foundation
     44  * by IWAMOTO Toshihiro and Ichiro FUKUHARA.
     45  *
     46  * Redistribution and use in source and binary forms, with or without
     47  * modification, are permitted provided that the following conditions
     48  * are met:
     49  * 1. Redistributions of source code must retain the above copyright
     50  *    notice, this list of conditions and the following disclaimer.
     51  * 2. Redistributions in binary form must reproduce the above copyright
     52  *    notice, this list of conditions and the following disclaimer in the
     53  *    documentation and/or other materials provided with the distribution.
     54  *
     55  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     56  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     57  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     58  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     59  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     60  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     61  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     62  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     63  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     64  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     65  * POSSIBILITY OF SUCH DAMAGE.
     66  */
     67 /*-
     68  * Copyright (c) 1999
     69  *         Shin Takemura and PocketBSD Project. All rights reserved.
     70  *
     71  * Redistribution and use in source and binary forms, with or without
     72  * modification, are permitted provided that the following conditions
     73  * are met:
     74  * 1. Redistributions of source code must retain the above copyright
     75  *    notice, this list of conditions and the following disclaimer.
     76  * 2. Redistributions in binary form must reproduce the above copyright
     77  *    notice, this list of conditions and the following disclaimer in the
     78  *    documentation and/or other materials provided with the distribution.
     79  * 3. All advertising materials mentioning features or use of this software
     80  *    must display the following acknowledgement:
     81  *	This product includes software developed by the PocketBSD project
     82  *	and its contributors.
     83  * 4. Neither the name of the project nor the names of its contributors
     84  *    may be used to endorse or promote products derived from this software
     85  *    without specific prior written permission.
     86  *
     87  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     88  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     89  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     90  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     91  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     92  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     93  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     94  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     95  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     96  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     97  * SUCH DAMAGE.
     98  *
     99  */
    100 
    101 #include <sys/cdefs.h>
    102 __KERNEL_RCSID(0, "$Id: imx31_ahb.c,v 1.5 2011/07/01 20:27:50 dyoung Exp $");
    103 
    104 #include "locators.h"
    105 #include "avic.h"
    106 #include "imxgpio.h"
    107 
    108 #include <sys/param.h>
    109 #include <sys/systm.h>
    110 #include <sys/device.h>
    111 #include <sys/kernel.h>
    112 #include <sys/reboot.h>
    113 
    114 #include <machine/cpu.h>
    115 #include <sys/bus.h>
    116 
    117 #include <arm/cpufunc.h>
    118 #include <arm/mainbus/mainbus.h>
    119 
    120 #include <machine/intr.h>
    121 
    122 #include <arm/imx/imx31reg.h>
    123 #include <arm/imx/imx31var.h>
    124 
    125 struct ahb_softc {
    126 	struct device sc_dev;
    127 	bus_dma_tag_t sc_dmat;
    128 	bus_space_tag_t sc_memt;
    129 	bus_space_handle_t sc_memh;
    130 };
    131 
    132 /* prototypes */
    133 static int	ahb_match(device_t, cfdata_t, void *);
    134 static void	ahb_attach(device_t, device_t, void *);
    135 static int 	ahb_search(device_t, cfdata_t, const int *, void *);
    136 static void	ahb_attach_critical(struct ahb_softc *);
    137 static int	ahbbus_print(void *, const char *);
    138 
    139 /* attach structures */
    140 CFATTACH_DECL(ahb, sizeof(struct ahb_softc),
    141     ahb_match, ahb_attach, NULL, NULL);
    142 
    143 static struct ahb_softc *ahb_sc;
    144 
    145 static int
    146 ahb_match(device_t parent, cfdata_t match, void *aux)
    147 {
    148 	return 1;
    149 }
    150 
    151 static void
    152 ahb_attach(device_t parent, device_t self, void *aux)
    153 {
    154 	struct ahb_softc *sc = (struct ahb_softc *)self;
    155 	struct ahb_attach_args ahba;
    156 
    157 	ahb_sc = sc;
    158 	sc->sc_memt = &imx_bs_tag;
    159 #if NBUS_DMA_GENERIC > 0
    160 	sc->sc_dmat = &imx_bus_dma_tag;
    161 #else
    162 	sc->sc_dmat = 0;
    163 #endif
    164 
    165 	aprint_normal(": AHB-Lite 2.v6 bus interface\n");
    166 
    167 	/*
    168 	 * Attach critical devices
    169 	 */
    170 	ahb_attach_critical(sc);
    171 
    172 	/*
    173 	 * Attach all other devices
    174 	 */
    175 	ahba.ahba_name = "ahb";
    176 	ahba.ahba_memt = sc->sc_memt;
    177 	ahba.ahba_dmat = sc->sc_dmat;
    178 	config_search_ia(ahb_search, self, "ahb", &ahba);
    179 }
    180 
    181 static int
    182 ahb_search(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
    183 {
    184 	struct ahb_attach_args * const ahba = aux;
    185 
    186 	ahba->ahba_addr = cf->cf_loc[AHBCF_ADDR];
    187 	ahba->ahba_size = cf->cf_loc[AHBCF_SIZE];
    188 	ahba->ahba_intr = cf->cf_loc[AHBCF_INTR];
    189 	ahba->ahba_irqbase = cf->cf_loc[AHBCF_IRQBASE];
    190 
    191 	if (config_match(parent, cf, ahba))
    192 		config_attach(parent, cf, ahba, ahbbus_print);
    193 
    194 	return 0;
    195 }
    196 
    197 static int
    198 ahb_find(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
    199 {
    200 	struct ahb_attach_args * const ahba = aux;
    201 	struct ahb_attach_args ahba0;
    202 
    203 	if (strcmp(ahba->ahba_name, "ahb"))
    204 		return 0;
    205 	if (ahba->ahba_addr != AHBCF_ADDR_DEFAULT
    206 	    && ahba->ahba_addr != cf->cf_loc[AHBCF_ADDR])
    207 		return 0;
    208 	if (ahba->ahba_size != AHBCF_SIZE_DEFAULT
    209 	    && ahba->ahba_size != cf->cf_loc[AHBCF_SIZE])
    210 		return 0;
    211 	if (ahba->ahba_intr != AHBCF_INTR_DEFAULT
    212 	    && ahba->ahba_intr != cf->cf_loc[AHBCF_INTR])
    213 		return 0;
    214 	if (ahba->ahba_irqbase != AHBCF_IRQBASE_DEFAULT
    215 	    && ahba->ahba_irqbase != cf->cf_loc[AHBCF_IRQBASE])
    216 		return 0;
    217 
    218 	ahba0 = *ahba;
    219 	ahba0.ahba_addr = cf->cf_loc[AHBCF_ADDR];
    220 	ahba0.ahba_size = cf->cf_loc[AHBCF_SIZE];
    221 	ahba0.ahba_intr = cf->cf_loc[AHBCF_INTR];
    222 	ahba0.ahba_irqbase = cf->cf_loc[AHBCF_IRQBASE];
    223 
    224 	return config_match(parent, cf, &ahba0);
    225 }
    226 
    227 #if NAVIC == 0
    228 #error no avic present in config file
    229 #endif
    230 
    231 static const struct {
    232 	const char *name;
    233 	bus_addr_t addr;
    234 	bool required;
    235 } critical_devs[] = {
    236 	{ .name = "avic", .addr = INTC_BASE, .required = true },
    237 	{ .name = "gpio1", .addr = GPIO1_BASE, .required = false },
    238 	{ .name = "gpio2", .addr = GPIO2_BASE, .required = false },
    239 	{ .name = "gpio3", .addr = GPIO3_BASE, .required = false },
    240 #if 0
    241 	{ .name = "dmac", .addr = DMAC_BASE, .required = true },
    242 #endif
    243 };
    244 
    245 static void
    246 ahb_attach_critical(struct ahb_softc *sc)
    247 {
    248 	struct ahb_attach_args ahba;
    249 	cfdata_t cf;
    250 	size_t i;
    251 
    252 	for (i = 0; i < __arraycount(critical_devs); i++) {
    253 		ahba.ahba_name = "ahb";
    254 		ahba.ahba_memt = sc->sc_memt;
    255 		ahba.ahba_dmat = sc->sc_dmat;
    256 
    257 		ahba.ahba_addr = critical_devs[i].addr;
    258 		ahba.ahba_size = AHBCF_SIZE_DEFAULT;
    259 		ahba.ahba_intr = AHBCF_INTR_DEFAULT;
    260 		ahba.ahba_irqbase = AHBCF_IRQBASE_DEFAULT;
    261 
    262 		cf = config_search_ia(ahb_find, &sc->sc_dev, "ahb", &ahba);
    263 		if (cf == NULL && critical_devs[i].required)
    264 			panic("ahb_attach_critical: failed to find %s!",
    265 			    critical_devs[i].name);
    266 
    267 		ahba.ahba_addr = cf->cf_loc[AHBCF_ADDR];
    268 		ahba.ahba_size = cf->cf_loc[AHBCF_SIZE];
    269 		ahba.ahba_intr = cf->cf_loc[AHBCF_INTR];
    270 		ahba.ahba_irqbase = cf->cf_loc[AHBCF_IRQBASE];
    271 		config_attach(&sc->sc_dev, cf, &ahba, ahbbus_print);
    272 	}
    273 }
    274 
    275 static int
    276 ahbbus_print(void *aux, const char *name)
    277 {
    278 	struct ahb_attach_args *ahba = (struct ahb_attach_args*)aux;
    279 
    280 	if (ahba->ahba_addr != AHBCF_ADDR_DEFAULT) {
    281 		aprint_normal(" addr 0x%lx", ahba->ahba_addr);
    282 		if (ahba->ahba_size > AHBCF_SIZE_DEFAULT)
    283 			aprint_normal("-0x%lx",
    284 			    ahba->ahba_addr + ahba->ahba_size-1);
    285 	}
    286 	if (ahba->ahba_intr != AHBCF_INTR_DEFAULT)
    287 		aprint_normal(" intr %d", ahba->ahba_intr);
    288 	if (ahba->ahba_irqbase != AHBCF_IRQBASE_DEFAULT)
    289 		aprint_normal(" irqbase %d", ahba->ahba_irqbase);
    290 
    291 	return (UNCONF);
    292 }
    293 
    294 static inline uint32_t
    295 read_clock_counter_xsc1(void)
    296 {
    297 	uint32_t x;
    298 	__asm volatile("mrc	p14, 0, %0, c1, c0, 0" : "=r" (x) );
    299 
    300 	return x;
    301 }
    302 
    303 static inline uint32_t
    304 read_clock_counter_xsc2(void)
    305 {
    306 	uint32_t x;
    307 	__asm volatile("mrc	p14, 0, %0, c1, c1, 0" : "=r" (x) );
    308 
    309 	return x;
    310 }
    311 
    312