Home | History | Annotate | Line # | Download | only in isa
isa_machdep.c revision 1.19
      1 /*	$NetBSD: isa_machdep.c,v 1.19 2021/08/07 16:19:00 thorpej Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2001 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Wayne Knowles
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * 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 copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: isa_machdep.c,v 1.19 2021/08/07 16:19:00 thorpej Exp $");
     34 
     35 #include <sys/param.h>
     36 #include <sys/systm.h>
     37 #include <sys/device.h>
     38 #include <sys/kmem.h>
     39 #include <sys/queue.h>
     40 
     41 #include <machine/sysconf.h>
     42 #include <machine/autoconf.h>
     43 #include <machine/mainboard.h>
     44 #include <machine/bus.h>
     45 
     46 #include <dev/isa/isavar.h>
     47 #include <dev/isa/isareg.h>
     48 
     49 static int	mipscoisabusprint(void *, const char *);
     50 static int	isabusmatch(device_t, cfdata_t, void *);
     51 static void	isabusattach(device_t, device_t, void *);
     52 
     53 struct isabus_softc {
     54 	struct mipsco_isa_chipset sc_isa_ic;
     55 };
     56 
     57 CFATTACH_DECL_NEW(isabus, sizeof(struct isabus_softc),
     58     isabusmatch, isabusattach, NULL, NULL);
     59 
     60 extern struct cfdriver isabus_cd;
     61 
     62 static struct mipsco_bus_space	isa_io_bst, isa_mem_bst, isa_ctl_bst;
     63 static struct mipsco_bus_dma_tag isa_dmatag;
     64 
     65 static void isa_bus_space_init(struct mipsco_bus_space *, const char *,
     66 				     paddr_t, size_t);
     67 int    isa_intr(void *);
     68 
     69 
     70 int
     71 isabusmatch(device_t parent, cfdata_t cfp, void *aux)
     72 {
     73 	struct confargs *ca = aux;
     74 
     75 	if (strcmp(ca->ca_name, isabus_cd.cd_name) != 0)
     76 		return 0;
     77 	return 1;
     78 }
     79 
     80 static void
     81 isa_bus_space_init(struct mipsco_bus_space *bst, const char *type, paddr_t paddr, size_t len)
     82 {
     83 	vaddr_t vaddr = MIPS_PHYS_TO_KSEG1(paddr); /* XXX */
     84 
     85 	/* Setup default bus_space */
     86 	mipsco_bus_space_init(bst, type, paddr, vaddr, 0x0, len);
     87 
     88 	/* ISA bus maps 1 word for every byte, therefore stride = 2 */
     89 	mipsco_bus_space_set_aligned_stride(bst, 2);
     90 
     91 	/*
     92 	 * ISA bus will do an automatic byte swap, but when accessing
     93 	 * memory using bus_space_stream functions we need to byte swap
     94 	 * to reverse the one performed in hardware
     95 	 */
     96 	bst->bs_bswap = 1;
     97 
     98 	bst->bs_intr_establish = (void *)isa_intr_establish;
     99 
    100 	printf(" %s %p", type, (void *)vaddr);
    101 }
    102 
    103 
    104 void
    105 isabusattach(device_t parent, device_t self, void *aux)
    106 {
    107 	struct isabus_softc *sc = device_private(self);
    108 	struct mipsco_isa_chipset *ic = &sc->sc_isa_ic;
    109 	struct isabus_attach_args iba;
    110 
    111 	printf(":");
    112 
    113 	iba.iba_iot	= &isa_io_bst;
    114 	iba.iba_memt	= &isa_mem_bst;
    115 	iba.iba_dmat	= &isa_dmatag;
    116 	iba.iba_ic	= ic;
    117 
    118 	isa_bus_space_init(&isa_io_bst,  "isa_io",   PIZAZZ_ISA_IOBASE,
    119 	  	PIZAZZ_ISA_IOSIZE);
    120 
    121 	isa_bus_space_init(&isa_mem_bst, "isa_mem",  PIZAZZ_ISA_MEMBASE,
    122 		PIZAZZ_ISA_MEMSIZE);
    123 
    124 	isa_bus_space_init(&isa_ctl_bst, "isa_intr", PIZAZZ_ISA_INTRLATCH,
    125 	  	sizeof(u_int32_t));
    126 
    127 	_bus_dma_tag_init(&isa_dmatag);
    128 
    129 	ic->ic_bst = &isa_ctl_bst;
    130 
    131 	if (bus_space_map(ic->ic_bst, 0x00000, sizeof(u_int32_t),
    132 			  BUS_SPACE_MAP_LINEAR, &ic->ic_bsh) != 0) {
    133 		printf(": can't map intrreg\n");
    134 		return;
    135 	}
    136 
    137 	/* Clear ISA interrupt latch */
    138 	bus_space_write_4(ic->ic_bst, ic->ic_bsh, 0, 0);
    139 
    140 	evcnt_attach_dynamic(&ic->ic_intrcnt, EVCNT_TYPE_INTR, NULL,
    141 			     device_xname(self), "intr");
    142 
    143 	LIST_INIT(&ic->intr_q);
    144 	(*platform.intr_establish)(SYS_INTR_ATBUS, isa_intr, ic);
    145 
    146 	printf("\n");
    147 	config_found(self, &iba, mipscoisabusprint, CFARGS_NONE);
    148 }
    149 
    150 int
    151 mipscoisabusprint(void *aux, const char *name)
    152 {
    153 	if (name == NULL)
    154 		return UNCONF;
    155 	return QUIET;
    156 }
    157 
    158 void
    159 isa_attach_hook(device_t parent, device_t self, struct isabus_attach_args *iba)
    160 {
    161 }
    162 
    163 void
    164 isa_detach_hook(isa_chipset_tag_t ic, device_t self)
    165 {
    166 }
    167 
    168 const struct evcnt *
    169 isa_intr_evcnt(isa_chipset_tag_t ic, int irq)
    170 {
    171 	/* XXX for now, no evcnt parent reported */
    172 	return NULL;
    173 }
    174 
    175 void *
    176 isa_intr_establish(isa_chipset_tag_t ic, int intr, int type, int level, int (*ih_fun)(void*), void *ih_arg)
    177 	/* type:   XXX not yet */
    178 	/* level:   XXX not yet */
    179 {
    180 	struct mipsco_intrhand *ih;
    181 
    182 	ih = kmem_alloc(sizeof *ih, KM_SLEEP);
    183 	ih->ih_fun = ih_fun;
    184 	ih->ih_arg  = ih_arg;
    185 	LIST_INSERT_HEAD(&ic->intr_q, ih, ih_q);
    186 	return ih;
    187 }
    188 
    189 void
    190 isa_intr_disestablish(isa_chipset_tag_t ic, void *cookie)
    191 {
    192 	struct mipsco_intrhand *ih = cookie;
    193 
    194 	LIST_REMOVE(ih, ih_q);
    195 	kmem_free(ih, sizeof(*ih));
    196 }
    197 
    198 int
    199 isa_intr_alloc(isa_chipset_tag_t ic, int mask, int type, int *irq)
    200 {
    201 	return 0;
    202 }
    203 
    204 int
    205 isa_intr(void *arg)
    206 {
    207 	struct mipsco_isa_chipset *ic = (struct mipsco_isa_chipset *)arg;
    208 	struct mipsco_intrhand *ih;
    209 	int rv, handled;
    210 
    211 	ic->ic_intrcnt.ev_count++;
    212 
    213 	handled = 0;
    214 	LIST_FOREACH(ih, &ic->intr_q, ih_q) {
    215 		/*
    216 		 * The handler returns one of three values:
    217 		 *   0:	This interrupt wasn't for me.
    218 		 *   1: This interrupt was for me.
    219 		 *  -1: This interrupt might have been for me, but I can't say
    220 		 *      for sure.
    221 		 */
    222 		rv = (*ih->ih_fun)(ih->ih_arg);
    223 		handled |= (rv != 0);
    224 	}
    225 
    226 	/* Clear ISA interrupt latch */
    227 	bus_space_write_4(ic->ic_bst, ic->ic_bsh, 0, 0);
    228 
    229 	return handled;
    230 }
    231