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