1/*	$NetBSD: bus_funcs.h,v 1.1 2011/07/01 17:10:01 dyoung Exp $	*/
2
3/*-
4 * Copyright (c) 1996, 1997, 1998, 2001 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33/*
34 * Copyright (c) 1997-1999, 2001 Eduardo E. Horvath. All rights reserved.
35 * Copyright (c) 1996 Charles M. Hannum.  All rights reserved.
36 * Copyright (c) 1996 Christopher G. Demetriou.  All rights reserved.
37 *
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
41 * 1. Redistributions of source code must retain the above copyright
42 *    notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 *    notice, this list of conditions and the following disclaimer in the
45 *    documentation and/or other materials provided with the distribution.
46 * 3. All advertising materials mentioning features or use of this software
47 *    must display the following acknowledgement:
48 *      This product includes software developed by Christopher G. Demetriou
49 *	for the NetBSD Project.
50 * 4. The name of the author may not be used to endorse or promote products
51 *    derived from this software without specific prior written permission
52 *
53 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
54 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
55 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
56 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
57 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
58 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
59 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
60 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
61 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
62 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
63 */
64
65#ifndef _SPARC64_BUS_FUNCS_H_
66#define _SPARC64_BUS_FUNCS_H_
67
68/*
69 * Debug hooks
70 */
71
72extern int bus_space_debug;
73
74bus_space_tag_t bus_space_tag_alloc(bus_space_tag_t, void *);
75int		bus_space_translate_address_generic(struct openprom_range *,
76						    int, bus_addr_t *);
77
78#if 0
79/*
80 * The following macro could be used to generate the bus_space*() functions
81 * but it uses a gcc extension and is ANSI-only.
82#define PROTO_bus_space_xxx		(bus_space_tag_t t, ...)
83#define RETURNTYPE_bus_space_xxx	void *
84#define BUSFUN(name, returntype, t, args...)			\
85	static __inline RETURNTYPE_##name			\
86	bus_##name PROTO_##name					\
87	{							\
88		while (t->sparc_##name == NULL)			\
89			t = t->parent;				\
90		return (*(t)->sparc_##name)(t, args);		\
91	}
92 */
93#endif
94
95/*
96 * Bus space function prototypes.
97 */
98static void	*bus_intr_establish(
99				bus_space_tag_t,
100				int,			/*bus-specific intr*/
101				int,			/*device class level,
102							  see machine/intr.h*/
103				int (*)(void *),	/*handler*/
104				void *);		/*handler arg*/
105
106
107/* This macro finds the first "upstream" implementation of method `f' */
108#define _BS_CALL(t,f)			\
109	while (t->f == NULL)		\
110		t = t->parent;		\
111	return (*(t)->f)
112
113#define _BS_VOID_CALL(t,f)			\
114	while (t->f == NULL)		\
115		t = t->parent;		\
116	(*(t)->f)
117
118static __inline void *
119bus_intr_establish(bus_space_tag_t t, int p, int l, int	(*h)(void *), void *a)
120{
121	_BS_CALL(t, sparc_intr_establish)(t, p, l, h, a, NULL);
122}
123
124/* XXXX Things get complicated if we use unmapped register accesses. */
125#define	bus_space_vaddr(t, h)	(PHYS_ASI((h)._asi) ? \
126			NULL : (void *)(vaddr_t)((h)._ptr))
127
128/*
129 *	uintN_t bus_space_read_N(bus_space_tag_t tag,
130 *	    bus_space_handle_t bsh, bus_size_t offset);
131 *
132 * Read a 1, 2, 4, or 8 byte quantity from bus space
133 * described by tag/handle/offset.
134 */
135#ifndef BUS_SPACE_DEBUG
136#define	bus_space_read_1(t, h, o)					\
137	    (0 ? (t)->type : lduba((h)._ptr + (o), (h)._asi))
138
139#define	bus_space_read_2(t, h, o)					\
140	    (0 ? (t)->type : lduha((h)._ptr + (o), (h)._asi))
141
142#define	bus_space_read_4(t, h, o)					\
143	    (0 ? (t)->type : lda((h)._ptr + (o), (h)._asi))
144
145#define	bus_space_read_8(t, h, o)					\
146	    (0 ? (t)->type : ldxa((h)._ptr + (o), (h)._asi))
147#else
148#define	bus_space_read_1(t, h, o) ({					\
149	uint8_t __bv =							\
150	    lduba((h)._ptr + (o), (h)._asi);				\
151	if (bus_space_debug & BSDB_ACCESS)				\
152	printf("bsr1(%llx + %llx, %x) -> %x\n", (long long)(h)._ptr,	\
153		(long long)(o),						\
154		(h)._asi, (uint32_t) __bv);				\
155	__bv; })
156
157#define	bus_space_read_2(t, h, o) ({					\
158	uint16_t __bv =							\
159	    lduha((h)._ptr + (o), (h)._asi);				\
160	if (bus_space_debug & BSDB_ACCESS)				\
161	printf("bsr2(%llx + %llx, %x) -> %x\n", (long long)(h)._ptr,	\
162		(long long)(o),						\
163		(h)._asi, (uint32_t)__bv);				\
164	__bv; })
165
166#define	bus_space_read_4(t, h, o) ({					\
167	uint32_t __bv =							\
168	    lda((h)._ptr + (o), (h)._asi);				\
169	if (bus_space_debug & BSDB_ACCESS)				\
170	printf("bsr4(%llx + %llx, %x) -> %x\n", (long long)(h)._ptr,	\
171		(long long)(o),						\
172		(h)._asi, __bv);					\
173	__bv; })
174
175#define	bus_space_read_8(t, h, o) ({					\
176	uint64_t __bv =							\
177	    ldxa((h)._ptr + (o), (h)._asi);				\
178	if (bus_space_debug & BSDB_ACCESS)				\
179	printf("bsr8(%llx + %llx, %x) -> %llx\n", (long long)(h)._ptr,	\
180		(long long)(o),						\
181		(h)._asi, (long long)__bv);				\
182	__bv; })
183#endif
184/*
185 *	void bus_space_write_N(bus_space_tag_t tag,
186 *	    bus_space_handle_t bsh, bus_size_t offset,
187 *	    uintN_t value);
188 *
189 * Write the 1, 2, 4, or 8 byte value `value' to bus space
190 * described by tag/handle/offset.
191 */
192#ifndef BUS_SPACE_DEBUG
193#define	bus_space_write_1(t, h, o, v)					\
194	(0 ? (t)->type : ((void)(stba((h)._ptr + (o), (h)._asi, (v)))))
195
196#define	bus_space_write_2(t, h, o, v)					\
197	(0 ? (t)->type : ((void)(stha((h)._ptr + (o), (h)._asi, (v)))))
198
199#define	bus_space_write_4(t, h, o, v)					\
200	(0 ? (t)->type : ((void)(sta((h)._ptr + (o), (h)._asi, (v)))))
201
202#define	bus_space_write_8(t, h, o, v)					\
203	(0 ? (t)->type : ((void)(stxa((h)._ptr + (o), (h)._asi, (v)))))
204#else
205#define	bus_space_write_1(t, h, o, v) ({				\
206	if (bus_space_debug & BSDB_ACCESS)				\
207	printf("bsw1(%llx + %llx, %x) <- %x\n", (long long)(h)._ptr,	\
208		(long long)(o),						\
209		(h)._asi, (uint32_t) v);				\
210	((void)(stba((h)._ptr + (o), (h)._asi, (v)))); })
211
212#define	bus_space_write_2(t, h, o, v) ({				\
213	if (bus_space_debug & BSDB_ACCESS)				\
214	printf("bsw2(%llx + %llx, %x) <- %x\n", (long long)(h)._ptr,	\
215		(long long)(o),						\
216		(h)._asi, (uint32_t) v);				\
217	((void)(stha((h)._ptr + (o), (h)._asi, (v)))); })
218
219#define	bus_space_write_4(t, h, o, v) ({				\
220	if (bus_space_debug & BSDB_ACCESS)				\
221	printf("bsw4(%llx + %llx, %x) <- %x\n", (long long)(h)._ptr,	\
222		(long long)(o),						\
223		(h)._asi, (uint32_t) v);				\
224	((void)(sta((h)._ptr + (o), (h)._asi, (v)))); })
225
226#define	bus_space_write_8(t, h, o, v) ({				\
227	if (bus_space_debug & BSDB_ACCESS)				\
228	printf("bsw8(%llx + %llx, %x) <- %llx\n", (long long)(h)._ptr,	\
229		(long long)(o),						\
230		(h)._asi, (long long) v);				\
231	((void)(stxa((h)._ptr + (o), (h)._asi, (v)))); })
232#endif
233/*
234 *	uintN_t bus_space_read_stream_N(bus_space_tag_t tag,
235 *	    bus_space_handle_t bsh, bus_size_t offset);
236 *
237 * Read a 1, 2, 4, or 8 byte quantity from bus space
238 * described by tag/handle/offset.
239 */
240#ifndef BUS_SPACE_DEBUG
241#define	bus_space_read_stream_1(t, h, o)				\
242	    (0 ? (t)->type : lduba((h)._ptr + (o), (h)._sasi))
243
244#define	bus_space_read_stream_2(t, h, o)				\
245	    (0 ? (t)->type : lduha((h)._ptr + (o), (h)._sasi))
246
247#define	bus_space_read_stream_4(t, h, o)				\
248	    (0 ? (t)->type : lda((h)._ptr + (o), (h)._sasi))
249
250#define	bus_space_read_stream_8(t, h, o)				\
251	    (0 ? (t)->type : ldxa((h)._ptr + (o), (h)._sasi))
252#else
253#define	bus_space_read_stream_1(t, h, o) ({				\
254	uint8_t __bv =							\
255	    lduba((h)._ptr + (o), (h)._sasi);				\
256	if (bus_space_debug & BSDB_ACCESS)				\
257	printf("bsr1(%llx + %llx, %x) -> %x\n", (long long)(h)._ptr,	\
258		(long long)(o),						\
259		(h)._sasi, (uint32_t) __bv);				\
260	__bv; })
261
262#define	bus_space_read_stream_2(t, h, o) ({				\
263	uint16_t __bv =							\
264	    lduha((h)._ptr + (o), (h)._sasi);				\
265	if (bus_space_debug & BSDB_ACCESS)				\
266	printf("bsr2(%llx + %llx, %x) -> %x\n", (long long)(h)._ptr,	\
267		(long long)(o),						\
268		(h)._sasi, (uint32_t)__bv);				\
269	__bv; })
270
271#define	bus_space_read_stream_4(t, h, o) ({				\
272	uint32_t __bv =							\
273	    lda((h)._ptr + (o), (h)._sasi);				\
274	if (bus_space_debug & BSDB_ACCESS)				\
275	printf("bsr4(%llx + %llx, %x) -> %x\n", (long long)(h)._ptr,	\
276		(long long)(o),						\
277		(h)._sasi, __bv);					\
278	__bv; })
279
280#define	bus_space_read_stream_8(t, h, o) ({				\
281	uint64_t __bv =							\
282	    ldxa((h)._ptr + (o), (h)._sasi);				\
283	if (bus_space_debug & BSDB_ACCESS)				\
284	printf("bsr8(%llx + %llx, %x) -> %llx\n", (long long)(h)._ptr,	\
285		(long long)(o),						\
286		(h)._sasi, (long long)__bv);				\
287	__bv; })
288#endif
289/*
290 *	void bus_space_write_stream_N(bus_space_tag_t tag,
291 *	    bus_space_handle_t bsh, bus_size_t offset,
292 *	    uintN_t value);
293 *
294 * Write the 1, 2, 4, or 8 byte value `value' to bus space
295 * described by tag/handle/offset.
296 */
297#ifndef BUS_SPACE_DEBUG
298#define	bus_space_write_stream_1(t, h, o, v)				\
299	(0 ? (t)->type : ((void)(stba((h)._ptr + (o), (h)._sasi, (v)))))
300
301#define	bus_space_write_stream_2(t, h, o, v)				\
302	(0 ? (t)->type : ((void)(stha((h)._ptr + (o), (h)._sasi, (v)))))
303
304#define	bus_space_write_stream_4(t, h, o, v)				\
305	(0 ? (t)->type : ((void)(sta((h)._ptr + (o), (h)._sasi, (v)))))
306
307#define	bus_space_write_stream_8(t, h, o, v)				\
308	(0 ? (t)->type : ((void)(stxa((h)._ptr + (o), (h)._sasi, (v)))))
309#else
310#define	bus_space_write_stream_1(t, h, o, v) ({				\
311	if (bus_space_debug & BSDB_ACCESS)				\
312	printf("bsw1(%llx + %llx, %x) <- %x\n", (long long)(h)._ptr,	\
313		(long long)(o),						\
314		(h)._sasi, (uint32_t) v);				\
315	((void)(stba((h)._ptr + (o), (h)._sasi, (v)))); })
316
317#define	bus_space_write_stream_2(t, h, o, v) ({				\
318	if (bus_space_debug & BSDB_ACCESS)				\
319	printf("bsw2(%llx + %llx, %x) <- %x\n", (long long)(h)._ptr,	\
320		(long long)(o),						\
321		(h)._sasi, (uint32_t) v);				\
322	((void)(stha((h)._ptr + (o), (h)._sasi, (v)))); })
323
324#define	bus_space_write_stream_4(t, h, o, v) ({				\
325	if (bus_space_debug & BSDB_ACCESS)				\
326	printf("bsw4(%llx + %llx, %x) <- %x\n", (long long)(h)._ptr,	\
327		(long long)(o),						\
328		(h)._sasi, (uint32_t) v);				\
329	((void)(sta((h)._ptr + (o), (h)._sasi, (v)))); })
330
331#define	bus_space_write_stream_8(t, h, o, v) ({				\
332	if (bus_space_debug & BSDB_ACCESS)				\
333	printf("bsw8(%llx + %llx, %x) <- %llx\n", (long long)(h)._ptr,	\
334		(long long)(o),						\
335		(h)._sasi, (long long) v);				\
336	((void)(stxa((h)._ptr + (o), (h)._sasi, (v)))); })
337#endif
338/* Forwards needed by prototypes below. */
339struct mbuf;
340struct uio;
341
342#define	bus_dmamap_create(t, s, n, m, b, f, p)			\
343	(*(t)->_dmamap_create)((t), (s), (n), (m), (b), (f), (p))
344#define	bus_dmamap_destroy(t, p)				\
345	(*(t)->_dmamap_destroy)((t), (p))
346#define	bus_dmamap_load(t, m, b, s, p, f)			\
347	(*(t)->_dmamap_load)((t), (m), (b), (s), (p), (f))
348#define	bus_dmamap_load_mbuf(t, m, b, f)			\
349	(*(t)->_dmamap_load_mbuf)((t), (m), (b), (f))
350#define	bus_dmamap_load_uio(t, m, u, f)				\
351	(*(t)->_dmamap_load_uio)((t), (m), (u), (f))
352#define	bus_dmamap_load_raw(t, m, sg, n, s, f)			\
353	(*(t)->_dmamap_load_raw)((t), (m), (sg), (n), (s), (f))
354#define	bus_dmamap_unload(t, p)					\
355	(*(t)->_dmamap_unload)((t), (p))
356#define	bus_dmamap_sync(t, p, o, l, ops)			\
357	(void)((t)->_dmamap_sync ?				\
358	    (*(t)->_dmamap_sync)((t), (p), (o), (l), (ops)) : (void)0)
359
360#define	bus_dmamem_alloc(t, s, a, b, sg, n, r, f)		\
361	(*(t)->_dmamem_alloc)((t), (s), (a), (b), (sg), (n), (r), (f))
362#define	bus_dmamem_free(t, sg, n)				\
363	(*(t)->_dmamem_free)((t), (sg), (n))
364#define	bus_dmamem_map(t, sg, n, s, k, f)			\
365	(*(t)->_dmamem_map)((t), (sg), (n), (s), (k), (f))
366#define	bus_dmamem_unmap(t, k, s)				\
367	(*(t)->_dmamem_unmap)((t), (k), (s))
368#define	bus_dmamem_mmap(t, sg, n, o, p, f)			\
369	(*(t)->_dmamem_mmap)((t), (sg), (n), (o), (p), (f))
370
371#define bus_dmatag_subregion(t, mna, mxa, nt, f) EOPNOTSUPP
372#define bus_dmatag_destroy(t)
373
374#ifdef _SPARC_BUS_DMA_PRIVATE
375int	_bus_dmamap_create(bus_dma_tag_t, bus_size_t, int, bus_size_t,
376	    bus_size_t, int, bus_dmamap_t *);
377void	_bus_dmamap_destroy(bus_dma_tag_t, bus_dmamap_t);
378int	_bus_dmamap_load(bus_dma_tag_t, bus_dmamap_t, void *,
379	    bus_size_t, struct proc *, int);
380int	_bus_dmamap_load_mbuf(bus_dma_tag_t, bus_dmamap_t,
381	    struct mbuf *, int);
382int	_bus_dmamap_load_uio(bus_dma_tag_t, bus_dmamap_t,
383	    struct uio *, int);
384int	_bus_dmamap_load_raw(bus_dma_tag_t, bus_dmamap_t,
385	    bus_dma_segment_t *, int, bus_size_t, int);
386void	_bus_dmamap_unload(bus_dma_tag_t, bus_dmamap_t);
387void	_bus_dmamap_sync(bus_dma_tag_t, bus_dmamap_t, bus_addr_t,
388	    bus_size_t, int);
389
390int	_bus_dmamem_alloc(bus_dma_tag_t tag, bus_size_t size,
391	    bus_size_t alignment, bus_size_t boundary,
392	    bus_dma_segment_t *segs, int nsegs, int *rsegs, int flags);
393void	_bus_dmamem_free(bus_dma_tag_t tag, bus_dma_segment_t *segs,
394	    int nsegs);
395int	_bus_dmamem_map(bus_dma_tag_t tag, bus_dma_segment_t *segs,
396	    int nsegs, size_t size, void **kvap, int flags);
397void	_bus_dmamem_unmap(bus_dma_tag_t tag, void *kva,
398	    size_t size);
399paddr_t	_bus_dmamem_mmap(bus_dma_tag_t tag, bus_dma_segment_t *segs,
400	    int nsegs, off_t off, int prot, int flags);
401
402int	_bus_dmamem_alloc_range(bus_dma_tag_t tag, bus_size_t size,
403	    bus_size_t alignment, bus_size_t boundary,
404	    bus_dma_segment_t *segs, int nsegs, int *rsegs, int flags,
405	    vaddr_t low, vaddr_t high);
406#endif /* _SPARC_BUS_DMA_PRIVATE */
407
408#endif /* _SPARC64_BUS_FUNCS_H_ */
409