Home | History | Annotate | Line # | Download | only in include
bus.h revision 1.2
      1 /*	$NetBSD: bus.h,v 1.2 2000/08/15 04:56:45 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 	for (; count > 0; --count)					\
    461 		*p = *datap++;						\
    462 }
    463 
    464 bus_space_write_multi(1,8)
    465 bus_space_write_multi(2,16)
    466 bus_space_write_multi(4,32)
    467 bus_space_write_multi(8,64)
    468 
    469 /*
    470  *	void bus_space_write_region_N __P((bus_space_tag_t tag,
    471  *	    bus_space_handle_t bsh, bus_size_t offset,
    472  *	    const u_intN_t *addr, size_t count));
    473  *
    474  * Write `count' 1, 2, 4, or 8 byte quantities from the buffer provided
    475  * to bus space described by tag/handle starting at `offset'.
    476  */
    477 
    478 #define bus_space_write_region(BYTES,BITS)				\
    479 static __inline void							\
    480 __CONCAT(bus_space_write_region_,BYTES)(bus_space_tag_t bst,		\
    481     bus_space_handle_t bsh, bus_size_t offset,				\
    482     const __CONCAT3(u_int,BITS,_t) *datap, bus_size_t count)		\
    483 {									\
    484 	int stride = 1 << __CONCAT(bst->bs_stride_,BYTES);		\
    485 	volatile __CONCAT3(u_int,BITS,_t) *p =				\
    486 	    (volatile __CONCAT3(u_int,BITS,_t) *)			\
    487 	    (bsh + (offset << __CONCAT(bst->bs_stride_,BYTES)));	\
    488 									\
    489 	for (; count > 0; --count) {					\
    490 		*p = *datap++;						\
    491 		p += stride;						\
    492 	}								\
    493 }
    494 
    495 bus_space_write_region(1,8)
    496 bus_space_write_region(2,16)
    497 bus_space_write_region(4,32)
    498 bus_space_write_region(8,64)
    499 
    500 /*
    501  *	void bus_space_set_multi_N __P((bus_space_tag_t tag,
    502  *	    bus_space_handle_t bsh, bus_size_t offset, u_intN_t val,
    503  *	    size_t count));
    504  *
    505  * Write the 1, 2, 4, or 8 byte value `val' to bus space described
    506  * by tag/handle/offset `count' times.
    507  */
    508 
    509 #define bus_space_set_multi(BYTES,BITS)					\
    510 static __inline void							\
    511 __CONCAT(bus_space_set_multi_,BYTES)(bus_space_tag_t bst,		\
    512     bus_space_handle_t bsh, bus_size_t offset,				\
    513     const __CONCAT3(u_int,BITS,_t) data, bus_size_t count)		\
    514 {									\
    515 	volatile __CONCAT3(u_int,BITS,_t) *p =				\
    516 	    (volatile __CONCAT3(u_int,BITS,_t) *)			\
    517 	    (bsh + (offset << __CONCAT(bst->bs_stride_,BYTES)));	\
    518 									\
    519 	for (; count > 0; --count)					\
    520 		*p = data;						\
    521 }
    522 
    523 bus_space_set_multi(1,8)
    524 bus_space_set_multi(2,16)
    525 bus_space_set_multi(4,32)
    526 bus_space_set_multi(8,64)
    527 
    528 /*
    529  *	void bus_space_set_region_N __P((bus_space_tag_t tag,
    530  *	    bus_space_handle_t bsh, bus_size_t offset, u_intN_t val,
    531  *	    size_t count));
    532  *
    533  * Write `count' 1, 2, 4, or 8 byte value `val' to bus space described
    534  * by tag/handle starting at `offset'.
    535  */
    536 
    537 #define bus_space_set_region(BYTES,BITS)				\
    538 static __inline void							\
    539 __CONCAT(bus_space_set_region_,BYTES)(bus_space_tag_t bst,		\
    540     bus_space_handle_t bsh, bus_size_t offset,				\
    541     __CONCAT3(u_int,BITS,_t) data, bus_size_t count)			\
    542 {									\
    543 	int stride = 1 << __CONCAT(bst->bs_stride_,BYTES);		\
    544 	volatile __CONCAT3(u_int,BITS,_t) *p =				\
    545 	    (volatile __CONCAT3(u_int,BITS,_t) *)			\
    546 	    (bsh + (offset << __CONCAT(bst->bs_stride_,BYTES)));	\
    547 									\
    548 	for (; count > 0; --count) {					\
    549 		*p = data;						\
    550 		p += stride;						\
    551 	}								\
    552 }
    553 
    554 bus_space_set_region(1,8)
    555 bus_space_set_region(2,16)
    556 bus_space_set_region(4,32)
    557 bus_space_set_region(8,64)
    558 
    559 /*
    560  *	void bus_space_copy_region_N __P((bus_space_tag_t tag,
    561  *	    bus_space_handle_t bsh1, bus_size_t off1,
    562  *	    bus_space_handle_t bsh2, bus_size_t off2,
    563  *	    size_t count));
    564  *
    565  * Copy `count' 1, 2, 4, or 8 byte values from bus space starting
    566  * at tag/bsh1/off1 to bus space starting at tag/bsh2/off2.
    567  */
    568 
    569 #define bus_space_copy_region(BYTES,BITS)				\
    570 static __inline void							\
    571 __CONCAT(bus_space_copy_region_,BYTES)(bus_space_tag_t bst,		\
    572     bus_space_handle_t srcbsh, bus_size_t srcoffset,			\
    573     bus_space_handle_t dstbsh, bus_size_t dstoffset, bus_size_t count)	\
    574 {									\
    575 	int stride = 1 << __CONCAT(bst->bs_stride_,BYTES);		\
    576 	volatile __CONCAT3(u_int,BITS,_t) *srcp =			\
    577 	    (volatile __CONCAT3(u_int,BITS,_t) *)			\
    578 	    (srcbsh + (srcoffset << __CONCAT(bst->bs_stride_,BYTES)));	\
    579 	volatile __CONCAT3(u_int,BITS,_t) *dstp =			\
    580 	    (volatile __CONCAT3(u_int,BITS,_t) *)			\
    581 	    (dstbsh + (dstoffset << __CONCAT(bst->bs_stride_,BYTES)));	\
    582 	bus_size_t offset;						\
    583 									\
    584 	if (srcp >= dstp) {						\
    585 		/* src after dest: copy forward */			\
    586 		for (offset = 0; count > 0; --count, offset += stride)	\
    587 			dstp[offset] = srcp[offset];			\
    588 	} else {							\
    589 		/* dest after src: copy backward */			\
    590 		offset = (count << __CONCAT(bst->bs_stride_,BYTES))	\
    591 		    - stride;						\
    592 		for (; count > 0; --count, offset -= stride)		\
    593 			dstp[offset] = srcp[offset];			\
    594 	}								\
    595 }
    596 
    597 bus_space_copy_region(1,8)
    598 bus_space_copy_region(2,16)
    599 bus_space_copy_region(4,32)
    600 bus_space_copy_region(8,64)
    601 
    602 /*
    603  * Operations which handle byte stream data on word access.
    604  *
    605  * These functions are defined to resolve endian mismatch, by either
    606  * - When normal (i.e. stream-less) operations perform byte swap
    607  *   to resolve endian mismatch, these functions bypass the byte swap.
    608  * or
    609  * - When bus bridge performs automatic byte swap, these functions
    610  *   perform byte swap once more, to cancel the bridge's behavior.
    611  *
    612  * Currently these are just same as normal operations, since all
    613  * supported buses are same endian with CPU (i.e. little-endian).
    614  *
    615  */
    616 #define __BUS_SPACE_HAS_STREAM_METHODS
    617 #define bus_space_read_stream_2(tag, bsh, offset)			\
    618 	bus_space_read_2(tag, bsh, offset)
    619 #define bus_space_read_stream_4(tag, bsh, offset)			\
    620 	bus_space_read_4(tag, bsh, offset)
    621 #define bus_space_read_stream_8(tag, bsh, offset)			\
    622 	bus_space_read_8(tag, bsh, offset)
    623 #define bus_space_read_multi_stream_2(tag, bsh, offset, datap, count)	\
    624 	bus_space_read_multi_2(tag, bsh, offset, datap, count)
    625 #define bus_space_read_multi_stream_4(tag, bsh, offset, datap, count)	\
    626 	bus_space_read_multi_4(tag, bsh, offset, datap, count)
    627 #define bus_space_read_multi_stream_8(tag, bsh, offset, datap, count)	\
    628 	bus_space_read_multi_8(tag, bsh, offset, datap, count)
    629 #define bus_space_read_region_stream_2(tag, bsh, offset, datap, count)	\
    630 	bus_space_read_region_2(tag, bsh, offset, datap, count)
    631 #define bus_space_read_region_stream_4(tag, bsh, offset, datap, count)	\
    632 	bus_space_read_region_4(tag, bsh, offset, datap, count)
    633 #define bus_space_read_region_stream_8(tag, bsh, offset, datap, count)	\
    634 	bus_space_read_region_8(tag, bsh, offset, datap, count)
    635 #define bus_space_write_stream_2(tag, bsh, offset, data)		\
    636 	bus_space_write_2(tag, bsh, offset, data)
    637 #define bus_space_write_stream_4(tag, bsh, offset, data)		\
    638 	bus_space_write_4(tag, bsh, offset, data)
    639 #define bus_space_write_stream_8(tag, bsh, offset, data)		\
    640 	bus_space_write_8(tag, bsh, offset, data)
    641 #define bus_space_write_multi_stream_2(tag, bsh, offset, datap, count)	\
    642 	bus_space_write_multi_2(tag, bsh, offset, datap, count)
    643 #define bus_space_write_multi_stream_4(tag, bsh, offset, datap, count)	\
    644 	bus_space_write_multi_4(tag, bsh, offset, datap, count)
    645 #define bus_space_write_multi_stream_8(tag, bsh, offset, datap, count)	\
    646 	bus_space_write_multi_8(tag, bsh, offset, datap, count)
    647 #define bus_space_write_region_stream_2(tag, bsh, offset, datap, count)	\
    648 	bus_space_write_region_2(tag, bsh, offset, datap, count)
    649 #define bus_space_write_region_stream_4(tag, bsh, offset, datap, count)	\
    650 	bus_space_write_region_4(tag, bsh, offset, datap, count)
    651 #define bus_space_write_region_stream_8(tag, bsh, offset, datap, count)	\
    652 	bus_space_write_region_8(tag, bsh, offset, datap, count)
    653 #define bus_space_write_region_stream_2(tag, bsh, offset, datap, count)	\
    654 	bus_space_write_region_2(tag, bsh, offset, datap, count)
    655 #define bus_space_write_region_stream_4(tag, bsh, offset, datap, count)	\
    656 	bus_space_write_region_4(tag, bsh, offset, datap, count)
    657 #define bus_space_write_region_stream_8(tag, bsh, offset, datap, count)	\
    658 	bus_space_write_region_8(tag, bsh, offset, datap, count)
    659 #define bus_space_set_multi_stream_2(tag, bsh, offset, data, count)	\
    660 	bus_space_set_multi_2(tag, bsh, offset, data, count)
    661 #define bus_space_set_multi_stream_4(tag, bsh, offset, data, count)	\
    662 	bus_space_set_multi_4(tag, bsh, offset, data, count)
    663 #define bus_space_set_multi_stream_8(tag, bsh, offset, data, count)	\
    664 	bus_space_set_multi_8(tag, bsh, offset, data, count)
    665 #define bus_space_set_region_stream_2(tag, bsh, offset, data, count)	\
    666 	bus_space_set_region_2(tag, bsh, offset, data, count)
    667 #define bus_space_set_region_stream_4(tag, bsh, offset, data, count)	\
    668 	bus_space_set_region_4(tag, bsh, offset, data, count)
    669 #define bus_space_set_region_stream_8(tag, bsh, offset, data, count)	\
    670 	bus_space_set_region_8(tag, bsh, offset, data, count)
    671 
    672 /*
    673  * Bus read/write barrier methods.
    674  *
    675  *	void bus_space_barrier __P((bus_space_tag_t tag,
    676  *	    bus_space_handle_t bsh, bus_size_t offset,
    677  *	    bus_size_t len, int flags));
    678  *
    679  * On the MIPS, we just flush the write buffer.
    680  */
    681 #define bus_space_barrier(t, h, o, l, f)				\
    682 	((void)((void)(t), (void)(h), (void)(o), (void)(l), (void)(f)),	\
    683 	 wbflush())
    684 
    685 #define BUS_SPACE_BARRIER_READ	0x01
    686 #define BUS_SPACE_BARRIER_WRITE	0x02
    687 
    688 /*
    689  * Flags used in various bus DMA methods.
    690  */
    691 #define BUS_DMA_WAITOK		0x00	/* safe to sleep (pseudo-flag) */
    692 #define BUS_DMA_NOWAIT		0x01	/* not safe to sleep */
    693 #define BUS_DMA_ALLOCNOW	0x02	/* perform resource allocation now */
    694 #define BUS_DMA_COHERENT	0x04	/* hint: map memory DMA coherent */
    695 #define BUS_DMA_BUS1		0x10	/* placeholders for bus functions... */
    696 #define BUS_DMA_BUS2		0x20
    697 #define BUS_DMA_BUS3		0x40
    698 #define BUS_DMA_BUS4		0x80
    699 
    700 #define MIPSCO_DMAMAP_COHERENT	0x100	/* no cache flush necessary on sync */
    701 
    702 /* Forwards needed by prototypes below. */
    703 struct mbuf;
    704 struct uio;
    705 
    706 /*
    707  * Operations performed by bus_dmamap_sync().
    708  */
    709 #define BUS_DMASYNC_PREREAD	0x01	/* pre-read synchronization */
    710 #define BUS_DMASYNC_POSTREAD	0x02	/* post-read synchronization */
    711 #define BUS_DMASYNC_PREWRITE	0x04	/* pre-write synchronization */
    712 #define BUS_DMASYNC_POSTWRITE	0x08	/* post-write synchronization */
    713 
    714 typedef struct mipsco_bus_dma_tag		*bus_dma_tag_t;
    715 typedef struct mipsco_bus_dmamap		*bus_dmamap_t;
    716 
    717 /*
    718  *	bus_dma_segment_t
    719  *
    720  *	Describes a single contiguous DMA transaction.  Values
    721  *	are suitable for programming into DMA registers.
    722  */
    723 struct mipsco_bus_dma_segment {
    724 	/*
    725 	 * PUBLIC MEMBERS: these are used by device drivers.
    726 	 */
    727 	bus_addr_t	ds_addr;	/* DMA address */
    728 	bus_size_t	ds_len;		/* length of transfer */
    729 	/*
    730 	 * PRIVATE MEMBERS for the DMA back-end.: not for use by drivers.
    731 	 */
    732 	vaddr_t		_ds_paddr;	/* CPU physical address */
    733 	vaddr_t		_ds_vaddr;	/* virtual address, 0 if invalid */
    734 };
    735 typedef struct mipsco_bus_dma_segment	bus_dma_segment_t;
    736 
    737 /*
    738  *	bus_dma_tag_t
    739  *
    740  *	A machine-dependent opaque type describing the implementation of
    741  *	DMA for a given bus.
    742  */
    743 
    744 struct mipsco_bus_dma_tag {
    745 	bus_addr_t	dma_offset;
    746 
    747 	/*
    748 	 * DMA mapping methods.
    749 	 */
    750 	int	(*_dmamap_create) __P((bus_dma_tag_t, bus_size_t, int,
    751 		    bus_size_t, bus_size_t, int, bus_dmamap_t *));
    752 	void	(*_dmamap_destroy) __P((bus_dma_tag_t, bus_dmamap_t));
    753 	int	(*_dmamap_load) __P((bus_dma_tag_t, bus_dmamap_t, void *,
    754 		    bus_size_t, struct proc *, int));
    755 	int	(*_dmamap_load_mbuf) __P((bus_dma_tag_t, bus_dmamap_t,
    756 		    struct mbuf *, int));
    757 	int	(*_dmamap_load_uio) __P((bus_dma_tag_t, bus_dmamap_t,
    758 		    struct uio *, int));
    759 	int	(*_dmamap_load_raw) __P((bus_dma_tag_t, bus_dmamap_t,
    760 		    bus_dma_segment_t *, int, bus_size_t, int));
    761 	void	(*_dmamap_unload) __P((bus_dma_tag_t, bus_dmamap_t));
    762 	void	(*_dmamap_sync) __P((bus_dma_tag_t, bus_dmamap_t,
    763 		    bus_addr_t, bus_size_t, int));
    764 
    765 	/*
    766 	 * DMA memory utility functions.
    767 	 */
    768 	int	(*_dmamem_alloc) __P((bus_dma_tag_t, bus_size_t, bus_size_t,
    769 		    bus_size_t, bus_dma_segment_t *, int, int *, int));
    770 	void	(*_dmamem_free) __P((bus_dma_tag_t,
    771 		    bus_dma_segment_t *, int));
    772 	int	(*_dmamem_map) __P((bus_dma_tag_t, bus_dma_segment_t *,
    773 		    int, size_t, caddr_t *, int));
    774 	void	(*_dmamem_unmap) __P((bus_dma_tag_t, caddr_t, size_t));
    775 	paddr_t	(*_dmamem_mmap) __P((bus_dma_tag_t, bus_dma_segment_t *,
    776 		    int, off_t, int, int));
    777 };
    778 
    779 #define bus_dmamap_create(t, s, n, m, b, f, p)			\
    780 	(*(t)->_dmamap_create)((t), (s), (n), (m), (b), (f), (p))
    781 #define bus_dmamap_destroy(t, p)				\
    782 	(*(t)->_dmamap_destroy)((t), (p))
    783 #define bus_dmamap_load(t, m, b, s, p, f)			\
    784 	(*(t)->_dmamap_load)((t), (m), (b), (s), (p), (f))
    785 #define bus_dmamap_load_mbuf(t, m, b, f)			\
    786 	(*(t)->_dmamap_load_mbuf)((t), (m), (b), (f))
    787 #define bus_dmamap_load_uio(t, m, u, f)				\
    788 	(*(t)->_dmamap_load_uio)((t), (m), (u), (f))
    789 #define bus_dmamap_load_raw(t, m, sg, n, s, f)			\
    790 	(*(t)->_dmamap_load_raw)((t), (m), (sg), (n), (s), (f))
    791 #define bus_dmamap_unload(t, p)					\
    792 	(*(t)->_dmamap_unload)((t), (p))
    793 #define bus_dmamap_sync(t, p, o, l, ops)			\
    794 	(*(t)->_dmamap_sync)((t), (p), (o), (l), (ops))
    795 #define bus_dmamem_alloc(t, s, a, b, sg, n, r, f)		\
    796 	(*(t)->_dmamem_alloc)((t), (s), (a), (b), (sg), (n), (r), (f))
    797 #define bus_dmamem_free(t, sg, n)				\
    798 	(*(t)->_dmamem_free)((t), (sg), (n))
    799 #define bus_dmamem_map(t, sg, n, s, k, f)			\
    800 	(*(t)->_dmamem_map)((t), (sg), (n), (s), (k), (f))
    801 #define bus_dmamem_unmap(t, k, s)				\
    802 	(*(t)->_dmamem_unmap)((t), (k), (s))
    803 #define bus_dmamem_mmap(t, sg, n, o, p, f)			\
    804 	(*(t)->_dmamem_mmap)((t), (sg), (n), (o), (p), (f))
    805 
    806 /*
    807  *	bus_dmamap_t
    808  *
    809  *	Describes a DMA mapping.
    810  */
    811 struct mipsco_bus_dmamap {
    812 	/*
    813 	 * PRIVATE MEMBERS: not for use by machine-independent code.
    814 	 */
    815 	bus_size_t	_dm_size;	/* largest DMA transfer mappable */
    816 	int		_dm_segcnt;	/* number of segs this map can map */
    817 	bus_size_t	_dm_maxsegsz;	/* largest possible segment */
    818 	bus_size_t	_dm_boundary;	/* don't cross this */
    819 	int		_dm_flags;	/* misc. flags */
    820 
    821 	/*
    822 	 * Private cookie to be used by the DMA back-end.
    823 	 */
    824 	void		*_dm_cookie;
    825 
    826 	/*
    827 	 * PUBLIC MEMBERS: these are used by machine-independent code.
    828 	 */
    829 	bus_size_t	dm_mapsize;	/* size of the mapping */
    830 	int		dm_nsegs;	/* # valid segments in mapping */
    831 	bus_dma_segment_t dm_segs[1];	/* segments; variable length */
    832 };
    833 
    834 #ifdef _MIPSCO_BUS_DMA_PRIVATE
    835 int	_bus_dmamap_create __P((bus_dma_tag_t, bus_size_t, int, bus_size_t,
    836 	    bus_size_t, int, bus_dmamap_t *));
    837 void	_bus_dmamap_destroy __P((bus_dma_tag_t, bus_dmamap_t));
    838 int	_bus_dmamap_load __P((bus_dma_tag_t, bus_dmamap_t, void *,
    839 	    bus_size_t, struct proc *, int));
    840 int	_bus_dmamap_load_mbuf __P((bus_dma_tag_t, bus_dmamap_t,
    841 	    struct mbuf *, int));
    842 int	_bus_dmamap_load_uio __P((bus_dma_tag_t, bus_dmamap_t,
    843 	    struct uio *, int));
    844 int	_bus_dmamap_load_raw __P((bus_dma_tag_t, bus_dmamap_t,
    845 	    bus_dma_segment_t *, int, bus_size_t, int));
    846 void	_bus_dmamap_unload __P((bus_dma_tag_t, bus_dmamap_t));
    847 void	_mips1_bus_dmamap_sync __P((bus_dma_tag_t, bus_dmamap_t, bus_addr_t,
    848 	    bus_size_t, int));
    849 void	_mips3_bus_dmamap_sync __P((bus_dma_tag_t, bus_dmamap_t, bus_addr_t,
    850 	    bus_size_t, int));
    851 
    852 int	_bus_dmamem_alloc __P((bus_dma_tag_t tag, bus_size_t size,
    853 	    bus_size_t alignment, bus_size_t boundary,
    854 	    bus_dma_segment_t *segs, int nsegs, int *rsegs, int flags));
    855 int	_bus_dmamem_alloc_range __P((bus_dma_tag_t tag, bus_size_t size,
    856 	    bus_size_t alignment, bus_size_t boundary,
    857 	    bus_dma_segment_t *segs, int nsegs, int *rsegs, int flags,
    858 	    paddr_t low, paddr_t high));
    859 void	_bus_dmamem_free __P((bus_dma_tag_t tag, bus_dma_segment_t *segs,
    860 	    int nsegs));
    861 int	_bus_dmamem_map __P((bus_dma_tag_t tag, bus_dma_segment_t *segs,
    862 	    int nsegs, size_t size, caddr_t *kvap, int flags));
    863 void	_bus_dmamem_unmap __P((bus_dma_tag_t tag, caddr_t kva,
    864 	    size_t size));
    865 paddr_t	_bus_dmamem_mmap __P((bus_dma_tag_t tag, bus_dma_segment_t *segs,
    866 	    int nsegs, off_t off, int prot, int flags));
    867 
    868 int	_bus_dmamem_alloc_range __P((bus_dma_tag_t tag, bus_size_t size,
    869 	    bus_size_t alignment, bus_size_t boundary,
    870 	    bus_dma_segment_t *segs, int nsegs, int *rsegs, int flags,
    871 	    paddr_t low, paddr_t high));
    872 #endif /* _MIPSCO_BUS_DMA_PRIVATE */
    873 
    874 void	_bus_dma_tag_init __P((bus_dma_tag_t tag));
    875 void	jazz_bus_dma_tag_init __P((bus_dma_tag_t tag));
    876 void	isadma_bounce_tag_init __P((bus_dma_tag_t tag));
    877 
    878 #endif /* _KERNEL */
    879 #endif /* _MIPSCO_BUS_H_ */
    880