mvsoc_space.c revision 1.4 1 /* $NetBSD: mvsoc_space.c,v 1.4 2012/07/18 09:56:49 kiyohara Exp $ */
2 /*
3 * Copyright (c) 2007 KIYOHARA Takashi
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
19 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
23 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
24 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include <sys/cdefs.h>
29 __KERNEL_RCSID(0, "$NetBSD: mvsoc_space.c,v 1.4 2012/07/18 09:56:49 kiyohara Exp $");
30
31 #include "opt_mvsoc.h"
32 #include "mvpex.h"
33 #include "gtpci.h"
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37
38 #include <uvm/uvm_extern.h>
39
40 #include <sys/bus.h>
41
42 #include <arm/marvell/mvsocreg.h>
43 #include <arm/marvell/mvsocvar.h>
44
45
46 /* Proto types for all the bus_space structure functions */
47 bs_protos(mvsoc);
48 bs_protos(generic);
49 bs_protos(generic_armv4);
50 bs_protos(bs_notimpl);
51
52 #define MVSOC_BUS_SPACE_NORMAL_FUNCS \
53 /* read (single) */ \
54 generic_bs_r_1, \
55 generic_armv4_bs_r_2, \
56 generic_bs_r_4, \
57 bs_notimpl_bs_r_8, \
58 \
59 /* read multiple */ \
60 generic_bs_rm_1, \
61 generic_armv4_bs_rm_2, \
62 generic_bs_rm_4, \
63 bs_notimpl_bs_rm_8, \
64 \
65 /* read region */ \
66 generic_bs_rr_1, \
67 generic_armv4_bs_rr_2, \
68 generic_bs_rr_4, \
69 bs_notimpl_bs_rr_8, \
70 \
71 /* write (single) */ \
72 generic_bs_w_1, \
73 generic_armv4_bs_w_2, \
74 generic_bs_w_4, \
75 bs_notimpl_bs_w_8, \
76 \
77 /* write multiple */ \
78 generic_bs_wm_1, \
79 generic_armv4_bs_wm_2, \
80 generic_bs_wm_4, \
81 bs_notimpl_bs_wm_8, \
82 \
83 /* write region */ \
84 generic_bs_wr_1, \
85 generic_armv4_bs_wr_2, \
86 generic_bs_wr_4, \
87 bs_notimpl_bs_wr_8
88
89 #define MVSOC_BUS_SPACE_DEFAULT_FUNCS \
90 /* mapping/unmapping */ \
91 mvsoc_bs_map, \
92 mvsoc_bs_unmap, \
93 mvsoc_bs_subregion, \
94 \
95 /* allocation/deallocation */ \
96 mvsoc_bs_alloc, \
97 mvsoc_bs_free, \
98 \
99 /* get kernel virtual address */ \
100 mvsoc_bs_vaddr, \
101 \
102 /* mmap bus space for userland */ \
103 bs_notimpl_bs_mmap, \
104 \
105 /* barrier */ \
106 mvsoc_bs_barrier, \
107 \
108 MVSOC_BUS_SPACE_NORMAL_FUNCS, \
109 \
110 /* set multiple */ \
111 bs_notimpl_bs_sm_1, \
112 bs_notimpl_bs_sm_2, \
113 bs_notimpl_bs_sm_4, \
114 bs_notimpl_bs_sm_8, \
115 \
116 /* set region */ \
117 bs_notimpl_bs_sr_1, \
118 generic_armv4_bs_sr_2, \
119 generic_bs_sr_4, \
120 bs_notimpl_bs_sr_8, \
121 \
122 /* copy */ \
123 bs_notimpl_bs_c_1, \
124 generic_armv4_bs_c_2, \
125 bs_notimpl_bs_c_4, \
126 bs_notimpl_bs_c_8
127
128
129 struct bus_space mvsoc_bs_tag = {
130 /* cookie */
131 (void *)0,
132
133 MVSOC_BUS_SPACE_DEFAULT_FUNCS,
134 #ifdef __BUS_SPACE_HAS_STREAM_METHODS
135 MVSOC_BUS_SPACE_NORMAL_FUNCS,
136 #endif
137 };
138
139 #if NMVPEX > 0
140 #if defined(ORION)
141 struct bus_space orion_pex0_mem_bs_tag = {
142 /* cookie */
143 (void *)ORION_TAG_PEX0_MEM,
144
145 MVSOC_BUS_SPACE_DEFAULT_FUNCS,
146 #ifdef __BUS_SPACE_HAS_STREAM_METHODS
147 MVSOC_BUS_SPACE_NORMAL_FUNCS,
148 #endif
149 };
150 struct bus_space orion_pex0_io_bs_tag = {
151 /* cookie */
152 (void *)ORION_TAG_PEX0_IO,
153
154 MVSOC_BUS_SPACE_DEFAULT_FUNCS,
155 #ifdef __BUS_SPACE_HAS_STREAM_METHODS
156 MVSOC_BUS_SPACE_NORMAL_FUNCS,
157 #endif
158 };
159 struct bus_space orion_pex1_mem_bs_tag = {
160 /* cookie */
161 (void *)ORION_TAG_PEX1_MEM,
162
163 MVSOC_BUS_SPACE_DEFAULT_FUNCS,
164 #ifdef __BUS_SPACE_HAS_STREAM_METHODS
165 MVSOC_BUS_SPACE_NORMAL_FUNCS,
166 #endif
167 };
168 struct bus_space orion_pex1_io_bs_tag = {
169 /* cookie */
170 (void *)ORION_TAG_PEX1_IO,
171
172 MVSOC_BUS_SPACE_DEFAULT_FUNCS
173 #ifdef __BUS_SPACE_HAS_STREAM_METHODS
174 MVSOC_BUS_SPACE_NORMAL_FUNCS,
175 #endif
176 };
177 #endif
178
179 #if defined(KIRKWOOD)
180 struct bus_space kirkwood_pex_mem_bs_tag = {
181 /* cookie */
182 (void *)KIRKWOOD_TAG_PEX_MEM,
183
184 MVSOC_BUS_SPACE_DEFAULT_FUNCS,
185 #ifdef __BUS_SPACE_HAS_STREAM_METHODS
186 MVSOC_BUS_SPACE_NORMAL_FUNCS,
187 #endif
188 };
189 struct bus_space kirkwood_pex_io_bs_tag = {
190 /* cookie */
191 (void *)KIRKWOOD_TAG_PEX_IO,
192
193 MVSOC_BUS_SPACE_DEFAULT_FUNCS,
194 #ifdef __BUS_SPACE_HAS_STREAM_METHODS
195 MVSOC_BUS_SPACE_NORMAL_FUNCS,
196 #endif
197 };
198 struct bus_space kirkwood_pex1_mem_bs_tag = {
199 /* cookie */
200 (void *)KIRKWOOD_TAG_PEX1_MEM,
201
202 MVSOC_BUS_SPACE_DEFAULT_FUNCS
203 };
204 struct bus_space kirkwood_pex1_io_bs_tag = {
205 /* cookie */
206 (void *)KIRKWOOD_TAG_PEX1_IO,
207
208 MVSOC_BUS_SPACE_DEFAULT_FUNCS
209 };
210 #endif
211 #endif
212
213 #if NGTPCI > 0
214 #if defined(ORION)
215 struct bus_space orion_pci_mem_bs_tag = {
216 /* cookie */
217 (void *)ORION_TAG_PCI_MEM,
218
219 MVSOC_BUS_SPACE_DEFAULT_FUNCS,
220 #ifdef __BUS_SPACE_HAS_STREAM_METHODS
221 MVSOC_BUS_SPACE_NORMAL_FUNCS,
222 #endif
223 };
224 struct bus_space orion_pci_io_bs_tag = {
225 /* cookie */
226 (void *)ORION_TAG_PCI_IO,
227
228 MVSOC_BUS_SPACE_DEFAULT_FUNCS,
229 #ifdef __BUS_SPACE_HAS_STREAM_METHODS
230 MVSOC_BUS_SPACE_NORMAL_FUNCS,
231 #endif
232 };
233 #endif
234 #endif
235
236
237 int
238 mvsoc_bs_map(void *space, bus_addr_t address, bus_size_t size, int flags,
239 bus_space_handle_t *handlep)
240 {
241 const struct pmap_devmap *pd;
242 paddr_t startpa, endpa, offset, pa;
243 pt_entry_t *pte;
244 vaddr_t va;
245 int tag = (int)space;
246
247 if (tag != 0) {
248 bus_addr_t remap;
249 uint32_t base;
250 int window;
251
252 window = mvsoc_target(tag, NULL, NULL, &base, NULL);
253 if (window == -1)
254 return ENOMEM;
255 if (window < nremap) {
256 remap = read_mlmbreg(MVSOC_MLMB_WRLR(window)) &
257 MVSOC_MLMB_WRLR_REMAP_MASK;
258 remap |=
259 (read_mlmbreg(MVSOC_MLMB_WRHR(window)) << 16) << 16;
260 address = address - remap + base;
261 }
262 }
263
264 if ((pd = pmap_devmap_find_pa(address, size)) != NULL) {
265 /* Device was statically mapped. */
266 *handlep = pd->pd_va + (address - pd->pd_pa);
267 return 0;
268 }
269
270 startpa = trunc_page(address);
271 endpa = round_page(address + size);
272 offset = address & PAGE_MASK;
273
274 /* XXX use extent manager to check duplicate mapping */
275
276 va = uvm_km_alloc(kernel_map, endpa - startpa, 0,
277 UVM_KMF_VAONLY | UVM_KMF_NOWAIT);
278 if (va == 0x00000000)
279 return ENOMEM;
280
281 *handlep = va + offset;
282
283 /* Now map the pages */
284 for (pa = startpa; pa < endpa; pa += PAGE_SIZE, va += PAGE_SIZE) {
285 pmap_kenter_pa(va, pa, VM_PROT_READ | VM_PROT_WRITE, 0);
286 if ((flags & BUS_SPACE_MAP_CACHEABLE) == 0) {
287 pte = vtopte(va);
288 *pte &= ~L2_S_CACHE_MASK;
289 PTE_SYNC(pte);
290 /*
291 * XXX: pmap_kenter_pa() also does PTE_SYNC(). a bit of
292 * waste.
293 */
294 }
295 }
296 pmap_update(pmap_kernel());
297
298 return 0;
299 }
300
301 void
302 mvsoc_bs_unmap(void *space, bus_space_handle_t handle, bus_size_t size)
303 {
304 vaddr_t va, sz;
305
306 if (pmap_devmap_find_va(handle, size) != NULL)
307 /* Device was statically mapped; nothing to do. */
308 return;
309
310 va = trunc_page(handle);
311 sz = round_page(handle + size) - va;
312
313 pmap_kremove(va, sz);
314 pmap_update(pmap_kernel());
315 uvm_km_free(kernel_map, va, sz, UVM_KMF_VAONLY);
316 }
317
318 /* ARGSUSED */
319 int
320 mvsoc_bs_subregion(void *space, bus_space_handle_t handle,
321 bus_size_t offset, bus_size_t size,
322 bus_space_handle_t *nhandlep)
323 {
324
325 *nhandlep = handle + offset;
326 return 0;
327 }
328
329 /* ARGSUSED */
330 int
331 mvsoc_bs_alloc(void *space, bus_addr_t reg_start, bus_addr_t reg_end,
332 bus_size_t size, bus_size_t alignment, bus_size_t boundary,
333 int flags, bus_addr_t *addrp, bus_space_handle_t *handlep)
334 {
335
336 panic("%s(): not implemented\n", __func__);
337 }
338
339 /* ARGSUSED */
340 void
341 mvsoc_bs_free(void *space, bus_space_handle_t handle, bus_size_t size)
342 {
343
344 panic("%s(): not implemented\n", __func__);
345 }
346
347 /* ARGSUSED */
348 void
349 mvsoc_bs_barrier(void *space, bus_space_handle_t handle, bus_size_t offset,
350 bus_size_t length, int flags)
351 {
352
353 /* Nothing to do. */
354 }
355
356 /* ARGSUSED */
357 void *
358 mvsoc_bs_vaddr(void *space, bus_space_handle_t handle)
359 {
360
361 return (void *)handle;
362 }
363