Home | History | Annotate | Line # | Download | only in atheros
arbus.c revision 1.9
      1  1.9  gdamore /* $Id: arbus.c,v 1.9 2006/08/28 07:21:15 gdamore Exp $ */
      2  1.1  gdamore /*
      3  1.1  gdamore  * Copyright (c) 2006 Urbana-Champaign Independent Media Center.
      4  1.1  gdamore  * Copyright (c) 2006 Garrett D'Amore.
      5  1.1  gdamore  * All rights reserved.
      6  1.1  gdamore  *
      7  1.1  gdamore  * This code was written by Garrett D'Amore for the Champaign-Urbana
      8  1.1  gdamore  * Community Wireless Network Project.
      9  1.1  gdamore  *
     10  1.1  gdamore  * Redistribution and use in source and binary forms, with or
     11  1.1  gdamore  * without modification, are permitted provided that the following
     12  1.1  gdamore  * conditions are met:
     13  1.1  gdamore  * 1. Redistributions of source code must retain the above copyright
     14  1.1  gdamore  *    notice, this list of conditions and the following disclaimer.
     15  1.1  gdamore  * 2. Redistributions in binary form must reproduce the above
     16  1.1  gdamore  *    copyright notice, this list of conditions and the following
     17  1.1  gdamore  *    disclaimer in the documentation and/or other materials provided
     18  1.1  gdamore  *    with the distribution.
     19  1.1  gdamore  * 3. All advertising materials mentioning features or use of this
     20  1.1  gdamore  *    software must display the following acknowledgements:
     21  1.1  gdamore  *      This product includes software developed by the Urbana-Champaign
     22  1.1  gdamore  *      Independent Media Center.
     23  1.1  gdamore  *	This product includes software developed by Garrett D'Amore.
     24  1.1  gdamore  * 4. Urbana-Champaign Independent Media Center's name and Garrett
     25  1.1  gdamore  *    D'Amore's name may not be used to endorse or promote products
     26  1.1  gdamore  *    derived from this software without specific prior written permission.
     27  1.1  gdamore  *
     28  1.1  gdamore  * THIS SOFTWARE IS PROVIDED BY THE URBANA-CHAMPAIGN INDEPENDENT
     29  1.1  gdamore  * MEDIA CENTER AND GARRETT D'AMORE ``AS IS'' AND ANY EXPRESS OR
     30  1.1  gdamore  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     31  1.1  gdamore  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     32  1.1  gdamore  * ARE DISCLAIMED.  IN NO EVENT SHALL THE URBANA-CHAMPAIGN INDEPENDENT
     33  1.1  gdamore  * MEDIA CENTER OR GARRETT D'AMORE BE LIABLE FOR ANY DIRECT, INDIRECT,
     34  1.1  gdamore  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     35  1.1  gdamore  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     36  1.1  gdamore  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
     37  1.1  gdamore  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     38  1.1  gdamore  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     39  1.1  gdamore  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     40  1.1  gdamore  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     41  1.1  gdamore  */
     42  1.1  gdamore 
     43  1.1  gdamore #include <sys/cdefs.h>
     44  1.9  gdamore __KERNEL_RCSID(0, "$NetBSD: arbus.c,v 1.9 2006/08/28 07:21:15 gdamore Exp $");
     45  1.1  gdamore 
     46  1.1  gdamore #include "locators.h"
     47  1.1  gdamore #include <sys/param.h>
     48  1.1  gdamore #include <sys/systm.h>
     49  1.1  gdamore #include <sys/device.h>
     50  1.1  gdamore #include <sys/extent.h>
     51  1.1  gdamore #include <sys/malloc.h>
     52  1.1  gdamore 
     53  1.1  gdamore #define	_MIPS_BUS_DMA_PRIVATE
     54  1.1  gdamore #include <machine/bus.h>
     55  1.9  gdamore #include <mips/atheros/include/ar5312reg.h>
     56  1.1  gdamore #include <mips/atheros/include/ar531xvar.h>
     57  1.1  gdamore #include <mips/atheros/include/arbusvar.h>
     58  1.1  gdamore 
     59  1.1  gdamore static int arbus_match(struct device *, struct cfdata *, void *);
     60  1.1  gdamore static void arbus_attach(struct device *, struct device *, void *);
     61  1.1  gdamore static int arbus_print(void *, const char *);
     62  1.1  gdamore static void arbus_bus_mem_init(bus_space_tag_t, void *);
     63  1.1  gdamore static void arbus_dma_init(struct device *, bus_dma_tag_t);
     64  1.1  gdamore 
     65  1.1  gdamore struct arbus_intrhand {
     66  1.1  gdamore 	int		ih_irq;
     67  1.1  gdamore 	int		ih_misc;
     68  1.1  gdamore 	void		*ih_cookie;
     69  1.1  gdamore };
     70  1.1  gdamore 
     71  1.1  gdamore CFATTACH_DECL(arbus, sizeof(struct device), arbus_match, arbus_attach,
     72  1.1  gdamore     NULL, NULL);
     73  1.1  gdamore 
     74  1.1  gdamore struct mips_bus_space	arbus_mbst;
     75  1.1  gdamore struct mips_bus_dma_tag	arbus_mdt;
     76  1.1  gdamore 
     77  1.1  gdamore void
     78  1.1  gdamore arbus_init(void)
     79  1.1  gdamore {
     80  1.1  gdamore 	static int done = 0;
     81  1.1  gdamore 	if (done)
     82  1.1  gdamore 		return;
     83  1.1  gdamore 	done++;
     84  1.1  gdamore 
     85  1.1  gdamore 	arbus_bus_mem_init(&arbus_mbst, NULL);
     86  1.1  gdamore 	arbus_dma_init(NULL, &arbus_mdt);
     87  1.1  gdamore }
     88  1.1  gdamore 
     89  1.1  gdamore static struct {
     90  1.1  gdamore 	const char	*name;
     91  1.1  gdamore 	bus_addr_t	addr;
     92  1.1  gdamore 	int		irq;
     93  1.1  gdamore 	uint32_t	mask;
     94  1.1  gdamore 	uint32_t	reset;
     95  1.1  gdamore 	uint32_t	enable;
     96  1.1  gdamore } arbus_devices[] = {
     97  1.1  gdamore     {
     98  1.1  gdamore 	    "ae",
     99  1.9  gdamore 	    AR5312_ENET0_BASE,
    100  1.1  gdamore 	    ARBUS_IRQ_ENET0,
    101  1.9  gdamore 	    AR5312_BOARD_CONFIG_ENET0,
    102  1.9  gdamore 	    AR5312_RESET_ENET0 | AR5312_RESET_PHY0,
    103  1.9  gdamore 	    AR5312_ENABLE_ENET0
    104  1.1  gdamore     },
    105  1.1  gdamore     {
    106  1.1  gdamore 	    "ae",
    107  1.9  gdamore 	    AR5312_ENET1_BASE,
    108  1.1  gdamore 	    ARBUS_IRQ_ENET1,
    109  1.9  gdamore 	    AR5312_BOARD_CONFIG_ENET1,
    110  1.9  gdamore 	    AR5312_RESET_ENET1 | AR5312_RESET_PHY1,
    111  1.9  gdamore 	    AR5312_ENABLE_ENET1
    112  1.1  gdamore     },
    113  1.1  gdamore     {
    114  1.1  gdamore 	    "com",
    115  1.9  gdamore 	    AR5312_UART0_BASE,
    116  1.1  gdamore 	    ARBUS_IRQ_UART0,
    117  1.9  gdamore 	    AR5312_BOARD_CONFIG_UART0,
    118  1.1  gdamore 	    0,
    119  1.1  gdamore 	    0,
    120  1.1  gdamore     },
    121  1.1  gdamore     {
    122  1.1  gdamore 	    "com",
    123  1.9  gdamore 	    AR5312_UART1_BASE,
    124  1.1  gdamore 	    -1,
    125  1.9  gdamore 	    AR5312_BOARD_CONFIG_UART1,
    126  1.1  gdamore 	    0,
    127  1.1  gdamore 	    0,
    128  1.1  gdamore     },
    129  1.1  gdamore     {
    130  1.1  gdamore 	    "ath",
    131  1.9  gdamore 	    AR5312_WLAN0_BASE,
    132  1.1  gdamore 	    ARBUS_IRQ_WLAN0,
    133  1.9  gdamore 	    AR5312_BOARD_CONFIG_WLAN0,
    134  1.9  gdamore 	    AR5312_RESET_WLAN0 |
    135  1.9  gdamore 	    	AR5312_RESET_WARM_WLAN0_MAC |
    136  1.9  gdamore 	    	AR5312_RESET_WARM_WLAN0_BB,
    137  1.9  gdamore 	    AR5312_ENABLE_WLAN0
    138  1.1  gdamore     },
    139  1.1  gdamore     {
    140  1.1  gdamore 	    "ath",
    141  1.9  gdamore 	    AR5312_WLAN1_BASE,
    142  1.1  gdamore 	    ARBUS_IRQ_WLAN1,
    143  1.9  gdamore 	    AR5312_BOARD_CONFIG_WLAN1,
    144  1.9  gdamore 	    AR5312_RESET_WLAN1 |
    145  1.9  gdamore 	    	AR5312_RESET_WARM_WLAN1_MAC |
    146  1.9  gdamore 	    	AR5312_RESET_WARM_WLAN1_BB,
    147  1.9  gdamore 	    AR5312_ENABLE_WLAN1
    148  1.1  gdamore     },
    149  1.5  gdamore     {
    150  1.6  gdamore 	    "athflash",
    151  1.9  gdamore 	    AR5312_FLASH_BASE,
    152  1.5  gdamore 	    -1,
    153  1.5  gdamore 	    0,
    154  1.5  gdamore 	    0,
    155  1.5  gdamore 	    0,
    156  1.5  gdamore     },
    157  1.1  gdamore     {
    158  1.1  gdamore 	    "argpio",
    159  1.9  gdamore 	    AR5312_GPIO_BASE,
    160  1.8  gdamore 	    ARBUS_IRQ_GPIO,
    161  1.1  gdamore 	    0,
    162  1.1  gdamore 	    0,
    163  1.1  gdamore 	    0
    164  1.1  gdamore     },
    165  1.1  gdamore     { NULL }
    166  1.1  gdamore };
    167  1.1  gdamore 
    168  1.1  gdamore /* this primarily exists so we can get to the console... */
    169  1.1  gdamore bus_space_tag_t
    170  1.1  gdamore arbus_get_bus_space_tag(void)
    171  1.1  gdamore {
    172  1.1  gdamore 	arbus_init();
    173  1.1  gdamore 	return (&arbus_mbst);
    174  1.1  gdamore }
    175  1.1  gdamore 
    176  1.1  gdamore bus_dma_tag_t
    177  1.1  gdamore arbus_get_bus_dma_tag(void)
    178  1.1  gdamore {
    179  1.1  gdamore 	arbus_init();
    180  1.1  gdamore 	return (&arbus_mdt);
    181  1.1  gdamore }
    182  1.1  gdamore 
    183  1.1  gdamore int
    184  1.1  gdamore arbus_match(struct device *parent, struct cfdata *match, void *aux)
    185  1.1  gdamore {
    186  1.1  gdamore 
    187  1.1  gdamore 	return 1;
    188  1.1  gdamore }
    189  1.1  gdamore 
    190  1.1  gdamore void
    191  1.1  gdamore arbus_attach(struct device *parent, struct device *self, void *aux)
    192  1.1  gdamore {
    193  1.1  gdamore 	struct arbus_attach_args aa;
    194  1.7  gdamore 	const struct ar531x_boarddata *info;
    195  1.1  gdamore 	int i;
    196  1.1  gdamore 
    197  1.1  gdamore 	printf("\n");
    198  1.1  gdamore 	int locs[ARBUSCF_NLOCS];
    199  1.1  gdamore 
    200  1.1  gdamore 	info = ar531x_board_info();
    201  1.1  gdamore 	arbus_init();
    202  1.1  gdamore 
    203  1.1  gdamore 	for (i = 0; arbus_devices[i].name; i++) {
    204  1.3  gdamore 		if (arbus_devices[i].mask &&
    205  1.7  gdamore 		    ((arbus_devices[i].mask & info->config) == 0)) {
    206  1.1  gdamore 			continue;
    207  1.1  gdamore 		}
    208  1.1  gdamore 		aa.aa_name = arbus_devices[i].name;
    209  1.1  gdamore 		aa.aa_dmat = &arbus_mdt;
    210  1.1  gdamore 		aa.aa_bst = &arbus_mbst;
    211  1.1  gdamore 		aa.aa_irq = arbus_devices[i].irq;
    212  1.1  gdamore 		aa.aa_addr = arbus_devices[i].addr;
    213  1.1  gdamore 
    214  1.9  gdamore 		if (aa.aa_addr < AR5312_UART0_BASE)
    215  1.1  gdamore 			aa.aa_size = 0x00100000;
    216  1.9  gdamore 		else if (aa.aa_addr < AR5312_FLASH_BASE)
    217  1.1  gdamore 			aa.aa_size = 0x1000;
    218  1.1  gdamore 
    219  1.1  gdamore 		locs[ARBUSCF_ADDR] = aa.aa_addr;
    220  1.1  gdamore 
    221  1.1  gdamore 		if (arbus_devices[i].reset) {
    222  1.1  gdamore 			/* put device into reset */
    223  1.9  gdamore 			PUTSYSREG(AR5312_SYSREG_RESETCTL,
    224  1.9  gdamore 			    GETSYSREG(AR5312_SYSREG_RESETCTL) |
    225  1.1  gdamore 			    arbus_devices[i].reset);
    226  1.1  gdamore 
    227  1.1  gdamore 			/* this could probably be a tsleep */
    228  1.1  gdamore 			delay(15000);
    229  1.1  gdamore 
    230  1.1  gdamore 			/* take it out of reset */
    231  1.9  gdamore 			PUTSYSREG(AR5312_SYSREG_RESETCTL,
    232  1.9  gdamore 			    GETSYSREG(AR5312_SYSREG_RESETCTL) &
    233  1.1  gdamore 			    ~arbus_devices[i].reset);
    234  1.1  gdamore 
    235  1.1  gdamore 			delay(25);
    236  1.1  gdamore 		}
    237  1.1  gdamore 
    238  1.1  gdamore 		if (arbus_devices[i].enable) {
    239  1.1  gdamore 			/* enable it */
    240  1.9  gdamore 			PUTSYSREG(AR5312_SYSREG_ENABLE,
    241  1.9  gdamore 			    GETSYSREG(AR5312_SYSREG_ENABLE) |
    242  1.1  gdamore 			    arbus_devices[i].enable);
    243  1.1  gdamore 		}
    244  1.1  gdamore 
    245  1.1  gdamore 		(void) config_found_sm_loc(self, "arbus", locs, &aa,
    246  1.1  gdamore 		    arbus_print, config_stdsubmatch);
    247  1.1  gdamore 	}
    248  1.1  gdamore }
    249  1.1  gdamore 
    250  1.1  gdamore int
    251  1.1  gdamore arbus_print(void *aux, const char *pnp)
    252  1.1  gdamore {
    253  1.1  gdamore 	struct arbus_attach_args *aa = aux;
    254  1.1  gdamore 
    255  1.1  gdamore 	if (pnp)
    256  1.1  gdamore 		aprint_normal("%s at %s", aa->aa_name, pnp);
    257  1.1  gdamore 
    258  1.1  gdamore 	if (aa->aa_addr)
    259  1.1  gdamore 		aprint_normal(" addr 0x%lx", aa->aa_addr);
    260  1.1  gdamore 
    261  1.2  gdamore 	if (aa->aa_irq >= 0) {
    262  1.1  gdamore 		aprint_normal(" interrupt %d", ARBUS_IRQ_CPU(aa->aa_irq));
    263  1.1  gdamore 
    264  1.2  gdamore 		if (ARBUS_IRQ_MISC(aa->aa_irq))
    265  1.2  gdamore 			aprint_normal(" irq %d", ARBUS_IRQ_MISC(aa->aa_irq));
    266  1.2  gdamore 	}
    267  1.1  gdamore 
    268  1.1  gdamore 	return (UNCONF);
    269  1.1  gdamore }
    270  1.1  gdamore 
    271  1.1  gdamore void *
    272  1.1  gdamore arbus_intr_establish(int irq, int (*handler)(void *), void *arg)
    273  1.1  gdamore {
    274  1.1  gdamore 	struct arbus_intrhand	*ih;
    275  1.1  gdamore 
    276  1.1  gdamore 	ih = malloc(sizeof(*ih), M_DEVBUF, M_NOWAIT);
    277  1.1  gdamore 	if (ih == NULL)
    278  1.1  gdamore 		return NULL;
    279  1.1  gdamore 
    280  1.1  gdamore 	ih->ih_irq = irq;
    281  1.1  gdamore 	ih->ih_cookie = NULL;
    282  1.1  gdamore 
    283  1.1  gdamore 	if (ARBUS_IRQ_MISC(irq)) {
    284  1.1  gdamore 		irq = ARBUS_IRQ_MISC(irq);
    285  1.1  gdamore 		ih->ih_misc = 1;
    286  1.1  gdamore 		ih->ih_cookie = ar531x_misc_intr_establish(irq, handler, arg);
    287  1.1  gdamore 	} else {
    288  1.1  gdamore 		irq = ARBUS_IRQ_CPU(irq);
    289  1.1  gdamore 		ih->ih_misc = 0;
    290  1.1  gdamore 		ih->ih_cookie = ar531x_intr_establish(irq, handler, arg);
    291  1.1  gdamore 	}
    292  1.1  gdamore 
    293  1.1  gdamore 	if (ih->ih_cookie == NULL) {
    294  1.1  gdamore 		free(ih, M_DEVBUF);
    295  1.1  gdamore 		return NULL;
    296  1.1  gdamore 	}
    297  1.1  gdamore 	return ih;
    298  1.1  gdamore }
    299  1.1  gdamore 
    300  1.1  gdamore void
    301  1.1  gdamore arbus_intr_disestablish(void *arg)
    302  1.1  gdamore {
    303  1.1  gdamore 	struct arbus_intrhand	*ih = arg;
    304  1.1  gdamore 	if (ih->ih_misc)
    305  1.1  gdamore 		ar531x_misc_intr_disestablish(ih->ih_cookie);
    306  1.1  gdamore 	else
    307  1.1  gdamore 		ar531x_intr_disestablish(ih->ih_cookie);
    308  1.1  gdamore 	free(ih, M_DEVBUF);
    309  1.1  gdamore }
    310  1.1  gdamore 
    311  1.1  gdamore 
    312  1.1  gdamore void
    313  1.1  gdamore arbus_dma_init(struct device *sc, bus_dma_tag_t pdt)
    314  1.1  gdamore {
    315  1.1  gdamore 	bus_dma_tag_t	t;
    316  1.1  gdamore 
    317  1.1  gdamore 	t = pdt;
    318  1.1  gdamore 	t->_cookie = sc;
    319  1.1  gdamore 	t->_wbase = 0;
    320  1.1  gdamore 	t->_physbase = 0;
    321  1.1  gdamore 	t->_wsize = MIPS_KSEG1_START - MIPS_KSEG0_START;
    322  1.1  gdamore 	t->_dmamap_create = _bus_dmamap_create;
    323  1.1  gdamore 	t->_dmamap_destroy = _bus_dmamap_destroy;
    324  1.1  gdamore 	t->_dmamap_load = _bus_dmamap_load;
    325  1.1  gdamore 	t->_dmamap_load_mbuf = _bus_dmamap_load_mbuf;
    326  1.1  gdamore 	t->_dmamap_load_uio = _bus_dmamap_load_uio;
    327  1.1  gdamore 	t->_dmamap_load_raw = _bus_dmamap_load_raw;
    328  1.1  gdamore 	t->_dmamap_unload = _bus_dmamap_unload;
    329  1.1  gdamore 	t->_dmamap_sync = _bus_dmamap_sync;
    330  1.1  gdamore 	t->_dmamem_alloc = _bus_dmamem_alloc;
    331  1.1  gdamore 	t->_dmamem_free = _bus_dmamem_free;
    332  1.1  gdamore 	t->_dmamem_map = _bus_dmamem_map;
    333  1.1  gdamore 	t->_dmamem_unmap = _bus_dmamem_unmap;
    334  1.1  gdamore 	t->_dmamem_mmap = _bus_dmamem_mmap;
    335  1.1  gdamore }
    336  1.1  gdamore 
    337  1.1  gdamore /*
    338  1.1  gdamore  * CPU memory/register stuff
    339  1.1  gdamore  */
    340  1.1  gdamore 
    341  1.1  gdamore #define CHIP	   		arbus
    342  1.1  gdamore #define	CHIP_MEM		/* defined */
    343  1.1  gdamore #define	CHIP_W1_BUS_START(v)	0x00000000UL
    344  1.1  gdamore #define CHIP_W1_BUS_END(v)	0x1fffffffUL
    345  1.1  gdamore #define	CHIP_W1_SYS_START(v)	CHIP_W1_BUS_START(v)
    346  1.1  gdamore #define	CHIP_W1_SYS_END(v)	CHIP_W1_BUS_END(v)
    347  1.1  gdamore 
    348  1.1  gdamore #include <mips/mips/bus_space_alignstride_chipdep.c>
    349