mvsoc_space.c revision 1.7.10.1 1 /* $NetBSD: mvsoc_space.c,v 1.7.10.1 2017/03/20 06:57:11 pgoyette 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.7.10.1 2017/03/20 06:57:11 pgoyette 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 #ifdef __BUS_SPACE_HAS_STREAM_METHODS
204 MVSOC_BUS_SPACE_NORMAL_FUNCS,
205 #endif
206 };
207 struct bus_space kirkwood_pex1_io_bs_tag = {
208 /* cookie */
209 (void *)KIRKWOOD_TAG_PEX1_IO,
210
211 MVSOC_BUS_SPACE_DEFAULT_FUNCS,
212 #ifdef __BUS_SPACE_HAS_STREAM_METHODS
213 MVSOC_BUS_SPACE_NORMAL_FUNCS,
214 #endif
215 };
216 #endif
217
218 #if defined(DOVE)
219 struct bus_space dove_pex0_mem_bs_tag = {
220 /* cookie */
221 (void *)DOVE_TAG_PEX0_MEM,
222
223 MVSOC_BUS_SPACE_DEFAULT_FUNCS,
224 #ifdef __BUS_SPACE_HAS_STREAM_METHODS
225 MVSOC_BUS_SPACE_NORMAL_FUNCS,
226 #endif
227 };
228 struct bus_space dove_pex0_io_bs_tag = {
229 /* cookie */
230 (void *)DOVE_TAG_PEX0_IO,
231
232 MVSOC_BUS_SPACE_DEFAULT_FUNCS,
233 #ifdef __BUS_SPACE_HAS_STREAM_METHODS
234 MVSOC_BUS_SPACE_NORMAL_FUNCS,
235 #endif
236 };
237 struct bus_space dove_pex1_mem_bs_tag = {
238 /* cookie */
239 (void *)DOVE_TAG_PEX1_MEM,
240
241 MVSOC_BUS_SPACE_DEFAULT_FUNCS,
242 #ifdef __BUS_SPACE_HAS_STREAM_METHODS
243 MVSOC_BUS_SPACE_NORMAL_FUNCS,
244 #endif
245 };
246 struct bus_space dove_pex1_io_bs_tag = {
247 /* cookie */
248 (void *)DOVE_TAG_PEX1_IO,
249
250 MVSOC_BUS_SPACE_DEFAULT_FUNCS,
251 #ifdef __BUS_SPACE_HAS_STREAM_METHODS
252 MVSOC_BUS_SPACE_NORMAL_FUNCS,
253 #endif
254 };
255 #endif
256
257 #if defined(ARMADAXP)
258 struct bus_space armadaxp_pex00_mem_bs_tag = {
259 /* cookie */
260 (void *)ARMADAXP_TAG_PEX00_MEM,
261
262 MVSOC_BUS_SPACE_DEFAULT_FUNCS
263 };
264 struct bus_space armadaxp_pex00_io_bs_tag = {
265 /* cookie */
266 (void *)ARMADAXP_TAG_PEX00_IO,
267
268 MVSOC_BUS_SPACE_DEFAULT_FUNCS
269 };
270 struct bus_space armadaxp_pex01_mem_bs_tag = {
271 /* cookie */
272 (void *)ARMADAXP_TAG_PEX01_MEM,
273
274 MVSOC_BUS_SPACE_DEFAULT_FUNCS
275 };
276 struct bus_space armadaxp_pex01_io_bs_tag = {
277 /* cookie */
278 (void *)ARMADAXP_TAG_PEX01_IO,
279
280 MVSOC_BUS_SPACE_DEFAULT_FUNCS
281 };
282 struct bus_space armadaxp_pex02_mem_bs_tag = {
283 /* cookie */
284 (void *)ARMADAXP_TAG_PEX02_MEM,
285
286 MVSOC_BUS_SPACE_DEFAULT_FUNCS
287 };
288 struct bus_space armadaxp_pex02_io_bs_tag = {
289 /* cookie */
290 (void *)ARMADAXP_TAG_PEX02_IO,
291
292 MVSOC_BUS_SPACE_DEFAULT_FUNCS
293 };
294 struct bus_space armadaxp_pex03_mem_bs_tag = {
295 /* cookie */
296 (void *)ARMADAXP_TAG_PEX03_MEM,
297
298 MVSOC_BUS_SPACE_DEFAULT_FUNCS
299 };
300 struct bus_space armadaxp_pex03_io_bs_tag = {
301 /* cookie */
302 (void *)ARMADAXP_TAG_PEX03_IO,
303
304 MVSOC_BUS_SPACE_DEFAULT_FUNCS
305 };
306 struct bus_space armadaxp_pex10_mem_bs_tag = {
307 /* cookie */
308 (void *)ARMADAXP_TAG_PEX10_MEM,
309
310 MVSOC_BUS_SPACE_DEFAULT_FUNCS
311 };
312 struct bus_space armadaxp_pex10_io_bs_tag = {
313 /* cookie */
314 (void *)ARMADAXP_TAG_PEX10_IO,
315
316 MVSOC_BUS_SPACE_DEFAULT_FUNCS
317 };
318 struct bus_space armadaxp_pex2_mem_bs_tag = {
319 /* cookie */
320 (void *)ARMADAXP_TAG_PEX2_MEM,
321
322 MVSOC_BUS_SPACE_DEFAULT_FUNCS
323 };
324 struct bus_space armadaxp_pex2_io_bs_tag = {
325 /* cookie */
326 (void *)ARMADAXP_TAG_PEX2_IO,
327
328 MVSOC_BUS_SPACE_DEFAULT_FUNCS
329 };
330 struct bus_space armadaxp_pex3_mem_bs_tag = {
331 /* cookie */
332 (void *)ARMADAXP_TAG_PEX3_MEM,
333
334 MVSOC_BUS_SPACE_DEFAULT_FUNCS
335 };
336 struct bus_space armadaxp_pex3_io_bs_tag = {
337 /* cookie */
338 (void *)ARMADAXP_TAG_PEX3_IO,
339
340 MVSOC_BUS_SPACE_DEFAULT_FUNCS
341 };
342 #endif
343 #endif
344
345 #if NGTPCI > 0
346 #if defined(ORION)
347 struct bus_space orion_pci_mem_bs_tag = {
348 /* cookie */
349 (void *)ORION_TAG_PCI_MEM,
350
351 MVSOC_BUS_SPACE_DEFAULT_FUNCS,
352 #ifdef __BUS_SPACE_HAS_STREAM_METHODS
353 MVSOC_BUS_SPACE_NORMAL_FUNCS,
354 #endif
355 };
356 struct bus_space orion_pci_io_bs_tag = {
357 /* cookie */
358 (void *)ORION_TAG_PCI_IO,
359
360 MVSOC_BUS_SPACE_DEFAULT_FUNCS,
361 #ifdef __BUS_SPACE_HAS_STREAM_METHODS
362 MVSOC_BUS_SPACE_NORMAL_FUNCS,
363 #endif
364 };
365 #endif
366 #endif
367
368
369 int
370 mvsoc_bs_map(void *space, bus_addr_t address, bus_size_t size, int flags,
371 bus_space_handle_t *handlep)
372 {
373 const struct pmap_devmap *pd;
374 paddr_t startpa, endpa, offset, pa;
375 vaddr_t va;
376
377 /*
378 * XXX: We are not configuring any decode windows for Armada XP
379 * at the moment. We rely on those that have been set by u-boot.
380 * Hence we don't want to mess around with decode windows,
381 * till we get full controll over them.
382 */
383
384 int tag = (int)space;
385
386 if (tag != 0) {
387 bus_addr_t remap;
388 uint32_t base;
389 int window;
390
391 window = mvsoc_target(tag, NULL, NULL, &base, NULL);
392 if (window == -1)
393 return ENOMEM;
394 if (window < nremap) {
395 remap = read_mlmbreg(MVSOC_MLMB_WRLR(window)) &
396 MVSOC_MLMB_WRLR_REMAP_MASK;
397 remap |=
398 (read_mlmbreg(MVSOC_MLMB_WRHR(window)) << 16) << 16;
399 address = address - remap + base;
400 }
401 }
402
403 if ((pd = pmap_devmap_find_pa(address, size)) != NULL) {
404 /* Device was statically mapped. */
405 *handlep = pd->pd_va + (address - pd->pd_pa);
406 return 0;
407 }
408
409 startpa = trunc_page(address);
410 endpa = round_page(address + size);
411 offset = address & PAGE_MASK;
412
413 /* XXX use extent manager to check duplicate mapping */
414
415 va = uvm_km_alloc(kernel_map, endpa - startpa, 0,
416 UVM_KMF_VAONLY | UVM_KMF_NOWAIT);
417 if (va == 0x00000000)
418 return ENOMEM;
419
420 *handlep = va + offset;
421
422 const int pmapflags =
423 (flags & (BUS_SPACE_MAP_CACHEABLE|BUS_SPACE_MAP_PREFETCHABLE))
424 ? 0
425 : PMAP_NOCACHE;
426
427 /* Now map the pages */
428 for (pa = startpa; pa < endpa; pa += PAGE_SIZE, va += PAGE_SIZE) {
429 pmap_kenter_pa(va, pa, VM_PROT_READ | VM_PROT_WRITE, pmapflags);
430 }
431 pmap_update(pmap_kernel());
432
433 return 0;
434 }
435
436 void
437 mvsoc_bs_unmap(void *space, bus_space_handle_t handle, bus_size_t size)
438 {
439 vaddr_t va, sz;
440
441 if (pmap_devmap_find_va(handle, size) != NULL)
442 /* Device was statically mapped; nothing to do. */
443 return;
444
445 va = trunc_page(handle);
446 sz = round_page(handle + size) - va;
447
448 pmap_kremove(va, sz);
449 pmap_update(pmap_kernel());
450 uvm_km_free(kernel_map, va, sz, UVM_KMF_VAONLY);
451 }
452
453 /* ARGSUSED */
454 int
455 mvsoc_bs_subregion(void *space, bus_space_handle_t handle,
456 bus_size_t offset, bus_size_t size,
457 bus_space_handle_t *nhandlep)
458 {
459
460 *nhandlep = handle + offset;
461 return 0;
462 }
463
464 /* ARGSUSED */
465 int
466 mvsoc_bs_alloc(void *space, bus_addr_t reg_start, bus_addr_t reg_end,
467 bus_size_t size, bus_size_t alignment, bus_size_t boundary,
468 int flags, bus_addr_t *addrp, bus_space_handle_t *handlep)
469 {
470
471 panic("%s(): not implemented\n", __func__);
472 }
473
474 /* ARGSUSED */
475 void
476 mvsoc_bs_free(void *space, bus_space_handle_t handle, bus_size_t size)
477 {
478
479 panic("%s(): not implemented\n", __func__);
480 }
481
482 /* ARGSUSED */
483 void
484 mvsoc_bs_barrier(void *space, bus_space_handle_t handle, bus_size_t offset,
485 bus_size_t length, int flags)
486 {
487
488 /* Nothing to do. */
489 }
490
491 /* ARGSUSED */
492 void *
493 mvsoc_bs_vaddr(void *space, bus_space_handle_t handle)
494 {
495
496 return (void *)handle;
497 }
498