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