xref: /netbsd-src/sys/arch/arm/ixp12x0/ixp12x0_io.c (revision 2d48ac808c43ea6701ba8f33cfc3645685301f79)
1 /*	$NetBSD: ixp12x0_io.c,v 1.12 2009/03/14 21:04:05 dsl Exp $ */
2 
3 /*
4  * Copyright (c) 2002, 2003
5  *	Ichiro FUKUHARA <ichiro@ichiro.org>.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by Ichiro FUKUHARA.
19  * 4. The name of the company nor the name of the author may be used to
20  *    endorse or promote products derived from this software without specific
21  *    prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY ICHIRO FUKUHARA ``AS IS'' AND ANY EXPRESS OR
24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26  * IN NO EVENT SHALL ICHIRO FUKUHARA OR THE VOICES IN HIS HEAD BE LIABLE FOR
27  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: ixp12x0_io.c,v 1.12 2009/03/14 21:04:05 dsl Exp $");
38 
39 /*
40  * bus_space I/O functions for ixp12x0
41  */
42 
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/queue.h>
46 
47 #include <uvm/uvm.h>
48 
49 #include <machine/bus.h>
50 
51 #include <arm/ixp12x0/ixp12x0reg.h>
52 #include <arm/ixp12x0/ixp12x0var.h>
53 
54 /* Proto types for all the bus_space structure functions */
55 bs_protos(ixp12x0);
56 bs_protos(generic);
57 bs_protos(generic_armv4);
58 bs_protos(bs_notimpl);
59 
60 struct bus_space ixp12x0_bs_tag = {
61 	/* cookie */
62 	(void *) 0,
63 
64 	/* mapping/unmapping */
65 	ixp12x0_bs_map,
66 	ixp12x0_bs_unmap,
67 	ixp12x0_bs_subregion,
68 
69 	/* allocation/deallocation */
70 	ixp12x0_bs_alloc,
71 	ixp12x0_bs_free,
72 
73 	/* get kernel virtual address */
74 	ixp12x0_bs_vaddr,
75 
76 	/* mmap bus space for userland */
77 	bs_notimpl_bs_mmap,
78 
79 	/* barrier */
80 	ixp12x0_bs_barrier,
81 
82 	/* read (single) */
83 	generic_bs_r_1,
84 	generic_armv4_bs_r_2,
85 	generic_bs_r_4,
86 	bs_notimpl_bs_r_8,
87 
88 	/* read multiple */
89 	generic_bs_rm_1,
90 	generic_armv4_bs_rm_2,
91 	generic_bs_rm_4,
92 	bs_notimpl_bs_rm_8,
93 
94 	/* read region */
95 	generic_bs_rr_1,
96 	generic_armv4_bs_rr_2,
97 	generic_bs_rr_4,
98 	bs_notimpl_bs_rr_8,
99 
100 	/* write (single) */
101 	generic_bs_w_1,
102 	generic_armv4_bs_w_2,
103 	generic_bs_w_4,
104 	bs_notimpl_bs_w_8,
105 
106 	/* write multiple */
107 	generic_bs_wm_1,
108 	generic_armv4_bs_wm_2,
109 	generic_bs_wm_4,
110 	bs_notimpl_bs_wm_8,
111 
112 	/* write region */
113 	generic_bs_wr_1,
114 	generic_armv4_bs_wr_2,
115 	generic_bs_wr_4,
116 	bs_notimpl_bs_wr_8,
117 
118 	/* set multiple */
119 	bs_notimpl_bs_sm_1,
120 	bs_notimpl_bs_sm_2,
121 	bs_notimpl_bs_sm_4,
122 	bs_notimpl_bs_sm_8,
123 
124 	/* set region */
125 	bs_notimpl_bs_sr_1,
126 	generic_armv4_bs_sr_2,
127 	generic_bs_sr_4,
128 	bs_notimpl_bs_sr_8,
129 
130 	/* copy */
131 	bs_notimpl_bs_c_1,
132 	generic_armv4_bs_c_2,
133 	bs_notimpl_bs_c_4,
134 	bs_notimpl_bs_c_8,
135 };
136 
137 /* Common routines */
138 
139 int
140 ixp12x0_bs_map(void *t, bus_addr_t bpa, bus_size_t size,
141 	       int flags, bus_space_handle_t *bshp)
142 {
143 	const struct pmap_devmap	*pd;
144 
145 	paddr_t		startpa;
146 	paddr_t		endpa;
147 	paddr_t		pa;
148 	paddr_t		offset;
149 	vaddr_t		va;
150 	pt_entry_t	*pte;
151 
152 	if ((pd = pmap_devmap_find_pa(bpa, size)) != NULL) {
153 		/* Device was statically mapped. */
154 		*bshp = pd->pd_va + (bpa - pd->pd_pa);
155 		return 0;
156 	}
157 
158 	endpa = round_page(bpa + size);
159 	offset = bpa & PAGE_MASK;
160 	startpa = trunc_page(bpa);
161 
162 	va = uvm_km_alloc(kernel_map, endpa - startpa, 0,
163 	    UVM_KMF_VAONLY | UVM_KMF_NOWAIT);
164 	if (va  == 0)
165 		return ENOMEM;
166 
167 	*bshp = va + offset;
168 
169 	for (pa = startpa; pa < endpa; pa += PAGE_SIZE, va += PAGE_SIZE) {
170 		pmap_kenter_pa(va, pa, VM_PROT_READ | VM_PROT_WRITE);
171 		pte = vtopte(va);
172 		*pte &= ~L2_S_CACHE_MASK;
173 		PTE_SYNC(pte);
174 	}
175 	pmap_update(pmap_kernel());
176 
177 	return 0;
178 }
179 
180 void
181 ixp12x0_bs_unmap(void *t, bus_space_handle_t bsh, bus_size_t size)
182 {
183 	vaddr_t	va;
184 	vaddr_t	endva;
185 
186 	if (pmap_devmap_find_va(bsh, size) != NULL) {
187 		/* Device was statically mapped; nothing to do. */
188 		return;
189 	}
190 
191 	endva = round_page(bsh + size);
192 	va = trunc_page(bsh);
193 
194 	pmap_kremove(va, endva - va);
195 	uvm_km_free(kernel_map, va, endva - va, UVM_KMF_VAONLY);
196 }
197 
198 int
199 ixp12x0_bs_subregion(void *t, bus_space_handle_t bsh, bus_size_t offset, bus_size_t size, bus_space_handle_t *nbshp)
200 {
201 
202 	*nbshp = bsh + offset;
203 	return (0);
204 }
205 
206 void *
207 ixp12x0_bs_vaddr(void *t, bus_space_handle_t bsh)
208 {
209 	return ((void *)bsh);
210 }
211 
212 int
213 ixp12x0_bs_alloc(void *t,
214 		 bus_addr_t rstart, bus_addr_t rend, bus_size_t size,
215 		 bus_size_t alignment, bus_size_t boundary,
216 		 int flags, bus_addr_t *bpap,
217 		 bus_space_handle_t *bshp)
218 {
219 	panic("ixp12x0_bs_alloc(): not implemented\n");
220 }
221 
222 void
223 ixp12x0_bs_free(void *t, bus_space_handle_t bsh, bus_size_t size)
224 {
225 	panic("ixp12x0_bs_free(): not implemented\n");
226 }
227 
228 void
229 ixp12x0_bs_barrier(void *t, bus_space_handle_t bsh, bus_size_t offset, bus_size_t len, int flags)
230 {
231 /* NULL */
232 }
233 
234 /* End of ixp12x0_io.c */
235