Home | History | Annotate | Line # | Download | only in imx
imx31_ahb.c revision 1.7.12.4
      1  1.7.12.4  thorpej /*	$NetBSD: imx31_ahb.c,v 1.7.12.4 2021/04/04 22:01:12 thorpej 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.7.12.4  thorpej __KERNEL_RCSID(0, "$Id: imx31_ahb.c,v 1.7.12.4 2021/04/04 22:01:12 thorpej Exp $");
    103       1.2     matt 
    104       1.2     matt #include "locators.h"
    105       1.2     matt #include "avic.h"
    106       1.2     matt 
    107       1.2     matt #include <sys/param.h>
    108       1.2     matt #include <sys/systm.h>
    109       1.2     matt #include <sys/device.h>
    110       1.2     matt #include <sys/kernel.h>
    111       1.2     matt #include <sys/reboot.h>
    112       1.2     matt 
    113       1.2     matt #include <machine/cpu.h>
    114       1.5   dyoung #include <sys/bus.h>
    115       1.2     matt 
    116       1.2     matt #include <arm/cpufunc.h>
    117       1.2     matt #include <arm/mainbus/mainbus.h>
    118       1.2     matt 
    119       1.2     matt #include <machine/intr.h>
    120       1.2     matt 
    121       1.2     matt #include <arm/imx/imx31reg.h>
    122       1.2     matt #include <arm/imx/imx31var.h>
    123       1.2     matt 
    124       1.2     matt struct ahb_softc {
    125       1.6      chs 	device_t sc_dev;
    126       1.2     matt 	bus_dma_tag_t sc_dmat;
    127       1.2     matt 	bus_space_tag_t sc_memt;
    128       1.2     matt 	bus_space_handle_t sc_memh;
    129       1.2     matt };
    130       1.2     matt 
    131       1.2     matt /* prototypes */
    132       1.2     matt static int	ahb_match(device_t, cfdata_t, void *);
    133       1.2     matt static void	ahb_attach(device_t, device_t, void *);
    134       1.2     matt static int 	ahb_search(device_t, cfdata_t, const int *, void *);
    135       1.2     matt static void	ahb_attach_critical(struct ahb_softc *);
    136       1.2     matt static int	ahbbus_print(void *, const char *);
    137       1.2     matt 
    138       1.2     matt /* attach structures */
    139       1.6      chs CFATTACH_DECL_NEW(ahb, sizeof(struct ahb_softc),
    140       1.2     matt     ahb_match, ahb_attach, NULL, NULL);
    141       1.2     matt 
    142       1.2     matt static struct ahb_softc *ahb_sc;
    143       1.2     matt 
    144       1.2     matt static int
    145       1.2     matt ahb_match(device_t parent, cfdata_t match, void *aux)
    146       1.2     matt {
    147       1.2     matt 	return 1;
    148       1.2     matt }
    149       1.2     matt 
    150       1.2     matt static void
    151       1.2     matt ahb_attach(device_t parent, device_t self, void *aux)
    152       1.2     matt {
    153       1.6      chs 	struct ahb_softc *sc = device_private(self);
    154       1.2     matt 	struct ahb_attach_args ahba;
    155       1.2     matt 
    156       1.2     matt 	ahb_sc = sc;
    157       1.6      chs 	sc->sc_dev = self;
    158       1.4      bsh 	sc->sc_memt = &imx_bs_tag;
    159       1.2     matt #if NBUS_DMA_GENERIC > 0
    160       1.4      bsh 	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.7.12.1  thorpej 	config_search(self, &ahba,
    179  1.7.12.4  thorpej 	    CFARG_SEARCH, ahb_search,
    180  1.7.12.1  thorpej 	    CFARG_EOL);
    181       1.2     matt }
    182       1.2     matt 
    183       1.2     matt static int
    184       1.2     matt ahb_search(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
    185       1.2     matt {
    186       1.2     matt 	struct ahb_attach_args * const ahba = aux;
    187       1.2     matt 
    188       1.2     matt 	ahba->ahba_addr = cf->cf_loc[AHBCF_ADDR];
    189       1.2     matt 	ahba->ahba_size = cf->cf_loc[AHBCF_SIZE];
    190       1.2     matt 	ahba->ahba_intr = cf->cf_loc[AHBCF_INTR];
    191       1.2     matt 	ahba->ahba_irqbase = cf->cf_loc[AHBCF_IRQBASE];
    192       1.2     matt 
    193       1.2     matt 	if (config_match(parent, cf, ahba))
    194  1.7.12.3  thorpej 		config_attach(parent, cf, ahba, ahbbus_print, CFARG_EOL);
    195       1.2     matt 
    196       1.2     matt 	return 0;
    197       1.2     matt }
    198       1.2     matt 
    199       1.2     matt static int
    200       1.2     matt ahb_find(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
    201       1.2     matt {
    202       1.2     matt 	struct ahb_attach_args * const ahba = aux;
    203       1.2     matt 	struct ahb_attach_args ahba0;
    204       1.2     matt 
    205       1.2     matt 	if (strcmp(ahba->ahba_name, "ahb"))
    206       1.2     matt 		return 0;
    207       1.2     matt 	if (ahba->ahba_addr != AHBCF_ADDR_DEFAULT
    208       1.2     matt 	    && ahba->ahba_addr != cf->cf_loc[AHBCF_ADDR])
    209       1.2     matt 		return 0;
    210       1.2     matt 	if (ahba->ahba_size != AHBCF_SIZE_DEFAULT
    211       1.2     matt 	    && ahba->ahba_size != cf->cf_loc[AHBCF_SIZE])
    212       1.2     matt 		return 0;
    213       1.2     matt 	if (ahba->ahba_intr != AHBCF_INTR_DEFAULT
    214       1.2     matt 	    && ahba->ahba_intr != cf->cf_loc[AHBCF_INTR])
    215       1.2     matt 		return 0;
    216       1.2     matt 	if (ahba->ahba_irqbase != AHBCF_IRQBASE_DEFAULT
    217       1.2     matt 	    && ahba->ahba_irqbase != cf->cf_loc[AHBCF_IRQBASE])
    218       1.2     matt 		return 0;
    219       1.2     matt 
    220       1.2     matt 	ahba0 = *ahba;
    221       1.2     matt 	ahba0.ahba_addr = cf->cf_loc[AHBCF_ADDR];
    222       1.2     matt 	ahba0.ahba_size = cf->cf_loc[AHBCF_SIZE];
    223       1.2     matt 	ahba0.ahba_intr = cf->cf_loc[AHBCF_INTR];
    224       1.2     matt 	ahba0.ahba_irqbase = cf->cf_loc[AHBCF_IRQBASE];
    225       1.2     matt 
    226       1.2     matt 	return config_match(parent, cf, &ahba0);
    227       1.2     matt }
    228       1.2     matt 
    229       1.2     matt #if NAVIC == 0
    230       1.2     matt #error no avic present in config file
    231       1.2     matt #endif
    232       1.2     matt 
    233       1.2     matt static const struct {
    234       1.2     matt 	const char *name;
    235       1.2     matt 	bus_addr_t addr;
    236       1.2     matt 	bool required;
    237       1.2     matt } critical_devs[] = {
    238       1.2     matt 	{ .name = "avic", .addr = INTC_BASE, .required = true },
    239       1.2     matt 	{ .name = "gpio1", .addr = GPIO1_BASE, .required = false },
    240       1.2     matt 	{ .name = "gpio2", .addr = GPIO2_BASE, .required = false },
    241       1.2     matt 	{ .name = "gpio3", .addr = GPIO3_BASE, .required = false },
    242       1.2     matt #if 0
    243       1.2     matt 	{ .name = "dmac", .addr = DMAC_BASE, .required = true },
    244       1.2     matt #endif
    245       1.2     matt };
    246       1.2     matt 
    247       1.2     matt static void
    248       1.2     matt ahb_attach_critical(struct ahb_softc *sc)
    249       1.2     matt {
    250       1.2     matt 	struct ahb_attach_args ahba;
    251       1.2     matt 	cfdata_t cf;
    252       1.2     matt 	size_t i;
    253       1.2     matt 
    254       1.2     matt 	for (i = 0; i < __arraycount(critical_devs); i++) {
    255       1.2     matt 		ahba.ahba_name = "ahb";
    256       1.2     matt 		ahba.ahba_memt = sc->sc_memt;
    257       1.2     matt 		ahba.ahba_dmat = sc->sc_dmat;
    258       1.2     matt 
    259       1.2     matt 		ahba.ahba_addr = critical_devs[i].addr;
    260       1.2     matt 		ahba.ahba_size = AHBCF_SIZE_DEFAULT;
    261       1.2     matt 		ahba.ahba_intr = AHBCF_INTR_DEFAULT;
    262       1.2     matt 		ahba.ahba_irqbase = AHBCF_IRQBASE_DEFAULT;
    263       1.2     matt 
    264  1.7.12.1  thorpej 		cf = config_search(sc->sc_dev, &ahba,
    265  1.7.12.1  thorpej 		    CFARG_SUBMATCH, ahb_find,
    266  1.7.12.1  thorpej 		    CFARG_EOL);
    267       1.2     matt 		if (cf == NULL && critical_devs[i].required)
    268       1.2     matt 			panic("ahb_attach_critical: failed to find %s!",
    269       1.2     matt 			    critical_devs[i].name);
    270       1.2     matt 
    271       1.2     matt 		ahba.ahba_addr = cf->cf_loc[AHBCF_ADDR];
    272       1.2     matt 		ahba.ahba_size = cf->cf_loc[AHBCF_SIZE];
    273       1.2     matt 		ahba.ahba_intr = cf->cf_loc[AHBCF_INTR];
    274       1.2     matt 		ahba.ahba_irqbase = cf->cf_loc[AHBCF_IRQBASE];
    275  1.7.12.3  thorpej 		config_attach(sc->sc_dev, cf, &ahba, ahbbus_print, CFARG_EOL);
    276       1.2     matt 	}
    277       1.2     matt }
    278       1.2     matt 
    279       1.2     matt static int
    280       1.2     matt ahbbus_print(void *aux, const char *name)
    281       1.2     matt {
    282       1.6      chs 	struct ahb_attach_args *ahba = aux;
    283       1.2     matt 
    284       1.2     matt 	if (ahba->ahba_addr != AHBCF_ADDR_DEFAULT) {
    285       1.2     matt 		aprint_normal(" addr 0x%lx", ahba->ahba_addr);
    286       1.2     matt 		if (ahba->ahba_size > AHBCF_SIZE_DEFAULT)
    287       1.2     matt 			aprint_normal("-0x%lx",
    288       1.2     matt 			    ahba->ahba_addr + ahba->ahba_size-1);
    289       1.2     matt 	}
    290       1.2     matt 	if (ahba->ahba_intr != AHBCF_INTR_DEFAULT)
    291       1.2     matt 		aprint_normal(" intr %d", ahba->ahba_intr);
    292       1.2     matt 	if (ahba->ahba_irqbase != AHBCF_IRQBASE_DEFAULT)
    293       1.2     matt 		aprint_normal(" irqbase %d", ahba->ahba_irqbase);
    294       1.2     matt 
    295       1.2     matt 	return (UNCONF);
    296       1.2     matt }
    297       1.2     matt 
    298       1.2     matt static inline uint32_t
    299       1.2     matt read_clock_counter_xsc1(void)
    300       1.2     matt {
    301       1.2     matt 	uint32_t x;
    302       1.2     matt 	__asm volatile("mrc	p14, 0, %0, c1, c0, 0" : "=r" (x) );
    303       1.2     matt 
    304       1.2     matt 	return x;
    305       1.2     matt }
    306       1.2     matt 
    307       1.2     matt static inline uint32_t
    308       1.2     matt read_clock_counter_xsc2(void)
    309       1.2     matt {
    310       1.2     matt 	uint32_t x;
    311       1.2     matt 	__asm volatile("mrc	p14, 0, %0, c1, c1, 0" : "=r" (x) );
    312       1.2     matt 
    313       1.2     matt 	return x;
    314       1.2     matt }
    315       1.2     matt 
    316