1 1.2 ryo /* $NetBSD: clps711x_space.c,v 1.2 2018/03/16 17:56:31 ryo Exp $ */ 2 1.1 kiyohara 3 1.1 kiyohara /* 4 1.1 kiyohara * Copyright (c) 2004 Jesse Off 5 1.1 kiyohara * All rights reserved. 6 1.1 kiyohara * 7 1.1 kiyohara * Redistribution and use in source and binary forms, with or without 8 1.1 kiyohara * modification, are permitted provided that the following conditions 9 1.1 kiyohara * are met: 10 1.1 kiyohara * 1. Redistributions of source code must retain the above copyright 11 1.1 kiyohara * notice, this list of conditions and the following disclaimer. 12 1.1 kiyohara * 2. Redistributions in binary form must reproduce the above copyright 13 1.1 kiyohara * notice, this list of conditions and the following disclaimer in the 14 1.1 kiyohara * documentation and/or other materials provided with the distribution. 15 1.1 kiyohara * 16 1.1 kiyohara * THIS SOFTWARE IS PROVIDED BY ICHIRO FUKUHARA ``AS IS'' AND ANY EXPRESS OR 17 1.1 kiyohara * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 1.1 kiyohara * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 1.1 kiyohara * IN NO EVENT SHALL ICHIRO FUKUHARA OR THE VOICES IN HIS HEAD BE LIABLE FOR 20 1.1 kiyohara * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 1.1 kiyohara * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 1.1 kiyohara * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 1.1 kiyohara * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 1.1 kiyohara * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 1.1 kiyohara * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 1.1 kiyohara * SUCH DAMAGE. 27 1.1 kiyohara */ 28 1.1 kiyohara 29 1.1 kiyohara #include <sys/cdefs.h> 30 1.2 ryo __KERNEL_RCSID(0, "$NetBSD: clps711x_space.c,v 1.2 2018/03/16 17:56:31 ryo Exp $"); 31 1.1 kiyohara 32 1.1 kiyohara /* 33 1.1 kiyohara * bus_space I/O functions for clps711x 34 1.1 kiyohara */ 35 1.1 kiyohara 36 1.1 kiyohara #include <sys/param.h> 37 1.1 kiyohara #include <sys/bus.h> 38 1.1 kiyohara #include <sys/systm.h> 39 1.1 kiyohara 40 1.1 kiyohara #include <uvm/uvm_extern.h> 41 1.1 kiyohara 42 1.1 kiyohara #include <machine/pmap.h> 43 1.1 kiyohara 44 1.1 kiyohara /* Proto types for all the bus_space structure functions */ 45 1.1 kiyohara bs_protos(clps711x); 46 1.1 kiyohara bs_protos(generic); 47 1.1 kiyohara bs_protos(bs_notimpl); 48 1.1 kiyohara 49 1.1 kiyohara struct bus_space clps711x_bs_tag = { 50 1.1 kiyohara /* cookie */ 51 1.2 ryo .bs_cookie = (void *) 0, 52 1.1 kiyohara 53 1.1 kiyohara /* mapping/unmapping */ 54 1.2 ryo .bs_map = clps711x_bs_map, 55 1.2 ryo .bs_unmap = clps711x_bs_unmap, 56 1.2 ryo .bs_subregion = clps711x_bs_subregion, 57 1.1 kiyohara 58 1.1 kiyohara /* allocation/deallocation */ 59 1.2 ryo .bs_alloc = clps711x_bs_alloc, 60 1.2 ryo .bs_free = clps711x_bs_free, 61 1.1 kiyohara 62 1.1 kiyohara /* get kernel virtual address */ 63 1.2 ryo .bs_vaddr = clps711x_bs_vaddr, 64 1.1 kiyohara 65 1.1 kiyohara /* mmap bus space for userland */ 66 1.2 ryo .bs_mmap = clps711x_bs_mmap, 67 1.1 kiyohara 68 1.1 kiyohara /* barrier */ 69 1.2 ryo .bs_barrier = clps711x_bs_barrier, 70 1.1 kiyohara 71 1.1 kiyohara /* read (single) */ 72 1.2 ryo .bs_r_1 = generic_bs_r_1, 73 1.2 ryo .bs_r_2 = bs_notimpl_bs_r_2, /* XXXX */ 74 1.2 ryo .bs_r_4 = generic_bs_r_4, 75 1.2 ryo .bs_r_8 = bs_notimpl_bs_r_8, 76 1.1 kiyohara 77 1.1 kiyohara /* read multiple */ 78 1.2 ryo .bs_rm_1 = generic_bs_rm_1, 79 1.2 ryo .bs_rm_2 = bs_notimpl_bs_rm_2, /* XXXX */ 80 1.2 ryo .bs_rm_4 = generic_bs_rm_4, 81 1.2 ryo .bs_rm_8 = bs_notimpl_bs_rm_8, 82 1.1 kiyohara 83 1.1 kiyohara /* read region */ 84 1.2 ryo .bs_rr_1 = generic_bs_rr_1, 85 1.2 ryo .bs_rr_2 = bs_notimpl_bs_rr_2, /* XXXX */ 86 1.2 ryo .bs_rr_4 = generic_bs_rr_4, 87 1.2 ryo .bs_rr_8 = bs_notimpl_bs_rr_8, 88 1.1 kiyohara 89 1.1 kiyohara /* write (single) */ 90 1.2 ryo .bs_w_1 = generic_bs_w_1, 91 1.2 ryo .bs_w_2 = bs_notimpl_bs_w_2, /* XXXX */ 92 1.2 ryo .bs_w_4 = generic_bs_w_4, 93 1.2 ryo .bs_w_8 = bs_notimpl_bs_w_8, 94 1.1 kiyohara 95 1.1 kiyohara /* write multiple */ 96 1.2 ryo .bs_wm_1 = generic_bs_wm_1, 97 1.2 ryo .bs_wm_2 = bs_notimpl_bs_wm_2, /* XXXX */ 98 1.2 ryo .bs_wm_4 = generic_bs_wm_4, 99 1.2 ryo .bs_wm_8 = bs_notimpl_bs_wm_8, 100 1.1 kiyohara 101 1.1 kiyohara /* write region */ 102 1.2 ryo .bs_wr_1 = generic_bs_wr_1, 103 1.2 ryo .bs_wr_2 = bs_notimpl_bs_wr_2, /* XXXX */ 104 1.2 ryo .bs_wr_4 = generic_bs_wr_4, 105 1.2 ryo .bs_wr_8 = bs_notimpl_bs_wr_8, 106 1.1 kiyohara 107 1.1 kiyohara /* set multiple */ 108 1.2 ryo .bs_sm_1 = bs_notimpl_bs_sm_1, 109 1.2 ryo .bs_sm_2 = bs_notimpl_bs_sm_2, 110 1.2 ryo .bs_sm_4 = bs_notimpl_bs_sm_4, 111 1.2 ryo .bs_sm_8 = bs_notimpl_bs_sm_8, 112 1.1 kiyohara 113 1.1 kiyohara /* set region */ 114 1.2 ryo .bs_sr_1 = bs_notimpl_bs_sr_1, 115 1.2 ryo .bs_sr_2 = bs_notimpl_bs_sr_2, /* XXXX */ 116 1.2 ryo .bs_sr_4 = generic_bs_sr_4, 117 1.2 ryo .bs_sr_8 = bs_notimpl_bs_sr_8, 118 1.1 kiyohara 119 1.1 kiyohara /* copy */ 120 1.2 ryo .bs_c_1 = bs_notimpl_bs_c_1, 121 1.2 ryo .bs_c_2 = bs_notimpl_bs_c_2, /* XXXX */ 122 1.2 ryo .bs_c_4 = bs_notimpl_bs_c_4, 123 1.2 ryo .bs_c_8 = bs_notimpl_bs_c_8, 124 1.1 kiyohara }; 125 1.1 kiyohara 126 1.1 kiyohara int 127 1.1 kiyohara clps711x_bs_map(void *t, bus_addr_t bpa, bus_size_t size, 128 1.1 kiyohara int cacheable, bus_space_handle_t *bshp) 129 1.1 kiyohara { 130 1.1 kiyohara const struct pmap_devmap *pd; 131 1.1 kiyohara paddr_t startpa, endpa, pa, offset; 132 1.1 kiyohara vaddr_t va; 133 1.1 kiyohara 134 1.1 kiyohara if ((pd = pmap_devmap_find_pa(bpa, size)) != NULL) { 135 1.1 kiyohara /* Device was statically mapped. */ 136 1.1 kiyohara *bshp = pd->pd_va + (bpa - pd->pd_pa); 137 1.1 kiyohara return 0; 138 1.1 kiyohara } 139 1.1 kiyohara 140 1.1 kiyohara endpa = round_page(bpa + size); 141 1.1 kiyohara offset = bpa & PAGE_MASK; 142 1.1 kiyohara startpa = trunc_page(bpa); 143 1.1 kiyohara 144 1.1 kiyohara /* Get some VM. */ 145 1.1 kiyohara va = uvm_km_alloc(kernel_map, endpa - startpa, 0, 146 1.1 kiyohara UVM_KMF_VAONLY | UVM_KMF_NOWAIT); 147 1.1 kiyohara if (va == 0) 148 1.1 kiyohara return ENOMEM; 149 1.1 kiyohara 150 1.1 kiyohara /* Store the bus space handle */ 151 1.1 kiyohara *bshp = va + offset; 152 1.1 kiyohara 153 1.1 kiyohara /* Now map the pages */ 154 1.1 kiyohara for (pa = startpa; pa < endpa; pa += PAGE_SIZE, va += PAGE_SIZE) { 155 1.1 kiyohara pmap_enter(pmap_kernel(), va, pa, 156 1.1 kiyohara VM_PROT_READ | VM_PROT_WRITE, 157 1.1 kiyohara VM_PROT_READ | VM_PROT_WRITE | PMAP_WIRED); 158 1.1 kiyohara } 159 1.1 kiyohara pmap_update(pmap_kernel()); 160 1.1 kiyohara 161 1.1 kiyohara return 0; 162 1.1 kiyohara } 163 1.1 kiyohara 164 1.1 kiyohara void 165 1.1 kiyohara clps711x_bs_unmap(void *t, bus_space_handle_t bsh, bus_size_t size) 166 1.1 kiyohara { 167 1.1 kiyohara vaddr_t va, endva; 168 1.1 kiyohara 169 1.1 kiyohara if (pmap_devmap_find_va(bsh, size) != NULL) 170 1.1 kiyohara /* Device was statically mapped; nothing to do. */ 171 1.1 kiyohara return; 172 1.1 kiyohara 173 1.1 kiyohara endva = round_page(bsh + size); 174 1.1 kiyohara va = trunc_page(bsh); 175 1.1 kiyohara 176 1.1 kiyohara pmap_remove(pmap_kernel(), va, endva); 177 1.1 kiyohara pmap_update(pmap_kernel()); 178 1.1 kiyohara uvm_km_free(kernel_map, va, endva - va, UVM_KMF_VAONLY); 179 1.1 kiyohara } 180 1.1 kiyohara 181 1.1 kiyohara int 182 1.1 kiyohara clps711x_bs_alloc(void *t, bus_addr_t rstart, bus_addr_t rend, bus_size_t size, 183 1.1 kiyohara bus_size_t alignment, bus_size_t boundary, int cacheable, 184 1.1 kiyohara bus_addr_t *bpap, bus_space_handle_t *bshp) 185 1.1 kiyohara { 186 1.1 kiyohara 187 1.1 kiyohara panic("clps711x_bs_alloc(): not implemented\n"); 188 1.1 kiyohara } 189 1.1 kiyohara 190 1.1 kiyohara void 191 1.1 kiyohara clps711x_bs_free(void *t, bus_space_handle_t bsh, bus_size_t size) 192 1.1 kiyohara { 193 1.1 kiyohara 194 1.1 kiyohara panic("clps711x_bs_free(): not implemented\n"); 195 1.1 kiyohara } 196 1.1 kiyohara 197 1.1 kiyohara int 198 1.1 kiyohara clps711x_bs_subregion(void *t, bus_space_handle_t bsh, bus_size_t offset, 199 1.1 kiyohara bus_size_t size, bus_space_handle_t *nbshp) 200 1.1 kiyohara { 201 1.1 kiyohara 202 1.1 kiyohara *nbshp = bsh + offset; 203 1.1 kiyohara return 0; 204 1.1 kiyohara } 205 1.1 kiyohara 206 1.1 kiyohara void * 207 1.1 kiyohara clps711x_bs_vaddr(void *t, bus_space_handle_t bsh) 208 1.1 kiyohara { 209 1.1 kiyohara 210 1.1 kiyohara return (void *)bsh; 211 1.1 kiyohara } 212 1.1 kiyohara 213 1.1 kiyohara paddr_t 214 1.1 kiyohara clps711x_bs_mmap(void *t, bus_addr_t addr, off_t off, int prot, int flags) 215 1.1 kiyohara { 216 1.1 kiyohara /* Not supported. */ 217 1.1 kiyohara return -1; 218 1.1 kiyohara } 219 1.1 kiyohara 220 1.1 kiyohara void 221 1.1 kiyohara clps711x_bs_barrier(void *t, bus_space_handle_t bsh, bus_size_t offset, 222 1.1 kiyohara bus_size_t len, int flags) 223 1.1 kiyohara { 224 1.1 kiyohara /* Nothing */ 225 1.1 kiyohara } 226