Home | History | Annotate | Line # | Download | only in include
bus.h revision 1.9
      1 /*	$NetBSD: bus.h,v 1.9 2002/03/17 21:45:08 simonb 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 	u_int8_t	bs_stride;	/* log2(stride) */
    135 	u_int8_t	bs_bswap;	/* byte swap in stream methods */
    136 
    137 	u_int8_t	bs_offset_1;
    138 	u_int8_t	bs_offset_2;
    139 	u_int8_t	bs_offset_4;
    140 	u_int8_t	bs_offset_8;
    141 
    142 	/* compose a bus_space handle from tag/handle/addr/size/flags (MD) */
    143 	int	(*bs_compose_handle) __P((bus_space_tag_t, bus_addr_t,
    144 				bus_size_t, int, bus_space_handle_t *));
    145 
    146 	/* dispose a bus_space handle (MD) */
    147 	int	(*bs_dispose_handle) __P((bus_space_tag_t, bus_space_handle_t,
    148 				bus_size_t));
    149 
    150 	/* convert bus_space tag/handle to physical address (MD) */
    151 	int	(*bs_paddr) __P((bus_space_tag_t, bus_space_handle_t,
    152 				paddr_t *));
    153 
    154 	/* mapping/unmapping */
    155 	int	(*bs_map) __P((bus_space_tag_t, bus_addr_t, bus_size_t, int,
    156 				bus_space_handle_t *));
    157 	void	(*bs_unmap) __P((bus_space_tag_t, bus_space_handle_t,
    158 				bus_size_t));
    159 	int	(*bs_subregion) __P((bus_space_tag_t, bus_space_handle_t,
    160 				bus_size_t, bus_size_t,	bus_space_handle_t *));
    161 	paddr_t	(*bs_mmap) __P((bus_space_tag_t, bus_addr_t, off_t, int, int));
    162 
    163 
    164 	/* allocation/deallocation */
    165 	int	(*bs_alloc) __P((bus_space_tag_t, bus_addr_t, bus_addr_t,
    166 				bus_size_t, bus_size_t,	bus_size_t, int,
    167 				bus_addr_t *, bus_space_handle_t *));
    168 	void	(*bs_free) __P((bus_space_tag_t, bus_space_handle_t,
    169 				bus_size_t));
    170 
    171 	/* interrupt attach */
    172 	void	(*bs_intr_establish) __P((
    173 				bus_space_tag_t,
    174 				int,			/*bus-specific intr*/
    175 				int,			/*priority/class*/
    176 				int,			/*flags*/
    177 				int (*) __P((void *)),	/*handler*/
    178 				void *));		/*handler arg*/
    179 
    180 	void	*bs_aux;
    181 };
    182 
    183 /* vaddr_t argument of mipsco_bus_space_init() */
    184 #define MIPSCO_BUS_SPACE_UNMAPPED	((vaddr_t)0)
    185 
    186 /* machine dependent utility function for bus_space users */
    187 void	mipsco_bus_space_malloc_set_safe __P((void));
    188 void	mipsco_bus_space_init __P((bus_space_tag_t, const char *,
    189 	    paddr_t, vaddr_t, bus_addr_t, bus_size_t));
    190 void	mipsco_bus_space_init_extent __P((bus_space_tag_t, caddr_t, size_t));
    191 void	mipsco_bus_space_set_aligned_stride __P((bus_space_tag_t, unsigned int));
    192 void	mipsco_sparse_bus_space_init __P((bus_space_tag_t, const char *,
    193 	    paddr_t, bus_addr_t, bus_size_t));
    194 void	mipsco_large_bus_space_init __P((bus_space_tag_t, const char *,
    195 	    paddr_t, bus_addr_t, bus_size_t));
    196 
    197 /* machine dependent utility function for bus_space implementations */
    198 int	mipsco_bus_space_extent_malloc_flag __P((void));
    199 
    200 /* these are provided for subclasses which override base bus_space. */
    201 
    202 int	mipsco_bus_space_compose_handle __P((bus_space_tag_t,
    203 	    bus_addr_t, bus_size_t, int, bus_space_handle_t *));
    204 int	mipsco_bus_space_dispose_handle __P((bus_space_tag_t,
    205 	    bus_space_handle_t, bus_size_t));
    206 int	mipsco_bus_space_paddr __P((bus_space_tag_t,
    207 	    bus_space_handle_t, paddr_t *));
    208 
    209 int	mipsco_sparse_bus_space_compose_handle __P((bus_space_tag_t,
    210 	    bus_addr_t, bus_size_t, int, bus_space_handle_t *));
    211 int	mipsco_sparse_bus_space_dispose_handle __P((bus_space_tag_t,
    212 	    bus_space_handle_t, bus_size_t));
    213 int	mipsco_sparse_bus_space_paddr __P((bus_space_tag_t,
    214 	    bus_space_handle_t, paddr_t *));
    215 
    216 int	mipsco_bus_space_map __P((bus_space_tag_t, bus_addr_t, bus_size_t, int,
    217 	    bus_space_handle_t *));
    218 void	mipsco_bus_space_unmap __P((bus_space_tag_t, bus_space_handle_t,
    219 	    bus_size_t));
    220 int	mipsco_bus_space_subregion __P((bus_space_tag_t, bus_space_handle_t,
    221 	    bus_size_t, bus_size_t, bus_space_handle_t *));
    222 paddr_t	mipsco_bus_space_mmap __P((bus_space_tag_t, bus_addr_t, off_t,
    223 	    int, int));
    224 int	mipsco_bus_space_alloc __P((bus_space_tag_t, bus_addr_t, bus_addr_t,
    225 	    bus_size_t, bus_size_t, bus_size_t, int, bus_addr_t *,
    226 	    bus_space_handle_t *));
    227 #define mipsco_bus_space_free	mipsco_bus_space_unmap
    228 
    229 /*
    230  *	int bus_space_compose_handle __P((bus_space_tag_t t, bus_addr_t addr,
    231  *	    bus_size_t size, int flags, bus_space_handle_t *bshp));
    232  *
    233  * MACHINE DEPENDENT, NOT PORTABLE INTERFACE:
    234  * Compose a bus_space handle from tag/handle/addr/size/flags.
    235  * A helper function for bus_space_map()/bus_space_alloc() implementation.
    236  */
    237 #define bus_space_compose_handle(bst, addr, size, flags, bshp)		\
    238 	(*(bst)->bs_compose_handle)(bst, addr, size, flags, bshp)
    239 
    240 /*
    241  *	int bus_space_dispose_handle __P((bus_space_tag_t t, bus_addr_t addr,
    242  *	    bus_space_handle_t bsh, bus_size_t size));
    243  *
    244  * MACHINE DEPENDENT, NOT PORTABLE INTERFACE:
    245  * Dispose a bus_space handle.
    246  * A helper function for bus_space_unmap()/bus_space_free() implementation.
    247  */
    248 #define bus_space_dispose_handle(bst, bsh, size)			\
    249 	(*(bst)->bs_dispose_handle)(bst, bsh, size)
    250 
    251 /*
    252  *	int bus_space_paddr __P((bus_space_tag_t tag,
    253  *	    bus_space_handle_t bsh, paddr_t *pap));
    254  *
    255  * MACHINE DEPENDENT, NOT PORTABLE INTERFACE:
    256  * (cannot be implemented on e.g. I/O space on i386, non-linear space on alpha)
    257  * Return physical address of a region.
    258  * A helper function for device mmap entry.
    259  */
    260 #define bus_space_paddr(bst, bsh, pap)					\
    261 	(*(bst)->bs_paddr)(bst, bsh, pap)
    262 
    263 /*
    264  *	void *bus_space_vaddr __P((bus_space_tag_t, bus_space_handle_t));
    265  *
    266  * Get the kernel virtual address for the mapped bus space.
    267  * Only allowed for regions mapped with BUS_SPACE_MAP_LINEAR.
    268  *  (XXX not enforced)
    269  */
    270 #define bus_space_vaddr(bst, bsh)					\
    271 	((void *)(bsh))
    272 
    273 /*
    274  *	paddr_t bus_space_mmap __P((bus_space_tag_t, bus_addr_t, off_t,
    275  *	    int, int));
    276  *
    277  * Mmap bus space on behalf of the user.
    278  */
    279 #define	bus_space_mmap(bst, addr, off, prot, flags)			\
    280 	(*(bst)->bs_mmap)((bst), (addr), (off), (prot), (flags))
    281 
    282 /*
    283  *	int bus_space_map __P((bus_space_tag_t t, bus_addr_t addr,
    284  *	    bus_size_t size, int flags, bus_space_handle_t *bshp));
    285  *
    286  * Map a region of bus space.
    287  */
    288 
    289 #define BUS_SPACE_MAP_CACHEABLE		0x01
    290 #define BUS_SPACE_MAP_LINEAR		0x02
    291 #define BUS_SPACE_MAP_PREFETCHABLE	0x04
    292 
    293 #define bus_space_map(t, a, s, f, hp)					\
    294 	(*(t)->bs_map)((t), (a), (s), (f), (hp))
    295 
    296 /*
    297  *	void bus_space_unmap __P((bus_space_tag_t t,
    298  *	    bus_space_handle_t bsh, bus_size_t size));
    299  *
    300  * Unmap a region of bus space.
    301  */
    302 
    303 #define bus_space_unmap(t, h, s)					\
    304 	(*(t)->bs_unmap)((t), (h), (s))
    305 
    306 /*
    307  *	int bus_space_subregion __P((bus_space_tag_t t,
    308  *	    bus_space_handle_t bsh, bus_size_t offset, bus_size_t size,
    309  *	    bus_space_handle_t *nbshp));
    310  *
    311  * Get a new handle for a subregion of an already-mapped area of bus space.
    312  */
    313 
    314 #define bus_space_subregion(t, h, o, s, hp)				\
    315 	(*(t)->bs_subregion)((t), (h), (o), (s), (hp))
    316 
    317 /*
    318  *	int bus_space_alloc __P((bus_space_tag_t t, bus_addr_t, rstart,
    319  *	    bus_addr_t rend, bus_size_t size, bus_size_t align,
    320  *	    bus_size_t boundary, int flags, bus_addr_t *addrp,
    321  *	    bus_space_handle_t *bshp));
    322  *
    323  * Allocate a region of bus space.
    324  */
    325 
    326 #define bus_space_alloc(t, rs, re, s, a, b, f, ap, hp)			\
    327 	(*(t)->bs_alloc)((t), (rs), (re), (s), (a), (b), (f), (ap), (hp))
    328 
    329 /*
    330  *	int bus_space_free __P((bus_space_tag_t t,
    331  *	    bus_space_handle_t bsh, bus_size_t size));
    332  *
    333  * Free a region of bus space.
    334  */
    335 
    336 #define bus_space_free(t, h, s)						\
    337 	(*(t)->bs_free)((t), (h), (s))
    338 
    339 /*
    340  *	void bus_intr_establish __P((bus_space_tag_t bst,
    341  *	     int level, int pri, int flags, int (*func) __P((void *))
    342  *	     void *arg));
    343  *
    344  *  Attach interrupt handler and softc argument
    345  */
    346 
    347 #define bus_intr_establish(t, i, c, f, ihf, iha)			\
    348 	(*(t)->bs_intr_establish)((t), (i), (c), (f), (ihf), (iha))
    349 
    350 
    351 /*
    352  * Utility macros; do not use outside this file.
    353  */
    354 #define	__BS_TYPENAME(BITS)		__CONCAT3(u_int,BITS,_t)
    355 #define __BS_OFFSET(t, o, BYTES)	((o) << (t)->bs_stride)
    356 #define __BS_FUNCTION(func,BYTES)	__CONCAT3(func,_,BYTES)
    357 
    358 /*
    359  * Calculate the target address using the bus_space parameters
    360  */
    361 #define __BS_ADDR(t, h, offset, BITS, BYTES)				\
    362 	((volatile __CONCAT3(u_int,BITS,_t) *)				\
    363 	 ((h) + __BS_OFFSET(t, offset, BYTES) +				\
    364 	 (t)->__CONCAT(bs_offset_,BYTES)))
    365 
    366 /*
    367  *	u_intN_t bus_space_read_N __P((bus_space_tag_t tag,
    368  *	    bus_space_handle_t bsh, bus_size_t offset));
    369  *
    370  * Read a 1, 2, 4, or 8 byte quantity from bus space
    371  * described by tag/handle/offset.
    372  */
    373 
    374 #define __bus_space_read(BYTES,BITS)					\
    375 static __inline __CONCAT3(u_int,BITS,_t)				\
    376 __CONCAT(bus_space_read_,BYTES)(bus_space_tag_t bst,			\
    377     bus_space_handle_t bsh, bus_size_t offset)				\
    378 {									\
    379 	return (*__BS_ADDR(bst, bsh, offset, BITS, BYTES));		\
    380 }
    381 
    382 __bus_space_read(1,8)
    383 __bus_space_read(2,16)
    384 __bus_space_read(4,32)
    385 __bus_space_read(8,64)
    386 
    387 /*
    388  *	void bus_space_read_multi_N __P((bus_space_tag_t tag,
    389  *	    bus_space_handle_t bsh, bus_size_t offset,
    390  *	    u_intN_t *addr, size_t count));
    391  *
    392  * Read `count' 1, 2, 4, or 8 byte quantities from bus space
    393  * described by tag/handle/offset and copy into buffer provided.
    394  */
    395 
    396 #define __bus_space_read_multi(BYTES,BITS)				\
    397 static __inline void __BS_FUNCTION(bus_space_read_multi,BYTES)		\
    398 	__P((bus_space_tag_t, bus_space_handle_t, bus_size_t,		\
    399 	__BS_TYPENAME(BITS) *, size_t));				\
    400 									\
    401 static __inline void							\
    402 __BS_FUNCTION(bus_space_read_multi,BYTES)(t, h, o, a, c)		\
    403 	bus_space_tag_t t;						\
    404 	bus_space_handle_t h;						\
    405 	bus_size_t o;							\
    406 	__BS_TYPENAME(BITS) *a;						\
    407 	size_t c;							\
    408 {									\
    409 									\
    410 	while (c--)							\
    411 		*a++ = __BS_FUNCTION(bus_space_read,BYTES)(t, h, o);	\
    412 }
    413 
    414 __bus_space_read_multi(1,8)
    415 __bus_space_read_multi(2,16)
    416 __bus_space_read_multi(4,32)
    417 __bus_space_read_multi(8,64)
    418 
    419 
    420 /*
    421  *	void bus_space_read_region_N __P((bus_space_tag_t tag,
    422  *	    bus_space_handle_t bsh, bus_size_t offset,
    423  *	    u_intN_t *addr, size_t count));
    424  *
    425  * Read `count' 1, 2, 4, or 8 byte quantities from bus space
    426  * described by tag/handle and starting at `offset' and copy into
    427  * buffer provided.
    428  */
    429 
    430 #define __bus_space_read_region(BYTES,BITS)				\
    431 static __inline void __BS_FUNCTION(bus_space_read_region,BYTES)		\
    432 	__P((bus_space_tag_t, bus_space_handle_t, bus_size_t,		\
    433 	__BS_TYPENAME(BITS) *, size_t));				\
    434 									\
    435 static __inline void							\
    436 __BS_FUNCTION(bus_space_read_region,BYTES)(t, h, o, a, c)		\
    437 	bus_space_tag_t t;						\
    438 	bus_space_handle_t h;						\
    439 	bus_size_t o;							\
    440 	__BS_TYPENAME(BITS) *a;						\
    441 	size_t c;							\
    442 {									\
    443 									\
    444 	while (c--) {							\
    445 		*a++ = __BS_FUNCTION(bus_space_read,BYTES)(t, h, o);	\
    446 		o += BYTES;						\
    447 	}								\
    448 }
    449 
    450 __bus_space_read_region(1,8)
    451 __bus_space_read_region(2,16)
    452 __bus_space_read_region(4,32)
    453 __bus_space_read_region(8,64)
    454 
    455 
    456 /*
    457  *	void bus_space_write_N __P((bus_space_tag_t tag,
    458  *	    bus_space_handle_t bsh, bus_size_t offset,
    459  *	    u_intN_t value));
    460  *
    461  * Write the 1, 2, 4, or 8 byte value `value' to bus space
    462  * described by tag/handle/offset.
    463  */
    464 
    465 #define __bus_space_write(BYTES,BITS)					\
    466 static __inline void							\
    467 __CONCAT(bus_space_write_,BYTES)(bus_space_tag_t bst,			\
    468     bus_space_handle_t bsh,						\
    469     bus_size_t offset, __CONCAT3(u_int,BITS,_t) data)			\
    470 {									\
    471 	*__BS_ADDR(bst, bsh, offset, BITS, BYTES) = data;		\
    472 	wbflush();							\
    473 }
    474 
    475 __bus_space_write(1,8)
    476 __bus_space_write(2,16)
    477 __bus_space_write(4,32)
    478 __bus_space_write(8,64)
    479 
    480 /*
    481  *	void bus_space_write_multi_N __P((bus_space_tag_t tag,
    482  *	    bus_space_handle_t bsh, bus_size_t offset,
    483  *	    const u_intN_t *addr, size_t count));
    484  *
    485  * Write `count' 1, 2, 4, or 8 byte quantities from the buffer
    486  * provided to bus space described by tag/handle/offset.
    487  */
    488 
    489 #define __bus_space_write_multi(BYTES,BITS)				\
    490 static __inline void __BS_FUNCTION(bus_space_write_multi,BYTES)       	\
    491 	__P((bus_space_tag_t, bus_space_handle_t, bus_size_t,		\
    492 	__BS_TYPENAME(BITS) *, size_t));				\
    493 									\
    494 static __inline void							\
    495 __BS_FUNCTION(bus_space_write_multi,BYTES)(t, h, o, a, c)     		\
    496 	bus_space_tag_t t;						\
    497 	bus_space_handle_t h;						\
    498 	bus_size_t o;							\
    499 	__BS_TYPENAME(BITS) *a;						\
    500 	size_t c;							\
    501 {									\
    502 									\
    503 	while (c--)							\
    504 		__BS_FUNCTION(bus_space_write,BYTES)(t, h, o, *a++);	\
    505 }
    506 
    507 __bus_space_write_multi(1,8)
    508 __bus_space_write_multi(2,16)
    509 __bus_space_write_multi(4,32)
    510 __bus_space_write_multi(8,64)
    511 
    512 
    513 /*
    514  *	void bus_space_write_region_N __P((bus_space_tag_t tag,
    515  *	    bus_space_handle_t bsh, bus_size_t offset,
    516  *	    const u_intN_t *addr, size_t count));
    517  *
    518  * Write `count' 1, 2, 4, or 8 byte quantities from the buffer provided
    519  * to bus space described by tag/handle starting at `offset'.
    520  */
    521 
    522 #define __bus_space_write_region(BYTES,BITS)				\
    523 static __inline void __BS_FUNCTION(bus_space_write_region,BYTES)      	\
    524 	__P((bus_space_tag_t, bus_space_handle_t, bus_size_t,		\
    525 	const __BS_TYPENAME(BITS) *, size_t));				\
    526 									\
    527 static __inline void							\
    528 __BS_FUNCTION(bus_space_write_region,BYTES)(t, h, o, a, c)    		\
    529 	bus_space_tag_t t;						\
    530 	bus_space_handle_t h;						\
    531 	bus_size_t o;							\
    532 	const __BS_TYPENAME(BITS) *a;					\
    533 	size_t c;							\
    534 {									\
    535 									\
    536 	while (c--) {							\
    537 		__BS_FUNCTION(bus_space_write,BYTES)(t, h, o, *a++);	\
    538 		o += BYTES;						\
    539 	}								\
    540 }
    541 
    542 __bus_space_write_region(1,8)
    543 __bus_space_write_region(2,16)
    544 __bus_space_write_region(4,32)
    545 __bus_space_write_region(8,64)
    546 
    547 
    548 /*
    549  *	void bus_space_set_multi_N __P((bus_space_tag_t tag,
    550  *	    bus_space_handle_t bsh, bus_size_t offset, u_intN_t val,
    551  *	    size_t count));
    552  *
    553  * Write the 1, 2, 4, or 8 byte value `val' to bus space described
    554  * by tag/handle/offset `count' times.
    555  */
    556 
    557 #define __bus_space_set_multi(BYTES,BITS)				\
    558 static __inline void __BS_FUNCTION(bus_space_set_multi,BYTES)		\
    559 	__P((bus_space_tag_t, bus_space_handle_t, bus_size_t,		\
    560 	__BS_TYPENAME(BITS), size_t));					\
    561 									\
    562 static __inline void							\
    563 __BS_FUNCTION(bus_space_set_multi,BYTES)(t, h, o, v, c)       		\
    564 	bus_space_tag_t t;						\
    565 	bus_space_handle_t h;						\
    566 	bus_size_t o;							\
    567 	__BS_TYPENAME(BITS) v;						\
    568 	size_t c;							\
    569 {									\
    570 									\
    571 	while (c--)							\
    572 		__BS_FUNCTION(bus_space_write,BYTES)(t, h, o, v);	\
    573 }
    574 
    575 __bus_space_set_multi(1,8)
    576 __bus_space_set_multi(2,16)
    577 __bus_space_set_multi(4,32)
    578 __bus_space_set_multi(8,64)
    579 
    580 
    581 /*
    582  *	void bus_space_set_region_N __P((bus_space_tag_t tag,
    583  *	    bus_space_handle_t bsh, bus_size_t offset, u_intN_t val,
    584  *	    size_t count));
    585  *
    586  * Write `count' 1, 2, 4, or 8 byte value `val' to bus space described
    587  * by tag/handle starting at `offset'.
    588  */
    589 
    590 #define __bus_space_set_region(BYTES,BITS)				\
    591 static __inline void __BS_FUNCTION(bus_space_set_region,BYTES)		\
    592 	__P((bus_space_tag_t, bus_space_handle_t, bus_size_t,		\
    593 	__BS_TYPENAME(BITS), size_t));					\
    594 									\
    595 static __inline void							\
    596 __BS_FUNCTION(bus_space_set_region,BYTES)(t, h, o, v, c)		\
    597 	bus_space_tag_t t;						\
    598 	bus_space_handle_t h;						\
    599 	bus_size_t o;							\
    600 	__BS_TYPENAME(BITS) v;						\
    601 	size_t c;							\
    602 {									\
    603 									\
    604 	while (c--) {							\
    605 		__BS_FUNCTION(bus_space_write,BYTES)(t, h, o, v);     	\
    606 		o += BYTES;						\
    607 	}								\
    608 }
    609 
    610 __bus_space_set_region(1,8)
    611 __bus_space_set_region(2,16)
    612 __bus_space_set_region(4,32)
    613 __bus_space_set_region(8,64)
    614 
    615 
    616 /*
    617  *	void bus_space_copy_region_N __P((bus_space_tag_t tag,
    618  *	    bus_space_handle_t bsh1, bus_size_t off1,
    619  *	    bus_space_handle_t bsh2, bus_size_t off2,
    620  *	    bus_size_t count));
    621  *
    622  * Copy `count' 1, 2, 4, or 8 byte values from bus space starting
    623  * at tag/bsh1/off1 to bus space starting at tag/bsh2/off2.
    624  */
    625 
    626 #define	__bus_space_copy_region(BYTES)					\
    627 static __inline void __BS_FUNCTION(bus_space_copy_region,BYTES)		\
    628 	__P((bus_space_tag_t,						\
    629 	    bus_space_handle_t bsh1, bus_size_t off1,			\
    630 	    bus_space_handle_t bsh2, bus_size_t off2,			\
    631 	    bus_size_t count));						\
    632 									\
    633 static __inline void							\
    634 __BS_FUNCTION(bus_space_copy_region,BYTES)(t, h1, o1, h2, o2, c)	\
    635 	bus_space_tag_t t;						\
    636 	bus_space_handle_t h1, h2;					\
    637 	bus_size_t o1, o2, c;						\
    638 {									\
    639 	bus_size_t o;							\
    640 									\
    641 	if ((h1 + o1) >= (h2 + o2)) {					\
    642 		/* src after dest: copy forward */			\
    643 		for (o = 0; c != 0; c--, o += BYTES)			\
    644 		   __BS_FUNCTION(bus_space_write,BYTES)(t, h2, o2 + o,	\
    645 		       __BS_FUNCTION(bus_space_read,BYTES)(t, h1, o1 + o)); \
    646 	} else {							\
    647 		/* dest after src: copy backwards */			\
    648 		for (o = (c - 1) * BYTES; c != 0; c--, o -= BYTES)	\
    649 		   __BS_FUNCTION(bus_space_write,BYTES)(t, h2, o2 + o,	\
    650 		       __BS_FUNCTION(bus_space_read,BYTES)(t, h1, o1 + o)); \
    651 	}								\
    652 }
    653 
    654 __bus_space_copy_region(1)
    655 __bus_space_copy_region(2)
    656 __bus_space_copy_region(4)
    657 __bus_space_copy_region(8)
    658 
    659 
    660 /*
    661  * Operations which handle byte stream data on word access.
    662  *
    663  * These functions are defined to resolve endian mismatch, by either
    664  * - When normal (i.e. stream-less) operations perform byte swap
    665  *   to resolve endian mismatch, these functions bypass the byte swap.
    666  * or
    667  * - When bus bridge performs automatic byte swap, these functions
    668  *   perform byte swap once more, to cancel the bridge's behavior.
    669  *
    670  * Mips Computer Systems platforms perform harware byte swapping -
    671  * therefore the streaming methods can byte swap as determined from
    672  * the bus space tag settings
    673  *
    674  */
    675 #define __BUS_SPACE_HAS_STREAM_METHODS
    676 
    677 /* Force creation of stream methods using the standard template macros */
    678 #undef  __BS_FUNCTION
    679 #define __BS_FUNCTION(func,BYTES)	__CONCAT3(func,_stream_,BYTES)
    680 
    681 #define __BS_BSWAP(bst, val, BITS)					\
    682 	((bst->bs_bswap) ? __CONCAT(bswap,BITS)(val) : (val))
    683 
    684 
    685 #define __bus_space_read_stream(BYTES,BITS)				\
    686 static __inline __BS_TYPENAME(BITS)					\
    687 __CONCAT(bus_space_read_stream_,BYTES)(bus_space_tag_t bst,		\
    688     bus_space_handle_t bsh, bus_size_t offset)				\
    689 {									\
    690 	register __BS_TYPENAME(BITS) val =				\
    691 		__CONCAT(bus_space_read_,BYTES)(bst, bsh, offset);	\
    692 									\
    693 	return __BS_BSWAP(bst, val, BITS);				\
    694 }
    695 
    696 __bus_space_read_stream(2, 16)		/* bus_space_read_stream_2 */
    697 __bus_space_read_stream(4, 32)		/* bus_space_read_stream_4 */
    698 __bus_space_read_stream(8, 64)		/* bus_space_read_stream_8 */
    699 
    700 
    701 #define __bus_space_write_stream(BYTES,BITS)				\
    702 static __inline void							\
    703 __CONCAT(bus_space_write_stream_,BYTES)(bus_space_tag_t bst,		\
    704     bus_space_handle_t bsh,						\
    705     bus_size_t offset, __CONCAT3(u_int,BITS,_t) data)			\
    706 {									\
    707 	*__BS_ADDR(bst, bsh, offset, BITS, BYTES) =			\
    708 		__BS_BSWAP(bst, data, BITS);				\
    709 	wbflush();							\
    710 }
    711 
    712 __bus_space_write_stream(2,16)		/* bus_space_write_stream_2 */
    713 __bus_space_write_stream(4,32)		/* bus_space_write_stream_4 */
    714 __bus_space_write_stream(8,64)		/* bus_space_write_stream_8 */
    715 
    716 __bus_space_read_multi(2,16)		/* bus_space_read_multi_stream_2 */
    717 __bus_space_read_multi(4,32)		/* bus_space_read_multi_stream_4 */
    718 __bus_space_read_multi(8,64)		/* bus_space_read_multi_stream_8 */
    719 
    720 __bus_space_read_region(2,16)		/* bus_space_read_region_stream_2 */
    721 __bus_space_read_region(4,32)		/* bus_space_read_region_stream_4 */
    722 __bus_space_read_region(8,64)		/* bus_space_read_region_stream_8 */
    723 
    724 __bus_space_write_multi(2,16)		/* bus_space_write_multi_stream_2 */
    725 __bus_space_write_multi(4,32)		/* bus_space_write_multi_stream_4 */
    726 __bus_space_write_multi(8,64)		/* bus_space_write_multi_stream_8 */
    727 
    728 __bus_space_write_region(2,16)		/* bus_space_write_region_stream_2 */
    729 __bus_space_write_region(4,32)		/* bus_space_write_region_stream_4 */
    730 __bus_space_write_region(8,64)		/* bus_space_write_region_stream_8 */
    731 
    732 __bus_space_set_multi(2,16)		/* bus_space_set_multi_stream_2 */
    733 __bus_space_set_multi(4,32)		/* bus_space_set_multi_stream_4 */
    734 __bus_space_set_multi(8,64)		/* bus_space_set_multi_stream_8 */
    735 
    736 __bus_space_set_region(2,16)		/* bus_space_set_region_stream_2 */
    737 __bus_space_set_region(4,32)		/* bus_space_set_region_stream_4 */
    738 __bus_space_set_region(8, 64)		/* bus_space_set_region_stream_8 */
    739 
    740 #undef __bus_space_read
    741 #undef __bus_space_write
    742 #undef __bus_space_read_stream
    743 #undef __bus_space_write_stream
    744 #undef __bus_space_read_multi
    745 #undef __bus_space_read_region
    746 #undef __bus_space_write_multi
    747 #undef __bus_space_write_region
    748 #undef __bus_space_set_multi
    749 #undef __bus_space_set_region
    750 #undef __bus_space_copy_region
    751 
    752 #undef __BS_TYPENAME
    753 #undef __BS_OFFSET
    754 #undef __BS_FUNCTION
    755 #undef __BS_ADDR
    756 
    757 /*
    758  * Bus read/write barrier methods.
    759  *
    760  *	void bus_space_barrier __P((bus_space_tag_t tag,
    761  *	    bus_space_handle_t bsh, bus_size_t offset,
    762  *	    bus_size_t len, int flags));
    763  *
    764  * On the MIPS, we just flush the write buffer.
    765  */
    766 #define bus_space_barrier(t, h, o, l, f)				\
    767 	((void)((void)(t), (void)(h), (void)(o), (void)(l), (void)(f)),	\
    768 	 wbflush())
    769 
    770 #define BUS_SPACE_BARRIER_READ	0x01
    771 #define BUS_SPACE_BARRIER_WRITE	0x02
    772 
    773 /*
    774  * Flags used in various bus DMA methods.
    775  */
    776 #define BUS_DMA_WAITOK		0x000	/* safe to sleep (pseudo-flag) */
    777 #define BUS_DMA_NOWAIT		0x001	/* not safe to sleep */
    778 #define BUS_DMA_ALLOCNOW	0x002	/* perform resource allocation now */
    779 #define BUS_DMA_COHERENT	0x004	/* hint: map memory DMA coherent */
    780 #define	BUS_DMA_STREAMING	0x008	/* hint: sequential, unidirectional */
    781 #define BUS_DMA_BUS1		0x010	/* placeholders for bus functions... */
    782 #define BUS_DMA_BUS2		0x020
    783 #define BUS_DMA_BUS3		0x040
    784 #define BUS_DMA_BUS4		0x080
    785 #define	BUS_DMA_READ		0x100	/* mapping is device -> memory only */
    786 #define	BUS_DMA_WRITE		0x200	/* mapping is memory -> device only */
    787 
    788 #define MIPSCO_DMAMAP_COHERENT	0x10000	/* no cache flush necessary on sync */
    789 
    790 /* Forwards needed by prototypes below. */
    791 struct mbuf;
    792 struct uio;
    793 
    794 /*
    795  * Operations performed by bus_dmamap_sync().
    796  */
    797 #define BUS_DMASYNC_PREREAD	0x01	/* pre-read synchronization */
    798 #define BUS_DMASYNC_POSTREAD	0x02	/* post-read synchronization */
    799 #define BUS_DMASYNC_PREWRITE	0x04	/* pre-write synchronization */
    800 #define BUS_DMASYNC_POSTWRITE	0x08	/* post-write synchronization */
    801 
    802 typedef struct mipsco_bus_dma_tag		*bus_dma_tag_t;
    803 typedef struct mipsco_bus_dmamap		*bus_dmamap_t;
    804 
    805 /*
    806  *	bus_dma_segment_t
    807  *
    808  *	Describes a single contiguous DMA transaction.  Values
    809  *	are suitable for programming into DMA registers.
    810  */
    811 struct mipsco_bus_dma_segment {
    812 	/*
    813 	 * PUBLIC MEMBERS: these are used by device drivers.
    814 	 */
    815 	bus_addr_t	ds_addr;	/* DMA address */
    816 	bus_size_t	ds_len;		/* length of transfer */
    817 	/*
    818 	 * PRIVATE MEMBERS for the DMA back-end.: not for use by drivers.
    819 	 */
    820 	vaddr_t		_ds_paddr;	/* CPU physical address */
    821 	vaddr_t		_ds_vaddr;	/* virtual address, 0 if invalid */
    822 };
    823 typedef struct mipsco_bus_dma_segment	bus_dma_segment_t;
    824 
    825 /*
    826  *	bus_dma_tag_t
    827  *
    828  *	A machine-dependent opaque type describing the implementation of
    829  *	DMA for a given bus.
    830  */
    831 
    832 struct mipsco_bus_dma_tag {
    833 	bus_addr_t	dma_offset;
    834 
    835 	/*
    836 	 * DMA mapping methods.
    837 	 */
    838 	int	(*_dmamap_create) __P((bus_dma_tag_t, bus_size_t, int,
    839 		    bus_size_t, bus_size_t, int, bus_dmamap_t *));
    840 	void	(*_dmamap_destroy) __P((bus_dma_tag_t, bus_dmamap_t));
    841 	int	(*_dmamap_load) __P((bus_dma_tag_t, bus_dmamap_t, void *,
    842 		    bus_size_t, struct proc *, int));
    843 	int	(*_dmamap_load_mbuf) __P((bus_dma_tag_t, bus_dmamap_t,
    844 		    struct mbuf *, int));
    845 	int	(*_dmamap_load_uio) __P((bus_dma_tag_t, bus_dmamap_t,
    846 		    struct uio *, int));
    847 	int	(*_dmamap_load_raw) __P((bus_dma_tag_t, bus_dmamap_t,
    848 		    bus_dma_segment_t *, int, bus_size_t, int));
    849 	void	(*_dmamap_unload) __P((bus_dma_tag_t, bus_dmamap_t));
    850 	void	(*_dmamap_sync) __P((bus_dma_tag_t, bus_dmamap_t,
    851 		    bus_addr_t, bus_size_t, int));
    852 
    853 	/*
    854 	 * DMA memory utility functions.
    855 	 */
    856 	int	(*_dmamem_alloc) __P((bus_dma_tag_t, bus_size_t, bus_size_t,
    857 		    bus_size_t, bus_dma_segment_t *, int, int *, int));
    858 	void	(*_dmamem_free) __P((bus_dma_tag_t,
    859 		    bus_dma_segment_t *, int));
    860 	int	(*_dmamem_map) __P((bus_dma_tag_t, bus_dma_segment_t *,
    861 		    int, size_t, caddr_t *, int));
    862 	void	(*_dmamem_unmap) __P((bus_dma_tag_t, caddr_t, size_t));
    863 	paddr_t	(*_dmamem_mmap) __P((bus_dma_tag_t, bus_dma_segment_t *,
    864 		    int, off_t, int, int));
    865 };
    866 
    867 #define bus_dmamap_create(t, s, n, m, b, f, p)			\
    868 	(*(t)->_dmamap_create)((t), (s), (n), (m), (b), (f), (p))
    869 #define bus_dmamap_destroy(t, p)				\
    870 	(*(t)->_dmamap_destroy)((t), (p))
    871 #define bus_dmamap_load(t, m, b, s, p, f)			\
    872 	(*(t)->_dmamap_load)((t), (m), (b), (s), (p), (f))
    873 #define bus_dmamap_load_mbuf(t, m, b, f)			\
    874 	(*(t)->_dmamap_load_mbuf)((t), (m), (b), (f))
    875 #define bus_dmamap_load_uio(t, m, u, f)				\
    876 	(*(t)->_dmamap_load_uio)((t), (m), (u), (f))
    877 #define bus_dmamap_load_raw(t, m, sg, n, s, f)			\
    878 	(*(t)->_dmamap_load_raw)((t), (m), (sg), (n), (s), (f))
    879 #define bus_dmamap_unload(t, p)					\
    880 	(*(t)->_dmamap_unload)((t), (p))
    881 #define bus_dmamap_sync(t, p, o, l, ops)			\
    882 	(*(t)->_dmamap_sync)((t), (p), (o), (l), (ops))
    883 #define bus_dmamem_alloc(t, s, a, b, sg, n, r, f)		\
    884 	(*(t)->_dmamem_alloc)((t), (s), (a), (b), (sg), (n), (r), (f))
    885 #define bus_dmamem_free(t, sg, n)				\
    886 	(*(t)->_dmamem_free)((t), (sg), (n))
    887 #define bus_dmamem_map(t, sg, n, s, k, f)			\
    888 	(*(t)->_dmamem_map)((t), (sg), (n), (s), (k), (f))
    889 #define bus_dmamem_unmap(t, k, s)				\
    890 	(*(t)->_dmamem_unmap)((t), (k), (s))
    891 #define bus_dmamem_mmap(t, sg, n, o, p, f)			\
    892 	(*(t)->_dmamem_mmap)((t), (sg), (n), (o), (p), (f))
    893 
    894 /*
    895  *	bus_dmamap_t
    896  *
    897  *	Describes a DMA mapping.
    898  */
    899 struct mipsco_bus_dmamap {
    900 	/*
    901 	 * PRIVATE MEMBERS: not for use by machine-independent code.
    902 	 */
    903 	bus_size_t	_dm_size;	/* largest DMA transfer mappable */
    904 	int		_dm_segcnt;	/* number of segs this map can map */
    905 	bus_size_t	_dm_maxsegsz;	/* largest possible segment */
    906 	bus_size_t	_dm_boundary;	/* don't cross this */
    907 	int		_dm_flags;	/* misc. flags */
    908 
    909 	/*
    910 	 * Private cookie to be used by the DMA back-end.
    911 	 */
    912 	void		*_dm_cookie;
    913 
    914 	/*
    915 	 * PUBLIC MEMBERS: these are used by machine-independent code.
    916 	 */
    917 	bus_size_t	dm_mapsize;	/* size of the mapping */
    918 	int		dm_nsegs;	/* # valid segments in mapping */
    919 	bus_dma_segment_t dm_segs[1];	/* segments; variable length */
    920 };
    921 
    922 #ifdef _MIPSCO_BUS_DMA_PRIVATE
    923 int	_bus_dmamap_create __P((bus_dma_tag_t, bus_size_t, int, bus_size_t,
    924 	    bus_size_t, int, bus_dmamap_t *));
    925 void	_bus_dmamap_destroy __P((bus_dma_tag_t, bus_dmamap_t));
    926 int	_bus_dmamap_load __P((bus_dma_tag_t, bus_dmamap_t, void *,
    927 	    bus_size_t, struct proc *, int));
    928 int	_bus_dmamap_load_mbuf __P((bus_dma_tag_t, bus_dmamap_t,
    929 	    struct mbuf *, int));
    930 int	_bus_dmamap_load_uio __P((bus_dma_tag_t, bus_dmamap_t,
    931 	    struct uio *, int));
    932 int	_bus_dmamap_load_raw __P((bus_dma_tag_t, bus_dmamap_t,
    933 	    bus_dma_segment_t *, int, bus_size_t, int));
    934 void	_bus_dmamap_unload __P((bus_dma_tag_t, bus_dmamap_t));
    935 void	_bus_dmamap_sync __P((bus_dma_tag_t, bus_dmamap_t, bus_addr_t,
    936 	    bus_size_t, int));
    937 
    938 int	_bus_dmamem_alloc __P((bus_dma_tag_t tag, bus_size_t size,
    939 	    bus_size_t alignment, bus_size_t boundary,
    940 	    bus_dma_segment_t *segs, int nsegs, int *rsegs, int flags));
    941 int	_bus_dmamem_alloc_range __P((bus_dma_tag_t tag, bus_size_t size,
    942 	    bus_size_t alignment, bus_size_t boundary,
    943 	    bus_dma_segment_t *segs, int nsegs, int *rsegs, int flags,
    944 	    paddr_t low, paddr_t high));
    945 void	_bus_dmamem_free __P((bus_dma_tag_t tag, bus_dma_segment_t *segs,
    946 	    int nsegs));
    947 int	_bus_dmamem_map __P((bus_dma_tag_t tag, bus_dma_segment_t *segs,
    948 	    int nsegs, size_t size, caddr_t *kvap, int flags));
    949 void	_bus_dmamem_unmap __P((bus_dma_tag_t tag, caddr_t kva,
    950 	    size_t size));
    951 paddr_t	_bus_dmamem_mmap __P((bus_dma_tag_t tag, bus_dma_segment_t *segs,
    952 	    int nsegs, off_t off, int prot, int flags));
    953 
    954 int	_bus_dmamem_alloc_range __P((bus_dma_tag_t tag, bus_size_t size,
    955 	    bus_size_t alignment, bus_size_t boundary,
    956 	    bus_dma_segment_t *segs, int nsegs, int *rsegs, int flags,
    957 	    paddr_t low, paddr_t high));
    958 #endif /* _MIPSCO_BUS_DMA_PRIVATE */
    959 
    960 void	_bus_dma_tag_init __P((bus_dma_tag_t tag));
    961 
    962 #endif /* _KERNEL */
    963 #endif /* _MIPSCO_BUS_H_ */
    964