Home | History | Annotate | Line # | Download | only in apbus
apbus.c revision 1.3
      1  1.3    onoe /*	$NetBSD: apbus.c,v 1.3 2000/10/12 03:11:38 onoe Exp $	*/
      2  1.1  tsubai 
      3  1.1  tsubai /*-
      4  1.1  tsubai  * Copyright (C) 1999 SHIMIZU Ryo.  All rights reserved.
      5  1.1  tsubai  *
      6  1.1  tsubai  * Redistribution and use in source and binary forms, with or without
      7  1.1  tsubai  * modification, are permitted provided that the following conditions
      8  1.1  tsubai  * are met:
      9  1.1  tsubai  * 1. Redistributions of source code must retain the above copyright
     10  1.1  tsubai  *    notice, this list of conditions and the following disclaimer.
     11  1.1  tsubai  * 2. Redistributions in binary form must reproduce the above copyright
     12  1.1  tsubai  *    notice, this list of conditions and the following disclaimer in the
     13  1.1  tsubai  *    documentation and/or other materials provided with the distribution.
     14  1.1  tsubai  * 3. The name of the author may not be used to endorse or promote products
     15  1.1  tsubai  *    derived from this software without specific prior written permission.
     16  1.1  tsubai  *
     17  1.1  tsubai  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     18  1.1  tsubai  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     19  1.1  tsubai  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20  1.1  tsubai  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     21  1.1  tsubai  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     22  1.1  tsubai  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23  1.1  tsubai  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24  1.1  tsubai  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25  1.1  tsubai  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     26  1.1  tsubai  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27  1.1  tsubai  */
     28  1.1  tsubai 
     29  1.1  tsubai #include <sys/param.h>
     30  1.1  tsubai #include <sys/systm.h>
     31  1.3    onoe #include <sys/malloc.h>
     32  1.1  tsubai #include <sys/device.h>
     33  1.1  tsubai 
     34  1.1  tsubai #include <machine/adrsmap.h>
     35  1.1  tsubai #include <machine/autoconf.h>
     36  1.3    onoe #define _NEWSMIPS_BUS_DMA_PRIVATE
     37  1.3    onoe #include <machine/bus.h>
     38  1.1  tsubai #include <newsmips/apbus/apbusvar.h>
     39  1.1  tsubai 
     40  1.1  tsubai static int  apbusmatch __P((struct device *, struct cfdata *, void *));
     41  1.1  tsubai static void apbusattach __P((struct device *, struct device *, void *));
     42  1.1  tsubai static int apbusprint __P((void *, const char *));
     43  1.1  tsubai /* static void *aptokseg0 __P((void *)); */
     44  1.1  tsubai 
     45  1.1  tsubai #define	MAXAPDEVNUM	32
     46  1.1  tsubai 
     47  1.1  tsubai struct apbus_softc {
     48  1.1  tsubai 	struct device apbs_dev;
     49  1.1  tsubai };
     50  1.1  tsubai 
     51  1.1  tsubai struct cfattach ap_ca = {
     52  1.1  tsubai 	sizeof(struct apbus_softc), apbusmatch, apbusattach
     53  1.1  tsubai };
     54  1.1  tsubai 
     55  1.1  tsubai #define	APBUS_DEVNAMELEN	16
     56  1.1  tsubai 
     57  1.1  tsubai struct ap_intrhand {
     58  1.3    onoe 	struct ap_intrhand *ai_next;
     59  1.1  tsubai 	int ai_mask;
     60  1.1  tsubai 	int ai_priority;
     61  1.3    onoe 	int (*ai_func) __P((void*));	/* function */
     62  1.1  tsubai 	void *ai_aux;			/* softc */
     63  1.1  tsubai 	char ai_name[APBUS_DEVNAMELEN];
     64  1.1  tsubai 	int ai_ctlno;
     65  1.1  tsubai };
     66  1.1  tsubai 
     67  1.1  tsubai #define	NLEVEL	2
     68  1.1  tsubai 
     69  1.3    onoe static struct ap_intrhand *apintr[NLEVEL];
     70  1.1  tsubai 
     71  1.1  tsubai static int
     72  1.1  tsubai apbusmatch(parent, cfdata, aux)
     73  1.1  tsubai         struct device *parent;
     74  1.1  tsubai         struct cfdata *cfdata;
     75  1.1  tsubai         void *aux;
     76  1.1  tsubai {
     77  1.1  tsubai 	struct confargs *ca = aux;
     78  1.1  tsubai 
     79  1.1  tsubai 	if (strcmp(ca->ca_name, "ap") != 0)
     80  1.1  tsubai 		return 0;
     81  1.1  tsubai 
     82  1.1  tsubai 	return 1;
     83  1.1  tsubai }
     84  1.1  tsubai 
     85  1.1  tsubai 
     86  1.1  tsubai static void
     87  1.1  tsubai apbusattach(parent, self, aux)
     88  1.1  tsubai         struct device *parent;
     89  1.1  tsubai         struct device *self;
     90  1.1  tsubai         void *aux;
     91  1.1  tsubai {
     92  1.1  tsubai 	struct apbus_attach_args child;
     93  1.2  tsubai 	struct apbus_dev *apdev;
     94  1.1  tsubai 	struct apbus_ctl *apctl;
     95  1.1  tsubai 
     96  1.2  tsubai 	*(volatile u_int *)(NEWS5000_APBUS_INTST) = 0xffffffff;
     97  1.2  tsubai 	*(volatile u_int *)(NEWS5000_APBUS_INTMSK) = 0xffffffff;
     98  1.2  tsubai 	*(volatile u_int *)(NEWS5000_APBUS_CTRL) = 0x00000004;
     99  1.2  tsubai 	*(volatile u_int *)(NEWS5000_APBUS_DMA) = 0xffffffff;
    100  1.1  tsubai 
    101  1.1  tsubai 	printf("\n");
    102  1.1  tsubai 
    103  1.1  tsubai 	/*
    104  1.1  tsubai 	 * get first ap-device
    105  1.1  tsubai 	 */
    106  1.1  tsubai 	apdev = apbus_lookupdev(NULL);
    107  1.1  tsubai 
    108  1.1  tsubai 	/*
    109  1.1  tsubai 	 * trace device chain
    110  1.1  tsubai 	 */
    111  1.1  tsubai 	while (apdev) {
    112  1.1  tsubai 		apctl = apdev->apbd_ctl;
    113  1.1  tsubai 
    114  1.1  tsubai 		/*
    115  1.1  tsubai 		 * probe physical device only
    116  1.1  tsubai 		 * (no pseudo device)
    117  1.1  tsubai 		 */
    118  1.1  tsubai 		if (apctl && apctl->apbc_hwbase) {
    119  1.1  tsubai 			/*
    120  1.1  tsubai 			 * ... and, all units
    121  1.1  tsubai 			 */
    122  1.1  tsubai 			while (apctl) {
    123  1.1  tsubai 				/* make apbus_attach_args for devices */
    124  1.1  tsubai 				child.apa_name = apdev->apbd_name;
    125  1.1  tsubai 				child.apa_ctlnum = apctl->apbc_ctlno;
    126  1.1  tsubai 				child.apa_slotno = apctl->apbc_sl;
    127  1.1  tsubai 				child.apa_hwbase = apctl->apbc_hwbase;
    128  1.1  tsubai 
    129  1.1  tsubai 				config_found(self, &child, apbusprint);
    130  1.1  tsubai 
    131  1.1  tsubai 				apctl = apctl->apbc_link;
    132  1.1  tsubai 			}
    133  1.1  tsubai 		}
    134  1.1  tsubai 
    135  1.1  tsubai 		apdev = apdev->apbd_link;
    136  1.1  tsubai 	}
    137  1.1  tsubai }
    138  1.1  tsubai 
    139  1.1  tsubai int
    140  1.1  tsubai apbusprint(aux, pnp)
    141  1.1  tsubai 	void *aux;
    142  1.1  tsubai 	const char *pnp;
    143  1.1  tsubai {
    144  1.1  tsubai 	struct apbus_attach_args *a = aux;
    145  1.1  tsubai 
    146  1.1  tsubai 	if (pnp)
    147  1.1  tsubai 		printf("%s at %s slot%d addr 0x%lx",
    148  1.1  tsubai 			a->apa_name, pnp, a->apa_slotno, a->apa_hwbase);
    149  1.1  tsubai 
    150  1.1  tsubai 	return UNCONF;
    151  1.1  tsubai }
    152  1.1  tsubai 
    153  1.1  tsubai #if 0
    154  1.1  tsubai void *
    155  1.1  tsubai aptokseg0(va)
    156  1.1  tsubai 	void *va;
    157  1.1  tsubai {
    158  1.1  tsubai 	vaddr_t addr = (vaddr_t)va;
    159  1.1  tsubai 
    160  1.1  tsubai 	if (addr >= 0xfff00000) {
    161  1.1  tsubai 		addr -= 0xfff00000;
    162  1.1  tsubai 		addr += physmem << PGSHIFT;
    163  1.1  tsubai 		addr += 0x80000000;
    164  1.1  tsubai 		va = (void *)addr;
    165  1.1  tsubai 	}
    166  1.1  tsubai 	return va;
    167  1.1  tsubai }
    168  1.1  tsubai #endif
    169  1.1  tsubai 
    170  1.1  tsubai void
    171  1.1  tsubai apbus_wbflush()
    172  1.1  tsubai {
    173  1.2  tsubai 	volatile int *wbflush = (int *)NEWS5000_WBFLUSH;
    174  1.1  tsubai 
    175  1.1  tsubai 	(void)*wbflush;
    176  1.1  tsubai }
    177  1.1  tsubai 
    178  1.1  tsubai /*
    179  1.1  tsubai  * called by hardware interrupt routine
    180  1.1  tsubai  */
    181  1.1  tsubai int
    182  1.1  tsubai apbus_intr_call(level, stat)
    183  1.1  tsubai 	int level;
    184  1.1  tsubai 	int stat;
    185  1.1  tsubai {
    186  1.1  tsubai 	int nintr = 0;
    187  1.3    onoe 	struct ap_intrhand *ai;
    188  1.1  tsubai 
    189  1.3    onoe 	for (ai = apintr[level]; ai != NULL; ai = ai->ai_next) {
    190  1.3    onoe 		if (ai->ai_mask & stat) {
    191  1.3    onoe 			nintr += (*ai->ai_func)(ai->ai_aux);
    192  1.1  tsubai 		}
    193  1.1  tsubai 	}
    194  1.1  tsubai 	return nintr;
    195  1.1  tsubai }
    196  1.1  tsubai 
    197  1.1  tsubai /*
    198  1.1  tsubai  * register device interrupt routine
    199  1.1  tsubai  */
    200  1.1  tsubai void *
    201  1.1  tsubai apbus_intr_establish(level, mask, priority, func, aux, name, ctlno)
    202  1.1  tsubai 	int level;
    203  1.1  tsubai 	int mask;
    204  1.1  tsubai 	int priority;
    205  1.3    onoe 	int (*func) __P((void *));
    206  1.1  tsubai 	void *aux;
    207  1.1  tsubai 	char *name;
    208  1.1  tsubai 	int ctlno;
    209  1.1  tsubai {
    210  1.3    onoe 	struct ap_intrhand *ai, **aip;
    211  1.3    onoe 	volatile unsigned int *inten0 = (volatile unsigned int *)NEWS5000_INTEN0;
    212  1.3    onoe 	volatile unsigned int *inten1 = (volatile unsigned int *)NEWS5000_INTEN1;
    213  1.3    onoe 
    214  1.3    onoe 	ai = malloc(sizeof(*ai), M_DEVBUF, M_NOWAIT);
    215  1.3    onoe 	if (ai == NULL)
    216  1.3    onoe 		panic("apbus_intr_establish: can't malloc handler info");
    217  1.3    onoe 	ai->ai_mask = mask;
    218  1.3    onoe 	ai->ai_priority = priority;
    219  1.3    onoe 	ai->ai_func = func;
    220  1.3    onoe 	ai->ai_aux = aux;
    221  1.3    onoe 	strncpy(ai->ai_name, name, APBUS_DEVNAMELEN-1);
    222  1.3    onoe 	ai->ai_ctlno = ctlno;
    223  1.3    onoe 
    224  1.3    onoe 	for (aip = &apintr[level]; *aip != NULL; aip = &(*aip)->ai_next) {
    225  1.3    onoe 		if ((*aip)->ai_priority < priority)
    226  1.1  tsubai 			break;
    227  1.1  tsubai 	}
    228  1.3    onoe 	ai->ai_next = *aip;
    229  1.3    onoe 	*aip = ai;
    230  1.3    onoe 	switch (level) {
    231  1.3    onoe 	case 0:
    232  1.3    onoe 		*inten0 |= mask;
    233  1.3    onoe 		break;
    234  1.3    onoe 	case 1:
    235  1.3    onoe 		*inten1 |= mask;
    236  1.3    onoe 		break;
    237  1.3    onoe 	}
    238  1.3    onoe 
    239  1.3    onoe 	return (void *)ai;
    240  1.3    onoe }
    241  1.3    onoe 
    242  1.3    onoe void
    243  1.3    onoe apbus_dmamap_sync(t, map, offset, len, ops)
    244  1.3    onoe 	bus_dma_tag_t t;
    245  1.3    onoe 	bus_dmamap_t map;
    246  1.3    onoe 	bus_addr_t offset;
    247  1.3    onoe 	bus_size_t len;
    248  1.3    onoe 	int ops;
    249  1.3    onoe {
    250  1.1  tsubai 
    251  1.3    onoe 	/*
    252  1.3    onoe 	 * Flush DMA cache by issueing IO read for the AProm of specified slot.
    253  1.3    onoe 	 */
    254  1.3    onoe 	bus_space_read_4(t->_slotbaset, t->_slotbaseh, 0);
    255  1.3    onoe 
    256  1.3    onoe 	_bus_dmamap_sync(t, map, offset, len, ops);
    257  1.3    onoe }
    258  1.1  tsubai 
    259  1.3    onoe struct newsmips_bus_dma_tag apbus_dma_tag = {
    260  1.3    onoe 	_bus_dmamap_create,
    261  1.3    onoe 	_bus_dmamap_destroy,
    262  1.3    onoe 	_bus_dmamap_load,
    263  1.3    onoe 	_bus_dmamap_load_mbuf,
    264  1.3    onoe 	_bus_dmamap_load_uio,
    265  1.3    onoe 	_bus_dmamap_load_raw,
    266  1.3    onoe 	_bus_dmamap_unload,
    267  1.3    onoe 	apbus_dmamap_sync,
    268  1.3    onoe 	_bus_dmamem_alloc,
    269  1.3    onoe 	_bus_dmamem_free,
    270  1.3    onoe 	_bus_dmamem_map,
    271  1.3    onoe 	_bus_dmamem_unmap,
    272  1.3    onoe 	_bus_dmamem_mmap,
    273  1.3    onoe };
    274  1.3    onoe 
    275  1.3    onoe struct newsmips_bus_dma_tag *
    276  1.3    onoe apbus_dmatag_init(apa)
    277  1.3    onoe 	struct apbus_attach_args *apa;
    278  1.3    onoe {
    279  1.3    onoe 	struct newsmips_bus_dma_tag *dmat;
    280  1.1  tsubai 
    281  1.3    onoe 	dmat = malloc(sizeof(*dmat), M_DEVBUF, M_NOWAIT);
    282  1.3    onoe 	if (dmat != NULL) {
    283  1.3    onoe 		memcpy(dmat, &apbus_dma_tag, sizeof(*dmat));
    284  1.3    onoe 		dmat->_slotno = apa->apa_slotno;
    285  1.3    onoe 		dmat->_slotbaset = 0;
    286  1.3    onoe 		dmat->_slotbaseh = apa->apa_hwbase;
    287  1.3    onoe 	}
    288  1.3    onoe 	return dmat;
    289  1.1  tsubai }
    290