Home | History | Annotate | Line # | Download | only in mainbus
      1 /*	$NetBSD: mainbus_io.c,v 1.24 2018/03/16 17:56:32 ryo Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1997 Mark Brinicombe.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by Mark Brinicombe.
     18  * 4. The name of the company nor the name of the author may be used to
     19  *    endorse or promote products derived from this software without specific
     20  *    prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
     23  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     24  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
     26  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     27  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     28  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  * SUCH DAMAGE.
     33  */
     34 
     35 /*
     36  * bus_space I/O functions for mainbus
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 __KERNEL_RCSID(0, "$NetBSD: mainbus_io.c,v 1.24 2018/03/16 17:56:32 ryo Exp $");
     41 
     42 #include <sys/param.h>
     43 #include <sys/systm.h>
     44 #include <sys/queue.h>
     45 
     46 #include <uvm/uvm.h>
     47 
     48 #include <sys/bus.h>
     49 #include <machine/pmap.h>
     50 
     51 /* Proto types for all the bus_space structure functions */
     52 
     53 bs_protos(mainbus);
     54 bs_protos(bs_notimpl);
     55 
     56 /* Declare the mainbus bus space tag */
     57 
     58 struct bus_space mainbus_bs_tag = {
     59 	/* cookie */
     60 	.bs_cookie = NULL,
     61 
     62 	/* mapping/unmapping */
     63 	.bs_map = mainbus_bs_map,
     64 	.bs_unmap = mainbus_bs_unmap,
     65 	.bs_subregion = mainbus_bs_subregion,
     66 
     67 	/* allocation/deallocation */
     68 	.bs_alloc = mainbus_bs_alloc,
     69 	.bs_free = mainbus_bs_free,
     70 
     71 	/* get kernel virtual address */
     72 	.bs_vaddr = NULL, /* there is no linear mapping */
     73 
     74 	/* Mmap bus space for user */
     75 	.bs_mmap = mainbus_bs_mmap,
     76 
     77 	/* barrier */
     78 	.bs_barrier = mainbus_bs_barrier,
     79 
     80 	/* read (single) */
     81 	.bs_r_1 = mainbus_bs_r_1,
     82 	.bs_r_2 = mainbus_bs_r_2,
     83 	.bs_r_4 = mainbus_bs_r_4,
     84 	.bs_r_8 = bs_notimpl_bs_r_8,
     85 
     86 	/* read multiple */
     87 	.bs_rm_1 =bs_notimpl_bs_rm_1,
     88 	.bs_rm_2 = mainbus_bs_rm_2,
     89 	.bs_rm_4 = bs_notimpl_bs_rm_4,
     90 	.bs_rm_8 = bs_notimpl_bs_rm_8,
     91 
     92 	/* read region */
     93 	.bs_rr_1 = bs_notimpl_bs_rr_1,
     94 	.bs_rr_2 = bs_notimpl_bs_rr_2,
     95 	.bs_rr_4 = bs_notimpl_bs_rr_4,
     96 	.bs_rr_8 = bs_notimpl_bs_rr_8,
     97 
     98 	/* write (single) */
     99 	.bs_w_1 = mainbus_bs_w_1,
    100 	.bs_w_2 = mainbus_bs_w_2,
    101 	.bs_w_4 = mainbus_bs_w_4,
    102 	.bs_w_8 = bs_notimpl_bs_w_8,
    103 
    104 	/* write multiple */
    105 	.bs_wm_1 = mainbus_bs_wm_1,
    106 	.bs_wm_2 = mainbus_bs_wm_2,
    107 	.bs_wm_4 = bs_notimpl_bs_wm_4,
    108 	.bs_wm_8 = bs_notimpl_bs_wm_8,
    109 
    110 	/* write region */
    111 	.bs_wr_1 = bs_notimpl_bs_wr_1,
    112 	.bs_wr_2 = bs_notimpl_bs_wr_2,
    113 	.bs_wr_4 = bs_notimpl_bs_wr_4,
    114 	.bs_wr_8 = bs_notimpl_bs_wr_8,
    115 
    116 	.bs_sm_1 = bs_notimpl_bs_sm_1,
    117 	.bs_sm_2 = bs_notimpl_bs_sm_2,
    118 	.bs_sm_4 = bs_notimpl_bs_sm_4,
    119 	.bs_sm_8 = bs_notimpl_bs_sm_8,
    120 
    121 	/* set region */
    122 	.bs_sr_1 = bs_notimpl_bs_sr_1,
    123 	.bs_sr_2 = bs_notimpl_bs_sr_2,
    124 	.bs_sr_4 = bs_notimpl_bs_sr_4,
    125 	.bs_sr_8 = bs_notimpl_bs_sr_8,
    126 
    127 	/* copy */
    128 	.bs_c_1 = bs_notimpl_bs_c_1,
    129 	.bs_c_2 = bs_notimpl_bs_c_2,
    130 	.bs_c_4 = bs_notimpl_bs_c_4,
    131 	.bs_c_8 = bs_notimpl_bs_c_8,
    132 
    133 #ifdef __BUS_SPACE_HAS_STREAM_METHODS
    134 	/* stream methods */
    135 	/* read (single) */
    136 	.bs_r_1_s = mainbus_bs_r_1,
    137 	.bs_r_2_s = mainbus_bs_r_2,
    138 	.bs_r_4_s = mainbus_bs_r_4,
    139 	.bs_r_8_s = bs_notimpl_bs_r_8,
    140 
    141 	/* read multiple */
    142 	.bs_rm_1_s = bs_notimpl_bs_rm_1,
    143 	.bs_rm_2_s = mainbus_bs_rm_2,
    144 	.bs_rm_4_s = bs_notimpl_bs_rm_4,
    145 	.bs_rm_8_s = bs_notimpl_bs_rm_8,
    146 
    147 	/* read region */
    148 	.bs_rr_1_s = bs_notimpl_bs_rr_1,
    149 	.bs_rr_2_s = bs_notimpl_bs_rr_2,
    150 	.bs_rr_4_s = bs_notimpl_bs_rr_4,
    151 	.bs_rr_8_s = bs_notimpl_bs_rr_8,
    152 
    153 	/* write (single) */
    154 	.bs_w_1_s = mainbus_bs_w_1,
    155 	.bs_w_2_s = mainbus_bs_w_2,
    156 	.bs_w_4_s = mainbus_bs_w_4,
    157 	.bs_w_8_s = bs_notimpl_bs_w_8,
    158 
    159 	/* write multiple */
    160 	.bs_wm_1_s = mainbus_bs_wm_1,
    161 	.bs_wm_2_s = mainbus_bs_wm_2,
    162 	.bs_wm_4_s = bs_notimpl_bs_wm_4,
    163 	.bs_wm_8_s = bs_notimpl_bs_wm_8,
    164 
    165 	/* write region */
    166 	.bs_wr_1_s = bs_notimpl_bs_wr_1,
    167 	.bs_wr_2_s = bs_notimpl_bs_wr_2,
    168 	.bs_wr_4_s = bs_notimpl_bs_wr_4,
    169 	.bs_wr_8_s = bs_notimpl_bs_wr_8,
    170 #endif
    171 };
    172 
    173 /* bus space functions */
    174 
    175 int
    176 mainbus_bs_map(void *t, bus_addr_t bpa, bus_size_t size, int flags, bus_space_handle_t *bshp)
    177 {
    178 	u_long startpa, endpa, pa;
    179 	vaddr_t va;
    180 
    181 	if ((u_long)bpa > (u_long)KERNEL_BASE) {
    182 		/* XXX This is a temporary hack to aid transition. */
    183 		*bshp = bpa;
    184 		return(0);
    185 	}
    186 
    187 	startpa = trunc_page(bpa);
    188 	endpa = round_page(bpa + size);
    189 
    190 	/* XXX use extent manager to check duplicate mapping */
    191 
    192 	va = uvm_km_alloc(kernel_map, endpa - startpa, 0,
    193 	    UVM_KMF_VAONLY | UVM_KMF_NOWAIT);
    194 	if (! va)
    195 		return(ENOMEM);
    196 
    197 	*bshp = (bus_space_handle_t)(va + (bpa - startpa));
    198 
    199 	const int pmapflags =
    200 	    (flags & (BUS_SPACE_MAP_CACHEABLE|BUS_SPACE_MAP_PREFETCHABLE))
    201 		? 0
    202 		: PMAP_NOCACHE;
    203 	for (pa = startpa; pa < endpa; pa += PAGE_SIZE, va += PAGE_SIZE) {
    204 		pmap_kenter_pa(va, pa, VM_PROT_READ|VM_PROT_WRITE, pmapflags);
    205 	}
    206 	pmap_update(pmap_kernel());
    207 
    208 	return(0);
    209 }
    210 
    211 int
    212 mainbus_bs_alloc(void *t, bus_addr_t rstart, bus_addr_t rend,
    213     bus_size_t size, bus_size_t alignment, bus_size_t boundary,
    214     int cacheable,
    215     bus_addr_t *bpap, bus_space_handle_t *bshp)
    216 {
    217 	panic("mainbus_bs_alloc(): Help!");
    218 }
    219 
    220 
    221 void
    222 mainbus_bs_unmap(void *t, bus_space_handle_t bsh, bus_size_t size)
    223 {
    224 	/*
    225 	 * Temporary implementation
    226 	 */
    227 }
    228 
    229 void
    230 mainbus_bs_free(void *t, bus_space_handle_t bsh, bus_size_t size)
    231 {
    232 
    233 	panic("mainbus_bs_free(): Help!");
    234 	/* mainbus_bs_unmap() does all that we need to do. */
    235 /*	mainbus_bs_unmap(t, bsh, size);*/
    236 }
    237 
    238 int
    239 mainbus_bs_subregion(void *t, bus_space_handle_t bsh, bus_size_t offset, bus_size_t size, bus_space_handle_t *nbshp)
    240 {
    241 
    242 	*nbshp = bsh + (offset << 2);
    243 	return (0);
    244 }
    245 
    246 paddr_t
    247 mainbus_bs_mmap(void *t, bus_addr_t paddr, off_t offset, int prot, int flags)
    248 {
    249 	/*
    250 	 * mmap from address `paddr+offset' for one page
    251 	 */
    252 	 return (arm_btop((paddr + offset)));
    253 }
    254 
    255 void
    256 mainbus_bs_barrier(void *t, bus_space_handle_t bsh, bus_size_t offset, bus_size_t len, int flags)
    257 {
    258 }
    259 
    260 /* End of mainbus_io.c */
    261