1 /* $OpenBSD: bus.h,v 1.5 2009/07/26 18:48:55 miod Exp $ */ 2 /* $NetBSD: bus.h,v 1.9 1998/01/13 18:32:15 scottr Exp $ */ 3 4 /*- 5 * Copyright (c) 1996, 1997, 1998 The NetBSD Foundation, Inc. 6 * All rights reserved. 7 * 8 * This code is derived from software contributed to The NetBSD Foundation 9 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, 10 * NASA Ames Research Center. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31 * POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 /* 35 * Copyright (C) 1997 Scott Reynolds. All rights reserved. 36 * 37 * Redistribution and use in source and binary forms, with or without 38 * modification, are permitted provided that the following conditions 39 * are met: 40 * 1. Redistributions of source code must retain the above copyright 41 * notice, this list of conditions and the following disclaimer. 42 * 2. Redistributions in binary form must reproduce the above copyright 43 * notice, this list of conditions and the following disclaimer in the 44 * documentation and/or other materials provided with the distribution. 45 * 3. The name of the author may not be used to endorse or promote products 46 * derived from this software without specific prior written permission 47 * 48 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 49 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 50 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 51 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 52 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 53 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 54 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 55 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 56 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 57 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 58 */ 59 60 #ifndef _LUNA88K_BUS_H_ 61 #define _LUNA88K_BUS_H_ 62 63 /* 64 * Bus address and size types 65 */ 66 typedef u_long bus_addr_t; 67 typedef u_long bus_size_t; 68 69 /* 70 * Access methods for bus resources and address space. 71 */ 72 typedef u_long bus_space_handle_t; 73 typedef struct luna88k_bus_space_tag *bus_space_tag_t; 74 75 struct luna88k_bus_space_tag { 76 /* 'stride' for each bytes reading/writing */ 77 uint8_t bs_stride_1; 78 uint8_t bs_stride_2; 79 uint8_t bs_stride_4; 80 uint8_t bs_stride_8; 81 }; 82 83 /* 84 * int bus_space_map(bus_space_tag_t t, bus_addr_t addr, 85 * bus_size_t size, int flags, bus_space_handle_t *bshp); 86 * 87 * Map a region of bus space. 88 */ 89 90 #define BUS_SPACE_MAP_CACHEABLE 0x01 91 #define BUS_SPACE_MAP_LINEAR 0x02 92 93 int bus_space_map(bus_space_tag_t, bus_addr_t, bus_size_t, 94 int, bus_space_handle_t *); 95 96 /* 97 * void bus_space_unmap(bus_space_tag_t t, 98 * bus_space_handle_t bsh, bus_size_t size); 99 * 100 * Unmap a region of bus space. 101 */ 102 103 void bus_space_unmap(bus_space_tag_t, bus_space_handle_t, bus_size_t); 104 105 /* 106 * int bus_space_subregion(bus_space_tag_t t, 107 * bus_space_handle_t bsh, bus_size_t offset, bus_size_t size, 108 * bus_space_handle_t *nbshp); 109 * 110 * Get a new handle for a subregion of an already-mapped area of bus space. 111 */ 112 113 int bus_space_subregion(bus_space_tag_t t, bus_space_handle_t bsh, 114 bus_size_t offset, bus_size_t size, bus_space_handle_t *nbshp); 115 116 /* 117 * int bus_space_alloc(bus_space_tag_t t, bus_addr_t, rstart, 118 * bus_addr_t rend, bus_size_t size, bus_size_t align, 119 * bus_size_t boundary, int flags, bus_addr_t *addrp, 120 * bus_space_handle_t *bshp); 121 * 122 * Allocate a region of bus space. 123 */ 124 125 int bus_space_alloc(bus_space_tag_t t, bus_addr_t rstart, 126 bus_addr_t rend, bus_size_t size, bus_size_t align, 127 bus_size_t boundary, int flags, bus_addr_t *addrp, 128 bus_space_handle_t *bshp); 129 130 /* 131 * int bus_space_free(bus_space_tag_t t, 132 * bus_space_handle_t bsh, bus_size_t size); 133 * 134 * Free a region of bus space. 135 */ 136 137 void bus_space_free(bus_space_tag_t t, bus_space_handle_t bsh, 138 bus_size_t size); 139 140 /* 141 * int luna88k_bus_space_probe(bus_space_tag_t t, 142 * bus_space_handle_t bsh, bus_size_t offset, int sz); 143 * 144 * Probe the bus at t/bsh/offset, using sz as the size of the load. 145 * 146 * This is a machine-dependent extension, and is not to be used by 147 * machine-independent code. 148 */ 149 150 int luna88k_bus_space_probe(bus_space_tag_t t, 151 bus_space_handle_t bsh, bus_size_t offset, int sz); 152 153 /* 154 * u_intN_t bus_space_read_N(bus_space_tag_t tag, 155 * bus_space_handle_t bsh, bus_size_t offset); 156 * 157 * Read a 1, 2, 4, or 8 byte quantity from bus space 158 * described by tag/handle/offset. 159 */ 160 161 #define bus_space_read_1(t, h, o) \ 162 (*(volatile u_int8_t *)((h) + (t->bs_stride_1) * (o))) 163 164 #define bus_space_read_2(t, h, o) \ 165 (*(volatile u_int16_t *)((h) + (t->bs_stride_2) * (o))) 166 167 #define bus_space_read_4(t, h, o) \ 168 (*(volatile u_int32_t *)((h) + (t->bs_stride_4) * (o))) 169 170 #if 0 /* Cause a link error for bus_space_read_8 */ 171 #define bus_space_read_8(t, h, o) !!! bus_space_read_8 unimplemented !!! 172 #endif 173 174 /* 175 * void bus_space_read_multi_N(bus_space_tag_t tag, 176 * bus_space_handle_t bsh, bus_size_t offset, 177 * u_intN_t *addr, size_t count); 178 * 179 * Read `count' 1, 2, 4, or 8 byte quantities from bus space 180 * described by tag/handle/offset and copy into buffer provided. 181 */ 182 183 static __inline__ void 184 bus_space_read_multi_1(bus_space_tag_t tag, bus_space_handle_t handle, 185 bus_addr_t offset, u_int8_t *dest, size_t count) 186 { 187 while ((int)--count >= 0) 188 *dest++ = bus_space_read_1(tag, handle, offset); 189 } 190 191 static __inline__ void 192 bus_space_read_multi_2(bus_space_tag_t tag, bus_space_handle_t handle, 193 bus_addr_t offset, u_int16_t *dest, size_t count) 194 { 195 while ((int)--count >= 0) 196 *dest++ = bus_space_read_2(tag, handle, offset); 197 } 198 199 static __inline__ void 200 bus_space_read_multi_4(bus_space_tag_t tag, bus_space_handle_t handle, 201 bus_addr_t offset, u_int32_t *dest, size_t count) 202 { 203 while ((int)--count >= 0) 204 *dest++ = bus_space_read_4(tag, handle, offset); 205 } 206 207 #if 0 /* Cause a link error for bus_space_read_multi_8 */ 208 #define bus_space_read_multi_8 !!! bus_space_read_multi_8 unimplemented !!! 209 #endif 210 211 /* 212 * void bus_space_read_region_N(bus_space_tag_t tag, 213 * bus_space_handle_t bsh, bus_size_t offset, 214 * u_intN_t *addr, size_t count); 215 * 216 * Read `count' 1, 2, 4, or 8 byte quantities from bus space 217 * described by tag/handle and starting at `offset' and copy into 218 * buffer provided. 219 */ 220 221 static __inline__ void 222 bus_space_read_region_1(bus_space_tag_t tag, bus_space_handle_t handle, 223 bus_addr_t offset, u_int8_t *dest, size_t count) 224 { 225 while ((int)--count >= 0) 226 *dest++ = bus_space_read_1(tag, handle, offset++); 227 } 228 229 static __inline__ void 230 bus_space_read_region_2(bus_space_tag_t tag, bus_space_handle_t handle, 231 bus_addr_t offset, u_int16_t *dest, size_t count) 232 { 233 while ((int)--count >= 0) { 234 *dest++ = bus_space_read_2(tag, handle, offset); 235 offset += 2; 236 } 237 } 238 239 static __inline__ void 240 bus_space_read_region_4(bus_space_tag_t tag, bus_space_handle_t handle, 241 bus_addr_t offset, u_int32_t *dest, size_t count) 242 { 243 while ((int)--count >= 0) { 244 *dest++ = bus_space_read_4(tag, handle, offset); 245 offset += 4; 246 } 247 } 248 249 #if 0 /* Cause a link error for bus_space_read_region_8 */ 250 #define bus_space_read_region_8 !!! bus_space_read_region_8 unimplemented !!! 251 #endif 252 253 /* 254 * void bus_space_write_N(bus_space_tag_t tag, 255 * bus_space_handle_t bsh, bus_size_t offset, 256 * u_intN_t value); 257 * 258 * Write the 1, 2, 4, or 8 byte value `value' to bus space 259 * described by tag/handle/offset. 260 */ 261 262 #define bus_space_write_1(t, h, o, v) \ 263 ((void)(*(volatile u_int8_t *)((h) + (t->bs_stride_1) * (o)) = (v))) 264 265 #define bus_space_write_2(t, h, o, v) \ 266 ((void)(*(volatile u_int16_t *)((h) + (t->bs_stride_2) * (o)) = (v))) 267 268 #define bus_space_write_4(t, h, o, v) \ 269 ((void)(*(volatile u_int32_t *)((h) + (t->bs_stride_4) * (o)) = (v))) 270 271 #if 0 /* Cause a link error for bus_space_write_8 */ 272 #define bus_space_write_8 !!! bus_space_write_8 not implemented !!! 273 #endif 274 275 /* 276 * void bus_space_write_multi_N(bus_space_tag_t tag, 277 * bus_space_handle_t bsh, bus_size_t offset, 278 * const u_intN_t *addr, size_t count); 279 * 280 * Write `count' 1, 2, 4, or 8 byte quantities from the buffer 281 * provided to bus space described by tag/handle/offset. 282 */ 283 284 static __inline__ void 285 bus_space_write_multi_1(bus_space_tag_t tag, bus_space_handle_t handle, 286 bus_addr_t offset, u_int8_t *dest, size_t count) 287 { 288 while ((int)--count >= 0) 289 bus_space_write_1(tag, handle, offset, *dest++); 290 } 291 292 static __inline__ void 293 bus_space_write_multi_2(bus_space_tag_t tag, bus_space_handle_t handle, 294 bus_addr_t offset, u_int16_t *dest, size_t count) 295 { 296 while ((int)--count >= 0) 297 bus_space_write_2(tag, handle, offset, *dest++); 298 } 299 300 static __inline__ void 301 bus_space_write_multi_4(bus_space_tag_t tag, bus_space_handle_t handle, 302 bus_addr_t offset, u_int32_t *dest, size_t count) 303 { 304 while ((int)--count >= 0) 305 bus_space_write_4(tag, handle, offset, *dest++); 306 } 307 308 #if 0 /* Cause a link error for bus_space_write_8 */ 309 #define bus_space_write_multi_8(t, h, o, a, c) \ 310 !!! bus_space_write_multi_8 unimplemented !!! 311 #endif 312 313 /* 314 * void bus_space_write_region_N(bus_space_tag_t tag, 315 * bus_space_handle_t bsh, bus_size_t offset, 316 * const u_intN_t *addr, size_t count); 317 * 318 * Write `count' 1, 2, 4, or 8 byte quantities from the buffer provided 319 * to bus space described by tag/handle starting at `offset'. 320 */ 321 322 static __inline__ void 323 bus_space_write_region_1(bus_space_tag_t tag, bus_space_handle_t handle, 324 bus_addr_t offset, u_int8_t *dest, size_t count) 325 { 326 while ((int)--count >= 0) 327 bus_space_write_1(tag, handle, offset++, *dest++); 328 } 329 330 static __inline__ void 331 bus_space_write_region_2(bus_space_tag_t tag, bus_space_handle_t handle, 332 bus_addr_t offset, u_int16_t *dest, size_t count) 333 { 334 while ((int)--count >= 0) { 335 bus_space_write_2(tag, handle, offset, *dest++); 336 offset += 2; 337 } 338 } 339 340 static __inline__ void 341 bus_space_write_region_4(bus_space_tag_t tag, bus_space_handle_t handle, 342 bus_addr_t offset, u_int32_t *dest, size_t count) 343 { 344 while ((int)--count >= 0) { 345 bus_space_write_4(tag, handle, offset, *dest++); 346 offset += 4; 347 } 348 } 349 350 #if 0 /* Cause a link error for bus_space_write_region_8 */ 351 #define bus_space_write_region_8 \ 352 !!! bus_space_write_region_8 unimplemented !!! 353 #endif 354 355 /* 356 * void bus_space_set_multi_N(bus_space_tag_t tag, 357 * bus_space_handle_t bsh, bus_size_t offset, u_intN_t val, 358 * size_t count); 359 * 360 * Write the 1, 2, 4, or 8 byte value `val' to bus space described 361 * by tag/handle/offset `count' times. 362 */ 363 364 static __inline__ void 365 bus_space_set_multi_1(bus_space_tag_t tag, bus_space_handle_t handle, 366 bus_addr_t offset, u_int8_t value, size_t count) 367 { 368 while ((int)--count >= 0) 369 bus_space_write_1(tag, handle, offset, value); 370 } 371 372 static __inline__ void 373 bus_space_set_multi_2(bus_space_tag_t tag, bus_space_handle_t handle, 374 bus_addr_t offset, u_int16_t value, size_t count) 375 { 376 while ((int)--count >= 0) 377 bus_space_write_2(tag, handle, offset, value); 378 } 379 380 static __inline__ void 381 bus_space_set_multi_4(bus_space_tag_t tag, bus_space_handle_t handle, 382 bus_addr_t offset, u_int32_t value, size_t count) 383 { 384 while ((int)--count >= 0) 385 bus_space_write_4(tag, handle, offset, value); 386 } 387 388 #if 0 /* Cause a link error for bus_space_set_multi_8 */ 389 #define bus_space_set_multi_8 \ 390 !!! bus_space_set_multi_8 unimplemented !!! 391 #endif 392 393 /* 394 * void bus_space_set_region_N(bus_space_tag_t tag, 395 * bus_space_handle_t bsh, bus_size_t offset, u_intN_t val, 396 * size_t count); 397 * 398 * Write `count' 1, 2, 4, or 8 byte value `val' to bus space described 399 * by tag/handle starting at `offset'. 400 */ 401 402 static __inline__ void 403 bus_space_set_region_1(bus_space_tag_t tag, bus_space_handle_t handle, 404 bus_addr_t offset, u_int8_t value, size_t count) 405 { 406 while ((int)--count >= 0) 407 bus_space_write_1(tag, handle, offset++, value); 408 } 409 410 static __inline__ void 411 bus_space_set_region_2(bus_space_tag_t tag, bus_space_handle_t handle, 412 bus_addr_t offset, u_int16_t value, size_t count) 413 { 414 while ((int)--count >= 0) { 415 bus_space_write_2(tag, handle, offset, value); 416 offset += 2; 417 } 418 } 419 420 static __inline__ void 421 bus_space_set_region_4(bus_space_tag_t tag, bus_space_handle_t handle, 422 bus_addr_t offset, u_int32_t value, size_t count) 423 { 424 while ((int)--count >= 0) { 425 bus_space_write_4(tag, handle, offset, value); 426 offset += 4; 427 } 428 } 429 430 #if 0 /* Cause a link error for bus_space_set_region_8 */ 431 #define bus_space_set_region_8 \ 432 !!! bus_space_set_region_8 unimplemented !!! 433 #endif 434 435 /* 436 * void bus_space_copy_N(bus_space_tag_t tag, 437 * bus_space_handle_t bsh1, bus_size_t off1, 438 * bus_space_handle_t bsh2, bus_size_t off2, 439 * size_t count); 440 * 441 * Copy `count' 1, 2, 4, or 8 byte values from bus space starting 442 * at tag/bsh1/off1 to bus space starting at tag/bsh2/off2. 443 */ 444 445 #define __LUNA88K_copy_region_N(BYTES) \ 446 static __inline void __CONCAT(bus_space_copy_region_,BYTES) \ 447 (bus_space_tag_t, \ 448 bus_space_handle_t bsh1, bus_size_t off1, \ 449 bus_space_handle_t bsh2, bus_size_t off2, \ 450 bus_size_t count); \ 451 \ 452 static __inline void \ 453 __CONCAT(bus_space_copy_region_,BYTES)(t, h1, o1, h2, o2, c) \ 454 bus_space_tag_t t; \ 455 bus_space_handle_t h1, h2; \ 456 bus_size_t o1, o2, c; \ 457 { \ 458 bus_size_t o; \ 459 \ 460 if ((h1 + o1) >= (h2 + o2)) { \ 461 /* src after dest: copy forward */ \ 462 for (o = 0; c != 0; c--, o += BYTES) \ 463 __CONCAT(bus_space_write_,BYTES)(t, h2, o2 + o, \ 464 __CONCAT(bus_space_read_,BYTES)(t, h1, o1 + o)); \ 465 } else { \ 466 /* dest after src: copy backwards */ \ 467 for (o = (c - 1) * BYTES; c != 0; c--, o -= BYTES) \ 468 __CONCAT(bus_space_write_,BYTES)(t, h2, o2 + o, \ 469 __CONCAT(bus_space_read_,BYTES)(t, h1, o1 + o)); \ 470 } \ 471 } 472 __LUNA88K_copy_region_N(1) 473 __LUNA88K_copy_region_N(2) 474 __LUNA88K_copy_region_N(4) 475 #if 0 /* Cause a link error for bus_space_copy_8 */ 476 #define bus_space_copy_8 \ 477 !!! bus_space_copy_8 unimplemented !!! 478 #endif 479 480 #undef __LUNA88K_copy_region_N 481 482 /* 483 * Bus read/write barrier methods. 484 * 485 * void bus_space_barrier(bus_space_tag_t tag, 486 * bus_space_handle_t bsh, bus_size_t offset, 487 * bus_size_t len, int flags); 488 * 489 */ 490 #define bus_space_barrier(t, h, o, l, f) \ 491 ((void)((void)(t), (void)(h), (void)(o), (void)(l), (void)(f))) 492 #define BUS_SPACE_BARRIER_READ 0x01 /* force read barrier */ 493 #define BUS_SPACE_BARRIER_WRITE 0x02 /* force write barrier */ 494 495 #endif /* _LUNA88K_BUS_H_ */ 496