Home | History | Annotate | Line # | Download | only in hp300
      1 /*	$NetBSD: bus_space.c,v 1.25 2026/04/26 10:52:14 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe.
      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 /*
     33  * Implementation of bus_space mapping for the hp300.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 __KERNEL_RCSID(0, "$NetBSD: bus_space.c,v 1.25 2026/04/26 10:52:14 thorpej Exp $");
     38 
     39 #include <sys/param.h>
     40 #include <sys/systm.h>
     41 #include <sys/vmem.h>
     42 
     43 #include <machine/autoconf.h>
     44 #include <machine/bus.h>
     45 
     46 #include <uvm/uvm_extern.h>
     47 
     48 int
     49 bus_space_map(bus_space_tag_t t, bus_addr_t bpa, bus_size_t size, int flags,
     50     bus_space_handle_t *bshp)
     51 {
     52 	vmem_addr_t kva;
     53 	vsize_t offset;
     54 	int error;
     55 
     56 	if (t->bustype == HP300_BUS_SPACE_INTIO) {
     57 		/*
     58 		 * Intio space is direct-mapped in pmap_bootstrap(); just
     59 		 * do the translation.
     60 		 */
     61 		*bshp = (bus_space_handle_t)IIOV(INTIOBASE + bpa);
     62 		return 0;
     63 	}
     64 
     65 	if (t->bustype != HP300_BUS_SPACE_DIO &&
     66 	    t->bustype != HP300_BUS_SPACE_SGC)
     67 		panic("%s: bad space tag", __func__);
     68 
     69 	/*
     70 	 * Allocate virtual address space from the extio extent map.
     71 	 */
     72 	offset = m68k_page_offset(bpa);
     73 	size = m68k_round_page(offset + size);
     74 	error = vmem_alloc(extio_arena, size, VM_BESTFIT | VM_NOSLEEP, &kva);
     75 	if (error) {
     76 		return error;
     77 	}
     78 
     79 	vaddr_t va = kva;
     80 	vaddr_t eva = kva + size;
     81 	bpa = m68k_trunc_page(bpa);
     82 	while (va < eva) {
     83 		pmap_kenter_pa(va, bpa, VM_PROT_READ | VM_PROT_WRITE,
     84 		    PMAP_NOCACHE);
     85 		va += PAGE_SIZE;
     86 		bpa += PAGE_SIZE;
     87 	}
     88 
     89 	/*
     90 	 * All done.
     91 	 */
     92 	*bshp = (bus_space_handle_t)(kva + offset);
     93 	return 0;
     94 }
     95 
     96 int
     97 bus_space_alloc(bus_space_tag_t t, bus_addr_t rstart, bus_addr_t rend,
     98     bus_size_t size, bus_size_t alignment, bus_size_t boundary, int flags,
     99     bus_addr_t *bpap, bus_space_handle_t *bshp)
    100 {
    101 
    102 	/*
    103 	 * Not meaningful on any currently-supported hp300 bus.
    104 	 */
    105 	return EINVAL;
    106 }
    107 
    108 void
    109 bus_space_free(bus_space_tag_t t, bus_space_handle_t bsh, bus_size_t size)
    110 {
    111 
    112 	/*
    113 	 * Not meaningful on any currently-supported hp300 bus.
    114 	 */
    115 	panic("%s: shouldn't be here", __func__);
    116 }
    117 
    118 void
    119 bus_space_unmap(bus_space_tag_t t, bus_space_handle_t bsh, bus_size_t size)
    120 {
    121 	vaddr_t kva;
    122 	vsize_t offset;
    123 
    124 	if (t->bustype == HP300_BUS_SPACE_INTIO) {
    125 		/*
    126 		 * Intio space is direct-mapped in pmap_bootstrap(); nothing
    127 		 * to do
    128 		 */
    129 		return;
    130 	}
    131 
    132 	if (t->bustype != HP300_BUS_SPACE_DIO &&
    133 	    t->bustype != HP300_BUS_SPACE_SGC)
    134 		panic("%s: bad space tag", __func__);
    135 
    136 	kva = m68k_trunc_page(bsh);
    137 	offset = m68k_page_offset(bsh);
    138 	size = m68k_round_page(offset + size);
    139 
    140 	KASSERT(bsh >= (vaddr_t)extiobase);
    141 	KASSERT(bsh < ((vaddr_t)extiobase + ptoa(EIOMAPSIZE)));
    142 
    143 	/*
    144 	 * Unmap the range.
    145 	 */
    146 	pmap_kremove(kva, size);
    147 
    148 	/*
    149 	 * Free it from the extio arena.
    150 	 */
    151 	vmem_free(extio_arena, kva, size);
    152 }
    153 
    154 int
    155 bus_space_subregion(bus_space_tag_t t, bus_space_handle_t bsh,
    156     bus_size_t offset, bus_size_t size, bus_space_handle_t *nbshp)
    157 {
    158 
    159 	*nbshp = bsh + offset;
    160 	return 0;
    161 }
    162 
    163 int
    164 hp300_bus_space_probe(bus_space_tag_t t, bus_space_handle_t bsh,
    165     bus_size_t offset, int sz)
    166 {
    167 	label_t faultbuf;
    168 	int i;
    169 
    170 	nofault = &faultbuf;
    171 	if (setjmp(nofault)) {
    172 		nofault = NULL;
    173 		return 0;
    174 	}
    175 
    176 	switch (sz) {
    177 	case 1:
    178 		i = bus_space_read_1(t, bsh, offset);
    179 		break;
    180 
    181 	case 2:
    182 		i = bus_space_read_2(t, bsh, offset);
    183 		break;
    184 
    185 	case 4:
    186 		i = bus_space_read_4(t, bsh, offset);
    187 		break;
    188 
    189 	default:
    190 		panic("%s: unupported data size %d", __func__, sz);
    191 		/* NOTREACHED */
    192 	}
    193 	__USE(i);
    194 	nofault = NULL;
    195 	return 1;
    196 }
    197