Home | History | Annotate | Line # | Download | only in atheros
arbus.c revision 1.16
      1 /* $Id: arbus.c,v 1.16 2021/04/24 23:36:42 thorpej Exp $ */
      2 /*
      3  * Copyright (c) 2006 Urbana-Champaign Independent Media Center.
      4  * Copyright (c) 2006 Garrett D'Amore.
      5  * All rights reserved.
      6  *
      7  * This code was written by Garrett D'Amore for the Champaign-Urbana
      8  * Community Wireless Network Project.
      9  *
     10  * Redistribution and use in source and binary forms, with or
     11  * without modification, are permitted provided that the following
     12  * conditions are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above
     16  *    copyright notice, this list of conditions and the following
     17  *    disclaimer in the documentation and/or other materials provided
     18  *    with the distribution.
     19  * 3. All advertising materials mentioning features or use of this
     20  *    software must display the following acknowledgements:
     21  *      This product includes software developed by the Urbana-Champaign
     22  *      Independent Media Center.
     23  *	This product includes software developed by Garrett D'Amore.
     24  * 4. Urbana-Champaign Independent Media Center's name and Garrett
     25  *    D'Amore's name may not be used to endorse or promote products
     26  *    derived from this software without specific prior written permission.
     27  *
     28  * THIS SOFTWARE IS PROVIDED BY THE URBANA-CHAMPAIGN INDEPENDENT
     29  * MEDIA CENTER AND GARRETT D'AMORE ``AS IS'' AND ANY EXPRESS OR
     30  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     31  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     32  * ARE DISCLAIMED.  IN NO EVENT SHALL THE URBANA-CHAMPAIGN INDEPENDENT
     33  * MEDIA CENTER OR GARRETT D'AMORE BE LIABLE FOR ANY DIRECT, INDIRECT,
     34  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     35  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     36  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
     37  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     38  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     39  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     40  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     41  */
     42 
     43 #include <sys/cdefs.h>
     44 __KERNEL_RCSID(0, "$NetBSD: arbus.c,v 1.16 2021/04/24 23:36:42 thorpej Exp $");
     45 
     46 #include "locators.h"
     47 #define	_MIPS_BUS_DMA_PRIVATE
     48 
     49 #include <sys/param.h>
     50 #include <sys/bus.h>
     51 #include <sys/device.h>
     52 #include <sys/extent.h>
     53 #include <sys/kmem.h>
     54 #include <sys/systm.h>
     55 
     56 #include <mips/atheros/include/platform.h>
     57 #include <mips/atheros/include/arbusvar.h>
     58 
     59 static int arbus_match(device_t, cfdata_t, void *);
     60 static void arbus_attach(device_t, device_t, void *);
     61 static int arbus_print(void *, const char *);
     62 static void arbus_bus_mem_init(bus_space_tag_t, void *);
     63 
     64 struct arbus_intrhand {
     65 	int		ih_cirq;
     66 	int		ih_mirq;
     67 	void		*ih_cookie;
     68 };
     69 
     70 CFATTACH_DECL_NEW(arbus, 0, arbus_match, arbus_attach, NULL, NULL);
     71 
     72 struct mips_bus_space	arbus_mbst;
     73 #if _BYTE_ORDER == _BIG_ENDIAN
     74 struct mips_bus_space	arbus_mbst_le;
     75 #endif
     76 struct mips_bus_dma_tag	arbus_mdt = {
     77 	._dmamap_ops = _BUS_DMAMAP_OPS_INITIALIZER,
     78 	._dmamem_ops = _BUS_DMAMEM_OPS_INITIALIZER,
     79 	._dmatag_ops = _BUS_DMATAG_OPS_INITIALIZER,
     80 };
     81 
     82 void
     83 arbus_init(void)
     84 {
     85 	static bool done = false;
     86 	if (done)
     87 		return;
     88 	done = true;
     89 
     90 	arbus_bus_mem_init(&arbus_mbst, NULL);
     91 #if _BYTE_ORDER == _BIG_ENDIAN
     92 	arbusle_bus_mem_init(&arbus_mbst_le, NULL);
     93 #endif
     94 }
     95 
     96 /* this primarily exists so we can get to the console... */
     97 bus_space_tag_t
     98 arbus_get_bus_space_tag(void)
     99 {
    100 	arbus_init();
    101 	return (&arbus_mbst);
    102 }
    103 
    104 bus_dma_tag_t
    105 arbus_get_bus_dma_tag(void)
    106 {
    107 	arbus_init();
    108 	return (&arbus_mdt);
    109 }
    110 
    111 int
    112 arbus_match(device_t parent, cfdata_t match, void *aux)
    113 {
    114 
    115 	return 1;
    116 }
    117 
    118 void
    119 arbus_attach(device_t parent, device_t self, void *aux)
    120 {
    121 	aprint_normal("\n");
    122 
    123 	arbus_init();
    124 
    125 	for (const struct atheros_device *adv = platformsw->apsw_devices;
    126 	     adv->adv_name;
    127 	     adv++) {
    128 		struct arbus_attach_args aa;
    129 		aa.aa_name = adv->adv_name;
    130 		aa.aa_addr = adv->adv_addr;
    131 		aa.aa_size = adv->adv_size;
    132 		aa.aa_dmat = &arbus_mdt;
    133 		aa.aa_bst = &arbus_mbst;
    134 #if _BYTE_ORDER == _BIG_ENDIAN
    135 		aa.aa_bst_le = &arbus_mbst_le;
    136 #else
    137 		aa.aa_bst_le = &arbus_mbst;
    138 #endif
    139 		aa.aa_cirq = adv->adv_cirq;
    140 		aa.aa_mirq = adv->adv_mirq;
    141 
    142 		const int locs[ARBUSCF_NLOCS] = {
    143 			[ARBUSCF_ADDR] = aa.aa_addr,
    144 		};
    145 
    146 		if (atheros_enable_device(adv) != 0) {
    147 			continue;
    148 		}
    149 
    150 		config_found(self, &aa, arbus_print,
    151 		    CFARG_SUBMATCH, config_stdsubmatch,
    152 		    CFARG_LOCATORS, locs,
    153 		    CFARG_EOL);
    154 	}
    155 }
    156 
    157 int
    158 arbus_print(void *aux, const char *pnp)
    159 {
    160 	struct arbus_attach_args *aa = aux;
    161 
    162 	if (pnp)
    163 		aprint_normal("%s at %s", aa->aa_name, pnp);
    164 
    165 	if (aa->aa_addr)
    166 		aprint_normal(" addr 0x%" PRIxBUSADDR, aa->aa_addr);
    167 
    168 	if (aa->aa_cirq >= 0)
    169 		aprint_normal(" cpu irq %d", aa->aa_cirq);
    170 
    171 	if (aa->aa_mirq >= 0)
    172 		aprint_normal(" misc irq %d", aa->aa_mirq);
    173 
    174 	return (UNCONF);
    175 }
    176 
    177 void *
    178 arbus_intr_establish(int cirq, int mirq, int (*handler)(void *), void *arg)
    179 {
    180 
    181 	struct arbus_intrhand * const ih = kmem_zalloc(sizeof(*ih), KM_NOSLEEP);
    182 	if (ih == NULL)
    183 		return NULL;
    184 
    185 	ih->ih_cirq = ih->ih_mirq = -1;
    186 	ih->ih_cookie = NULL;
    187 
    188 	if (mirq >= 0) {
    189 		ih->ih_mirq = mirq;
    190 		ih->ih_cookie = atheros_misc_intr_establish(mirq, handler, arg);
    191 	} else if (cirq >= 0) {
    192 		ih->ih_cirq = cirq;
    193 		ih->ih_cookie = atheros_cpu_intr_establish(cirq, handler, arg);
    194 	} else
    195 		return ih;
    196 
    197 	if (ih->ih_cookie == NULL) {
    198 		kmem_free(ih, sizeof(*ih));
    199 		return NULL;
    200 	}
    201 	return ih;
    202 }
    203 
    204 void
    205 arbus_intr_disestablish(void *arg)
    206 {
    207 	struct arbus_intrhand * const ih = arg;
    208 	if (ih->ih_mirq >= 0)
    209 		atheros_misc_intr_disestablish(ih->ih_cookie);
    210 	else if (ih->ih_cirq >= 0)
    211 		atheros_cpu_intr_disestablish(ih->ih_cookie);
    212 	kmem_free(ih, sizeof(*ih));
    213 }
    214 
    215 /*
    216  * CPU memory/register stuff
    217  */
    218 
    219 #define CHIP	   		arbus
    220 #define	CHIP_MEM		/* defined */
    221 #define	CHIP_W1_BUS_START(v)	0x00000000UL
    222 #define CHIP_W1_BUS_END(v)	0x1fffffffUL
    223 #define	CHIP_W1_SYS_START(v)	CHIP_W1_BUS_START(v)
    224 #define	CHIP_W1_SYS_END(v)	CHIP_W1_BUS_END(v)
    225 
    226 #include <mips/mips/bus_space_alignstride_chipdep.c>
    227