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