Home | History | Annotate | Line # | Download | only in virt68k
      1  1.1  thorpej /*	$NetBSD: bus_space.c,v 1.1 2024/01/02 07:41:02 thorpej Exp $	*/
      2  1.1  thorpej 
      3  1.1  thorpej /*-
      4  1.1  thorpej  * Copyright (c) 1998, 2000 The NetBSD Foundation, Inc.
      5  1.1  thorpej  * All rights reserved.
      6  1.1  thorpej  *
      7  1.1  thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  thorpej  * by Steve C. Woodford and Jason R. Thorpe.
      9  1.1  thorpej  *
     10  1.1  thorpej  * Redistribution and use in source and binary forms, with or without
     11  1.1  thorpej  * modification, are permitted provided that the following conditions
     12  1.1  thorpej  * are met:
     13  1.1  thorpej  * 1. Redistributions of source code must retain the above copyright
     14  1.1  thorpej  *    notice, this list of conditions and the following disclaimer.
     15  1.1  thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  thorpej  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  thorpej  *    documentation and/or other materials provided with the distribution.
     18  1.1  thorpej  *
     19  1.1  thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.1  thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.1  thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.1  thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.1  thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.1  thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.1  thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.1  thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.1  thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.1  thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.1  thorpej  * POSSIBILITY OF SUCH DAMAGE.
     30  1.1  thorpej  */
     31  1.1  thorpej 
     32  1.1  thorpej #include <sys/cdefs.h>
     33  1.1  thorpej __KERNEL_RCSID(0, "$NetBSD: bus_space.c,v 1.1 2024/01/02 07:41:02 thorpej Exp $");
     34  1.1  thorpej 
     35  1.1  thorpej #define _VIRT68K_BUS_DMA_PRIVATE    /* For _bus_dmamem_map/_bus_dmamem_unmap */
     36  1.1  thorpej #define _VIRT68K_BUS_SPACE_PRIVATE
     37  1.1  thorpej 
     38  1.1  thorpej #include <sys/param.h>
     39  1.1  thorpej #include <sys/systm.h>
     40  1.1  thorpej 
     41  1.1  thorpej #include <uvm/uvm_extern.h>
     42  1.1  thorpej 
     43  1.1  thorpej #include <machine/cpu.h>
     44  1.1  thorpej #include <machine/pte.h>
     45  1.1  thorpej #include <machine/bus.h>
     46  1.1  thorpej 
     47  1.1  thorpej static	void	peek1(void *, void *);
     48  1.1  thorpej static	void	peek2(void *, void *);
     49  1.1  thorpej static	void	peek4(void *, void *);
     50  1.1  thorpej static	void	poke1(void *, u_int);
     51  1.1  thorpej static	void	poke2(void *, u_int);
     52  1.1  thorpej static	void	poke4(void *, u_int);
     53  1.1  thorpej static	int	do_peek(void (*)(void *, void *), void *, void *);
     54  1.1  thorpej static	int	do_poke(void (*)(void *, u_int), void *, u_int);
     55  1.1  thorpej 
     56  1.1  thorpej /*
     57  1.1  thorpej  * Used in locore.s/trap.c to determine if faults are being trapped.
     58  1.1  thorpej  */
     59  1.1  thorpej label_t *nofault;
     60  1.1  thorpej 
     61  1.1  thorpej /* ARGSUSED */
     62  1.1  thorpej int
     63  1.1  thorpej _bus_space_map(void *cookie, bus_addr_t addr, bus_size_t size, int flags,
     64  1.1  thorpej     bus_space_handle_t *bushp)
     65  1.1  thorpej {
     66  1.1  thorpej 	/*
     67  1.1  thorpej 	 * All devices on virt68k are direct-mapped by TT registers.
     68  1.1  thorpej 	 */
     69  1.1  thorpej 	if (addr < VIRT68K_IO_BASE) {
     70  1.1  thorpej 		return EINVAL;
     71  1.1  thorpej 	}
     72  1.1  thorpej 
     73  1.1  thorpej 	*bushp = (bus_space_handle_t)addr;
     74  1.1  thorpej 	return 0;
     75  1.1  thorpej }
     76  1.1  thorpej 
     77  1.1  thorpej /* ARGSUSED */
     78  1.1  thorpej void
     79  1.1  thorpej _bus_space_unmap(void *cookie, bus_space_handle_t bush, bus_size_t size)
     80  1.1  thorpej {
     81  1.1  thorpej 	KASSERT((bus_addr_t)bush >= VIRT68K_IO_BASE);
     82  1.1  thorpej }
     83  1.1  thorpej 
     84  1.1  thorpej /* ARGSUSED */
     85  1.1  thorpej int
     86  1.1  thorpej _bus_space_peek_1(void *cookie, bus_space_handle_t bush, bus_size_t offset,
     87  1.1  thorpej     uint8_t *valuep)
     88  1.1  thorpej {
     89  1.1  thorpej 	uint8_t v;
     90  1.1  thorpej 
     91  1.1  thorpej 	if (valuep == NULL)
     92  1.1  thorpej 		valuep = &v;
     93  1.1  thorpej 
     94  1.1  thorpej 	return do_peek(&peek1, (void *)(bush + offset), (void *)valuep);
     95  1.1  thorpej }
     96  1.1  thorpej 
     97  1.1  thorpej /* ARGSUSED */
     98  1.1  thorpej int
     99  1.1  thorpej _bus_space_peek_2(void *cookie, bus_space_handle_t bush, bus_size_t offset,
    100  1.1  thorpej     uint16_t *valuep)
    101  1.1  thorpej {
    102  1.1  thorpej 	uint16_t v;
    103  1.1  thorpej 
    104  1.1  thorpej 	if (valuep == NULL)
    105  1.1  thorpej 		valuep = &v;
    106  1.1  thorpej 
    107  1.1  thorpej 	return do_peek(&peek2, (void *)(bush + offset), (void *)valuep);
    108  1.1  thorpej }
    109  1.1  thorpej 
    110  1.1  thorpej /* ARGSUSED */
    111  1.1  thorpej int
    112  1.1  thorpej _bus_space_peek_4(void *cookie, bus_space_handle_t bush, bus_size_t offset,
    113  1.1  thorpej     uint32_t *valuep)
    114  1.1  thorpej {
    115  1.1  thorpej 	uint32_t v;
    116  1.1  thorpej 
    117  1.1  thorpej 	if (valuep == NULL)
    118  1.1  thorpej 		valuep = &v;
    119  1.1  thorpej 
    120  1.1  thorpej 	return do_peek(&peek4, (void *)(bush + offset), (void *)valuep);
    121  1.1  thorpej }
    122  1.1  thorpej 
    123  1.1  thorpej /* ARGSUSED */
    124  1.1  thorpej int
    125  1.1  thorpej _bus_space_poke_1(void *cookie, bus_space_handle_t bush, bus_size_t offset,
    126  1.1  thorpej     uint8_t value)
    127  1.1  thorpej {
    128  1.1  thorpej 
    129  1.1  thorpej 	return do_poke(&poke1, (void *)(bush + offset), (u_int)value);
    130  1.1  thorpej }
    131  1.1  thorpej 
    132  1.1  thorpej /* ARGSUSED */
    133  1.1  thorpej int
    134  1.1  thorpej _bus_space_poke_2(void *cookie, bus_space_handle_t bush, bus_size_t offset,
    135  1.1  thorpej     uint16_t value)
    136  1.1  thorpej {
    137  1.1  thorpej 
    138  1.1  thorpej 	return do_poke(&poke2, (void *)(bush + offset), (u_int)value);
    139  1.1  thorpej }
    140  1.1  thorpej 
    141  1.1  thorpej /* ARGSUSED */
    142  1.1  thorpej int
    143  1.1  thorpej _bus_space_poke_4(void *cookie, bus_space_handle_t bush, bus_size_t offset,
    144  1.1  thorpej     uint32_t value)
    145  1.1  thorpej {
    146  1.1  thorpej 
    147  1.1  thorpej 	return do_poke(&poke4, (void *)(bush + offset), (u_int)value);
    148  1.1  thorpej }
    149  1.1  thorpej 
    150  1.1  thorpej static void
    151  1.1  thorpej peek1(void *addr, void *vp)
    152  1.1  thorpej {
    153  1.1  thorpej 
    154  1.1  thorpej 	*((uint8_t *)vp) =  *((uint8_t *)addr);
    155  1.1  thorpej }
    156  1.1  thorpej 
    157  1.1  thorpej static void
    158  1.1  thorpej peek2(void *addr, void *vp)
    159  1.1  thorpej {
    160  1.1  thorpej 
    161  1.1  thorpej 	*((uint16_t *)vp) = *((uint16_t *)addr);
    162  1.1  thorpej }
    163  1.1  thorpej 
    164  1.1  thorpej static void
    165  1.1  thorpej peek4(void *addr, void *vp)
    166  1.1  thorpej {
    167  1.1  thorpej 
    168  1.1  thorpej 	*((uint32_t *)vp) = *((uint32_t *)addr);
    169  1.1  thorpej }
    170  1.1  thorpej 
    171  1.1  thorpej static void
    172  1.1  thorpej poke1(void *addr, u_int value)
    173  1.1  thorpej {
    174  1.1  thorpej 
    175  1.1  thorpej 	*((uint8_t *)addr) = value;
    176  1.1  thorpej }
    177  1.1  thorpej 
    178  1.1  thorpej static void
    179  1.1  thorpej poke2(void *addr, u_int value)
    180  1.1  thorpej {
    181  1.1  thorpej 
    182  1.1  thorpej 	*((uint16_t *)addr) = value;
    183  1.1  thorpej }
    184  1.1  thorpej 
    185  1.1  thorpej static void
    186  1.1  thorpej poke4(void *addr, u_int value)
    187  1.1  thorpej {
    188  1.1  thorpej 
    189  1.1  thorpej 	*((uint32_t *)addr) = value;
    190  1.1  thorpej }
    191  1.1  thorpej 
    192  1.1  thorpej static int
    193  1.1  thorpej do_peek(void (*peekfn)(void *, void *), void *addr, void *valuep)
    194  1.1  thorpej {
    195  1.1  thorpej 	label_t faultbuf;
    196  1.1  thorpej 
    197  1.1  thorpej 	nofault = &faultbuf;
    198  1.1  thorpej 	if (setjmp(&faultbuf)) {
    199  1.1  thorpej 		nofault = NULL;
    200  1.1  thorpej 		return 1;
    201  1.1  thorpej 	}
    202  1.1  thorpej 
    203  1.1  thorpej 	(*peekfn)(addr, valuep);
    204  1.1  thorpej 
    205  1.1  thorpej 	nofault = NULL;
    206  1.1  thorpej 	return 0;
    207  1.1  thorpej }
    208  1.1  thorpej 
    209  1.1  thorpej static int
    210  1.1  thorpej do_poke(void (*pokefn)(void *, u_int), void *addr, u_int value)
    211  1.1  thorpej {
    212  1.1  thorpej 	label_t faultbuf;
    213  1.1  thorpej 
    214  1.1  thorpej 	nofault = &faultbuf;
    215  1.1  thorpej 	if (setjmp(&faultbuf)) {
    216  1.1  thorpej 		nofault = NULL;
    217  1.1  thorpej 		return 1;
    218  1.1  thorpej 	}
    219  1.1  thorpej 
    220  1.1  thorpej 	(*pokefn)(addr, value);
    221  1.1  thorpej 
    222  1.1  thorpej 	nofault = NULL;
    223  1.1  thorpej 	return 0;
    224  1.1  thorpej }
    225