xref: /netbsd-src/sys/arch/arm/marvell/mvsoc_space.c (revision 82ad575716605df31379cf04a2f3efbc97b8a6f5)
1 /*	$NetBSD: mvsoc_space.c,v 1.5 2012/07/28 23:13:16 matt 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.5 2012/07/28 23:13:16 matt 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 #endif
218 
219 #if NGTPCI > 0
220 #if defined(ORION)
221 struct bus_space orion_pci_mem_bs_tag = {
222 	/* cookie */
223 	(void *)ORION_TAG_PCI_MEM,
224 
225 	MVSOC_BUS_SPACE_DEFAULT_FUNCS,
226 #ifdef __BUS_SPACE_HAS_STREAM_METHODS
227 	MVSOC_BUS_SPACE_NORMAL_FUNCS,
228 #endif
229 };
230 struct bus_space orion_pci_io_bs_tag = {
231 	/* cookie */
232 	(void *)ORION_TAG_PCI_IO,
233 
234 	MVSOC_BUS_SPACE_DEFAULT_FUNCS,
235 #ifdef __BUS_SPACE_HAS_STREAM_METHODS
236 	MVSOC_BUS_SPACE_NORMAL_FUNCS,
237 #endif
238 };
239 #endif
240 #endif
241 
242 
243 int
244 mvsoc_bs_map(void *space, bus_addr_t address, bus_size_t size, int flags,
245 	     bus_space_handle_t *handlep)
246 {
247 	const struct pmap_devmap *pd;
248 	paddr_t startpa, endpa, offset, pa;
249 	pt_entry_t *pte;
250 	vaddr_t va;
251 	int tag = (int)space;
252 
253 	if (tag != 0) {
254 		bus_addr_t remap;
255 		uint32_t base;
256 		int window;
257 
258 		window = mvsoc_target(tag, NULL, NULL, &base, NULL);
259 		if (window == -1)
260 			return ENOMEM;
261 		if (window < nremap) {
262 			remap = read_mlmbreg(MVSOC_MLMB_WRLR(window)) &
263 			    MVSOC_MLMB_WRLR_REMAP_MASK;
264 			remap |=
265 			    (read_mlmbreg(MVSOC_MLMB_WRHR(window)) << 16) << 16;
266 			address = address - remap + base;
267 		}
268 	}
269 
270 	if ((pd = pmap_devmap_find_pa(address, size)) != NULL) {
271 		/* Device was statically mapped. */
272 		*handlep = pd->pd_va + (address - pd->pd_pa);
273 		return 0;
274 	}
275 
276 	startpa = trunc_page(address);
277 	endpa = round_page(address + size);
278 	offset = address & PAGE_MASK;
279 
280 	/* XXX use extent manager to check duplicate mapping */
281 
282 	va = uvm_km_alloc(kernel_map, endpa - startpa, 0,
283 	    UVM_KMF_VAONLY | UVM_KMF_NOWAIT);
284 	if (va == 0x00000000)
285 		return ENOMEM;
286 
287 	*handlep = va + offset;
288 
289 	/* Now map the pages */
290 	for (pa = startpa; pa < endpa; pa += PAGE_SIZE, va += PAGE_SIZE) {
291 		pmap_kenter_pa(va, pa, VM_PROT_READ | VM_PROT_WRITE, 0);
292 		if ((flags & BUS_SPACE_MAP_CACHEABLE) == 0) {
293 			pte = vtopte(va);
294 			*pte &= ~L2_S_CACHE_MASK;
295 			PTE_SYNC(pte);
296 			/*
297 			 * XXX: pmap_kenter_pa() also does PTE_SYNC(). a bit of
298 			 *      waste.
299 			 */
300 		}
301 	}
302 	pmap_update(pmap_kernel());
303 
304 	return 0;
305 }
306 
307 void
308 mvsoc_bs_unmap(void *space, bus_space_handle_t handle, bus_size_t size)
309 {
310 	vaddr_t va, sz;
311 
312 	if (pmap_devmap_find_va(handle, size) != NULL)
313 		/* Device was statically mapped; nothing to do. */
314 		return;
315 
316 	va = trunc_page(handle);
317         sz = round_page(handle + size) - va;
318 
319 	pmap_kremove(va, sz);
320 	pmap_update(pmap_kernel());
321 	uvm_km_free(kernel_map, va, sz, UVM_KMF_VAONLY);
322 }
323 
324 /* ARGSUSED */
325 int
326 mvsoc_bs_subregion(void *space, bus_space_handle_t handle,
327 		       bus_size_t offset, bus_size_t size,
328 		       bus_space_handle_t *nhandlep)
329 {
330 
331 	*nhandlep = handle + offset;
332 	return 0;
333 }
334 
335 /* ARGSUSED */
336 int
337 mvsoc_bs_alloc(void *space, bus_addr_t reg_start, bus_addr_t reg_end,
338 	       bus_size_t size, bus_size_t alignment, bus_size_t boundary,
339 	       int flags, bus_addr_t *addrp, bus_space_handle_t *handlep)
340 {
341 
342 	panic("%s(): not implemented\n", __func__);
343 }
344 
345 /* ARGSUSED */
346 void
347 mvsoc_bs_free(void *space, bus_space_handle_t handle, bus_size_t size)
348 {
349 
350 	panic("%s(): not implemented\n", __func__);
351 }
352 
353 /* ARGSUSED */
354 void
355 mvsoc_bs_barrier(void *space, bus_space_handle_t handle, bus_size_t offset,
356 		 bus_size_t length, int flags)
357 {
358 
359 	/* Nothing to do. */
360 }
361 
362 /* ARGSUSED */
363 void *
364 mvsoc_bs_vaddr(void *space, bus_space_handle_t handle)
365 {
366 
367 	return (void *)handle;
368 }
369