xref: /netbsd-src/sys/arch/arm/imx/imx_space.c (revision 30ba21540f737b3d029b915d1d262a41c81df6da)
1*30ba2154Sskrll /* $Id: imx_space.c,v 1.7 2023/04/21 15:00:48 skrll Exp $ */
29bea404cSbsh 
39bea404cSbsh /* derived from: */
4*30ba2154Sskrll /*	$NetBSD: imx_space.c,v 1.7 2023/04/21 15:00:48 skrll Exp $ */
59bea404cSbsh 
69bea404cSbsh /*
79bea404cSbsh  * Copyright (c) 2001, 2002 Wasabi Systems, Inc.
89bea404cSbsh  * All rights reserved.
99bea404cSbsh  *
109bea404cSbsh  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
119bea404cSbsh  *
129bea404cSbsh  * Redistribution and use in source and binary forms, with or without
139bea404cSbsh  * modification, are permitted provided that the following conditions
149bea404cSbsh  * are met:
159bea404cSbsh  * 1. Redistributions of source code must retain the above copyright
169bea404cSbsh  *    notice, this list of conditions and the following disclaimer.
179bea404cSbsh  * 2. Redistributions in binary form must reproduce the above copyright
189bea404cSbsh  *    notice, this list of conditions and the following disclaimer in the
199bea404cSbsh  *    documentation and/or other materials provided with the distribution.
209bea404cSbsh  * 3. All advertising materials mentioning features or use of this software
219bea404cSbsh  *    must display the following acknowledgement:
229bea404cSbsh  *	This product includes software developed for the NetBSD Project by
239bea404cSbsh  *	Wasabi Systems, Inc.
249bea404cSbsh  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
259bea404cSbsh  *    or promote products derived from this software without specific prior
269bea404cSbsh  *    written permission.
279bea404cSbsh  *
289bea404cSbsh  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
299bea404cSbsh  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
309bea404cSbsh  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
319bea404cSbsh  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
329bea404cSbsh  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
339bea404cSbsh  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
349bea404cSbsh  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
359bea404cSbsh  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
369bea404cSbsh  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
379bea404cSbsh  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
389bea404cSbsh  * POSSIBILITY OF SUCH DAMAGE.
399bea404cSbsh  */
409bea404cSbsh /*
419bea404cSbsh  * Copyright (c) 1997 Mark Brinicombe.
429bea404cSbsh  * Copyright (c) 1997 Causality Limited.
439bea404cSbsh  * All rights reserved.
449bea404cSbsh  *
459bea404cSbsh  * This code is derived from software contributed to The NetBSD Foundation
469bea404cSbsh  * by Ichiro FUKUHARA.
479bea404cSbsh  *
489bea404cSbsh  * Redistribution and use in source and binary forms, with or without
499bea404cSbsh  * modification, are permitted provided that the following conditions
509bea404cSbsh  * are met:
519bea404cSbsh  * 1. Redistributions of source code must retain the above copyright
529bea404cSbsh  *    notice, this list of conditions and the following disclaimer.
539bea404cSbsh  * 2. Redistributions in binary form must reproduce the above copyright
549bea404cSbsh  *    notice, this list of conditions and the following disclaimer in the
559bea404cSbsh  *    documentation and/or other materials provided with the distribution.
569bea404cSbsh  * 3. All advertising materials mentioning features or use of this software
579bea404cSbsh  *    must display the following acknowledgement:
589bea404cSbsh  *	This product includes software developed by Mark Brinicombe.
599bea404cSbsh  * 4. The name of the company nor the name of the author may be used to
609bea404cSbsh  *    endorse or promote products derived from this software without specific
619bea404cSbsh  *    prior written permission.
629bea404cSbsh  *
639bea404cSbsh  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
649bea404cSbsh  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
659bea404cSbsh  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
669bea404cSbsh  * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
679bea404cSbsh  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
689bea404cSbsh  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
699bea404cSbsh  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
709bea404cSbsh  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
719bea404cSbsh  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
729bea404cSbsh  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
739bea404cSbsh  * SUCH DAMAGE.
749bea404cSbsh  */
759bea404cSbsh 
769bea404cSbsh /*
779bea404cSbsh  * bus_space(9) support for Freescale iMX31 processor
789bea404cSbsh  */
799bea404cSbsh 
809bea404cSbsh #include <sys/param.h>
819bea404cSbsh #include <sys/systm.h>
829bea404cSbsh #include <uvm/uvm_extern.h>
83ed9977b1Sdyoung #include <sys/bus.h>
849bea404cSbsh 
85ca93c6f9Sbsh bs_protos(imx);
869bea404cSbsh bs_protos(generic);
879bea404cSbsh bs_protos(generic_armv4);
889bea404cSbsh bs_protos(bs_notimpl);
899bea404cSbsh 
90ca93c6f9Sbsh struct bus_space imx_bs_tag = {
919bea404cSbsh 	/* cookie */
92509197b6Sryo 	.bs_cookie = (void *) 0,
939bea404cSbsh 
949bea404cSbsh 	/* mapping/unmapping */
95509197b6Sryo 	.bs_map = imx_bs_map,
96509197b6Sryo 	.bs_unmap = imx_bs_unmap,
97509197b6Sryo 	.bs_subregion = imx_bs_subregion,
989bea404cSbsh 
999bea404cSbsh 	/* allocation/deallocation */
100509197b6Sryo 	.bs_alloc = imx_bs_alloc,	/* not implemented */
101509197b6Sryo 	.bs_free = imx_bs_free,		/* not implemented */
1029bea404cSbsh 
1039bea404cSbsh 	/* get kernel virtual address */
104509197b6Sryo 	.bs_vaddr = imx_bs_vaddr,
1059bea404cSbsh 
1069bea404cSbsh 	/* mmap */
107509197b6Sryo 	.bs_mmap = bs_notimpl_bs_mmap,
1089bea404cSbsh 
1099bea404cSbsh 	/* barrier */
110509197b6Sryo 	.bs_barrier = imx_bs_barrier,
1119bea404cSbsh 
1129bea404cSbsh 	/* read (single) */
113509197b6Sryo 	.bs_r_1 = generic_bs_r_1,
114509197b6Sryo 	.bs_r_2 = generic_armv4_bs_r_2,
115509197b6Sryo 	.bs_r_4 = generic_bs_r_4,
116509197b6Sryo 	.bs_r_8 = bs_notimpl_bs_r_8,
1179bea404cSbsh 
1189bea404cSbsh 	/* read multiple */
119509197b6Sryo 	.bs_rm_1 = generic_bs_rm_1,
120509197b6Sryo 	.bs_rm_2 = generic_armv4_bs_rm_2,
121509197b6Sryo 	.bs_rm_4 = generic_bs_rm_4,
122509197b6Sryo 	.bs_rm_8 = bs_notimpl_bs_rm_8,
1239bea404cSbsh 
1249bea404cSbsh 	/* read region */
125509197b6Sryo 	.bs_rr_1 = generic_bs_rr_1,
126509197b6Sryo 	.bs_rr_2 = generic_armv4_bs_rr_2,
127509197b6Sryo 	.bs_rr_4 = generic_bs_rr_4,
128509197b6Sryo 	.bs_rr_8 = bs_notimpl_bs_rr_8,
1299bea404cSbsh 
1309bea404cSbsh 	/* write (single) */
131509197b6Sryo 	.bs_w_1 = generic_bs_w_1,
132509197b6Sryo 	.bs_w_2 = generic_armv4_bs_w_2,
133509197b6Sryo 	.bs_w_4 = generic_bs_w_4,
134509197b6Sryo 	.bs_w_8 = bs_notimpl_bs_w_8,
1359bea404cSbsh 
1369bea404cSbsh 	/* write multiple */
137509197b6Sryo 	.bs_wm_1 = generic_bs_wm_1,
138509197b6Sryo 	.bs_wm_2 = generic_armv4_bs_wm_2,
139509197b6Sryo 	.bs_wm_4 = generic_bs_wm_4,
140509197b6Sryo 	.bs_wm_8 = bs_notimpl_bs_wm_8,
1419bea404cSbsh 
1429bea404cSbsh 	/* write region */
143509197b6Sryo 	.bs_wr_1 = generic_bs_wr_1,
144509197b6Sryo 	.bs_wr_2 = generic_armv4_bs_wr_2,
145509197b6Sryo 	.bs_wr_4 = generic_bs_wr_4,
146509197b6Sryo 	.bs_wr_8 = bs_notimpl_bs_wr_8,
1479bea404cSbsh 
1489bea404cSbsh 	/* set multiple */
149509197b6Sryo 	.bs_sm_1 = bs_notimpl_bs_sm_1,
150509197b6Sryo 	.bs_sm_2 = bs_notimpl_bs_sm_2,
151509197b6Sryo 	.bs_sm_4 = bs_notimpl_bs_sm_4,
152509197b6Sryo 	.bs_sm_8 = bs_notimpl_bs_sm_8,
1539bea404cSbsh 
1549bea404cSbsh 	/* set region */
155509197b6Sryo 	.bs_sr_1 = generic_bs_sr_1,
156509197b6Sryo 	.bs_sr_2 = generic_armv4_bs_sr_2,
157509197b6Sryo 	.bs_sr_4 = bs_notimpl_bs_sr_4,
158509197b6Sryo 	.bs_sr_8 = bs_notimpl_bs_sr_8,
1599bea404cSbsh 
1609bea404cSbsh 	/* copy */
161509197b6Sryo 	.bs_c_1 = bs_notimpl_bs_c_1,
162509197b6Sryo 	.bs_c_2 = generic_armv4_bs_c_2,
163509197b6Sryo 	.bs_c_4 = bs_notimpl_bs_c_4,
164509197b6Sryo 	.bs_c_8 = bs_notimpl_bs_c_8,
165891c2536Smatt 
166891c2536Smatt #ifdef __BUS_SPACE_HAS_STREAM_METHODS
167891c2536Smatt 	/* read (single) */
168509197b6Sryo 	.bs_r_1_s = generic_bs_r_1,
169509197b6Sryo 	.bs_r_2_s = generic_armv4_bs_r_2,
170509197b6Sryo 	.bs_r_4_s = generic_bs_r_4,
171509197b6Sryo 	.bs_r_8_s = bs_notimpl_bs_r_8,
172891c2536Smatt 
173891c2536Smatt 	/* read multiple */
174509197b6Sryo 	.bs_rm_1_s = generic_bs_rm_1,
175509197b6Sryo 	.bs_rm_2_s = generic_armv4_bs_rm_2,
176509197b6Sryo 	.bs_rm_4_s = generic_bs_rm_4,
177509197b6Sryo 	.bs_rm_8_s = bs_notimpl_bs_rm_8,
178891c2536Smatt 
179891c2536Smatt 	/* read region */
180509197b6Sryo 	.bs_rr_1_s = generic_bs_rr_1,
181509197b6Sryo 	.bs_rr_2_s = generic_armv4_bs_rr_2,
182509197b6Sryo 	.bs_rr_4_s = generic_bs_rr_4,
183509197b6Sryo 	.bs_rr_8_s = bs_notimpl_bs_rr_8,
184891c2536Smatt 
185891c2536Smatt 	/* write (single) */
186509197b6Sryo 	.bs_w_1_s = generic_bs_w_1,
187509197b6Sryo 	.bs_w_2_s = generic_armv4_bs_w_2,
188509197b6Sryo 	.bs_w_4_s = generic_bs_w_4,
189509197b6Sryo 	.bs_w_8_s = bs_notimpl_bs_w_8,
190891c2536Smatt 
191891c2536Smatt 	/* write multiple */
192509197b6Sryo 	.bs_wm_1_s = generic_bs_wm_1,
193509197b6Sryo 	.bs_wm_2_s = generic_armv4_bs_wm_2,
194509197b6Sryo 	.bs_wm_4_s = generic_bs_wm_4,
195509197b6Sryo 	.bs_wm_8_s = bs_notimpl_bs_wm_8,
196891c2536Smatt 
197891c2536Smatt 	/* write region */
198509197b6Sryo 	.bs_wr_1_s = generic_bs_wr_1,
199509197b6Sryo 	.bs_wr_2_s = generic_armv4_bs_wr_2,
200509197b6Sryo 	.bs_wr_4_s = generic_bs_wr_4,
201509197b6Sryo 	.bs_wr_8_s = bs_notimpl_bs_wr_8,
202891c2536Smatt #endif
2039bea404cSbsh };
2049bea404cSbsh 
2059bea404cSbsh int
imx_bs_map(void * t,bus_addr_t bpa,bus_size_t size,int flag,bus_space_handle_t * bshp)206ca93c6f9Sbsh imx_bs_map(void *t, bus_addr_t bpa, bus_size_t size,
2079bea404cSbsh 	      int flag, bus_space_handle_t *bshp)
2089bea404cSbsh {
2099bea404cSbsh 	const struct pmap_devmap	*pd;
210f8486b3bSmatt 	paddr_t startpa, endpa, pa;
211f8486b3bSmatt 	vaddr_t va;
2129bea404cSbsh 
2139bea404cSbsh 	if ((pd = pmap_devmap_find_pa(bpa, size)) != NULL) {
2149bea404cSbsh 		/* Device was statically mapped. */
2159bea404cSbsh 		*bshp = pd->pd_va + (bpa - pd->pd_pa);
2169bea404cSbsh 		return 0;
2179bea404cSbsh 	}
2189bea404cSbsh 
2199bea404cSbsh 	startpa = trunc_page(bpa);
2209bea404cSbsh 	endpa = round_page(bpa + size);
2219bea404cSbsh 
2229bea404cSbsh 	/* XXX use extent manager to check duplicate mapping */
2239bea404cSbsh 
2249bea404cSbsh 	va = uvm_km_alloc(kernel_map, endpa - startpa, 0,
2259bea404cSbsh 	    UVM_KMF_VAONLY | UVM_KMF_NOWAIT);
2269bea404cSbsh 	if (! va)
2279bea404cSbsh 		return(ENOMEM);
2289bea404cSbsh 
2299bea404cSbsh 	*bshp = (bus_space_handle_t)(va + (bpa - startpa));
2309bea404cSbsh 
2319bea404cSbsh 	for (pa = startpa; pa < endpa; pa += PAGE_SIZE, va += PAGE_SIZE) {
232891c2536Smatt 		pmap_kenter_pa(va, pa, VM_PROT_READ | VM_PROT_WRITE,
233891c2536Smatt 		    (flag & BUS_SPACE_MAP_CACHEABLE) ? 0 : PMAP_NOCACHE);
2349bea404cSbsh 	}
2359bea404cSbsh 	pmap_update(pmap_kernel());
2369bea404cSbsh 
2379bea404cSbsh 	return(0);
2389bea404cSbsh }
2399bea404cSbsh 
2409bea404cSbsh void
imx_bs_unmap(void * t,bus_space_handle_t bsh,bus_size_t size)241ca93c6f9Sbsh imx_bs_unmap(void *t, bus_space_handle_t bsh, bus_size_t size)
2429bea404cSbsh {
2439bea404cSbsh 	vaddr_t	va;
2449bea404cSbsh 	vsize_t	sz;
2459bea404cSbsh 
2469bea404cSbsh 	if (pmap_devmap_find_va(bsh, size) != NULL) {
2479bea404cSbsh 		/* Device was statically mapped; nothing to do. */
2489bea404cSbsh 		return;
2499bea404cSbsh 	}
2509bea404cSbsh 
2519bea404cSbsh 	va = trunc_page(bsh);
2529bea404cSbsh 	sz = round_page(bsh + size) - va;
2539bea404cSbsh 
2549bea404cSbsh 	pmap_kremove(va, sz);
2559bea404cSbsh 	pmap_update(pmap_kernel());
2569bea404cSbsh 	uvm_km_free(kernel_map, va, sz, UVM_KMF_VAONLY);
2579bea404cSbsh }
2589bea404cSbsh 
2599bea404cSbsh 
2609bea404cSbsh int
imx_bs_subregion(void * t,bus_space_handle_t bsh,bus_size_t offset,bus_size_t size,bus_space_handle_t * nbshp)261ca93c6f9Sbsh imx_bs_subregion(void *t, bus_space_handle_t bsh, bus_size_t offset,
2629bea404cSbsh     bus_size_t size, bus_space_handle_t *nbshp)
2639bea404cSbsh {
2649bea404cSbsh 
2659bea404cSbsh 	*nbshp = bsh + offset;
2669bea404cSbsh 	return (0);
2679bea404cSbsh }
2689bea404cSbsh 
2699bea404cSbsh void
imx_bs_barrier(void * t,bus_space_handle_t bsh,bus_size_t offset,bus_size_t len,int flags)270ca93c6f9Sbsh imx_bs_barrier(void *t, bus_space_handle_t bsh, bus_size_t offset,
2719bea404cSbsh     bus_size_t len, int flags)
2729bea404cSbsh {
2739bea404cSbsh 
2749bea404cSbsh 	/* Nothing to do. */
2759bea404cSbsh }
2769bea404cSbsh 
2779bea404cSbsh void *
imx_bs_vaddr(void * t,bus_space_handle_t bsh)278ca93c6f9Sbsh imx_bs_vaddr(void *t, bus_space_handle_t bsh)
2799bea404cSbsh {
2809bea404cSbsh 
2819bea404cSbsh 	return ((void *)bsh);
2829bea404cSbsh }
2839bea404cSbsh 
2849bea404cSbsh 
2859bea404cSbsh int
imx_bs_alloc(void * t,bus_addr_t rstart,bus_addr_t rend,bus_size_t size,bus_size_t alignment,bus_size_t boundary,int flags,bus_addr_t * bpap,bus_space_handle_t * bshp)286ca93c6f9Sbsh imx_bs_alloc(void *t, bus_addr_t rstart, bus_addr_t rend,
2879bea404cSbsh     bus_size_t size, bus_size_t alignment, bus_size_t boundary, int flags,
2889bea404cSbsh     bus_addr_t *bpap, bus_space_handle_t *bshp)
2899bea404cSbsh {
2909bea404cSbsh 
291ca93c6f9Sbsh 	panic("imx_io_bs_alloc(): not implemented\n");
2929bea404cSbsh }
2939bea404cSbsh 
2949bea404cSbsh void
imx_bs_free(void * t,bus_space_handle_t bsh,bus_size_t size)295ca93c6f9Sbsh imx_bs_free(void *t, bus_space_handle_t bsh, bus_size_t size)
2969bea404cSbsh {
2979bea404cSbsh 
298ca93c6f9Sbsh 	panic("imx_io_bs_free(): not implemented\n");
2999bea404cSbsh }
3009bea404cSbsh 
3019bea404cSbsh 
302