2 * Copyright (c) KATO Takenori, 1999.
4 * All rights reserved. Unpublished rights reserved under the copyright
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer as
13 * the first lines of this file unmodified.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 /* $NecBSD: busio.h,v 3.25.4.2.2.1 2000/06/12 03:53:08 honda Exp $ */
35 /* $NetBSD: bus.h,v 1.12 1997/10/01 08:25:15 fvdl Exp $ */
38 * [NetBSD for NEC PC-98 series]
39 * Copyright (c) 1997, 1998
40 * NetBSD/pc98 porting staff. All rights reserved.
42 * [Ported for FreeBSD]
44 * TAKAHASHI Yoshihiro. All rights reserved.
46 * Redistribution and use in source and binary forms, with or without
47 * modification, are permitted provided that the following conditions
49 * 1. Redistributions of source code must retain the above copyright
50 * notice, this list of conditions and the following disclaimer.
51 * 2. Redistributions in binary form must reproduce the above copyright
52 * notice, this list of conditions and the following disclaimer in the
53 * documentation and/or other materials provided with the distribution.
54 * 3. The name of the author may not be used to endorse or promote products
55 * derived from this software without specific prior written permission.
57 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
58 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
59 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
60 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
61 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
62 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
63 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
64 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
65 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
66 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
67 * POSSIBILITY OF SUCH DAMAGE.
71 * Copyright (c) 1997, 1998
72 * Naofumi HONDA. All rights reserved.
74 * This module support generic bus address relocation mechanism.
75 * To reduce a function call overhead, we employ pascal call methods.
81 #include <sys/systm.h>
83 #include <machine/_bus.h>
84 #include <machine/cpufunc.h>
86 #define BUS_SPACE_MAXSIZE_24BIT 0xFFFFFF
87 #define BUS_SPACE_MAXSIZE_32BIT 0xFFFFFFFF
88 #define BUS_SPACE_MAXSIZE 0xFFFFFFFF
89 #define BUS_SPACE_MAXADDR_24BIT 0xFFFFFF
90 #define BUS_SPACE_MAXADDR_32BIT 0xFFFFFFFF
91 #define BUS_SPACE_MAXADDR 0xFFFFFFFF
93 #define BUS_SPACE_UNRESTRICTED (~0)
96 * address relocation table
98 #define BUS_SPACE_IAT_MAXSIZE 33
99 typedef bus_addr_t *bus_space_iat_t;
101 #define BUS_SPACE_IAT_SZ(IOTARRAY) (sizeof(IOTARRAY)/sizeof(bus_addr_t))
104 * Access methods for bus resources and address space.
111 #define _PASCAL_CALL (void)
113 #define _BUS_SPACE_CALL_FUNCS_TAB(NAME,TYPE,BWN) \
114 NAME##_space_read_##BWN, \
115 NAME##_space_read_multi_##BWN, \
116 NAME##_space_read_region_##BWN, \
117 NAME##_space_write_##BWN, \
118 NAME##_space_write_multi_##BWN, \
119 NAME##_space_write_region_##BWN, \
120 NAME##_space_set_multi_##BWN, \
121 NAME##_space_set_region_##BWN, \
122 NAME##_space_copy_region_##BWN
124 #define _BUS_SPACE_CALL_FUNCS_PROTO(NAME,TYPE,BWN) \
125 TYPE NAME##_space_read_##BWN _PASCAL_CALL; \
126 void NAME##_space_read_multi_##BWN _PASCAL_CALL; \
127 void NAME##_space_read_region_##BWN _PASCAL_CALL; \
128 void NAME##_space_write_##BWN _PASCAL_CALL; \
129 void NAME##_space_write_multi_##BWN _PASCAL_CALL; \
130 void NAME##_space_write_region_##BWN _PASCAL_CALL; \
131 void NAME##_space_set_multi_##BWN _PASCAL_CALL; \
132 void NAME##_space_set_region_##BWN _PASCAL_CALL; \
133 void NAME##_space_copy_region_##BWN _PASCAL_CALL;
135 #define _BUS_SPACE_CALL_FUNCS(NAME,TYPE,BWN) \
136 TYPE (* NAME##_read_##BWN) _PASCAL_CALL; \
137 void (* NAME##_read_multi_##BWN) _PASCAL_CALL; \
138 void (* NAME##_read_region_##BWN) _PASCAL_CALL; \
139 void (* NAME##_write_##BWN) _PASCAL_CALL; \
140 void (* NAME##_write_multi_##BWN) _PASCAL_CALL; \
141 void (* NAME##_write_region_##BWN) _PASCAL_CALL; \
142 void (* NAME##_set_multi_##BWN) _PASCAL_CALL; \
143 void (* NAME##_set_region_##BWN) _PASCAL_CALL; \
144 void (* NAME##_copy_region_##BWN) _PASCAL_CALL;
146 struct bus_space_access_methods {
147 /* 8 bits access methods */
148 _BUS_SPACE_CALL_FUNCS(bs,u_int8_t,1)
150 /* 16 bits access methods */
151 _BUS_SPACE_CALL_FUNCS(bs,u_int16_t,2)
153 /* 32 bits access methods */
154 _BUS_SPACE_CALL_FUNCS(bs,u_int32_t,4)
158 * Access methods for bus resources and address space.
160 struct bus_space_tag {
161 #define BUS_SPACE_TAG_IO 0
162 #define BUS_SPACE_TAG_MEM 1
163 u_int bs_tag; /* bus space flags */
165 struct bus_space_access_methods bs_da; /* direct access */
166 struct bus_space_access_methods bs_ra; /* relocate access */
168 struct bus_space_access_methods bs_ida; /* indexed direct access */
175 struct bus_space_handle {
179 bus_addr_t bsh_iat[BUS_SPACE_IAT_MAXSIZE];
183 struct resource **bsh_res;
186 struct bus_space_access_methods bsh_bam;
190 * Values for the pc98 bus space tag, not to be used directly by MI code.
192 extern struct bus_space_tag SBUS_io_space_tag;
193 extern struct bus_space_tag SBUS_mem_space_tag;
195 #define I386_BUS_SPACE_IO (&SBUS_io_space_tag)
196 #define I386_BUS_SPACE_MEM (&SBUS_mem_space_tag)
199 * Allocate/Free bus_space_handle
201 int i386_bus_space_handle_alloc(bus_space_tag_t t, bus_addr_t bpa,
202 bus_size_t size, bus_space_handle_t *bshp);
203 void i386_bus_space_handle_free(bus_space_tag_t t, bus_space_handle_t bsh,
207 * int bus_space_map (bus_space_tag_t t, bus_addr_t addr,
208 * bus_size_t size, int flag, bus_space_handle_t *bshp);
210 * Map a region of bus space.
213 int i386_memio_map(bus_space_tag_t t, bus_addr_t addr, bus_size_t size,
214 int flag, bus_space_handle_t *bshp);
216 #define bus_space_map(t, a, s, f, hp) \
217 i386_memio_map((t), (a), (s), (f), (hp))
220 * int bus_space_unmap (bus_space_tag_t t,
221 * bus_space_handle_t bsh, bus_size_t size);
223 * Unmap a region of bus space.
226 void i386_memio_unmap(bus_space_tag_t t, bus_space_handle_t bsh,
229 #define bus_space_unmap(t, h, s) \
230 i386_memio_unmap((t), (h), (s))
233 * int bus_space_map_load (bus_space_tag_t t, bus_space_handle_t bsh,
234 * bus_size_t size, bus_space_iat_t iat, u_int flags);
236 * Load I/O address table of bus space.
239 int i386_memio_map_load(bus_space_tag_t t, bus_space_handle_t bsh,
240 bus_size_t size, bus_space_iat_t iat, u_int flags);
242 #define bus_space_map_load(t, h, s, iat, f) \
243 i386_memio_map_load((t), (h), (s), (iat), (f))
246 * int bus_space_subregion (bus_space_tag_t t,
247 * bus_space_handle_t bsh, bus_size_t offset, bus_size_t size,
248 * bus_space_handle_t *nbshp);
250 * Get a new handle for a subregion of an already-mapped area of bus space.
253 int i386_memio_subregion(bus_space_tag_t t, bus_space_handle_t bsh,
254 bus_size_t offset, bus_size_t size,
255 bus_space_handle_t *nbshp);
257 #define bus_space_subregion(t, h, o, s, nhp) \
258 i386_memio_subregion((t), (h), (o), (s), (nhp))
261 * int bus_space_free (bus_space_tag_t t,
262 * bus_space_handle_t bsh, bus_size_t size);
264 * Free a region of bus space.
267 void i386_memio_free(bus_space_tag_t t, bus_space_handle_t bsh,
270 #define bus_space_free(t, h, s) \
271 i386_memio_free((t), (h), (s))
274 * int bus_space_compare (bus_space_tag_t t1, bus_space_handle_t bsh1,
275 * bus_space_tag_t t2, bus_space_handle_t bsh2);
277 * Compare two resources.
279 int i386_memio_compare(bus_space_tag_t t1, bus_space_handle_t bsh1,
280 bus_space_tag_t t2, bus_space_handle_t bsh2);
282 #define bus_space_compare(t1, h1, t2, h2) \
283 i386_memio_compare((t1), (h1), (t2), (h2))
286 * Access methods for bus resources and address space.
288 #define _BUS_ACCESS_METHODS_PROTO(TYPE,BWN) \
289 static __inline TYPE bus_space_read_##BWN \
290 (bus_space_tag_t, bus_space_handle_t, bus_size_t offset); \
291 static __inline void bus_space_read_multi_##BWN \
292 (bus_space_tag_t, bus_space_handle_t, \
293 bus_size_t, TYPE *, size_t); \
294 static __inline void bus_space_read_region_##BWN \
295 (bus_space_tag_t, bus_space_handle_t, \
296 bus_size_t, TYPE *, size_t); \
297 static __inline void bus_space_write_##BWN \
298 (bus_space_tag_t, bus_space_handle_t, bus_size_t, TYPE); \
299 static __inline void bus_space_write_multi_##BWN \
300 (bus_space_tag_t, bus_space_handle_t, \
301 bus_size_t, const TYPE *, size_t); \
302 static __inline void bus_space_write_region_##BWN \
303 (bus_space_tag_t, bus_space_handle_t, \
304 bus_size_t, const TYPE *, size_t); \
305 static __inline void bus_space_set_multi_##BWN \
306 (bus_space_tag_t, bus_space_handle_t, bus_size_t, TYPE, size_t);\
307 static __inline void bus_space_set_region_##BWN \
308 (bus_space_tag_t, bus_space_handle_t, bus_size_t, TYPE, size_t);\
309 static __inline void bus_space_copy_region_##BWN \
310 (bus_space_tag_t, bus_space_handle_t, bus_size_t, \
311 bus_space_handle_t, bus_size_t, size_t);
313 _BUS_ACCESS_METHODS_PROTO(u_int8_t,1)
314 _BUS_ACCESS_METHODS_PROTO(u_int16_t,2)
315 _BUS_ACCESS_METHODS_PROTO(u_int32_t,4)
320 #define _BUS_SPACE_READ(TYPE,BWN) \
321 static __inline TYPE \
322 bus_space_read_##BWN (tag, bsh, offset) \
323 bus_space_tag_t tag; \
324 bus_space_handle_t bsh; \
327 register TYPE result; \
329 __asm __volatile("call *%2" \
332 :"o" (bsh->bsh_bam.bs_read_##BWN), \
340 _BUS_SPACE_READ(u_int8_t,1)
341 _BUS_SPACE_READ(u_int16_t,2)
342 _BUS_SPACE_READ(u_int32_t,4)
347 #define _BUS_SPACE_WRITE(TYPE,BWN) \
348 static __inline void \
349 bus_space_write_##BWN (tag, bsh, offset, val) \
350 bus_space_tag_t tag; \
351 bus_space_handle_t bsh; \
356 __asm __volatile("call *%1" \
358 :"o" (bsh->bsh_bam.bs_write_##BWN), \
365 _BUS_SPACE_WRITE(u_int8_t,1)
366 _BUS_SPACE_WRITE(u_int16_t,2)
367 _BUS_SPACE_WRITE(u_int32_t,4)
372 #define _BUS_SPACE_READ_MULTI(TYPE,BWN) \
373 static __inline void \
374 bus_space_read_multi_##BWN (tag, bsh, offset, buf, cnt) \
375 bus_space_tag_t tag; \
376 bus_space_handle_t bsh; \
382 __asm __volatile("call *%3" \
386 :"o" (bsh->bsh_bam.bs_read_multi_##BWN), \
394 _BUS_SPACE_READ_MULTI(u_int8_t,1)
395 _BUS_SPACE_READ_MULTI(u_int16_t,2)
396 _BUS_SPACE_READ_MULTI(u_int32_t,4)
401 #define _BUS_SPACE_WRITE_MULTI(TYPE,BWN) \
402 static __inline void \
403 bus_space_write_multi_##BWN (tag, bsh, offset, buf, cnt) \
404 bus_space_tag_t tag; \
405 bus_space_handle_t bsh; \
411 __asm __volatile("call *%3" \
415 :"o" (bsh->bsh_bam.bs_write_multi_##BWN), \
423 _BUS_SPACE_WRITE_MULTI(u_int8_t,1)
424 _BUS_SPACE_WRITE_MULTI(u_int16_t,2)
425 _BUS_SPACE_WRITE_MULTI(u_int32_t,4)
430 #define _BUS_SPACE_READ_REGION(TYPE,BWN) \
431 static __inline void \
432 bus_space_read_region_##BWN (tag, bsh, offset, buf, cnt) \
433 bus_space_tag_t tag; \
434 bus_space_handle_t bsh; \
440 __asm __volatile("call *%3" \
444 :"o" (bsh->bsh_bam.bs_read_region_##BWN), \
452 _BUS_SPACE_READ_REGION(u_int8_t,1)
453 _BUS_SPACE_READ_REGION(u_int16_t,2)
454 _BUS_SPACE_READ_REGION(u_int32_t,4)
459 #define _BUS_SPACE_WRITE_REGION(TYPE,BWN) \
460 static __inline void \
461 bus_space_write_region_##BWN (tag, bsh, offset, buf, cnt) \
462 bus_space_tag_t tag; \
463 bus_space_handle_t bsh; \
469 __asm __volatile("call *%3" \
473 :"o" (bsh->bsh_bam.bs_write_region_##BWN), \
481 _BUS_SPACE_WRITE_REGION(u_int8_t,1)
482 _BUS_SPACE_WRITE_REGION(u_int16_t,2)
483 _BUS_SPACE_WRITE_REGION(u_int32_t,4)
488 #define _BUS_SPACE_SET_MULTI(TYPE,BWN) \
489 static __inline void \
490 bus_space_set_multi_##BWN (tag, bsh, offset, val, cnt) \
491 bus_space_tag_t tag; \
492 bus_space_handle_t bsh; \
498 __asm __volatile("call *%2" \
501 :"o" (bsh->bsh_bam.bs_set_multi_##BWN), \
509 _BUS_SPACE_SET_MULTI(u_int8_t,1)
510 _BUS_SPACE_SET_MULTI(u_int16_t,2)
511 _BUS_SPACE_SET_MULTI(u_int32_t,4)
516 #define _BUS_SPACE_SET_REGION(TYPE,BWN) \
517 static __inline void \
518 bus_space_set_region_##BWN (tag, bsh, offset, val, cnt) \
519 bus_space_tag_t tag; \
520 bus_space_handle_t bsh; \
526 __asm __volatile("call *%2" \
529 :"o" (bsh->bsh_bam.bs_set_region_##BWN), \
537 _BUS_SPACE_SET_REGION(u_int8_t,1)
538 _BUS_SPACE_SET_REGION(u_int16_t,2)
539 _BUS_SPACE_SET_REGION(u_int32_t,4)
544 #define _BUS_SPACE_COPY_REGION(BWN) \
545 static __inline void \
546 bus_space_copy_region_##BWN (tag, sbsh, src, dbsh, dst, cnt) \
547 bus_space_tag_t tag; \
548 bus_space_handle_t sbsh; \
550 bus_space_handle_t dbsh; \
555 if (dbsh->bsh_bam.bs_copy_region_1 != sbsh->bsh_bam.bs_copy_region_1) \
556 panic("bus_space_copy_region: funcs mismatch (ENOSUPPORT)");\
558 __asm __volatile("call *%3" \
562 :"o" (dbsh->bsh_bam.bs_copy_region_##BWN), \
571 _BUS_SPACE_COPY_REGION(1)
572 _BUS_SPACE_COPY_REGION(2)
573 _BUS_SPACE_COPY_REGION(4)
576 * Bus read/write barrier methods.
578 * void bus_space_barrier(bus_space_tag_t tag, bus_space_handle_t bsh,
579 * bus_size_t offset, bus_size_t len, int flags);
582 * Note that BUS_SPACE_BARRIER_WRITE doesn't do anything other than
583 * prevent reordering by the compiler; all Intel x86 processors currently
584 * retire operations outside the CPU in program order.
586 #define BUS_SPACE_BARRIER_READ 0x01 /* force read barrier */
587 #define BUS_SPACE_BARRIER_WRITE 0x02 /* force write barrier */
590 bus_space_barrier(bus_space_tag_t tag, bus_space_handle_t bsh,
591 bus_size_t offset, bus_size_t len, int flags)
593 if (flags & BUS_SPACE_BARRIER_READ)
594 __asm __volatile("lock; addl $0,0(%%esp)" : : : "memory");
596 __asm __volatile("" : : : "memory");
599 #ifdef BUS_SPACE_NO_LEGACY
602 #define inb(a) compiler_error
603 #define inw(a) compiler_error
604 #define inl(a) compiler_error
605 #define outb(a, b) compiler_error
606 #define outw(a, b) compiler_error
607 #define outl(a, b) compiler_error
610 #include <machine/bus_dma.h>
613 * Stream accesses are the same as normal accesses on i386/pc98; there are no
614 * supported bus systems with an endianess different from the host one.
616 #define bus_space_read_stream_1(t, h, o) bus_space_read_1((t), (h), (o))
617 #define bus_space_read_stream_2(t, h, o) bus_space_read_2((t), (h), (o))
618 #define bus_space_read_stream_4(t, h, o) bus_space_read_4((t), (h), (o))
620 #define bus_space_read_multi_stream_1(t, h, o, a, c) \
621 bus_space_read_multi_1((t), (h), (o), (a), (c))
622 #define bus_space_read_multi_stream_2(t, h, o, a, c) \
623 bus_space_read_multi_2((t), (h), (o), (a), (c))
624 #define bus_space_read_multi_stream_4(t, h, o, a, c) \
625 bus_space_read_multi_4((t), (h), (o), (a), (c))
627 #define bus_space_write_stream_1(t, h, o, v) \
628 bus_space_write_1((t), (h), (o), (v))
629 #define bus_space_write_stream_2(t, h, o, v) \
630 bus_space_write_2((t), (h), (o), (v))
631 #define bus_space_write_stream_4(t, h, o, v) \
632 bus_space_write_4((t), (h), (o), (v))
634 #define bus_space_write_multi_stream_1(t, h, o, a, c) \
635 bus_space_write_multi_1((t), (h), (o), (a), (c))
636 #define bus_space_write_multi_stream_2(t, h, o, a, c) \
637 bus_space_write_multi_2((t), (h), (o), (a), (c))
638 #define bus_space_write_multi_stream_4(t, h, o, a, c) \
639 bus_space_write_multi_4((t), (h), (o), (a), (c))
641 #define bus_space_set_multi_stream_1(t, h, o, v, c) \
642 bus_space_set_multi_1((t), (h), (o), (v), (c))
643 #define bus_space_set_multi_stream_2(t, h, o, v, c) \
644 bus_space_set_multi_2((t), (h), (o), (v), (c))
645 #define bus_space_set_multi_stream_4(t, h, o, v, c) \
646 bus_space_set_multi_4((t), (h), (o), (v), (c))
648 #define bus_space_read_region_stream_1(t, h, o, a, c) \
649 bus_space_read_region_1((t), (h), (o), (a), (c))
650 #define bus_space_read_region_stream_2(t, h, o, a, c) \
651 bus_space_read_region_2((t), (h), (o), (a), (c))
652 #define bus_space_read_region_stream_4(t, h, o, a, c) \
653 bus_space_read_region_4((t), (h), (o), (a), (c))
655 #define bus_space_write_region_stream_1(t, h, o, a, c) \
656 bus_space_write_region_1((t), (h), (o), (a), (c))
657 #define bus_space_write_region_stream_2(t, h, o, a, c) \
658 bus_space_write_region_2((t), (h), (o), (a), (c))
659 #define bus_space_write_region_stream_4(t, h, o, a, c) \
660 bus_space_write_region_4((t), (h), (o), (a), (c))
662 #define bus_space_set_region_stream_1(t, h, o, v, c) \
663 bus_space_set_region_1((t), (h), (o), (v), (c))
664 #define bus_space_set_region_stream_2(t, h, o, v, c) \
665 bus_space_set_region_2((t), (h), (o), (v), (c))
666 #define bus_space_set_region_stream_4(t, h, o, v, c) \
667 bus_space_set_region_4((t), (h), (o), (v), (c))
669 #define bus_space_copy_region_stream_1(t, h1, o1, h2, o2, c) \
670 bus_space_copy_region_1((t), (h1), (o1), (h2), (o2), (c))
671 #define bus_space_copy_region_stream_2(t, h1, o1, h2, o2, c) \
672 bus_space_copy_region_2((t), (h1), (o1), (h2), (o2), (c))
673 #define bus_space_copy_region_stream_4(t, h1, o1, h2, o2, c) \
674 bus_space_copy_region_4((t), (h1), (o1), (h2), (o2), (c))
676 #endif /* _PC98_BUS_H_ */