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