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