Home | History | Annotate | Line # | Download | only in atheros
arbus.c revision 1.2.6.3
      1 /* $Id: arbus.c,v 1.2.6.3 2006/06/01 22:35:03 kardel 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.2.6.3 2006/06/01 22:35:03 kardel Exp $");
     45 
     46 #include "locators.h"
     47 #include <sys/param.h>
     48 #include <sys/systm.h>
     49 #include <sys/device.h>
     50 #include <sys/extent.h>
     51 #include <sys/malloc.h>
     52 
     53 #define	_MIPS_BUS_DMA_PRIVATE
     54 #include <machine/bus.h>
     55 #include <mips/atheros/include/ar531xreg.h>
     56 #include <mips/atheros/include/ar531xvar.h>
     57 #include <mips/atheros/include/arbusvar.h>
     58 
     59 static int arbus_match(struct device *, struct cfdata *, void *);
     60 static void arbus_attach(struct device *, struct device *, void *);
     61 static int arbus_print(void *, const char *);
     62 static void arbus_bus_mem_init(bus_space_tag_t, void *);
     63 static void arbus_dma_init(struct device *, bus_dma_tag_t);
     64 
     65 struct arbus_intrhand {
     66 	int		ih_irq;
     67 	int		ih_misc;
     68 	void		*ih_cookie;
     69 };
     70 
     71 CFATTACH_DECL(arbus, sizeof(struct device), arbus_match, arbus_attach,
     72     NULL, NULL);
     73 
     74 struct mips_bus_space	arbus_mbst;
     75 struct mips_bus_dma_tag	arbus_mdt;
     76 
     77 void
     78 arbus_init(void)
     79 {
     80 	static int done = 0;
     81 	if (done)
     82 		return;
     83 	done++;
     84 
     85 	arbus_bus_mem_init(&arbus_mbst, NULL);
     86 	arbus_dma_init(NULL, &arbus_mdt);
     87 }
     88 
     89 static struct {
     90 	const char	*name;
     91 	bus_addr_t	addr;
     92 	int		irq;
     93 	uint32_t	mask;
     94 	uint32_t	reset;
     95 	uint32_t	enable;
     96 } arbus_devices[] = {
     97     {
     98 	    "ae",
     99 	    AR531X_ENET0_BASE,
    100 	    ARBUS_IRQ_ENET0,
    101 	    AR531X_BOARD_CONFIG_ENET0,
    102 	    AR531X_RESET_ENET0 | AR531X_RESET_PHY0,
    103 	    AR531X_ENABLE_ENET0
    104     },
    105     {
    106 	    "ae",
    107 	    AR531X_ENET1_BASE,
    108 	    ARBUS_IRQ_ENET1,
    109 	    AR531X_BOARD_CONFIG_ENET1,
    110 	    AR531X_RESET_ENET1 | AR531X_RESET_PHY1,
    111 	    AR531X_ENABLE_ENET1
    112     },
    113     {
    114 	    "com",
    115 	    AR531X_UART0_BASE,
    116 	    ARBUS_IRQ_UART0,
    117 	    AR531X_BOARD_CONFIG_UART0,
    118 	    0,
    119 	    0,
    120     },
    121     {
    122 	    "com",
    123 	    AR531X_UART1_BASE,
    124 	    -1,
    125 	    AR531X_BOARD_CONFIG_UART1,
    126 	    0,
    127 	    0,
    128     },
    129     {
    130 	    "ath",
    131 	    AR531X_WLAN0_BASE,
    132 	    ARBUS_IRQ_WLAN0,
    133 	    AR531X_BOARD_CONFIG_WLAN0,
    134 	    AR531X_RESET_WLAN0 |
    135 	    	AR531X_RESET_WARM_WLAN0_MAC |
    136 	    	AR531X_RESET_WARM_WLAN0_BB,
    137 	    AR531X_ENABLE_WLAN0
    138     },
    139     {
    140 	    "ath",
    141 	    AR531X_WLAN1_BASE,
    142 	    ARBUS_IRQ_WLAN1,
    143 	    AR531X_BOARD_CONFIG_WLAN1,
    144 	    AR531X_RESET_WLAN1 |
    145 	    	AR531X_RESET_WARM_WLAN1_MAC |
    146 	    	AR531X_RESET_WARM_WLAN1_BB,
    147 	    AR531X_ENABLE_WLAN1
    148     },
    149     {
    150 	    "athflash",
    151 	    AR531X_FLASH_BASE,
    152 	    -1,
    153 	    0,
    154 	    0,
    155 	    0,
    156     },
    157 #if 0
    158     {
    159 	    "argpio",
    160 	    AR531X_GPIO_BASE,
    161 	    -1,
    162 	    0,
    163 	    0,
    164 	    0
    165     },
    166 #endif
    167     { NULL }
    168 };
    169 
    170 /* this primarily exists so we can get to the console... */
    171 bus_space_tag_t
    172 arbus_get_bus_space_tag(void)
    173 {
    174 	arbus_init();
    175 	return (&arbus_mbst);
    176 }
    177 
    178 bus_dma_tag_t
    179 arbus_get_bus_dma_tag(void)
    180 {
    181 	arbus_init();
    182 	return (&arbus_mdt);
    183 }
    184 
    185 int
    186 arbus_match(struct device *parent, struct cfdata *match, void *aux)
    187 {
    188 
    189 	return 1;
    190 }
    191 
    192 void
    193 arbus_attach(struct device *parent, struct device *self, void *aux)
    194 {
    195 	struct arbus_attach_args aa;
    196 	struct ar531x_board_info *info;
    197 	int i;
    198 
    199 	printf("\n");
    200 	int locs[ARBUSCF_NLOCS];
    201 
    202 	info = ar531x_board_info();
    203 	arbus_init();
    204 
    205 	for (i = 0; arbus_devices[i].name; i++) {
    206 		if (arbus_devices[i].mask &&
    207 		    ((arbus_devices[i].mask & info->ab_config) == 0)) {
    208 			continue;
    209 		}
    210 		aa.aa_name = arbus_devices[i].name;
    211 		aa.aa_dmat = &arbus_mdt;
    212 		aa.aa_bst = &arbus_mbst;
    213 		aa.aa_irq = arbus_devices[i].irq;
    214 		aa.aa_addr = arbus_devices[i].addr;
    215 
    216 		if (aa.aa_addr < AR531X_UART0_BASE)
    217 			aa.aa_size = 0x00100000;
    218 		else if (aa.aa_addr < AR531X_FLASH_BASE)
    219 			aa.aa_size = 0x1000;
    220 
    221 		locs[ARBUSCF_ADDR] = aa.aa_addr;
    222 
    223 		if (arbus_devices[i].reset) {
    224 			/* put device into reset */
    225 			PUTSYSREG(AR531X_SYSREG_RESETCTL,
    226 			    GETSYSREG(AR531X_SYSREG_RESETCTL) |
    227 			    arbus_devices[i].reset);
    228 
    229 			/* this could probably be a tsleep */
    230 			delay(15000);
    231 
    232 			/* take it out of reset */
    233 			PUTSYSREG(AR531X_SYSREG_RESETCTL,
    234 			    GETSYSREG(AR531X_SYSREG_RESETCTL) &
    235 			    ~arbus_devices[i].reset);
    236 
    237 			delay(25);
    238 		}
    239 
    240 		if (arbus_devices[i].enable) {
    241 			/* enable it */
    242 			PUTSYSREG(AR531X_SYSREG_ENABLE,
    243 			    GETSYSREG(AR531X_SYSREG_ENABLE) |
    244 			    arbus_devices[i].enable);
    245 		}
    246 
    247 		(void) config_found_sm_loc(self, "arbus", locs, &aa,
    248 		    arbus_print, config_stdsubmatch);
    249 	}
    250 }
    251 
    252 int
    253 arbus_print(void *aux, const char *pnp)
    254 {
    255 	struct arbus_attach_args *aa = aux;
    256 
    257 	if (pnp)
    258 		aprint_normal("%s at %s", aa->aa_name, pnp);
    259 
    260 	if (aa->aa_addr)
    261 		aprint_normal(" addr 0x%lx", aa->aa_addr);
    262 
    263 	if (aa->aa_irq >= 0) {
    264 		aprint_normal(" interrupt %d", ARBUS_IRQ_CPU(aa->aa_irq));
    265 
    266 		if (ARBUS_IRQ_MISC(aa->aa_irq))
    267 			aprint_normal(" irq %d", ARBUS_IRQ_MISC(aa->aa_irq));
    268 	}
    269 
    270 	return (UNCONF);
    271 }
    272 
    273 void *
    274 arbus_intr_establish(int irq, int (*handler)(void *), void *arg)
    275 {
    276 	struct arbus_intrhand	*ih;
    277 
    278 	ih = malloc(sizeof(*ih), M_DEVBUF, M_NOWAIT);
    279 	if (ih == NULL)
    280 		return NULL;
    281 
    282 	ih->ih_irq = irq;
    283 	ih->ih_cookie = NULL;
    284 
    285 	if (ARBUS_IRQ_MISC(irq)) {
    286 		irq = ARBUS_IRQ_MISC(irq);
    287 		ih->ih_misc = 1;
    288 		ih->ih_cookie = ar531x_misc_intr_establish(irq, handler, arg);
    289 	} else {
    290 		irq = ARBUS_IRQ_CPU(irq);
    291 		ih->ih_misc = 0;
    292 		ih->ih_cookie = ar531x_intr_establish(irq, handler, arg);
    293 	}
    294 
    295 	if (ih->ih_cookie == NULL) {
    296 		free(ih, M_DEVBUF);
    297 		return NULL;
    298 	}
    299 	return ih;
    300 }
    301 
    302 void
    303 arbus_intr_disestablish(void *arg)
    304 {
    305 	struct arbus_intrhand	*ih = arg;
    306 	if (ih->ih_misc)
    307 		ar531x_misc_intr_disestablish(ih->ih_cookie);
    308 	else
    309 		ar531x_intr_disestablish(ih->ih_cookie);
    310 	free(ih, M_DEVBUF);
    311 }
    312 
    313 
    314 void
    315 arbus_dma_init(struct device *sc, bus_dma_tag_t pdt)
    316 {
    317 	bus_dma_tag_t	t;
    318 
    319 	t = pdt;
    320 	t->_cookie = sc;
    321 	t->_wbase = 0;
    322 	t->_physbase = 0;
    323 	t->_wsize = MIPS_KSEG1_START - MIPS_KSEG0_START;
    324 	t->_dmamap_create = _bus_dmamap_create;
    325 	t->_dmamap_destroy = _bus_dmamap_destroy;
    326 	t->_dmamap_load = _bus_dmamap_load;
    327 	t->_dmamap_load_mbuf = _bus_dmamap_load_mbuf;
    328 	t->_dmamap_load_uio = _bus_dmamap_load_uio;
    329 	t->_dmamap_load_raw = _bus_dmamap_load_raw;
    330 	t->_dmamap_unload = _bus_dmamap_unload;
    331 	t->_dmamap_sync = _bus_dmamap_sync;
    332 	t->_dmamem_alloc = _bus_dmamem_alloc;
    333 	t->_dmamem_free = _bus_dmamem_free;
    334 	t->_dmamem_map = _bus_dmamem_map;
    335 	t->_dmamem_unmap = _bus_dmamem_unmap;
    336 	t->_dmamem_mmap = _bus_dmamem_mmap;
    337 }
    338 
    339 /*
    340  * CPU memory/register stuff
    341  */
    342 
    343 #define CHIP	   		arbus
    344 #define	CHIP_MEM		/* defined */
    345 #define	CHIP_W1_BUS_START(v)	0x00000000UL
    346 #define CHIP_W1_BUS_END(v)	0x1fffffffUL
    347 #define	CHIP_W1_SYS_START(v)	CHIP_W1_BUS_START(v)
    348 #define	CHIP_W1_SYS_END(v)	CHIP_W1_BUS_END(v)
    349 
    350 #include <mips/mips/bus_space_alignstride_chipdep.c>
    351