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