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