1/*	$NetBSD: gemini_space.c,v 1.6 2023/04/21 15:00:48 skrll Exp $	*/
2
3/* adapted from:
4 *	NetBSD: pxa2x0_space.c,v 1.8 2005/11/24 13:08:32 yamt Exp
5 */
6
7/*
8 * Copyright (c) 2001, 2002 Wasabi Systems, Inc.
9 * All rights reserved.
10 *
11 * Written by Jason R. Thorpe for Wasabi Systems, Inc.
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 * 3. All advertising materials mentioning features or use of this software
22 *    must display the following acknowledgement:
23 *	This product includes software developed for the NetBSD Project by
24 *	Wasabi Systems, Inc.
25 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
26 *    or promote products derived from this software without specific prior
27 *    written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
31 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
32 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
33 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
34 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
35 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
36 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
37 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39 * POSSIBILITY OF SUCH DAMAGE.
40 */
41/*
42 * Copyright (c) 1997 Mark Brinicombe.
43 * Copyright (c) 1997 Causality Limited.
44 * All rights reserved.
45 *
46 * This code is derived from software contributed to The NetBSD Foundation
47 * by Ichiro FUKUHARA.
48 *
49 * Redistribution and use in source and binary forms, with or without
50 * modification, are permitted provided that the following conditions
51 * are met:
52 * 1. Redistributions of source code must retain the above copyright
53 *    notice, this list of conditions and the following disclaimer.
54 * 2. Redistributions in binary form must reproduce the above copyright
55 *    notice, this list of conditions and the following disclaimer in the
56 *    documentation and/or other materials provided with the distribution.
57 * 3. All advertising materials mentioning features or use of this software
58 *    must display the following acknowledgement:
59 *	This product includes software developed by Mark Brinicombe.
60 * 4. The name of the company nor the name of the author may be used to
61 *    endorse or promote products derived from this software without specific
62 *    prior written permission.
63 *
64 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
65 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
66 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
67 * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
68 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
69 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
70 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
71 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
72 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
73 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
74 * SUCH DAMAGE.
75 */
76
77/*
78 * bus_space functions for Gemini processor.
79 */
80
81#include <sys/cdefs.h>
82__KERNEL_RCSID(0, "$NetBSD: gemini_space.c,v 1.6 2023/04/21 15:00:48 skrll Exp $");
83
84#include <sys/param.h>
85#include <sys/systm.h>
86
87#include <uvm/uvm_extern.h>
88
89#include <sys/bus.h>
90
91/* Prototypes for all the bus_space structure functions */
92bs_protos(gemini);
93bs_protos(generic);
94bs_protos(generic_armv4);
95bs_protos(bs_notimpl);
96
97struct bus_space gemini_bs_tag = {
98	/* cookie */
99	.bs_cookie = (void *) 0,
100
101	/* mapping/unmapping */
102	.bs_map = gemini_bs_map,
103	.bs_unmap = gemini_bs_unmap,
104	.bs_subregion = gemini_bs_subregion,
105
106	/* allocation/deallocation */
107	.bs_alloc = gemini_bs_alloc,	/* not implemented */
108	.bs_free = gemini_bs_free,	/* not implemented */
109
110	/* get kernel virtual address */
111	.bs_vaddr = gemini_bs_vaddr,
112
113	/* mmap */
114	.bs_mmap = bs_notimpl_bs_mmap,
115
116	/* barrier */
117	.bs_barrier = gemini_bs_barrier,
118
119	/* read (single) */
120	.bs_r_1 = generic_bs_r_1,
121	.bs_r_2 = generic_armv4_bs_r_2,
122	.bs_r_4 = generic_bs_r_4,
123	.bs_r_8 = bs_notimpl_bs_r_8,
124
125	/* read multiple */
126	.bs_rm_1 = generic_bs_rm_1,
127	.bs_rm_2 = generic_armv4_bs_rm_2,
128	.bs_rm_4 = generic_bs_rm_4,
129	.bs_rm_8 = bs_notimpl_bs_rm_8,
130
131	/* read region */
132	.bs_rr_1 = generic_bs_rr_1,
133	.bs_rr_2 = generic_armv4_bs_rr_2,
134	.bs_rr_4 = generic_bs_rr_4,
135	.bs_rr_8 = bs_notimpl_bs_rr_8,
136
137	/* write (single) */
138	.bs_w_1 = generic_bs_w_1,
139	.bs_w_2 = generic_armv4_bs_w_2,
140	.bs_w_4 = generic_bs_w_4,
141	.bs_w_8 = bs_notimpl_bs_w_8,
142
143	/* write multiple */
144	.bs_wm_1 = generic_bs_wm_1,
145	.bs_wm_2 = generic_armv4_bs_wm_2,
146	.bs_wm_4 = generic_bs_wm_4,
147	.bs_wm_8 = bs_notimpl_bs_wm_8,
148
149	/* write region */
150	.bs_wr_1 = generic_bs_wr_1,
151	.bs_wr_2 = generic_armv4_bs_wr_2,
152	.bs_wr_4 = generic_bs_wr_4,
153	.bs_wr_8 = bs_notimpl_bs_wr_8,
154
155	/* set multiple */
156	.bs_sm_1 = bs_notimpl_bs_sm_1,
157	.bs_sm_2 = bs_notimpl_bs_sm_2,
158	.bs_sm_4 = bs_notimpl_bs_sm_4,
159	.bs_sm_8 = bs_notimpl_bs_sm_8,
160
161	/* set region */
162	.bs_sr_1 = generic_bs_sr_1,
163	.bs_sr_2 = generic_armv4_bs_sr_2,
164	.bs_sr_4 = bs_notimpl_bs_sr_4,
165	.bs_sr_8 = bs_notimpl_bs_sr_8,
166
167	/* copy */
168	.bs_c_1 = bs_notimpl_bs_c_1,
169	.bs_c_2 = generic_armv4_bs_c_2,
170	.bs_c_4 = bs_notimpl_bs_c_4,
171	.bs_c_8 = bs_notimpl_bs_c_8,
172};
173
174int
175gemini_bs_map(void *t, bus_addr_t bpa, bus_size_t size,
176	      int flag, bus_space_handle_t *bshp)
177{
178	const struct pmap_devmap *pd;
179
180	if ((pd = pmap_devmap_find_pa(bpa, size)) != NULL) {
181		/* Device was statically mapped. */
182		*bshp = pd->pd_va + (bpa - pd->pd_pa);
183		return 0;
184	}
185
186	paddr_t startpa = trunc_page(bpa);
187	paddr_t endpa = round_page(bpa + size);
188
189	/* XXX use extent manager to check duplicate mapping */
190
191	vaddr_t va = uvm_km_alloc(kernel_map, endpa - startpa, 0,
192	    UVM_KMF_VAONLY | UVM_KMF_NOWAIT);
193	if (! va)
194		return(ENOMEM);
195
196	*bshp = (bus_space_handle_t)(va + (bpa - startpa));
197
198	const int pmapflags =
199	    (flag & (BUS_SPACE_MAP_CACHEABLE|BUS_SPACE_MAP_PREFETCHABLE))
200		? 0
201		: PMAP_NOCACHE;
202	for (paddr_t pa = startpa; pa < endpa; pa += PAGE_SIZE, va += PAGE_SIZE) {
203		pmap_kenter_pa(va, pa, VM_PROT_READ | VM_PROT_WRITE, pmapflags);
204	}
205	pmap_update(pmap_kernel());
206
207	return(0);
208}
209
210void
211gemini_bs_unmap(void *t, bus_space_handle_t bsh, bus_size_t size)
212{
213	vaddr_t	va;
214	vsize_t	sz;
215
216	if (pmap_devmap_find_va(bsh, size) != NULL) {
217		/* Device was statically mapped; nothing to do. */
218		return;
219	}
220
221	va = trunc_page(bsh);
222	sz = round_page(bsh + size) - va;
223
224	pmap_kremove(va, sz);
225	pmap_update(pmap_kernel());
226	uvm_km_free(kernel_map, va, sz, UVM_KMF_VAONLY);
227}
228
229
230int
231gemini_bs_subregion(void *t, bus_space_handle_t bsh, bus_size_t offset,
232    bus_size_t size, bus_space_handle_t *nbshp)
233{
234
235	*nbshp = bsh + offset;
236	return (0);
237}
238
239void
240gemini_bs_barrier(void *t, bus_space_handle_t bsh, bus_size_t offset,
241    bus_size_t len, int flags)
242{
243
244	/* Nothing to do. */
245}
246
247void *
248gemini_bs_vaddr(void *t, bus_space_handle_t bsh)
249{
250
251	return ((void *)bsh);
252}
253
254
255int
256gemini_bs_alloc(void *t, bus_addr_t rstart, bus_addr_t rend,
257    bus_size_t size, bus_size_t alignment, bus_size_t boundary, int flags,
258    bus_addr_t *bpap, bus_space_handle_t *bshp)
259{
260
261	panic("gemini_io_bs_alloc(): not implemented\n");
262}
263
264void
265gemini_bs_free(void *t, bus_space_handle_t bsh, bus_size_t size)
266{
267
268	panic("gemini_io_bs_free(): not implemented\n");
269}
270
271