xref: /netbsd-src/sys/arch/arc/include/bus_funcs.h (revision 6a493d6bc668897c91594964a732d38505b70cbb)
1 /*	$NetBSD: bus_funcs.h,v 1.1 2011/07/01 17:09:58 dyoung Exp $	*/
2 /*	NetBSD: bus.h,v 1.27 2000/03/15 16:44:50 drochner Exp 	*/
3 /*	$OpenBSD: bus.h,v 1.15 1999/08/11 23:15:21 niklas Exp $	*/
4 
5 /*-
6  * Copyright (c) 1996, 1997, 1998, 2001 The NetBSD Foundation, Inc.
7  * All rights reserved.
8  *
9  * This code is derived from software contributed to The NetBSD Foundation
10  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
11  * NASA Ames Research Center.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 /*
36  * Copyright (c) 1997 Per Fogelstrom.  All rights reserved.
37  * Copyright (c) 1996 Niklas Hallqvist.  All rights reserved.
38  *
39  * Redistribution and use in source and binary forms, with or without
40  * modification, are permitted provided that the following conditions
41  * are met:
42  * 1. Redistributions of source code must retain the above copyright
43  *    notice, this list of conditions and the following disclaimer.
44  * 2. Redistributions in binary form must reproduce the above copyright
45  *    notice, this list of conditions and the following disclaimer in the
46  *    documentation and/or other materials provided with the distribution.
47  * 3. All advertising materials mentioning features or use of this software
48  *    must display the following acknowledgement:
49  *      This product includes software developed by Christopher G. Demetriou
50  *	for the NetBSD Project.
51  * 4. The name of the author may not be used to endorse or promote products
52  *    derived from this software without specific prior written permission
53  *
54  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
55  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
56  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
57  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
58  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
59  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
60  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
61  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
62  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
63  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
64  */
65 
66 #ifndef _ARC_BUS_FUNCS_H_
67 #define _ARC_BUS_FUNCS_H_
68 #ifdef _KERNEL
69 
70 /*
71  * Utility macro; do not use outside this file.
72  */
73 #ifdef __STDC__
74 #define __CONCAT3(a,b,c)	a##b##c
75 #else
76 #define __CONCAT3(a,b,c)	a/**/b/**/c
77 #endif
78 
79 /* machine dependent utility function for bus_space users */
80 void	arc_bus_space_malloc_set_safe(void);
81 void	arc_bus_space_init(bus_space_tag_t, const char *,
82 	    paddr_t, vaddr_t, bus_addr_t, bus_size_t);
83 void	arc_bus_space_init_extent(bus_space_tag_t, void *, size_t);
84 void	arc_bus_space_set_aligned_stride(bus_space_tag_t, unsigned int);
85 void	arc_sparse_bus_space_init(bus_space_tag_t, const char *,
86 	    paddr_t, bus_addr_t, bus_size_t);
87 void	arc_large_bus_space_init(bus_space_tag_t, const char *,
88 	    paddr_t, bus_addr_t, bus_size_t);
89 
90 /* machine dependent utility function for bus_space implementations */
91 int	arc_bus_space_extent_malloc_flag(void);
92 
93 /* these are provided for subclasses which override base bus_space. */
94 
95 int	arc_bus_space_compose_handle(bus_space_tag_t,
96 	    bus_addr_t, bus_size_t, int, bus_space_handle_t *);
97 int	arc_bus_space_dispose_handle(bus_space_tag_t,
98 	    bus_space_handle_t, bus_size_t);
99 int	arc_bus_space_paddr(bus_space_tag_t,
100 	    bus_space_handle_t, paddr_t *);
101 
102 int	arc_sparse_bus_space_compose_handle(bus_space_tag_t,
103 	    bus_addr_t, bus_size_t, int, bus_space_handle_t *);
104 int	arc_sparse_bus_space_dispose_handle(bus_space_tag_t,
105 	    bus_space_handle_t, bus_size_t);
106 int	arc_sparse_bus_space_paddr(bus_space_tag_t,
107 	    bus_space_handle_t, paddr_t *);
108 
109 int	arc_bus_space_map(bus_space_tag_t, bus_addr_t, bus_size_t, int,
110 	    bus_space_handle_t *);
111 void	arc_bus_space_unmap(bus_space_tag_t, bus_space_handle_t,
112 	    bus_size_t);
113 int	arc_bus_space_subregion(bus_space_tag_t, bus_space_handle_t,
114 	    bus_size_t, bus_size_t, bus_space_handle_t *);
115 paddr_t	arc_bus_space_mmap(bus_space_tag_t, bus_addr_t, off_t, int, int);
116 int	arc_bus_space_alloc(bus_space_tag_t, bus_addr_t, bus_addr_t,
117 	    bus_size_t, bus_size_t, bus_size_t, int, bus_addr_t *,
118 	    bus_space_handle_t *);
119 #define arc_bus_space_free	arc_bus_space_unmap
120 
121 /*
122  *	int bus_space_compose_handle(bus_space_tag_t t, bus_addr_t addr,
123  *	    bus_size_t size, int flags, bus_space_handle_t *bshp);
124  *
125  * MACHINE DEPENDENT, NOT PORTABLE INTERFACE:
126  * Compose a bus_space handle from tag/handle/addr/size/flags.
127  * A helper function for bus_space_map()/bus_space_alloc() implementation.
128  */
129 #define bus_space_compose_handle(bst, addr, size, flags, bshp)		\
130 	(*(bst)->bs_compose_handle)(bst, addr, size, flags, bshp)
131 
132 /*
133  *	int bus_space_dispose_handle(bus_space_tag_t t, bus_addr_t addr,
134  *	    bus_space_handle_t bsh, bus_size_t size);
135  *
136  * MACHINE DEPENDENT, NOT PORTABLE INTERFACE:
137  * Dispose a bus_space handle.
138  * A helper function for bus_space_unmap()/bus_space_free() implementation.
139  */
140 #define bus_space_dispose_handle(bst, bsh, size)			\
141 	(*(bst)->bs_dispose_handle)(bst, bsh, size)
142 
143 /*
144  *	int bus_space_paddr(bus_space_tag_t tag,
145  *	    bus_space_handle_t bsh, paddr_t *pap);
146  *
147  * MACHINE DEPENDENT, NOT PORTABLE INTERFACE:
148  * (cannot be implemented on e.g. I/O space on i386, non-linear space on alpha)
149  * Return physical address of a region.
150  * A helper function for machine-dependent device mmap entry.
151  */
152 #define bus_space_paddr(bst, bsh, pap)					\
153 	(*(bst)->bs_paddr)(bst, bsh, pap)
154 
155 /*
156  *	void *bus_space_vaddr(bus_space_tag_t, bus_space_handle_t);
157  *
158  * Get the kernel virtual address for the mapped bus space.
159  * Only allowed for regions mapped with BUS_SPACE_MAP_LINEAR.
160  *  (XXX not enforced)
161  */
162 #define bus_space_vaddr(bst, bsh)					\
163 	((void *)(bsh))
164 
165 /*
166  *	paddr_t bus_space_mmap(bus_space_tag_t, bus_addr_t, off_t,
167  *	    int, int);
168  *
169  * Mmap bus space on behalf of the user.
170  */
171 #define	bus_space_mmap(bst, addr, off, prot, flags)			\
172 	(*(bst)->bs_mmap)((bst), (addr), (off), (prot), (flags))
173 
174 /*
175  *	int bus_space_map(bus_space_tag_t t, bus_addr_t addr,
176  *	    bus_size_t size, int flags, bus_space_handle_t *bshp);
177  *
178  * Map a region of bus space.
179  */
180 
181 #define bus_space_map(t, a, s, f, hp)					\
182 	(*(t)->bs_map)((t), (a), (s), (f), (hp))
183 
184 /*
185  *	void bus_space_unmap(bus_space_tag_t t,
186  *	    bus_space_handle_t bsh, bus_size_t size);
187  *
188  * Unmap a region of bus space.
189  */
190 
191 #define bus_space_unmap(t, h, s)					\
192 	(*(t)->bs_unmap)((t), (h), (s))
193 
194 /*
195  *	int bus_space_subregion(bus_space_tag_t t,
196  *	    bus_space_handle_t bsh, bus_size_t offset, bus_size_t size,
197  *	    bus_space_handle_t *nbshp);
198  *
199  * Get a new handle for a subregion of an already-mapped area of bus space.
200  */
201 
202 #define bus_space_subregion(t, h, o, s, hp)				\
203 	(*(t)->bs_subregion)((t), (h), (o), (s), (hp))
204 
205 /*
206  *	int bus_space_alloc(bus_space_tag_t t, bus_addr_t, rstart,
207  *	    bus_addr_t rend, bus_size_t size, bus_size_t align,
208  *	    bus_size_t boundary, int flags, bus_addr_t *addrp,
209  *	    bus_space_handle_t *bshp);
210  *
211  * Allocate a region of bus space.
212  */
213 
214 #define bus_space_alloc(t, rs, re, s, a, b, f, ap, hp)			\
215 	(*(t)->bs_alloc)((t), (rs), (re), (s), (a), (b), (f), (ap), (hp))
216 
217 /*
218  *	int bus_space_free(bus_space_tag_t t,
219  *	    bus_space_handle_t bsh, bus_size_t size);
220  *
221  * Free a region of bus space.
222  */
223 
224 #define bus_space_free(t, h, s)						\
225 	(*(t)->bs_free)((t), (h), (s))
226 
227 /*
228  * Operations which handle byte stream data on word access.
229  *
230  * These functions are defined to resolve endian mismatch, by either
231  * - When normal (i.e. stream-less) operations perform byte swap
232  *   to resolve endian mismatch, these functions bypass the byte swap.
233  * or
234  * - When bus bridge performs automatic byte swap, these functions
235  *   perform byte swap once more, to cancel the bridge's behavior.
236  *
237  * Currently these are just same as normal operations, since all
238  * supported buses are same endian with CPU (i.e. little-endian).
239  *
240  */
241 #define __BUS_SPACE_HAS_STREAM_METHODS
242 #define bus_space_read_stream_2(tag, bsh, offset)			\
243 	bus_space_read_2(tag, bsh, offset)
244 #define bus_space_read_stream_4(tag, bsh, offset)			\
245 	bus_space_read_4(tag, bsh, offset)
246 #define bus_space_read_stream_8(tag, bsh, offset)			\
247 	bus_space_read_8(tag, bsh, offset)
248 #define bus_space_read_multi_stream_2(tag, bsh, offset, datap, count)	\
249 	bus_space_read_multi_2(tag, bsh, offset, datap, count)
250 #define bus_space_read_multi_stream_4(tag, bsh, offset, datap, count)	\
251 	bus_space_read_multi_4(tag, bsh, offset, datap, count)
252 #define bus_space_read_multi_stream_8(tag, bsh, offset, datap, count)	\
253 	bus_space_read_multi_8(tag, bsh, offset, datap, count)
254 #define bus_space_read_region_stream_2(tag, bsh, offset, datap, count)	\
255 	bus_space_read_region_2(tag, bsh, offset, datap, count)
256 #define bus_space_read_region_stream_4(tag, bsh, offset, datap, count)	\
257 	bus_space_read_region_4(tag, bsh, offset, datap, count)
258 #define bus_space_read_region_stream_8(tag, bsh, offset, datap, count)	\
259 	bus_space_read_region_8(tag, bsh, offset, datap, count)
260 #define bus_space_write_stream_2(tag, bsh, offset, data)		\
261 	bus_space_write_2(tag, bsh, offset, data)
262 #define bus_space_write_stream_4(tag, bsh, offset, data)		\
263 	bus_space_write_4(tag, bsh, offset, data)
264 #define bus_space_write_stream_8(tag, bsh, offset, data)		\
265 	bus_space_write_8(tag, bsh, offset, data)
266 #define bus_space_write_multi_stream_2(tag, bsh, offset, datap, count)	\
267 	bus_space_write_multi_2(tag, bsh, offset, datap, count)
268 #define bus_space_write_multi_stream_4(tag, bsh, offset, datap, count)	\
269 	bus_space_write_multi_4(tag, bsh, offset, datap, count)
270 #define bus_space_write_multi_stream_8(tag, bsh, offset, datap, count)	\
271 	bus_space_write_multi_8(tag, bsh, offset, datap, count)
272 #define bus_space_write_region_stream_2(tag, bsh, offset, datap, count)	\
273 	bus_space_write_region_2(tag, bsh, offset, datap, count)
274 #define bus_space_write_region_stream_4(tag, bsh, offset, datap, count)	\
275 	bus_space_write_region_4(tag, bsh, offset, datap, count)
276 #define bus_space_write_region_stream_8(tag, bsh, offset, datap, count)	\
277 	bus_space_write_region_8(tag, bsh, offset, datap, count)
278 #define bus_space_write_region_stream_2(tag, bsh, offset, datap, count)	\
279 	bus_space_write_region_2(tag, bsh, offset, datap, count)
280 #define bus_space_write_region_stream_4(tag, bsh, offset, datap, count)	\
281 	bus_space_write_region_4(tag, bsh, offset, datap, count)
282 #define bus_space_write_region_stream_8(tag, bsh, offset, datap, count)	\
283 	bus_space_write_region_8(tag, bsh, offset, datap, count)
284 #define bus_space_set_multi_stream_2(tag, bsh, offset, data, count)	\
285 	bus_space_set_multi_2(tag, bsh, offset, data, count)
286 #define bus_space_set_multi_stream_4(tag, bsh, offset, data, count)	\
287 	bus_space_set_multi_4(tag, bsh, offset, data, count)
288 #define bus_space_set_multi_stream_8(tag, bsh, offset, data, count)	\
289 	bus_space_set_multi_8(tag, bsh, offset, data, count)
290 #define bus_space_set_region_stream_2(tag, bsh, offset, data, count)	\
291 	bus_space_set_region_2(tag, bsh, offset, data, count)
292 #define bus_space_set_region_stream_4(tag, bsh, offset, data, count)	\
293 	bus_space_set_region_4(tag, bsh, offset, data, count)
294 #define bus_space_set_region_stream_8(tag, bsh, offset, data, count)	\
295 	bus_space_set_region_8(tag, bsh, offset, data, count)
296 
297 /*
298  * Bus read/write barrier methods.
299  *
300  *	void bus_space_barrier(bus_space_tag_t tag,
301  *	    bus_space_handle_t bsh, bus_size_t offset,
302  *	    bus_size_t len, int flags);
303  *
304  * On the MIPS, we just flush the write buffer.
305  */
306 #define bus_space_barrier(t, h, o, l, f)				\
307 	((void)((void)(t), (void)(h), (void)(o), (void)(l), (void)(f),	\
308 	 wbflush()))
309 
310 /* Forwards needed by prototypes below. */
311 struct mbuf;
312 struct uio;
313 
314 #define bus_dmamap_create(t, s, n, m, b, f, p)			\
315 	(*(t)->_dmamap_create)((t), (s), (n), (m), (b), (f), (p))
316 #define bus_dmamap_destroy(t, p)				\
317 	(*(t)->_dmamap_destroy)((t), (p))
318 #define bus_dmamap_load(t, m, b, s, p, f)			\
319 	(*(t)->_dmamap_load)((t), (m), (b), (s), (p), (f))
320 #define bus_dmamap_load_mbuf(t, m, b, f)			\
321 	(*(t)->_dmamap_load_mbuf)((t), (m), (b), (f))
322 #define bus_dmamap_load_uio(t, m, u, f)				\
323 	(*(t)->_dmamap_load_uio)((t), (m), (u), (f))
324 #define bus_dmamap_load_raw(t, m, sg, n, s, f)			\
325 	(*(t)->_dmamap_load_raw)((t), (m), (sg), (n), (s), (f))
326 #define bus_dmamap_unload(t, p)					\
327 	(*(t)->_dmamap_unload)((t), (p))
328 #define bus_dmamap_sync(t, p, o, l, ops)			\
329 	(*(t)->_dmamap_sync)((t), (p), (o), (l), (ops))
330 #define bus_dmamem_alloc(t, s, a, b, sg, n, r, f)		\
331 	(*(t)->_dmamem_alloc)((t), (s), (a), (b), (sg), (n), (r), (f))
332 #define bus_dmamem_free(t, sg, n)				\
333 	(*(t)->_dmamem_free)((t), (sg), (n))
334 #define bus_dmamem_map(t, sg, n, s, k, f)			\
335 	(*(t)->_dmamem_map)((t), (sg), (n), (s), (k), (f))
336 #define bus_dmamem_unmap(t, k, s)				\
337 	(*(t)->_dmamem_unmap)((t), (k), (s))
338 #define bus_dmamem_mmap(t, sg, n, o, p, f)			\
339 	(*(t)->_dmamem_mmap)((t), (sg), (n), (o), (p), (f))
340 
341 #define bus_dmatag_subregion(t, mna, mxa, nt, f) EOPNOTSUPP
342 #define bus_dmatag_destroy(t)
343 
344 #ifdef _ARC_BUS_DMA_PRIVATE
345 int	_bus_dmamap_create(bus_dma_tag_t, bus_size_t, int, bus_size_t,
346 	    bus_size_t, int, bus_dmamap_t *);
347 void	_bus_dmamap_destroy(bus_dma_tag_t, bus_dmamap_t);
348 int	_bus_dmamap_load(bus_dma_tag_t, bus_dmamap_t, void *,
349 	    bus_size_t, struct proc *, int);
350 int	_bus_dmamap_load_mbuf(bus_dma_tag_t, bus_dmamap_t,
351 	    struct mbuf *, int);
352 int	_bus_dmamap_load_uio(bus_dma_tag_t, bus_dmamap_t,
353 	    struct uio *, int);
354 int	_bus_dmamap_load_raw(bus_dma_tag_t, bus_dmamap_t,
355 	    bus_dma_segment_t *, int, bus_size_t, int);
356 void	_bus_dmamap_unload(bus_dma_tag_t, bus_dmamap_t);
357 void	_bus_dmamap_sync(bus_dma_tag_t, bus_dmamap_t, bus_addr_t,
358 	    bus_size_t, int);
359 
360 int	_bus_dmamem_alloc(bus_dma_tag_t tag, bus_size_t size,
361 	    bus_size_t alignment, bus_size_t boundary,
362 	    bus_dma_segment_t *segs, int nsegs, int *rsegs, int flags);
363 int	_bus_dmamem_alloc_range(bus_dma_tag_t tag, bus_size_t size,
364 	    bus_size_t alignment, bus_size_t boundary,
365 	    bus_dma_segment_t *segs, int nsegs, int *rsegs, int flags,
366 	    paddr_t low, paddr_t high);
367 void	_bus_dmamem_free(bus_dma_tag_t tag, bus_dma_segment_t *segs,
368 	    int nsegs);
369 int	_bus_dmamem_map(bus_dma_tag_t tag, bus_dma_segment_t *segs,
370 	    int nsegs, size_t size, void **kvap, int flags);
371 void	_bus_dmamem_unmap(bus_dma_tag_t tag, void *kva,
372 	    size_t size);
373 paddr_t	_bus_dmamem_mmap(bus_dma_tag_t tag, bus_dma_segment_t *segs,
374 	    int nsegs, off_t off, int prot, int flags);
375 
376 int	_bus_dmamem_alloc_range(bus_dma_tag_t tag, bus_size_t size,
377 	    bus_size_t alignment, bus_size_t boundary,
378 	    bus_dma_segment_t *segs, int nsegs, int *rsegs, int flags,
379 	    paddr_t low, paddr_t high);
380 #endif /* _ARC_BUS_DMA_PRIVATE */
381 
382 void	_bus_dma_tag_init(bus_dma_tag_t tag);
383 void	jazz_bus_dma_tag_init(bus_dma_tag_t tag);
384 void	isadma_bounce_tag_init(bus_dma_tag_t tag);
385 
386 #endif /* _KERNEL */
387 #endif /* _ARC_BUS_FUNCS_H_ */
388