Home | History | Annotate | Line # | Download | only in atheros
arbus.c revision 1.9
      1 /* $Id: arbus.c,v 1.9 2006/08/28 07:21:15 gdamore 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.9 2006/08/28 07:21:15 gdamore 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/ar5312reg.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 	    AR5312_ENET0_BASE,
    100 	    ARBUS_IRQ_ENET0,
    101 	    AR5312_BOARD_CONFIG_ENET0,
    102 	    AR5312_RESET_ENET0 | AR5312_RESET_PHY0,
    103 	    AR5312_ENABLE_ENET0
    104     },
    105     {
    106 	    "ae",
    107 	    AR5312_ENET1_BASE,
    108 	    ARBUS_IRQ_ENET1,
    109 	    AR5312_BOARD_CONFIG_ENET1,
    110 	    AR5312_RESET_ENET1 | AR5312_RESET_PHY1,
    111 	    AR5312_ENABLE_ENET1
    112     },
    113     {
    114 	    "com",
    115 	    AR5312_UART0_BASE,
    116 	    ARBUS_IRQ_UART0,
    117 	    AR5312_BOARD_CONFIG_UART0,
    118 	    0,
    119 	    0,
    120     },
    121     {
    122 	    "com",
    123 	    AR5312_UART1_BASE,
    124 	    -1,
    125 	    AR5312_BOARD_CONFIG_UART1,
    126 	    0,
    127 	    0,
    128     },
    129     {
    130 	    "ath",
    131 	    AR5312_WLAN0_BASE,
    132 	    ARBUS_IRQ_WLAN0,
    133 	    AR5312_BOARD_CONFIG_WLAN0,
    134 	    AR5312_RESET_WLAN0 |
    135 	    	AR5312_RESET_WARM_WLAN0_MAC |
    136 	    	AR5312_RESET_WARM_WLAN0_BB,
    137 	    AR5312_ENABLE_WLAN0
    138     },
    139     {
    140 	    "ath",
    141 	    AR5312_WLAN1_BASE,
    142 	    ARBUS_IRQ_WLAN1,
    143 	    AR5312_BOARD_CONFIG_WLAN1,
    144 	    AR5312_RESET_WLAN1 |
    145 	    	AR5312_RESET_WARM_WLAN1_MAC |
    146 	    	AR5312_RESET_WARM_WLAN1_BB,
    147 	    AR5312_ENABLE_WLAN1
    148     },
    149     {
    150 	    "athflash",
    151 	    AR5312_FLASH_BASE,
    152 	    -1,
    153 	    0,
    154 	    0,
    155 	    0,
    156     },
    157     {
    158 	    "argpio",
    159 	    AR5312_GPIO_BASE,
    160 	    ARBUS_IRQ_GPIO,
    161 	    0,
    162 	    0,
    163 	    0
    164     },
    165     { NULL }
    166 };
    167 
    168 /* this primarily exists so we can get to the console... */
    169 bus_space_tag_t
    170 arbus_get_bus_space_tag(void)
    171 {
    172 	arbus_init();
    173 	return (&arbus_mbst);
    174 }
    175 
    176 bus_dma_tag_t
    177 arbus_get_bus_dma_tag(void)
    178 {
    179 	arbus_init();
    180 	return (&arbus_mdt);
    181 }
    182 
    183 int
    184 arbus_match(struct device *parent, struct cfdata *match, void *aux)
    185 {
    186 
    187 	return 1;
    188 }
    189 
    190 void
    191 arbus_attach(struct device *parent, struct device *self, void *aux)
    192 {
    193 	struct arbus_attach_args aa;
    194 	const struct ar531x_boarddata *info;
    195 	int i;
    196 
    197 	printf("\n");
    198 	int locs[ARBUSCF_NLOCS];
    199 
    200 	info = ar531x_board_info();
    201 	arbus_init();
    202 
    203 	for (i = 0; arbus_devices[i].name; i++) {
    204 		if (arbus_devices[i].mask &&
    205 		    ((arbus_devices[i].mask & info->config) == 0)) {
    206 			continue;
    207 		}
    208 		aa.aa_name = arbus_devices[i].name;
    209 		aa.aa_dmat = &arbus_mdt;
    210 		aa.aa_bst = &arbus_mbst;
    211 		aa.aa_irq = arbus_devices[i].irq;
    212 		aa.aa_addr = arbus_devices[i].addr;
    213 
    214 		if (aa.aa_addr < AR5312_UART0_BASE)
    215 			aa.aa_size = 0x00100000;
    216 		else if (aa.aa_addr < AR5312_FLASH_BASE)
    217 			aa.aa_size = 0x1000;
    218 
    219 		locs[ARBUSCF_ADDR] = aa.aa_addr;
    220 
    221 		if (arbus_devices[i].reset) {
    222 			/* put device into reset */
    223 			PUTSYSREG(AR5312_SYSREG_RESETCTL,
    224 			    GETSYSREG(AR5312_SYSREG_RESETCTL) |
    225 			    arbus_devices[i].reset);
    226 
    227 			/* this could probably be a tsleep */
    228 			delay(15000);
    229 
    230 			/* take it out of reset */
    231 			PUTSYSREG(AR5312_SYSREG_RESETCTL,
    232 			    GETSYSREG(AR5312_SYSREG_RESETCTL) &
    233 			    ~arbus_devices[i].reset);
    234 
    235 			delay(25);
    236 		}
    237 
    238 		if (arbus_devices[i].enable) {
    239 			/* enable it */
    240 			PUTSYSREG(AR5312_SYSREG_ENABLE,
    241 			    GETSYSREG(AR5312_SYSREG_ENABLE) |
    242 			    arbus_devices[i].enable);
    243 		}
    244 
    245 		(void) config_found_sm_loc(self, "arbus", locs, &aa,
    246 		    arbus_print, config_stdsubmatch);
    247 	}
    248 }
    249 
    250 int
    251 arbus_print(void *aux, const char *pnp)
    252 {
    253 	struct arbus_attach_args *aa = aux;
    254 
    255 	if (pnp)
    256 		aprint_normal("%s at %s", aa->aa_name, pnp);
    257 
    258 	if (aa->aa_addr)
    259 		aprint_normal(" addr 0x%lx", aa->aa_addr);
    260 
    261 	if (aa->aa_irq >= 0) {
    262 		aprint_normal(" interrupt %d", ARBUS_IRQ_CPU(aa->aa_irq));
    263 
    264 		if (ARBUS_IRQ_MISC(aa->aa_irq))
    265 			aprint_normal(" irq %d", ARBUS_IRQ_MISC(aa->aa_irq));
    266 	}
    267 
    268 	return (UNCONF);
    269 }
    270 
    271 void *
    272 arbus_intr_establish(int irq, int (*handler)(void *), void *arg)
    273 {
    274 	struct arbus_intrhand	*ih;
    275 
    276 	ih = malloc(sizeof(*ih), M_DEVBUF, M_NOWAIT);
    277 	if (ih == NULL)
    278 		return NULL;
    279 
    280 	ih->ih_irq = irq;
    281 	ih->ih_cookie = NULL;
    282 
    283 	if (ARBUS_IRQ_MISC(irq)) {
    284 		irq = ARBUS_IRQ_MISC(irq);
    285 		ih->ih_misc = 1;
    286 		ih->ih_cookie = ar531x_misc_intr_establish(irq, handler, arg);
    287 	} else {
    288 		irq = ARBUS_IRQ_CPU(irq);
    289 		ih->ih_misc = 0;
    290 		ih->ih_cookie = ar531x_intr_establish(irq, handler, arg);
    291 	}
    292 
    293 	if (ih->ih_cookie == NULL) {
    294 		free(ih, M_DEVBUF);
    295 		return NULL;
    296 	}
    297 	return ih;
    298 }
    299 
    300 void
    301 arbus_intr_disestablish(void *arg)
    302 {
    303 	struct arbus_intrhand	*ih = arg;
    304 	if (ih->ih_misc)
    305 		ar531x_misc_intr_disestablish(ih->ih_cookie);
    306 	else
    307 		ar531x_intr_disestablish(ih->ih_cookie);
    308 	free(ih, M_DEVBUF);
    309 }
    310 
    311 
    312 void
    313 arbus_dma_init(struct device *sc, bus_dma_tag_t pdt)
    314 {
    315 	bus_dma_tag_t	t;
    316 
    317 	t = pdt;
    318 	t->_cookie = sc;
    319 	t->_wbase = 0;
    320 	t->_physbase = 0;
    321 	t->_wsize = MIPS_KSEG1_START - MIPS_KSEG0_START;
    322 	t->_dmamap_create = _bus_dmamap_create;
    323 	t->_dmamap_destroy = _bus_dmamap_destroy;
    324 	t->_dmamap_load = _bus_dmamap_load;
    325 	t->_dmamap_load_mbuf = _bus_dmamap_load_mbuf;
    326 	t->_dmamap_load_uio = _bus_dmamap_load_uio;
    327 	t->_dmamap_load_raw = _bus_dmamap_load_raw;
    328 	t->_dmamap_unload = _bus_dmamap_unload;
    329 	t->_dmamap_sync = _bus_dmamap_sync;
    330 	t->_dmamem_alloc = _bus_dmamem_alloc;
    331 	t->_dmamem_free = _bus_dmamem_free;
    332 	t->_dmamem_map = _bus_dmamem_map;
    333 	t->_dmamem_unmap = _bus_dmamem_unmap;
    334 	t->_dmamem_mmap = _bus_dmamem_mmap;
    335 }
    336 
    337 /*
    338  * CPU memory/register stuff
    339  */
    340 
    341 #define CHIP	   		arbus
    342 #define	CHIP_MEM		/* defined */
    343 #define	CHIP_W1_BUS_START(v)	0x00000000UL
    344 #define CHIP_W1_BUS_END(v)	0x1fffffffUL
    345 #define	CHIP_W1_SYS_START(v)	CHIP_W1_BUS_START(v)
    346 #define	CHIP_W1_SYS_END(v)	CHIP_W1_BUS_END(v)
    347 
    348 #include <mips/mips/bus_space_alignstride_chipdep.c>
    349