Home | History | Annotate | Line # | Download | only in sun2
      1 /*	$NetBSD: obmem.c,v 1.21 2021/08/07 16:19:06 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1996 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Adam Glass, Gordon W. Ross, and Matthew Fredette.
      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: obmem.c,v 1.21 2021/08/07 16:19:06 thorpej Exp $");
     34 
     35 #include <sys/param.h>
     36 #include <sys/systm.h>
     37 #include <sys/device.h>
     38 
     39 #include <uvm/uvm_extern.h>
     40 
     41 #include <machine/autoconf.h>
     42 #include <machine/pmap.h>
     43 
     44 #include <sun2/sun2/control.h>
     45 #include <sun2/sun2/machdep.h>
     46 
     47 static int  obmem_match(device_t, cfdata_t, void *);
     48 static void obmem_attach(device_t, device_t, void *);
     49 
     50 struct obmem_softc {
     51 	device_t	sc_dev;		/* base device */
     52 	bus_space_tag_t	sc_bustag;	/* parent bus tag */
     53 	bus_dma_tag_t	sc_dmatag;	/* parent bus dma tag */
     54 };
     55 
     56 CFATTACH_DECL_NEW(obmem, sizeof(struct obmem_softc),
     57     obmem_match, obmem_attach, NULL, NULL);
     58 
     59 static int obmem_attached;
     60 
     61 static	paddr_t obmem_bus_mmap(bus_space_tag_t, bus_type_t, bus_addr_t,
     62 	    off_t, int, int);
     63 static	int _obmem_bus_map(bus_space_tag_t, bus_type_t, bus_addr_t,
     64 	    bus_size_t, int, vaddr_t, bus_space_handle_t *);
     65 
     66 static struct sun68k_bus_space_tag obmem_space_tag = {
     67 	NULL,				/* cookie */
     68 	NULL,				/* parent bus tag */
     69 	_obmem_bus_map,			/* bus_space_map */
     70 	NULL,				/* bus_space_unmap */
     71 	NULL,				/* bus_space_subregion */
     72 	NULL,				/* bus_space_barrier */
     73 	obmem_bus_mmap,			/* bus_space_mmap */
     74 	NULL,				/* bus_intr_establish */
     75 	NULL,				/* bus_space_peek_N */
     76 	NULL				/* bus_space_poke_N */
     77 };
     78 
     79 static int
     80 obmem_match(device_t parent, cfdata_t cf, void *aux)
     81 {
     82 	struct mainbus_attach_args *ma = aux;
     83 
     84 	if (obmem_attached)
     85 		return 0;
     86 
     87 	return ma->ma_name == NULL || strcmp(cf->cf_name, ma->ma_name) == 0;
     88 }
     89 
     90 static void
     91 obmem_attach(device_t parent, device_t self, void *aux)
     92 {
     93 	struct mainbus_attach_args *ma = aux;
     94 	struct obmem_softc *sc = device_private(self);
     95 	struct obmem_attach_args obma;
     96 	const char *const *cpp;
     97 	static const char *const special[] = {
     98 		/* find these first */
     99 		NULL
    100 	};
    101 
    102 	obmem_attached = 1;
    103 
    104 	sc->sc_dev = self;
    105 	aprint_normal("\n");
    106 
    107 	sc->sc_bustag = ma->ma_bustag;
    108 	sc->sc_dmatag = ma->ma_dmatag;
    109 
    110 	obmem_space_tag.cookie = sc;
    111 	obmem_space_tag.parent = sc->sc_bustag;
    112 
    113 	/*
    114 	 * Prepare the skeleton attach arguments for our devices.
    115 	 * The values we give in the locators are indications to
    116 	 * sun68k_bus_search about which locators must and must not
    117 	 * be defined.
    118 	 */
    119 	obma = *ma;
    120 	obma.obma_bustag = &obmem_space_tag;
    121 	obma.obma_paddr = LOCATOR_REQUIRED;
    122 	obma.obma_pri = LOCATOR_OPTIONAL;
    123 
    124 	/* Find all `early' obmem devices */
    125 	for (cpp = special; *cpp != NULL; cpp++) {
    126 		obma.obma_name = *cpp;
    127 		config_search(self, &obma,
    128 		    CFARGS(.search = sun68k_bus_search));
    129 	}
    130 
    131 	/* Find all other obmem devices */
    132 	obma.obma_name = NULL;
    133 	config_search(self, &obma,
    134 	    CFARGS(.search = sun68k_bus_search));
    135 }
    136 
    137 int
    138 _obmem_bus_map(bus_space_tag_t t, bus_type_t btype, bus_addr_t paddr,
    139     bus_size_t size, int flags, vaddr_t vaddr, bus_space_handle_t *hp)
    140 {
    141 	struct obmem_softc *sc = t->cookie;
    142 
    143 	return bus_space_map2(sc->sc_bustag, PMAP_OBMEM, paddr,
    144 	    size, flags, vaddr, hp);
    145 }
    146 
    147 paddr_t
    148 obmem_bus_mmap(bus_space_tag_t t, bus_type_t btype, bus_addr_t paddr, off_t off,
    149     int prot, int flags)
    150 {
    151 	struct obmem_softc *sc = t->cookie;
    152 
    153 	return bus_space_mmap2(sc->sc_bustag, PMAP_OBMEM, paddr, off,
    154 	    prot, flags);
    155 }
    156