1 1.13 thorpej /* $NetBSD: isa_io.c,v 1.13 2023/12/20 13:55:18 thorpej Exp $ */ 2 1.1 joff 3 1.1 joff /* 4 1.1 joff * Copyright 1997 5 1.1 joff * Digital Equipment Corporation. All rights reserved. 6 1.1 joff * 7 1.1 joff * This software is furnished under license and may be used and 8 1.1 joff * copied only in accordance with the following terms and conditions. 9 1.1 joff * Subject to these conditions, you may download, copy, install, 10 1.1 joff * use, modify and distribute this software in source and/or binary 11 1.1 joff * form. No title or ownership is transferred hereby. 12 1.1 joff * 13 1.1 joff * 1) Any source code used, modified or distributed must reproduce 14 1.1 joff * and retain this copyright notice and list of conditions as 15 1.1 joff * they appear in the source file. 16 1.1 joff * 17 1.1 joff * 2) No right is granted to use any trade name, trademark, or logo of 18 1.1 joff * Digital Equipment Corporation. Neither the "Digital Equipment 19 1.1 joff * Corporation" name nor any trademark or logo of Digital Equipment 20 1.1 joff * Corporation may be used to endorse or promote products derived 21 1.1 joff * from this software without the prior written permission of 22 1.1 joff * Digital Equipment Corporation. 23 1.1 joff * 24 1.1 joff * 3) This software is provided "AS-IS" and any express or implied 25 1.1 joff * warranties, including but not limited to, any implied warranties 26 1.1 joff * of merchantability, fitness for a particular purpose, or 27 1.1 joff * non-infringement are disclaimed. In no event shall DIGITAL be 28 1.1 joff * liable for any damages whatsoever, and in particular, DIGITAL 29 1.1 joff * shall not be liable for special, indirect, consequential, or 30 1.1 joff * incidental damages or damages for lost profits, loss of 31 1.1 joff * revenue or loss of use, whether such damages arise in contract, 32 1.1 joff * negligence, tort, under statute, in equity, at law or otherwise, 33 1.1 joff * even if advised of the possibility of such damage. 34 1.1 joff */ 35 1.1 joff 36 1.1 joff /* 37 1.1 joff * bus_space I/O functions for isa 38 1.1 joff */ 39 1.1 joff 40 1.1 joff #include <sys/cdefs.h> 41 1.13 thorpej __KERNEL_RCSID(0, "$NetBSD: isa_io.c,v 1.13 2023/12/20 13:55:18 thorpej Exp $"); 42 1.1 joff 43 1.1 joff #include <sys/param.h> 44 1.1 joff #include <sys/systm.h> 45 1.13 thorpej #include <sys/vmem_impl.h> 46 1.8 dyoung #include <sys/bus.h> 47 1.1 joff #include <machine/pio.h> 48 1.1 joff #include <machine/isa_machdep.h> 49 1.1 joff 50 1.1 joff /* Proto types for all the bus_space structure functions */ 51 1.1 joff 52 1.1 joff bs_protos(isa); 53 1.1 joff bs_protos(bs_notimpl); 54 1.1 joff 55 1.1 joff /* 56 1.1 joff * Declare the isa bus space tags 57 1.1 joff * The IO and MEM structs are identical, except for the cookies, 58 1.1 joff * which contain the address space bases. 59 1.1 joff */ 60 1.1 joff 61 1.1 joff /* 62 1.1 joff * NOTE: ASSEMBLY LANGUAGE RELIES ON THE COOKIE -- THE FIRST MEMBER OF 63 1.1 joff * THIS STRUCTURE -- TO BE THE VIRTUAL ADDRESS OF 16 BIT ISA/IO! 64 1.1 joff */ 65 1.1 joff struct bus_space isa_io_bs_tag = { 66 1.1 joff /* cookie */ 67 1.12 ryo .bs_cookie = NULL, /* initialized below */ 68 1.1 joff 69 1.1 joff /* mapping/unmapping */ 70 1.12 ryo .bs_map = isa_bs_map, 71 1.12 ryo .bs_unmap = isa_bs_unmap, 72 1.12 ryo .bs_subregion = isa_bs_subregion, 73 1.1 joff 74 1.1 joff /* allocation/deallocation */ 75 1.12 ryo .bs_alloc = isa_bs_alloc, 76 1.12 ryo .bs_free = isa_bs_free, 77 1.1 joff 78 1.1 joff /* get kernel virtual address */ 79 1.12 ryo .bs_vaddr = isa_bs_vaddr, 80 1.1 joff 81 1.1 joff /* mmap bus space for userland */ 82 1.12 ryo .bs_mmap = bs_notimpl_bs_mmap, /* XXX possible even? XXX */ 83 1.1 joff 84 1.1 joff /* barrier */ 85 1.12 ryo .bs_barrier = isa_bs_barrier, 86 1.1 joff 87 1.1 joff /* read (single) */ 88 1.12 ryo .bs_r_1 = isa_bs_r_1, 89 1.12 ryo .bs_r_2 = isa_bs_r_2, 90 1.12 ryo .bs_r_4 = isa_bs_r_4, 91 1.12 ryo .bs_r_8 = bs_notimpl_bs_r_8, 92 1.1 joff 93 1.1 joff /* read multiple */ 94 1.12 ryo .bs_rm_1 = isa_bs_rm_1, 95 1.12 ryo .bs_rm_2 = isa_bs_rm_2, 96 1.12 ryo .bs_rm_4 = isa_bs_rm_4, 97 1.12 ryo .bs_rm_8 = bs_notimpl_bs_rm_8, 98 1.1 joff 99 1.1 joff /* read region */ 100 1.12 ryo .bs_rr_1 = isa_bs_rr_1, 101 1.12 ryo .bs_rr_2 = isa_bs_rr_2, 102 1.12 ryo .bs_rr_4 = isa_bs_rr_4, 103 1.12 ryo .bs_rr_8 = bs_notimpl_bs_rr_8, 104 1.1 joff 105 1.1 joff /* write (single) */ 106 1.12 ryo .bs_w_1 = isa_bs_w_1, 107 1.12 ryo .bs_w_2 = isa_bs_w_2, 108 1.12 ryo .bs_w_4 = isa_bs_w_4, 109 1.12 ryo .bs_w_8 = bs_notimpl_bs_w_8, 110 1.1 joff 111 1.1 joff /* write multiple */ 112 1.12 ryo .bs_wm_1 = isa_bs_wm_1, 113 1.12 ryo .bs_wm_2 = isa_bs_wm_2, 114 1.12 ryo .bs_wm_4 = isa_bs_wm_4, 115 1.12 ryo .bs_wm_8 = bs_notimpl_bs_wm_8, 116 1.1 joff 117 1.1 joff /* write region */ 118 1.12 ryo .bs_wr_1 = isa_bs_wr_1, 119 1.12 ryo .bs_wr_2 = isa_bs_wr_2, 120 1.12 ryo .bs_wr_4 = isa_bs_wr_4, 121 1.12 ryo .bs_wr_8 = bs_notimpl_bs_wr_8, 122 1.1 joff 123 1.1 joff /* set multiple */ 124 1.12 ryo .bs_sm_1 = bs_notimpl_bs_sm_1, 125 1.12 ryo .bs_sm_2 = bs_notimpl_bs_sm_2, 126 1.12 ryo .bs_sm_4 = bs_notimpl_bs_sm_4, 127 1.12 ryo .bs_sm_8 = bs_notimpl_bs_sm_8, 128 1.1 joff 129 1.1 joff /* set region */ 130 1.12 ryo .bs_sr_1 = bs_notimpl_bs_sr_1, 131 1.12 ryo .bs_sr_2 = isa_bs_sr_2, 132 1.12 ryo .bs_sr_4 = bs_notimpl_bs_sr_4, 133 1.12 ryo .bs_sr_8 = bs_notimpl_bs_sr_8, 134 1.1 joff 135 1.1 joff /* copy */ 136 1.12 ryo .bs_c_1 = bs_notimpl_bs_c_1, 137 1.12 ryo .bs_c_2 = bs_notimpl_bs_c_2, 138 1.12 ryo .bs_c_4 = bs_notimpl_bs_c_4, 139 1.12 ryo .bs_c_8 = bs_notimpl_bs_c_8, 140 1.1 joff }; 141 1.1 joff 142 1.1 joff /* 143 1.1 joff * NOTE: ASSEMBLY LANGUAGE RELIES ON THE COOKIE -- THE FIRST MEMBER OF 144 1.1 joff * THIS STRUCTURE -- TO BE THE VIRTUAL ADDRESS OF ISA/MEMORY! 145 1.1 joff */ 146 1.1 joff struct bus_space isa_mem_bs_tag = { 147 1.1 joff /* cookie */ 148 1.12 ryo .bs_cookie = NULL, /* initialized below */ 149 1.1 joff 150 1.1 joff /* mapping/unmapping */ 151 1.12 ryo .bs_map = isa_bs_map, 152 1.12 ryo .bs_unmap = isa_bs_unmap, 153 1.12 ryo .bs_subregion = isa_bs_subregion, 154 1.1 joff 155 1.1 joff /* allocation/deallocation */ 156 1.12 ryo .bs_alloc = isa_bs_alloc, 157 1.12 ryo .bs_free = isa_bs_free, 158 1.1 joff 159 1.1 joff /* get kernel virtual address */ 160 1.12 ryo .bs_vaddr = isa_bs_vaddr, 161 1.1 joff 162 1.1 joff /* mmap bus space for userland */ 163 1.12 ryo .bs_mmap = bs_notimpl_bs_mmap, /* XXX open for now ... XXX */ 164 1.1 joff 165 1.1 joff /* barrier */ 166 1.12 ryo .bs_barrier = isa_bs_barrier, 167 1.1 joff 168 1.1 joff /* read (single) */ 169 1.12 ryo .bs_r_1 = isa_bs_r_1, 170 1.12 ryo .bs_r_2 = isa_bs_r_2, 171 1.12 ryo .bs_r_4 = isa_bs_r_4, 172 1.12 ryo .bs_r_8 = bs_notimpl_bs_r_8, 173 1.1 joff 174 1.1 joff /* read multiple */ 175 1.12 ryo .bs_rm_1 = isa_bs_rm_1, 176 1.12 ryo .bs_rm_2 = isa_bs_rm_2, 177 1.12 ryo .bs_rm_4 = isa_bs_rm_4, 178 1.12 ryo .bs_rm_8 = bs_notimpl_bs_rm_8, 179 1.1 joff 180 1.1 joff /* read region */ 181 1.12 ryo .bs_rr_1 = isa_bs_rr_1, 182 1.12 ryo .bs_rr_2 = isa_bs_rr_2, 183 1.12 ryo .bs_rr_4 = isa_bs_rr_4, 184 1.12 ryo .bs_rr_8 = bs_notimpl_bs_rr_8, 185 1.1 joff 186 1.1 joff /* write (single) */ 187 1.12 ryo .bs_w_1 = isa_bs_w_1, 188 1.12 ryo .bs_w_2 = isa_bs_w_2, 189 1.12 ryo .bs_w_4 = isa_bs_w_4, 190 1.12 ryo .bs_w_8 = bs_notimpl_bs_w_8, 191 1.1 joff 192 1.1 joff /* write multiple */ 193 1.12 ryo .bs_wm_1 = isa_bs_wm_1, 194 1.12 ryo .bs_wm_2 = isa_bs_wm_2, 195 1.12 ryo .bs_wm_4 = isa_bs_wm_4, 196 1.12 ryo .bs_wm_8 = bs_notimpl_bs_wm_8, 197 1.1 joff 198 1.1 joff /* write region */ 199 1.12 ryo .bs_wr_1 = isa_bs_wr_1, 200 1.12 ryo .bs_wr_2 = isa_bs_wr_2, 201 1.12 ryo .bs_wr_4 = isa_bs_wr_4, 202 1.12 ryo .bs_wr_8 = bs_notimpl_bs_wr_8, 203 1.1 joff 204 1.1 joff /* set multiple */ 205 1.12 ryo .bs_sm_1 = bs_notimpl_bs_sm_1, 206 1.12 ryo .bs_sm_2 = bs_notimpl_bs_sm_2, 207 1.12 ryo .bs_sm_4 = bs_notimpl_bs_sm_4, 208 1.12 ryo .bs_sm_8 = bs_notimpl_bs_sm_8, 209 1.1 joff 210 1.1 joff /* set region */ 211 1.12 ryo .bs_sr_1 = bs_notimpl_bs_sr_1, 212 1.12 ryo .bs_sr_2 = isa_bs_sr_2, 213 1.12 ryo .bs_sr_4 = bs_notimpl_bs_sr_4, 214 1.12 ryo .bs_sr_8 = bs_notimpl_bs_sr_8, 215 1.1 joff 216 1.1 joff /* copy */ 217 1.12 ryo .bs_c_1 = bs_notimpl_bs_c_1, 218 1.12 ryo .bs_c_2 = bs_notimpl_bs_c_2, 219 1.12 ryo .bs_c_4 = bs_notimpl_bs_c_4, 220 1.12 ryo .bs_c_8 = bs_notimpl_bs_c_8, 221 1.1 joff }; 222 1.1 joff 223 1.13 thorpej #define ISAIO_BTAG_COUNT VMEM_EST_BTCOUNT(1, 8) 224 1.13 thorpej #define ISAMEM_BTAG_COUNT VMEM_EST_BTCOUNT(1, 8) 225 1.2 joff 226 1.13 thorpej static struct vmem isaio_arena_store; 227 1.13 thorpej static struct vmem isamem_arena_store; 228 1.13 thorpej static struct vmem_btag isaio_btag_store[ISAIO_BTAG_COUNT]; 229 1.13 thorpej static struct vmem_btag isamem_btag_store[ISAMEM_BTAG_COUNT]; 230 1.13 thorpej static vmem_t *isaio_arena; 231 1.13 thorpej static vmem_t *isamem_arena; 232 1.2 joff 233 1.1 joff /* bus space functions */ 234 1.1 joff 235 1.1 joff void 236 1.11 matt isa_io_init(vaddr_t isa_io_addr, vaddr_t isa_mem_addr) 237 1.1 joff { 238 1.13 thorpej int error __diagused; 239 1.13 thorpej 240 1.1 joff isa_io_bs_tag.bs_cookie = (void *)isa_io_addr; 241 1.1 joff isa_mem_bs_tag.bs_cookie = (void *)isa_mem_addr; 242 1.2 joff 243 1.13 thorpej isaio_arena = vmem_init(&isaio_arena_store, 244 1.13 thorpej "isaio", 245 1.13 thorpej 0, /* addr */ 246 1.13 thorpej 0, /* size */ 247 1.13 thorpej 1, /* quantum */ 248 1.13 thorpej NULL, /* importfn */ 249 1.13 thorpej NULL, /* releasefn */ 250 1.13 thorpej NULL, /* source */ 251 1.13 thorpej 0, /* qcache_max */ 252 1.13 thorpej VM_NOSLEEP | VM_PRIVTAGS, 253 1.13 thorpej IPL_NONE); 254 1.13 thorpej KASSERT(isaio_arena != NULL); 255 1.13 thorpej 256 1.13 thorpej vmem_add_bts(isaio_arena, isaio_btag_store, ISAIO_BTAG_COUNT); 257 1.13 thorpej error = vmem_add(isaio_arena, 0x0, 0x10000, VM_NOSLEEP); 258 1.13 thorpej KASSERT(error == 0); 259 1.13 thorpej 260 1.13 thorpej isamem_arena = vmem_init(&isamem_arena_store, 261 1.13 thorpej "isamem", 262 1.13 thorpej 0, /* addr */ 263 1.13 thorpej 0, /* size */ 264 1.13 thorpej 1, /* quantum */ 265 1.13 thorpej NULL, /* importfn */ 266 1.13 thorpej NULL, /* releasefn */ 267 1.13 thorpej NULL, /* source */ 268 1.13 thorpej 0, /* qcache_max */ 269 1.13 thorpej VM_NOSLEEP | VM_PRIVTAGS, 270 1.13 thorpej IPL_NONE); 271 1.13 thorpej KASSERT(isamem_arena != NULL); 272 1.13 thorpej 273 1.13 thorpej vmem_add_bts(isamem_arena, isamem_btag_store, ISAMEM_BTAG_COUNT); 274 1.13 thorpej error = vmem_add(isamem_arena, 0x0, 0x100000, VM_NOSLEEP); 275 1.13 thorpej KASSERT(error == 0); 276 1.1 joff } 277 1.1 joff 278 1.1 joff /* 279 1.1 joff * break the abstraction: sometimes, other parts of the system 280 1.1 joff * (e.g. X servers) need to map ISA space directly. use these 281 1.1 joff * functions sparingly! 282 1.1 joff */ 283 1.11 matt vaddr_t 284 1.1 joff isa_io_data_vaddr(void) 285 1.1 joff { 286 1.11 matt return (vaddr_t)isa_io_bs_tag.bs_cookie; 287 1.1 joff } 288 1.1 joff 289 1.11 matt vaddr_t 290 1.1 joff isa_mem_data_vaddr(void) 291 1.1 joff { 292 1.11 matt return (vaddr_t)isa_mem_bs_tag.bs_cookie; 293 1.1 joff } 294 1.1 joff 295 1.1 joff int 296 1.6 dsl isa_bs_map(void *t, bus_addr_t bpa, bus_size_t size, int cacheable, bus_space_handle_t *bshp) 297 1.1 joff { 298 1.13 thorpej vmem_t *vm; 299 1.2 joff int err; 300 1.2 joff 301 1.3 joff if (t == isa_io_bs_tag.bs_cookie) 302 1.13 thorpej vm = isaio_arena; 303 1.2 joff else 304 1.13 thorpej vm = isamem_arena; 305 1.13 thorpej 306 1.13 thorpej err = vmem_xalloc_addr(vm, bpa, size, VM_NOSLEEP); 307 1.2 joff if (err) 308 1.2 joff return err; 309 1.2 joff 310 1.1 joff *bshp = bpa + (bus_addr_t)t; 311 1.1 joff return(0); 312 1.1 joff } 313 1.1 joff 314 1.1 joff void 315 1.6 dsl isa_bs_unmap(void *t, bus_space_handle_t bsh, bus_size_t size) 316 1.1 joff { 317 1.2 joff isa_bs_free(t, bsh, size); 318 1.1 joff } 319 1.1 joff 320 1.1 joff int 321 1.7 dsl isa_bs_subregion(void *t, bus_space_handle_t bsh, bus_size_t offset, bus_size_t size, bus_space_handle_t *nbshp) 322 1.1 joff { 323 1.1 joff *nbshp = bsh + offset; 324 1.1 joff return(0); 325 1.1 joff } 326 1.1 joff 327 1.1 joff int 328 1.10 matt isa_bs_alloc( 329 1.10 matt void *t, 330 1.10 matt bus_addr_t rstart, 331 1.10 matt bus_addr_t rend, 332 1.10 matt bus_size_t size, 333 1.10 matt bus_size_t alignment, 334 1.10 matt bus_size_t boundary, 335 1.10 matt int cacheable, 336 1.10 matt bus_addr_t *bpap, 337 1.10 matt bus_space_handle_t *bshp) 338 1.1 joff { 339 1.13 thorpej vmem_t *vm; 340 1.13 thorpej vmem_addr_t bpa; 341 1.2 joff int err; 342 1.2 joff 343 1.3 joff if (t == isa_io_bs_tag.bs_cookie) 344 1.13 thorpej vm = isaio_arena; 345 1.2 joff else 346 1.13 thorpej vm = isamem_arena; 347 1.2 joff 348 1.13 thorpej err = vmem_xalloc(vm, size, 349 1.13 thorpej alignment, /* align */ 350 1.13 thorpej 0, /* phase */ 351 1.13 thorpej boundary, /* nocross */ 352 1.13 thorpej rstart, /* minaddr */ 353 1.13 thorpej rend, /* maxaddr */ 354 1.13 thorpej VM_BESTFIT | VM_NOSLEEP, 355 1.13 thorpej &bpa); 356 1.2 joff if (err) 357 1.2 joff return err; 358 1.2 joff 359 1.3 joff *bshp = *bpap = bpa + (bus_addr_t)t; 360 1.2 joff return 0; 361 1.1 joff } 362 1.1 joff 363 1.1 joff void 364 1.6 dsl isa_bs_free(void *t, bus_space_handle_t bsh, bus_size_t size) 365 1.1 joff { 366 1.13 thorpej vmem_t *vm; 367 1.2 joff 368 1.3 joff if (t == isa_io_bs_tag.bs_cookie) 369 1.13 thorpej vm = isaio_arena; 370 1.2 joff else 371 1.13 thorpej vm = isamem_arena; 372 1.2 joff 373 1.13 thorpej vmem_xfree(vm, bsh - (bus_addr_t)t, size); 374 1.1 joff } 375 1.1 joff 376 1.1 joff void * 377 1.6 dsl isa_bs_vaddr(void *t, bus_space_handle_t bsh) 378 1.1 joff { 379 1.1 joff 380 1.1 joff return ((void *)bsh); 381 1.1 joff } 382 1.1 joff 383 1.1 joff void 384 1.7 dsl isa_bs_barrier(void *t, bus_space_handle_t bsh, bus_size_t offset, bus_size_t len, int flags) 385 1.1 joff { 386 1.1 joff /* just return */ 387 1.1 joff } 388