xref: /netbsd-src/sys/arch/arm/xscale/i80321_space.c (revision aaf4ece63a859a04e37cf3a7229b5fab0157cc06)
1 /*	$NetBSD: i80321_space.c,v 1.9 2005/11/24 13:08:32 yamt 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 /*
39  * bus_space functions for i80321 I/O Processor.
40  */
41 
42 #include <sys/cdefs.h>
43 __KERNEL_RCSID(0, "$NetBSD: i80321_space.c,v 1.9 2005/11/24 13:08:32 yamt Exp $");
44 
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 
48 #include <uvm/uvm_extern.h>
49 
50 #include <machine/bus.h>
51 
52 #include <arm/xscale/i80321reg.h>
53 #include <arm/xscale/i80321var.h>
54 
55 /* Prototypes for all the bus_space structure functions */
56 bs_protos(i80321);
57 bs_protos(i80321_io);
58 bs_protos(i80321_mem);
59 bs_protos(generic);
60 bs_protos(generic_armv4);
61 bs_protos(bs_notimpl);
62 
63 /*
64  * Template bus_space -- copied, and the bits that are NULL are
65  * filled in.
66  */
67 const struct bus_space i80321_bs_tag_template = {
68 	/* cookie */
69 	(void *) 0,
70 
71 	/* mapping/unmapping */
72 	NULL,
73 	NULL,
74 	i80321_bs_subregion,
75 
76 	/* allocation/deallocation */
77 	NULL,
78 	NULL,
79 
80 	/* get kernel virtual address */
81 	i80321_bs_vaddr,
82 
83 	/* mmap */
84 	i80321_bs_mmap,
85 
86 	/* barrier */
87 	i80321_bs_barrier,
88 
89 	/* read (single) */
90 	generic_bs_r_1,
91 	generic_armv4_bs_r_2,
92 	generic_bs_r_4,
93 	bs_notimpl_bs_r_8,
94 
95 	/* read multiple */
96 	generic_bs_rm_1,
97 	generic_armv4_bs_rm_2,
98 	generic_bs_rm_4,
99 	bs_notimpl_bs_rm_8,
100 
101 	/* read region */
102 	generic_bs_rr_1,
103 	generic_armv4_bs_rr_2,
104 	generic_bs_rr_4,
105 	bs_notimpl_bs_rr_8,
106 
107 	/* write (single) */
108 	generic_bs_w_1,
109 	generic_armv4_bs_w_2,
110 	generic_bs_w_4,
111 	bs_notimpl_bs_w_8,
112 
113 	/* write multiple */
114 	generic_bs_wm_1,
115 	generic_armv4_bs_wm_2,
116 	generic_bs_wm_4,
117 	bs_notimpl_bs_wm_8,
118 
119 	/* write region */
120 	generic_bs_wr_1,
121 	generic_armv4_bs_wr_2,
122 	generic_bs_wr_4,
123 	bs_notimpl_bs_wr_8,
124 
125 	/* set multiple */
126 	bs_notimpl_bs_sm_1,
127 	bs_notimpl_bs_sm_2,
128 	bs_notimpl_bs_sm_4,
129 	bs_notimpl_bs_sm_8,
130 
131 	/* set region */
132 	bs_notimpl_bs_sr_1,
133 	generic_armv4_bs_sr_2,
134 	generic_bs_sr_4,
135 	bs_notimpl_bs_sr_8,
136 
137 	/* copy */
138 	bs_notimpl_bs_c_1,
139 	generic_armv4_bs_c_2,
140 	bs_notimpl_bs_c_4,
141 	bs_notimpl_bs_c_8,
142 };
143 
144 void
145 i80321_bs_init(bus_space_tag_t bs, void *cookie)
146 {
147 
148 	*bs = i80321_bs_tag_template;
149 	bs->bs_cookie = cookie;
150 }
151 
152 void
153 i80321_io_bs_init(bus_space_tag_t bs, void *cookie)
154 {
155 
156 	*bs = i80321_bs_tag_template;
157 	bs->bs_cookie = cookie;
158 
159 	bs->bs_map = i80321_io_bs_map;
160 	bs->bs_unmap = i80321_io_bs_unmap;
161 	bs->bs_alloc = i80321_io_bs_alloc;
162 	bs->bs_free = i80321_io_bs_free;
163 
164 	bs->bs_vaddr = i80321_io_bs_vaddr;
165 }
166 
167 void
168 i80321_mem_bs_init(bus_space_tag_t bs, void *cookie)
169 {
170 
171 	*bs = i80321_bs_tag_template;
172 	bs->bs_cookie = cookie;
173 
174 	bs->bs_map = i80321_mem_bs_map;
175 	bs->bs_unmap = i80321_mem_bs_unmap;
176 	bs->bs_alloc = i80321_mem_bs_alloc;
177 	bs->bs_free = i80321_mem_bs_free;
178 
179 	bs->bs_mmap = i80321_mem_bs_mmap;
180 }
181 
182 /* *** Routines shared by i80321, PCI IO, and PCI MEM. *** */
183 
184 int
185 i80321_bs_subregion(void *t, bus_space_handle_t bsh, bus_size_t offset,
186     bus_size_t size, bus_space_handle_t *nbshp)
187 {
188 
189 	*nbshp = bsh + offset;
190 	return (0);
191 }
192 
193 void
194 i80321_bs_barrier(void *t, bus_space_handle_t bsh, bus_size_t offset,
195     bus_size_t len, int flags)
196 {
197 
198 	/* Nothing to do. */
199 }
200 
201 void *
202 i80321_bs_vaddr(void *t, bus_space_handle_t bsh)
203 {
204 
205 	return ((void *)bsh);
206 }
207 
208 paddr_t
209 i80321_bs_mmap(void *t, bus_addr_t addr, off_t off, int prot, int flags)
210 {
211 
212 	/* Not supported. */
213 	return (-1);
214 }
215 
216 /* *** Routines for PCI IO. *** */
217 
218 int
219 i80321_io_bs_map(void *t, bus_addr_t bpa, bus_size_t size, int flags,
220     bus_space_handle_t *bshp)
221 {
222 	struct i80321_softc *sc = t;
223 	vaddr_t winvaddr;
224 	uint32_t busbase;
225 
226 	if (bpa >= sc->sc_ioout_xlate &&
227 	    bpa < (sc->sc_ioout_xlate + VERDE_OUT_XLATE_IO_WIN_SIZE)) {
228 		busbase = sc->sc_ioout_xlate;
229 		winvaddr = sc->sc_iow_vaddr;
230 	} else
231 		return (EINVAL);
232 
233 	if ((bpa + size) >= (busbase + VERDE_OUT_XLATE_IO_WIN_SIZE))
234 		return (EINVAL);
235 
236 	/*
237 	 * Found the window -- PCI I/O space is mapped at a fixed
238 	 * virtual address by board-specific code.  Translate the
239 	 * bus address to the virtual address.
240 	 */
241 	*bshp = winvaddr + (bpa - busbase);
242 
243 	return (0);
244 }
245 
246 void
247 i80321_io_bs_unmap(void *t, bus_space_handle_t bsh, bus_size_t size)
248 {
249 
250 	/* Nothing to do. */
251 }
252 
253 int
254 i80321_io_bs_alloc(void *t, bus_addr_t rstart, bus_addr_t rend,
255     bus_size_t size, bus_size_t alignment, bus_size_t boundary, int flags,
256     bus_addr_t *bpap, bus_space_handle_t *bshp)
257 {
258 
259 	panic("i80321_io_bs_alloc(): not implemented");
260 }
261 
262 void
263 i80321_io_bs_free(void *t, bus_space_handle_t bsh, bus_size_t size)
264 {
265 
266 	panic("i80321_io_bs_free(): not implemented");
267 }
268 
269 void *
270 i80321_io_bs_vaddr(void *t, bus_space_handle_t bsh)
271 {
272 
273 	/* Not supported. */
274 	return (NULL);
275 }
276 
277 /* *** Routines for PCI MEM. *** */
278 
279 int
280 i80321_mem_bs_map(void *t, bus_addr_t bpa, bus_size_t size, int flags,
281     bus_space_handle_t *bshp)
282 {
283 
284 	struct i80321_softc *sc = t;
285 	vaddr_t va;
286 	uint32_t busbase;
287 	paddr_t pa, endpa, physbase;
288 
289 	if (bpa >= sc->sc_owin[0].owin_xlate_lo &&
290 	    bpa < (sc->sc_owin[0].owin_xlate_lo +
291 		   VERDE_OUT_XLATE_MEM_WIN_SIZE)) {
292 		busbase = sc->sc_owin[0].owin_xlate_lo;
293 		physbase = sc->sc_iwin[1].iwin_xlate;
294 	} else
295 		return (EINVAL);
296 
297 	if ((bpa + size) >= (busbase + VERDE_OUT_XLATE_MEM_WIN_SIZE))
298 		return (EINVAL);
299 
300 	/*
301 	 * Found the window -- PCI MEM space is not mapped by allocating
302 	 * some kernel VA space and mapping the pages with pmap_enter().
303 	 * pmap_enter() will map unmanaged pages as non-cacheable.
304 	 */
305 	pa = trunc_page((bpa - busbase) + physbase);
306 	endpa = round_page(((bpa - busbase) + physbase) + size);
307 
308 	va = uvm_km_alloc(kernel_map, endpa - pa, 0,
309 	    UVM_KMF_VAONLY | UVM_KMF_NOWAIT);
310 	if (va == 0)
311 		return (ENOMEM);
312 
313 	*bshp = va + (bpa & PAGE_MASK);
314 
315 	for (; pa < endpa; pa += PAGE_SIZE, va += PAGE_SIZE) {
316 		pmap_enter(pmap_kernel(), va, pa,
317 		    VM_PROT_READ | VM_PROT_WRITE,
318 		    VM_PROT_READ | VM_PROT_WRITE | PMAP_WIRED);
319 	}
320 	pmap_update(pmap_kernel());
321 
322 	return (0);
323 }
324 
325 void
326 i80321_mem_bs_unmap(void *t, bus_space_handle_t bsh, bus_size_t size)
327 {
328 	vaddr_t va, endva;
329 
330 	va = trunc_page(bsh);
331 	endva = round_page(bsh + size);
332 
333 	pmap_remove(pmap_kernel(), va, endva - va);
334 	pmap_update(pmap_kernel());
335 
336 	/* Free the kernel virtual mapping. */
337 	uvm_km_free(kernel_map, va, endva - va, UVM_KMF_VAONLY);
338 }
339 
340 int
341 i80321_mem_bs_alloc(void *t, bus_addr_t rstart, bus_addr_t rend,
342     bus_size_t size, bus_size_t alignment, bus_size_t boundary, int flags,
343     bus_addr_t *bpap, bus_space_handle_t *bshp)
344 {
345 
346 	panic("i80321_mem_bs_alloc(): not implemented");
347 }
348 
349 void
350 i80321_mem_bs_free(void *t, bus_space_handle_t bsh, bus_size_t size)
351 {
352 
353 	panic("i80321_mem_bs_free(): not implemented");
354 }
355 
356 paddr_t
357 i80321_mem_bs_mmap(void *t, bus_addr_t addr, off_t off, int prot, int flags)
358 {
359 
360 	/* XXX */
361 	return (-1);
362 }
363