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