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