Home | History | Annotate | Line # | Download | only in include
bus.h revision 1.4
      1 /*	$NetBSD: bus.h,v 1.4 2001/03/07 22:42:19 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1996, 1997, 1998, 2001 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  * NASA Ames Research Center.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *	This product includes software developed by the NetBSD
     22  *	Foundation, Inc. and its contributors.
     23  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  *    contributors may be used to endorse or promote products derived
     25  *    from this software without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  * POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 
     40 /*
     41  * Copyright (c) 1997 Per Fogelstrom.  All rights reserved.
     42  * Copyright (c) 1996 Niklas Hallqvist.  All rights reserved.
     43  *
     44  * Redistribution and use in source and binary forms, with or without
     45  * modification, are permitted provided that the following conditions
     46  * are met:
     47  * 1. Redistributions of source code must retain the above copyright
     48  *    notice, this list of conditions and the following disclaimer.
     49  * 2. Redistributions in binary form must reproduce the above copyright
     50  *    notice, this list of conditions and the following disclaimer in the
     51  *    documentation and/or other materials provided with the distribution.
     52  * 3. All advertising materials mentioning features or use of this software
     53  *    must display the following acknowledgement:
     54  *      This product includes software developed by Christopher G. Demetriou
     55  *	for the NetBSD Project.
     56  * 4. The name of the author may not be used to endorse or promote products
     57  *    derived from this software without specific prior written permission
     58  *
     59  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     60  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     61  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     62  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     63  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     64  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     65  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     66  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     67  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     68  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     69  */
     70 
     71 #ifndef _MIPSCO_BUS_H_
     72 #define _MIPSCO_BUS_H_
     73 #ifdef _KERNEL
     74 
     75 #include <mips/locore.h>
     76 
     77 #ifdef BUS_SPACE_DEBUG
     78 #include <sys/systm.h> /* for printf() prototype */
     79 /*
     80  * Macros for checking the aligned-ness of pointers passed to bus
     81  * space ops.  Strict alignment is required by the MIPS architecture,
     82  * and a trap will occur if unaligned access is performed.  These
     83  * may aid in the debugging of a broken device driver by displaying
     84  * useful information about the problem.
     85  */
     86 #define __BUS_SPACE_ALIGNED_ADDRESS(p, t)				\
     87 	((((u_long)(p)) & (sizeof(t)-1)) == 0)
     88 
     89 #define __BUS_SPACE_ADDRESS_SANITY(p, t, d)				\
     90 ({									\
     91 	if (__BUS_SPACE_ALIGNED_ADDRESS((p), t) == 0) {			\
     92 		printf("%s 0x%lx not aligned to %d bytes %s:%d\n",	\
     93 		    d, (u_long)(p), sizeof(t), __FILE__, __LINE__);	\
     94 	}								\
     95 	(void) 0;							\
     96 })
     97 
     98 #define BUS_SPACE_ALIGNED_POINTER(p, t) __BUS_SPACE_ALIGNED_ADDRESS(p, t)
     99 #else
    100 #define __BUS_SPACE_ADDRESS_SANITY(p,t,d)	(void) 0
    101 #define BUS_SPACE_ALIGNED_POINTER(p, t) ALIGNED_POINTER(p, t)
    102 #endif /* BUS_SPACE_DEBUG */
    103 
    104 /*
    105  * Utility macro; do not use outside this file.
    106  */
    107 #ifdef __STDC__
    108 #define __CONCAT3(a,b,c)	a##b##c
    109 #else
    110 #define __CONCAT3(a,b,c)	a/**/b/**/c
    111 #endif
    112 
    113 /*
    114  * Bus address and size types
    115  */
    116 typedef u_long bus_addr_t;
    117 typedef u_long bus_size_t;
    118 
    119 /*
    120  * Access methods for bus resources and address space.
    121  */
    122 typedef u_int32_t bus_space_handle_t;
    123 typedef struct mipsco_bus_space *bus_space_tag_t;
    124 
    125 struct mipsco_bus_space {
    126 	const char	*bs_name;
    127 	struct extent	*bs_extent;
    128 	bus_addr_t	bs_start;
    129 	bus_size_t	bs_size;
    130 
    131 	paddr_t		bs_pbase;
    132 	vaddr_t		bs_vbase;
    133 
    134 	/* sparse addressing shift count */
    135 	u_int8_t	bs_stride_1;
    136 	u_int8_t	bs_stride_2;
    137 	u_int8_t	bs_stride_4;
    138 	u_int8_t	bs_stride_8;
    139 
    140 	/* compose a bus_space handle from tag/handle/addr/size/flags (MD) */
    141 	int	(*bs_compose_handle) __P((bus_space_tag_t, bus_addr_t,
    142 				bus_size_t, int, bus_space_handle_t *));
    143 
    144 	/* dispose a bus_space handle (MD) */
    145 	int	(*bs_dispose_handle) __P((bus_space_tag_t, bus_space_handle_t,
    146 				bus_size_t));
    147 
    148 	/* convert bus_space tag/handle to physical address (MD) */
    149 	int	(*bs_paddr) __P((bus_space_tag_t, bus_space_handle_t,
    150 				paddr_t *));
    151 
    152 	/* mapping/unmapping */
    153 	int	(*bs_map) __P((bus_space_tag_t, bus_addr_t, bus_size_t, int,
    154 				bus_space_handle_t *));
    155 	void	(*bs_unmap) __P((bus_space_tag_t, bus_space_handle_t,
    156 				bus_size_t));
    157 	int	(*bs_subregion) __P((bus_space_tag_t, bus_space_handle_t,
    158 				bus_size_t, bus_size_t,	bus_space_handle_t *));
    159 
    160 	/* allocation/deallocation */
    161 	int	(*bs_alloc) __P((bus_space_tag_t, bus_addr_t, bus_addr_t,
    162 				bus_size_t, bus_size_t,	bus_size_t, int,
    163 				bus_addr_t *, bus_space_handle_t *));
    164 	void	(*bs_free) __P((bus_space_tag_t, bus_space_handle_t,
    165 				bus_size_t));
    166 
    167 	/* interrupt attach */
    168 	void	(*bs_intr_establish) __P((
    169 				bus_space_tag_t,
    170 				int,			/*bus-specific intr*/
    171 				int,			/*priority/class*/
    172 				int,			/*flags*/
    173 				int (*) __P((void *)),	/*handler*/
    174 				void *));		/*handler arg*/
    175 
    176 	void	*bs_aux;
    177 };
    178 
    179 /* vaddr_t argument of mipsco_bus_space_init() */
    180 #define MIPSCO_BUS_SPACE_UNMAPPED	((vaddr_t)0)
    181 
    182 /* machine dependent utility function for bus_space users */
    183 void	mipsco_bus_space_malloc_set_safe __P((void));
    184 void	mipsco_bus_space_init __P((bus_space_tag_t, const char *,
    185 	    paddr_t, vaddr_t, bus_addr_t, bus_size_t));
    186 void	mipsco_bus_space_init_extent __P((bus_space_tag_t, caddr_t, size_t));
    187 void	mipsco_bus_space_set_aligned_stride __P((bus_space_tag_t, unsigned int));
    188 void	mipsco_sparse_bus_space_init __P((bus_space_tag_t, const char *,
    189 	    paddr_t, bus_addr_t, bus_size_t));
    190 void	mipsco_large_bus_space_init __P((bus_space_tag_t, const char *,
    191 	    paddr_t, bus_addr_t, bus_size_t));
    192 
    193 /* machine dependent utility function for bus_space implementations */
    194 int	mipsco_bus_space_extent_malloc_flag __P((void));
    195 
    196 /* these are provided for subclasses which override base bus_space. */
    197 
    198 int	mipsco_bus_space_compose_handle __P((bus_space_tag_t,
    199 	    bus_addr_t, bus_size_t, int, bus_space_handle_t *));
    200 int	mipsco_bus_space_dispose_handle __P((bus_space_tag_t,
    201 	    bus_space_handle_t, bus_size_t));
    202 int	mipsco_bus_space_paddr __P((bus_space_tag_t,
    203 	    bus_space_handle_t, paddr_t *));
    204 
    205 int	mipsco_sparse_bus_space_compose_handle __P((bus_space_tag_t,
    206 	    bus_addr_t, bus_size_t, int, bus_space_handle_t *));
    207 int	mipsco_sparse_bus_space_dispose_handle __P((bus_space_tag_t,
    208 	    bus_space_handle_t, bus_size_t));
    209 int	mipsco_sparse_bus_space_paddr __P((bus_space_tag_t,
    210 	    bus_space_handle_t, paddr_t *));
    211 
    212 int	mipsco_bus_space_map __P((bus_space_tag_t, bus_addr_t, bus_size_t, int,
    213 	    bus_space_handle_t *));
    214 void	mipsco_bus_space_unmap __P((bus_space_tag_t, bus_space_handle_t,
    215 	    bus_size_t));
    216 int	mipsco_bus_space_subregion __P((bus_space_tag_t, bus_space_handle_t,
    217 	    bus_size_t, bus_size_t, bus_space_handle_t *));
    218 int	mipsco_bus_space_alloc __P((bus_space_tag_t, bus_addr_t, bus_addr_t,
    219 	    bus_size_t, bus_size_t, bus_size_t, int, bus_addr_t *,
    220 	    bus_space_handle_t *));
    221 #define mipsco_bus_space_free	mipsco_bus_space_unmap
    222 
    223 /*
    224  *	int bus_space_compose_handle __P((bus_space_tag_t t, bus_addr_t addr,
    225  *	    bus_size_t size, int flags, bus_space_handle_t *bshp));
    226  *
    227  * MACHINE DEPENDENT, NOT PORTABLE INTERFACE:
    228  * Compose a bus_space handle from tag/handle/addr/size/flags.
    229  * A helper function for bus_space_map()/bus_space_alloc() implementation.
    230  */
    231 #define bus_space_compose_handle(bst, addr, size, flags, bshp)		\
    232 	(*(bst)->bs_compose_handle)(bst, addr, size, flags, bshp)
    233 
    234 /*
    235  *	int bus_space_dispose_handle __P((bus_space_tag_t t, bus_addr_t addr,
    236  *	    bus_space_handle_t bsh, bus_size_t size));
    237  *
    238  * MACHINE DEPENDENT, NOT PORTABLE INTERFACE:
    239  * Dispose a bus_space handle.
    240  * A helper function for bus_space_unmap()/bus_space_free() implementation.
    241  */
    242 #define bus_space_dispose_handle(bst, bsh, size)			\
    243 	(*(bst)->bs_dispose_handle)(bst, bsh, size)
    244 
    245 /*
    246  *	int bus_space_paddr __P((bus_space_tag_t tag,
    247  *	    bus_space_handle_t bsh, paddr_t *pap));
    248  *
    249  * MACHINE DEPENDENT, NOT PORTABLE INTERFACE:
    250  * (cannot be implemented on e.g. I/O space on i386, non-linear space on alpha)
    251  * Return physical address of a region.
    252  * A helper function for device mmap entry.
    253  */
    254 #define bus_space_paddr(bst, bsh, pap)					\
    255 	(*(bst)->bs_paddr)(bst, bsh, pap)
    256 
    257 /*
    258  *	void *bus_space_vaddr __P((bus_space_tag_t, bus_space_handle_t));
    259  *
    260  * Get the kernel virtual address for the mapped bus space.
    261  * Only allowed for regions mapped with BUS_SPACE_MAP_LINEAR.
    262  *  (XXX not enforced)
    263  */
    264 #define bus_space_vaddr(bst, bsh)					\
    265 	((void *)(bsh))
    266 
    267 /*
    268  *	int bus_space_map __P((bus_space_tag_t t, bus_addr_t addr,
    269  *	    bus_size_t size, int flags, bus_space_handle_t *bshp));
    270  *
    271  * Map a region of bus space.
    272  */
    273 
    274 #define BUS_SPACE_MAP_CACHEABLE		0x01
    275 #define BUS_SPACE_MAP_LINEAR		0x02
    276 #define BUS_SPACE_MAP_PREFETCHABLE	0x04
    277 
    278 #define bus_space_map(t, a, s, f, hp)					\
    279 	(*(t)->bs_map)((t), (a), (s), (f), (hp))
    280 
    281 /*
    282  *	void bus_space_unmap __P((bus_space_tag_t t,
    283  *	    bus_space_handle_t bsh, bus_size_t size));
    284  *
    285  * Unmap a region of bus space.
    286  */
    287 
    288 #define bus_space_unmap(t, h, s)					\
    289 	(*(t)->bs_unmap)((t), (h), (s))
    290 
    291 /*
    292  *	int bus_space_subregion __P((bus_space_tag_t t,
    293  *	    bus_space_handle_t bsh, bus_size_t offset, bus_size_t size,
    294  *	    bus_space_handle_t *nbshp));
    295  *
    296  * Get a new handle for a subregion of an already-mapped area of bus space.
    297  */
    298 
    299 #define bus_space_subregion(t, h, o, s, hp)				\
    300 	(*(t)->bs_subregion)((t), (h), (o), (s), (hp))
    301 
    302 /*
    303  *	int bus_space_alloc __P((bus_space_tag_t t, bus_addr_t, rstart,
    304  *	    bus_addr_t rend, bus_size_t size, bus_size_t align,
    305  *	    bus_size_t boundary, int flags, bus_addr_t *addrp,
    306  *	    bus_space_handle_t *bshp));
    307  *
    308  * Allocate a region of bus space.
    309  */
    310 
    311 #define bus_space_alloc(t, rs, re, s, a, b, f, ap, hp)			\
    312 	(*(t)->bs_alloc)((t), (rs), (re), (s), (a), (b), (f), (ap), (hp))
    313 
    314 /*
    315  *	int bus_space_free __P((bus_space_tag_t t,
    316  *	    bus_space_handle_t bsh, bus_size_t size));
    317  *
    318  * Free a region of bus space.
    319  */
    320 
    321 #define bus_space_free(t, h, s)						\
    322 	(*(t)->bs_free)((t), (h), (s))
    323 
    324 /*
    325  *	void bus_intr_establish __P((bus_space_tag_t bst,
    326  *	     int level, int pri, int flags, int (*func) __P((void *))
    327  *	     void *arg));
    328  *
    329  *  Attach interrupt handler and softc argument
    330  */
    331 
    332 #define bus_intr_establish(t, i, c, f, ihf, iha)			\
    333 	(*(t)->bs_intr_establish)((t), (i), (c), (f), (ihf), (iha))
    334 
    335 /*
    336  *	u_intN_t bus_space_read_N __P((bus_space_tag_t tag,
    337  *	    bus_space_handle_t bsh, bus_size_t offset));
    338  *
    339  * Read a 1, 2, 4, or 8 byte quantity from bus space
    340  * described by tag/handle/offset.
    341  */
    342 
    343 #define bus_space_read(BYTES,BITS)					\
    344 static __inline __CONCAT3(u_int,BITS,_t)				\
    345 __CONCAT(bus_space_read_,BYTES)(bus_space_tag_t bst,			\
    346     bus_space_handle_t bsh, bus_size_t offset)				\
    347 {									\
    348 	return (*(volatile __CONCAT3(u_int,BITS,_t) *)			\
    349 	    (bsh + (offset << __CONCAT(bst->bs_stride_,BYTES))));	\
    350 }
    351 
    352 bus_space_read(1,8)
    353 bus_space_read(2,16)
    354 bus_space_read(4,32)
    355 bus_space_read(8,64)
    356 
    357 /*
    358  *	void bus_space_read_multi_N __P((bus_space_tag_t tag,
    359  *	    bus_space_handle_t bsh, bus_size_t offset,
    360  *	    u_intN_t *addr, size_t count));
    361  *
    362  * Read `count' 1, 2, 4, or 8 byte quantities from bus space
    363  * described by tag/handle/offset and copy into buffer provided.
    364  */
    365 
    366 #define bus_space_read_multi(BYTES,BITS)				\
    367 static __inline void							\
    368 __CONCAT(bus_space_read_multi_,BYTES)(bus_space_tag_t bst,		\
    369     bus_space_handle_t bsh, bus_size_t offset,				\
    370     __CONCAT3(u_int,BITS,_t) *datap, bus_size_t count)			\
    371 {									\
    372 	volatile __CONCAT3(u_int,BITS,_t) *p =				\
    373 	    (volatile __CONCAT3(u_int,BITS,_t) *)			\
    374 	    (bsh + (offset << __CONCAT(bst->bs_stride_,BYTES)));	\
    375 									\
    376 	for (; count > 0; --count)					\
    377 		*datap++ = *p;						\
    378 }
    379 
    380 bus_space_read_multi(1,8)
    381 bus_space_read_multi(2,16)
    382 bus_space_read_multi(4,32)
    383 bus_space_read_multi(8,64)
    384 
    385 /*
    386  *	void bus_space_read_region_N __P((bus_space_tag_t tag,
    387  *	    bus_space_handle_t bsh, bus_size_t offset,
    388  *	    u_intN_t *addr, size_t count));
    389  *
    390  * Read `count' 1, 2, 4, or 8 byte quantities from bus space
    391  * described by tag/handle and starting at `offset' and copy into
    392  * buffer provided.
    393  */
    394 
    395 #define bus_space_read_region(BYTES,BITS)				\
    396 static __inline void							\
    397 __CONCAT(bus_space_read_region_,BYTES)(bus_space_tag_t bst,		\
    398     bus_space_handle_t bsh, bus_size_t offset,				\
    399     __CONCAT3(u_int,BITS,_t) *datap, bus_size_t count)			\
    400 {									\
    401 	int stride = 1 << __CONCAT(bst->bs_stride_,BYTES);		\
    402 	volatile __CONCAT3(u_int,BITS,_t) *p =				\
    403 	    (volatile __CONCAT3(u_int,BITS,_t) *)			\
    404 	    (bsh + (offset << __CONCAT(bst->bs_stride_,BYTES)));	\
    405 									\
    406 	for (; count > 0; --count) {					\
    407 		*datap++ = *p;						\
    408 		p += stride;						\
    409 	}								\
    410 }
    411 
    412 bus_space_read_region(1,8)
    413 bus_space_read_region(2,16)
    414 bus_space_read_region(4,32)
    415 bus_space_read_region(8,64)
    416 
    417 /*
    418  *	void bus_space_write_N __P((bus_space_tag_t tag,
    419  *	    bus_space_handle_t bsh, bus_size_t offset,
    420  *	    u_intN_t value));
    421  *
    422  * Write the 1, 2, 4, or 8 byte value `value' to bus space
    423  * described by tag/handle/offset.
    424  */
    425 
    426 #define bus_space_write(BYTES,BITS)					\
    427 static __inline void							\
    428 __CONCAT(bus_space_write_,BYTES)(bus_space_tag_t bst,			\
    429     bus_space_handle_t bsh,						\
    430     bus_size_t offset, __CONCAT3(u_int,BITS,_t) data)			\
    431 {									\
    432 	*(volatile __CONCAT3(u_int,BITS,_t) *)				\
    433 	    (bsh + (offset << __CONCAT(bst->bs_stride_,BYTES))) = data; \
    434 	wbflush();							\
    435 }
    436 
    437 bus_space_write(1,8)
    438 bus_space_write(2,16)
    439 bus_space_write(4,32)
    440 bus_space_write(8,64)
    441 
    442 /*
    443  *	void bus_space_write_multi_N __P((bus_space_tag_t tag,
    444  *	    bus_space_handle_t bsh, bus_size_t offset,
    445  *	    const u_intN_t *addr, size_t count));
    446  *
    447  * Write `count' 1, 2, 4, or 8 byte quantities from the buffer
    448  * provided to bus space described by tag/handle/offset.
    449  */
    450 
    451 #define bus_space_write_multi(BYTES,BITS)				\
    452 static __inline void							\
    453 __CONCAT(bus_space_write_multi_,BYTES)(bus_space_tag_t bst,		\
    454     bus_space_handle_t bsh, bus_size_t offset,				\
    455     const __CONCAT3(u_int,BITS,_t) *datap, bus_size_t count)		\
    456 {									\
    457 	volatile __CONCAT3(u_int,BITS,_t) *p =				\
    458 	    (volatile __CONCAT3(u_int,BITS,_t) *)			\
    459 	    (bsh + (offset << __CONCAT(bst->bs_stride_,BYTES)));	\
    460 									\
    461 	while (count--) {						\
    462 		*p = *datap++;						\
    463 		wbflush();						\
    464 	}								\
    465 }
    466 
    467 bus_space_write_multi(1,8)
    468 bus_space_write_multi(2,16)
    469 bus_space_write_multi(4,32)
    470 bus_space_write_multi(8,64)
    471 
    472 /*
    473  *	void bus_space_write_region_N __P((bus_space_tag_t tag,
    474  *	    bus_space_handle_t bsh, bus_size_t offset,
    475  *	    const u_intN_t *addr, size_t count));
    476  *
    477  * Write `count' 1, 2, 4, or 8 byte quantities from the buffer provided
    478  * to bus space described by tag/handle starting at `offset'.
    479  */
    480 
    481 #define bus_space_write_region(BYTES,BITS)				\
    482 static __inline void							\
    483 __CONCAT(bus_space_write_region_,BYTES)(bus_space_tag_t bst,		\
    484     bus_space_handle_t bsh, bus_size_t offset,				\
    485     const __CONCAT3(u_int,BITS,_t) *datap, bus_size_t count)		\
    486 {									\
    487 	int stride = 1 << __CONCAT(bst->bs_stride_,BYTES);		\
    488 	volatile __CONCAT3(u_int,BITS,_t) *p =				\
    489 	    (volatile __CONCAT3(u_int,BITS,_t) *)			\
    490 	    (bsh + (offset << __CONCAT(bst->bs_stride_,BYTES)));	\
    491 									\
    492 	while (count--) {						\
    493 		*p = *datap++;						\
    494 		p += stride;						\
    495 	}								\
    496 	wbflush();							\
    497 }
    498 
    499 bus_space_write_region(1,8)
    500 bus_space_write_region(2,16)
    501 bus_space_write_region(4,32)
    502 bus_space_write_region(8,64)
    503 
    504 /*
    505  *	void bus_space_set_multi_N __P((bus_space_tag_t tag,
    506  *	    bus_space_handle_t bsh, bus_size_t offset, u_intN_t val,
    507  *	    size_t count));
    508  *
    509  * Write the 1, 2, 4, or 8 byte value `val' to bus space described
    510  * by tag/handle/offset `count' times.
    511  */
    512 
    513 #define bus_space_set_multi(BYTES,BITS)					\
    514 static __inline void							\
    515 __CONCAT(bus_space_set_multi_,BYTES)(bus_space_tag_t bst,		\
    516     bus_space_handle_t bsh, bus_size_t offset,				\
    517     const __CONCAT3(u_int,BITS,_t) data, bus_size_t count)		\
    518 {									\
    519 	volatile __CONCAT3(u_int,BITS,_t) *p =				\
    520 	    (volatile __CONCAT3(u_int,BITS,_t) *)			\
    521 	    (bsh + (offset << __CONCAT(bst->bs_stride_,BYTES)));	\
    522 									\
    523 	while (count--)							\
    524 		*p = data;						\
    525 }
    526 
    527 bus_space_set_multi(1,8)
    528 bus_space_set_multi(2,16)
    529 bus_space_set_multi(4,32)
    530 bus_space_set_multi(8,64)
    531 
    532 /*
    533  *	void bus_space_set_region_N __P((bus_space_tag_t tag,
    534  *	    bus_space_handle_t bsh, bus_size_t offset, u_intN_t val,
    535  *	    size_t count));
    536  *
    537  * Write `count' 1, 2, 4, or 8 byte value `val' to bus space described
    538  * by tag/handle starting at `offset'.
    539  */
    540 
    541 #define bus_space_set_region(BYTES,BITS)				\
    542 static __inline void							\
    543 __CONCAT(bus_space_set_region_,BYTES)(bus_space_tag_t bst,		\
    544     bus_space_handle_t bsh, bus_size_t offset,				\
    545     __CONCAT3(u_int,BITS,_t) data, bus_size_t count)			\
    546 {									\
    547 	int stride = 1 << __CONCAT(bst->bs_stride_,BYTES);		\
    548 	volatile __CONCAT3(u_int,BITS,_t) *p =				\
    549 	    (volatile __CONCAT3(u_int,BITS,_t) *)			\
    550 	    (bsh + (offset << __CONCAT(bst->bs_stride_,BYTES)));	\
    551 									\
    552 	while (count--) {						\
    553 		*p = data;						\
    554 		p += stride;						\
    555 	}								\
    556 	wbflush();							\
    557 }
    558 
    559 bus_space_set_region(1,8)
    560 bus_space_set_region(2,16)
    561 bus_space_set_region(4,32)
    562 bus_space_set_region(8,64)
    563 
    564 /*
    565  *	void bus_space_copy_region_N __P((bus_space_tag_t tag,
    566  *	    bus_space_handle_t bsh1, bus_size_t off1,
    567  *	    bus_space_handle_t bsh2, bus_size_t off2,
    568  *	    size_t count));
    569  *
    570  * Copy `count' 1, 2, 4, or 8 byte values from bus space starting
    571  * at tag/bsh1/off1 to bus space starting at tag/bsh2/off2.
    572  */
    573 
    574 #define bus_space_copy_region(BYTES,BITS)				\
    575 static __inline void							\
    576 __CONCAT(bus_space_copy_region_,BYTES)(bus_space_tag_t bst,		\
    577     bus_space_handle_t srcbsh, bus_size_t srcoffset,			\
    578     bus_space_handle_t dstbsh, bus_size_t dstoffset, bus_size_t count)	\
    579 {									\
    580 	int stride = 1 << __CONCAT(bst->bs_stride_,BYTES);		\
    581 	volatile __CONCAT3(u_int,BITS,_t) *srcp =			\
    582 	    (volatile __CONCAT3(u_int,BITS,_t) *)			\
    583 	    (srcbsh + (srcoffset << __CONCAT(bst->bs_stride_,BYTES)));	\
    584 	volatile __CONCAT3(u_int,BITS,_t) *dstp =			\
    585 	    (volatile __CONCAT3(u_int,BITS,_t) *)			\
    586 	    (dstbsh + (dstoffset << __CONCAT(bst->bs_stride_,BYTES)));	\
    587 	bus_size_t offset;						\
    588 									\
    589 	if (srcp >= dstp) {						\
    590 		/* src after dest: copy forward */			\
    591 		for (offset = 0; count > 0; --count, offset += stride)	\
    592 			dstp[offset] = srcp[offset];			\
    593 	} else {							\
    594 		/* dest after src: copy backward */			\
    595 		offset = (count << __CONCAT(bst->bs_stride_,BYTES))	\
    596 		    - stride;						\
    597 		for (; count > 0; --count, offset -= stride)		\
    598 			dstp[offset] = srcp[offset];			\
    599 	}								\
    600 	wbflush();							\
    601 }
    602 
    603 bus_space_copy_region(1,8)
    604 bus_space_copy_region(2,16)
    605 bus_space_copy_region(4,32)
    606 bus_space_copy_region(8,64)
    607 
    608 /*
    609  * Operations which handle byte stream data on word access.
    610  *
    611  * These functions are defined to resolve endian mismatch, by either
    612  * - When normal (i.e. stream-less) operations perform byte swap
    613  *   to resolve endian mismatch, these functions bypass the byte swap.
    614  * or
    615  * - When bus bridge performs automatic byte swap, these functions
    616  *   perform byte swap once more, to cancel the bridge's behavior.
    617  *
    618  * Currently these are just same as normal operations, since all
    619  * supported buses are same endian with CPU (i.e. little-endian).
    620  *
    621  */
    622 #define __BUS_SPACE_HAS_STREAM_METHODS
    623 #define bus_space_read_stream_2(tag, bsh, offset)			\
    624 	bus_space_read_2(tag, bsh, offset)
    625 #define bus_space_read_stream_4(tag, bsh, offset)			\
    626 	bus_space_read_4(tag, bsh, offset)
    627 #define bus_space_read_stream_8(tag, bsh, offset)			\
    628 	bus_space_read_8(tag, bsh, offset)
    629 #define bus_space_read_multi_stream_2(tag, bsh, offset, datap, count)	\
    630 	bus_space_read_multi_2(tag, bsh, offset, datap, count)
    631 #define bus_space_read_multi_stream_4(tag, bsh, offset, datap, count)	\
    632 	bus_space_read_multi_4(tag, bsh, offset, datap, count)
    633 #define bus_space_read_multi_stream_8(tag, bsh, offset, datap, count)	\
    634 	bus_space_read_multi_8(tag, bsh, offset, datap, count)
    635 #define bus_space_read_region_stream_2(tag, bsh, offset, datap, count)	\
    636 	bus_space_read_region_2(tag, bsh, offset, datap, count)
    637 #define bus_space_read_region_stream_4(tag, bsh, offset, datap, count)	\
    638 	bus_space_read_region_4(tag, bsh, offset, datap, count)
    639 #define bus_space_read_region_stream_8(tag, bsh, offset, datap, count)	\
    640 	bus_space_read_region_8(tag, bsh, offset, datap, count)
    641 #define bus_space_write_stream_2(tag, bsh, offset, data)		\
    642 	bus_space_write_2(tag, bsh, offset, data)
    643 #define bus_space_write_stream_4(tag, bsh, offset, data)		\
    644 	bus_space_write_4(tag, bsh, offset, data)
    645 #define bus_space_write_stream_8(tag, bsh, offset, data)		\
    646 	bus_space_write_8(tag, bsh, offset, data)
    647 #define bus_space_write_multi_stream_2(tag, bsh, offset, datap, count)	\
    648 	bus_space_write_multi_2(tag, bsh, offset, datap, count)
    649 #define bus_space_write_multi_stream_4(tag, bsh, offset, datap, count)	\
    650 	bus_space_write_multi_4(tag, bsh, offset, datap, count)
    651 #define bus_space_write_multi_stream_8(tag, bsh, offset, datap, count)	\
    652 	bus_space_write_multi_8(tag, bsh, offset, datap, count)
    653 #define bus_space_write_region_stream_2(tag, bsh, offset, datap, count)	\
    654 	bus_space_write_region_2(tag, bsh, offset, datap, count)
    655 #define bus_space_write_region_stream_4(tag, bsh, offset, datap, count)	\
    656 	bus_space_write_region_4(tag, bsh, offset, datap, count)
    657 #define bus_space_write_region_stream_8(tag, bsh, offset, datap, count)	\
    658 	bus_space_write_region_8(tag, bsh, offset, datap, count)
    659 #define bus_space_write_region_stream_2(tag, bsh, offset, datap, count)	\
    660 	bus_space_write_region_2(tag, bsh, offset, datap, count)
    661 #define bus_space_write_region_stream_4(tag, bsh, offset, datap, count)	\
    662 	bus_space_write_region_4(tag, bsh, offset, datap, count)
    663 #define bus_space_write_region_stream_8(tag, bsh, offset, datap, count)	\
    664 	bus_space_write_region_8(tag, bsh, offset, datap, count)
    665 #define bus_space_set_multi_stream_2(tag, bsh, offset, data, count)	\
    666 	bus_space_set_multi_2(tag, bsh, offset, data, count)
    667 #define bus_space_set_multi_stream_4(tag, bsh, offset, data, count)	\
    668 	bus_space_set_multi_4(tag, bsh, offset, data, count)
    669 #define bus_space_set_multi_stream_8(tag, bsh, offset, data, count)	\
    670 	bus_space_set_multi_8(tag, bsh, offset, data, count)
    671 #define bus_space_set_region_stream_2(tag, bsh, offset, data, count)	\
    672 	bus_space_set_region_2(tag, bsh, offset, data, count)
    673 #define bus_space_set_region_stream_4(tag, bsh, offset, data, count)	\
    674 	bus_space_set_region_4(tag, bsh, offset, data, count)
    675 #define bus_space_set_region_stream_8(tag, bsh, offset, data, count)	\
    676 	bus_space_set_region_8(tag, bsh, offset, data, count)
    677 
    678 /*
    679  * Bus read/write barrier methods.
    680  *
    681  *	void bus_space_barrier __P((bus_space_tag_t tag,
    682  *	    bus_space_handle_t bsh, bus_size_t offset,
    683  *	    bus_size_t len, int flags));
    684  *
    685  * On the MIPS, we just flush the write buffer.
    686  */
    687 #define bus_space_barrier(t, h, o, l, f)				\
    688 	((void)((void)(t), (void)(h), (void)(o), (void)(l), (void)(f)),	\
    689 	 wbflush())
    690 
    691 #define BUS_SPACE_BARRIER_READ	0x01
    692 #define BUS_SPACE_BARRIER_WRITE	0x02
    693 
    694 /*
    695  * Flags used in various bus DMA methods.
    696  */
    697 #define BUS_DMA_WAITOK		0x00	/* safe to sleep (pseudo-flag) */
    698 #define BUS_DMA_NOWAIT		0x01	/* not safe to sleep */
    699 #define BUS_DMA_ALLOCNOW	0x02	/* perform resource allocation now */
    700 #define BUS_DMA_COHERENT	0x04	/* hint: map memory DMA coherent */
    701 #define	BUS_DMA_STREAMING	0x08	/* hint: sequential, unidirectional */
    702 #define BUS_DMA_BUS1		0x10	/* placeholders for bus functions... */
    703 #define BUS_DMA_BUS2		0x20
    704 #define BUS_DMA_BUS3		0x40
    705 #define BUS_DMA_BUS4		0x80
    706 
    707 #define MIPSCO_DMAMAP_COHERENT	0x100	/* no cache flush necessary on sync */
    708 
    709 /* Forwards needed by prototypes below. */
    710 struct mbuf;
    711 struct uio;
    712 
    713 /*
    714  * Operations performed by bus_dmamap_sync().
    715  */
    716 #define BUS_DMASYNC_PREREAD	0x01	/* pre-read synchronization */
    717 #define BUS_DMASYNC_POSTREAD	0x02	/* post-read synchronization */
    718 #define BUS_DMASYNC_PREWRITE	0x04	/* pre-write synchronization */
    719 #define BUS_DMASYNC_POSTWRITE	0x08	/* post-write synchronization */
    720 
    721 typedef struct mipsco_bus_dma_tag		*bus_dma_tag_t;
    722 typedef struct mipsco_bus_dmamap		*bus_dmamap_t;
    723 
    724 /*
    725  *	bus_dma_segment_t
    726  *
    727  *	Describes a single contiguous DMA transaction.  Values
    728  *	are suitable for programming into DMA registers.
    729  */
    730 struct mipsco_bus_dma_segment {
    731 	/*
    732 	 * PUBLIC MEMBERS: these are used by device drivers.
    733 	 */
    734 	bus_addr_t	ds_addr;	/* DMA address */
    735 	bus_size_t	ds_len;		/* length of transfer */
    736 	/*
    737 	 * PRIVATE MEMBERS for the DMA back-end.: not for use by drivers.
    738 	 */
    739 	vaddr_t		_ds_paddr;	/* CPU physical address */
    740 	vaddr_t		_ds_vaddr;	/* virtual address, 0 if invalid */
    741 };
    742 typedef struct mipsco_bus_dma_segment	bus_dma_segment_t;
    743 
    744 /*
    745  *	bus_dma_tag_t
    746  *
    747  *	A machine-dependent opaque type describing the implementation of
    748  *	DMA for a given bus.
    749  */
    750 
    751 struct mipsco_bus_dma_tag {
    752 	bus_addr_t	dma_offset;
    753 
    754 	/*
    755 	 * DMA mapping methods.
    756 	 */
    757 	int	(*_dmamap_create) __P((bus_dma_tag_t, bus_size_t, int,
    758 		    bus_size_t, bus_size_t, int, bus_dmamap_t *));
    759 	void	(*_dmamap_destroy) __P((bus_dma_tag_t, bus_dmamap_t));
    760 	int	(*_dmamap_load) __P((bus_dma_tag_t, bus_dmamap_t, void *,
    761 		    bus_size_t, struct proc *, int));
    762 	int	(*_dmamap_load_mbuf) __P((bus_dma_tag_t, bus_dmamap_t,
    763 		    struct mbuf *, int));
    764 	int	(*_dmamap_load_uio) __P((bus_dma_tag_t, bus_dmamap_t,
    765 		    struct uio *, int));
    766 	int	(*_dmamap_load_raw) __P((bus_dma_tag_t, bus_dmamap_t,
    767 		    bus_dma_segment_t *, int, bus_size_t, int));
    768 	void	(*_dmamap_unload) __P((bus_dma_tag_t, bus_dmamap_t));
    769 	void	(*_dmamap_sync) __P((bus_dma_tag_t, bus_dmamap_t,
    770 		    bus_addr_t, bus_size_t, int));
    771 
    772 	/*
    773 	 * DMA memory utility functions.
    774 	 */
    775 	int	(*_dmamem_alloc) __P((bus_dma_tag_t, bus_size_t, bus_size_t,
    776 		    bus_size_t, bus_dma_segment_t *, int, int *, int));
    777 	void	(*_dmamem_free) __P((bus_dma_tag_t,
    778 		    bus_dma_segment_t *, int));
    779 	int	(*_dmamem_map) __P((bus_dma_tag_t, bus_dma_segment_t *,
    780 		    int, size_t, caddr_t *, int));
    781 	void	(*_dmamem_unmap) __P((bus_dma_tag_t, caddr_t, size_t));
    782 	paddr_t	(*_dmamem_mmap) __P((bus_dma_tag_t, bus_dma_segment_t *,
    783 		    int, off_t, int, int));
    784 };
    785 
    786 #define bus_dmamap_create(t, s, n, m, b, f, p)			\
    787 	(*(t)->_dmamap_create)((t), (s), (n), (m), (b), (f), (p))
    788 #define bus_dmamap_destroy(t, p)				\
    789 	(*(t)->_dmamap_destroy)((t), (p))
    790 #define bus_dmamap_load(t, m, b, s, p, f)			\
    791 	(*(t)->_dmamap_load)((t), (m), (b), (s), (p), (f))
    792 #define bus_dmamap_load_mbuf(t, m, b, f)			\
    793 	(*(t)->_dmamap_load_mbuf)((t), (m), (b), (f))
    794 #define bus_dmamap_load_uio(t, m, u, f)				\
    795 	(*(t)->_dmamap_load_uio)((t), (m), (u), (f))
    796 #define bus_dmamap_load_raw(t, m, sg, n, s, f)			\
    797 	(*(t)->_dmamap_load_raw)((t), (m), (sg), (n), (s), (f))
    798 #define bus_dmamap_unload(t, p)					\
    799 	(*(t)->_dmamap_unload)((t), (p))
    800 #define bus_dmamap_sync(t, p, o, l, ops)			\
    801 	(*(t)->_dmamap_sync)((t), (p), (o), (l), (ops))
    802 #define bus_dmamem_alloc(t, s, a, b, sg, n, r, f)		\
    803 	(*(t)->_dmamem_alloc)((t), (s), (a), (b), (sg), (n), (r), (f))
    804 #define bus_dmamem_free(t, sg, n)				\
    805 	(*(t)->_dmamem_free)((t), (sg), (n))
    806 #define bus_dmamem_map(t, sg, n, s, k, f)			\
    807 	(*(t)->_dmamem_map)((t), (sg), (n), (s), (k), (f))
    808 #define bus_dmamem_unmap(t, k, s)				\
    809 	(*(t)->_dmamem_unmap)((t), (k), (s))
    810 #define bus_dmamem_mmap(t, sg, n, o, p, f)			\
    811 	(*(t)->_dmamem_mmap)((t), (sg), (n), (o), (p), (f))
    812 
    813 /*
    814  *	bus_dmamap_t
    815  *
    816  *	Describes a DMA mapping.
    817  */
    818 struct mipsco_bus_dmamap {
    819 	/*
    820 	 * PRIVATE MEMBERS: not for use by machine-independent code.
    821 	 */
    822 	bus_size_t	_dm_size;	/* largest DMA transfer mappable */
    823 	int		_dm_segcnt;	/* number of segs this map can map */
    824 	bus_size_t	_dm_maxsegsz;	/* largest possible segment */
    825 	bus_size_t	_dm_boundary;	/* don't cross this */
    826 	int		_dm_flags;	/* misc. flags */
    827 
    828 	/*
    829 	 * Private cookie to be used by the DMA back-end.
    830 	 */
    831 	void		*_dm_cookie;
    832 
    833 	/*
    834 	 * PUBLIC MEMBERS: these are used by machine-independent code.
    835 	 */
    836 	bus_size_t	dm_mapsize;	/* size of the mapping */
    837 	int		dm_nsegs;	/* # valid segments in mapping */
    838 	bus_dma_segment_t dm_segs[1];	/* segments; variable length */
    839 };
    840 
    841 #ifdef _MIPSCO_BUS_DMA_PRIVATE
    842 int	_bus_dmamap_create __P((bus_dma_tag_t, bus_size_t, int, bus_size_t,
    843 	    bus_size_t, int, bus_dmamap_t *));
    844 void	_bus_dmamap_destroy __P((bus_dma_tag_t, bus_dmamap_t));
    845 int	_bus_dmamap_load __P((bus_dma_tag_t, bus_dmamap_t, void *,
    846 	    bus_size_t, struct proc *, int));
    847 int	_bus_dmamap_load_mbuf __P((bus_dma_tag_t, bus_dmamap_t,
    848 	    struct mbuf *, int));
    849 int	_bus_dmamap_load_uio __P((bus_dma_tag_t, bus_dmamap_t,
    850 	    struct uio *, int));
    851 int	_bus_dmamap_load_raw __P((bus_dma_tag_t, bus_dmamap_t,
    852 	    bus_dma_segment_t *, int, bus_size_t, int));
    853 void	_bus_dmamap_unload __P((bus_dma_tag_t, bus_dmamap_t));
    854 void	_mips1_bus_dmamap_sync __P((bus_dma_tag_t, bus_dmamap_t, bus_addr_t,
    855 	    bus_size_t, int));
    856 void	_mips3_bus_dmamap_sync __P((bus_dma_tag_t, bus_dmamap_t, bus_addr_t,
    857 	    bus_size_t, int));
    858 
    859 int	_bus_dmamem_alloc __P((bus_dma_tag_t tag, bus_size_t size,
    860 	    bus_size_t alignment, bus_size_t boundary,
    861 	    bus_dma_segment_t *segs, int nsegs, int *rsegs, int flags));
    862 int	_bus_dmamem_alloc_range __P((bus_dma_tag_t tag, bus_size_t size,
    863 	    bus_size_t alignment, bus_size_t boundary,
    864 	    bus_dma_segment_t *segs, int nsegs, int *rsegs, int flags,
    865 	    paddr_t low, paddr_t high));
    866 void	_bus_dmamem_free __P((bus_dma_tag_t tag, bus_dma_segment_t *segs,
    867 	    int nsegs));
    868 int	_bus_dmamem_map __P((bus_dma_tag_t tag, bus_dma_segment_t *segs,
    869 	    int nsegs, size_t size, caddr_t *kvap, int flags));
    870 void	_bus_dmamem_unmap __P((bus_dma_tag_t tag, caddr_t kva,
    871 	    size_t size));
    872 paddr_t	_bus_dmamem_mmap __P((bus_dma_tag_t tag, bus_dma_segment_t *segs,
    873 	    int nsegs, off_t off, int prot, int flags));
    874 
    875 int	_bus_dmamem_alloc_range __P((bus_dma_tag_t tag, bus_size_t size,
    876 	    bus_size_t alignment, bus_size_t boundary,
    877 	    bus_dma_segment_t *segs, int nsegs, int *rsegs, int flags,
    878 	    paddr_t low, paddr_t high));
    879 #endif /* _MIPSCO_BUS_DMA_PRIVATE */
    880 
    881 void	_bus_dma_tag_init __P((bus_dma_tag_t tag));
    882 void	jazz_bus_dma_tag_init __P((bus_dma_tag_t tag));
    883 void	isadma_bounce_tag_init __P((bus_dma_tag_t tag));
    884 
    885 #endif /* _KERNEL */
    886 #endif /* _MIPSCO_BUS_H_ */
    887