ifpga_io.c revision 1.10 1 1.10 dyoung /* $NetBSD: ifpga_io.c,v 1.10 2011/07/01 20:39:34 dyoung Exp $ */
2 1.1 rearnsha
3 1.1 rearnsha /*
4 1.1 rearnsha * Copyright (c) 1997 Causality Limited
5 1.1 rearnsha * Copyright (c) 1997 Mark Brinicombe.
6 1.1 rearnsha * All rights reserved.
7 1.1 rearnsha *
8 1.1 rearnsha * Redistribution and use in source and binary forms, with or without
9 1.1 rearnsha * modification, are permitted provided that the following conditions
10 1.1 rearnsha * are met:
11 1.1 rearnsha * 1. Redistributions of source code must retain the above copyright
12 1.1 rearnsha * notice, this list of conditions and the following disclaimer.
13 1.1 rearnsha * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 rearnsha * notice, this list of conditions and the following disclaimer in the
15 1.1 rearnsha * documentation and/or other materials provided with the distribution.
16 1.1 rearnsha * 3. All advertising materials mentioning features or use of this software
17 1.1 rearnsha * must display the following acknowledgement:
18 1.1 rearnsha * This product includes software developed by Mark Brinicombe
19 1.1 rearnsha * for the NetBSD Project.
20 1.1 rearnsha * 4. The name of the company nor the name of the author may be used to
21 1.1 rearnsha * endorse or promote products derived from this software without specific
22 1.1 rearnsha * prior written permission.
23 1.1 rearnsha *
24 1.1 rearnsha * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
25 1.1 rearnsha * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26 1.1 rearnsha * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 1.1 rearnsha * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
28 1.1 rearnsha * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29 1.1 rearnsha * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
30 1.1 rearnsha * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 1.1 rearnsha * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 1.1 rearnsha * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 1.1 rearnsha * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 1.1 rearnsha * SUCH DAMAGE.
35 1.1 rearnsha *
36 1.1 rearnsha * From arm/footbridge/footbridge_io.c
37 1.1 rearnsha */
38 1.1 rearnsha
39 1.1 rearnsha /*
40 1.1 rearnsha * bus_space I/O functions for IFPGA
41 1.1 rearnsha */
42 1.6 lukem
43 1.6 lukem #include <sys/cdefs.h>
44 1.10 dyoung __KERNEL_RCSID(0, "$NetBSD: ifpga_io.c,v 1.10 2011/07/01 20:39:34 dyoung Exp $");
45 1.1 rearnsha
46 1.1 rearnsha #include <sys/param.h>
47 1.1 rearnsha #include <sys/systm.h>
48 1.10 dyoung #include <sys/bus.h>
49 1.1 rearnsha #include <uvm/uvm_extern.h>
50 1.4 thorpej
51 1.4 thorpej #include <evbarm/ifpga/ifpgavar.h>
52 1.1 rearnsha
53 1.1 rearnsha /* Proto types for all the bus_space structure functions */
54 1.1 rearnsha
55 1.1 rearnsha bs_protos(ifpga);
56 1.2 thorpej bs_protos(generic);
57 1.2 thorpej bs_protos(generic_armv4);
58 1.1 rearnsha bs_protos(bs_notimpl);
59 1.1 rearnsha bs_map_proto(ifpga_mem);
60 1.1 rearnsha bs_unmap_proto(ifpga_mem);
61 1.1 rearnsha
62 1.1 rearnsha /* Declare the ifpga bus space tag */
63 1.1 rearnsha
64 1.1 rearnsha struct bus_space ifpga_bs_tag = {
65 1.1 rearnsha /* cookie */
66 1.1 rearnsha (void *) 0, /* Physical base address */
67 1.1 rearnsha
68 1.1 rearnsha /* mapping/unmapping */
69 1.1 rearnsha ifpga_bs_map,
70 1.1 rearnsha ifpga_bs_unmap,
71 1.1 rearnsha ifpga_bs_subregion,
72 1.1 rearnsha
73 1.1 rearnsha /* allocation/deallocation */
74 1.1 rearnsha ifpga_bs_alloc,
75 1.1 rearnsha ifpga_bs_free,
76 1.1 rearnsha
77 1.1 rearnsha /* get kernel virtual address */
78 1.1 rearnsha ifpga_bs_vaddr,
79 1.1 rearnsha
80 1.1 rearnsha /* mmap */
81 1.1 rearnsha bs_notimpl_bs_mmap,
82 1.1 rearnsha
83 1.1 rearnsha /* barrier */
84 1.1 rearnsha ifpga_bs_barrier,
85 1.1 rearnsha
86 1.1 rearnsha /* read (single) */
87 1.2 thorpej generic_bs_r_1,
88 1.2 thorpej generic_armv4_bs_r_2,
89 1.2 thorpej generic_bs_r_4,
90 1.1 rearnsha bs_notimpl_bs_r_8,
91 1.1 rearnsha
92 1.1 rearnsha /* read multiple */
93 1.2 thorpej generic_bs_rm_1,
94 1.2 thorpej generic_armv4_bs_rm_2,
95 1.2 thorpej generic_bs_rm_4,
96 1.1 rearnsha bs_notimpl_bs_rm_8,
97 1.1 rearnsha
98 1.1 rearnsha /* read region */
99 1.1 rearnsha bs_notimpl_bs_rr_1,
100 1.2 thorpej generic_armv4_bs_rr_2,
101 1.2 thorpej generic_bs_rr_4,
102 1.1 rearnsha bs_notimpl_bs_rr_8,
103 1.1 rearnsha
104 1.1 rearnsha /* write (single) */
105 1.2 thorpej generic_bs_w_1,
106 1.2 thorpej generic_armv4_bs_w_2,
107 1.2 thorpej generic_bs_w_4,
108 1.1 rearnsha bs_notimpl_bs_w_8,
109 1.1 rearnsha
110 1.1 rearnsha /* write multiple */
111 1.2 thorpej generic_bs_wm_1,
112 1.2 thorpej generic_armv4_bs_wm_2,
113 1.2 thorpej generic_bs_wm_4,
114 1.1 rearnsha bs_notimpl_bs_wm_8,
115 1.1 rearnsha
116 1.1 rearnsha /* write region */
117 1.1 rearnsha bs_notimpl_bs_wr_1,
118 1.2 thorpej generic_armv4_bs_wr_2,
119 1.2 thorpej generic_bs_wr_4,
120 1.1 rearnsha bs_notimpl_bs_wr_8,
121 1.1 rearnsha
122 1.1 rearnsha /* set multiple */
123 1.1 rearnsha bs_notimpl_bs_sm_1,
124 1.1 rearnsha bs_notimpl_bs_sm_2,
125 1.1 rearnsha bs_notimpl_bs_sm_4,
126 1.1 rearnsha bs_notimpl_bs_sm_8,
127 1.1 rearnsha
128 1.1 rearnsha /* set region */
129 1.1 rearnsha bs_notimpl_bs_sr_1,
130 1.2 thorpej generic_armv4_bs_sr_2,
131 1.1 rearnsha bs_notimpl_bs_sr_4,
132 1.1 rearnsha bs_notimpl_bs_sr_8,
133 1.1 rearnsha
134 1.1 rearnsha /* copy */
135 1.1 rearnsha bs_notimpl_bs_c_1,
136 1.2 thorpej generic_armv4_bs_c_2,
137 1.1 rearnsha bs_notimpl_bs_c_4,
138 1.1 rearnsha bs_notimpl_bs_c_8,
139 1.1 rearnsha };
140 1.1 rearnsha
141 1.1 rearnsha void ifpga_create_io_bs_tag(t, cookie)
142 1.1 rearnsha struct bus_space *t;
143 1.1 rearnsha void *cookie;
144 1.1 rearnsha {
145 1.1 rearnsha *t = ifpga_bs_tag;
146 1.1 rearnsha t->bs_cookie = cookie;
147 1.1 rearnsha }
148 1.1 rearnsha
149 1.1 rearnsha void ifpga_create_mem_bs_tag(t, cookie)
150 1.1 rearnsha struct bus_space *t;
151 1.1 rearnsha void *cookie;
152 1.1 rearnsha {
153 1.1 rearnsha *t = ifpga_bs_tag;
154 1.1 rearnsha t->bs_map = ifpga_mem_bs_map;
155 1.1 rearnsha t->bs_unmap = ifpga_mem_bs_unmap;
156 1.1 rearnsha t->bs_cookie = cookie;
157 1.1 rearnsha }
158 1.1 rearnsha
159 1.1 rearnsha /* bus space functions */
160 1.1 rearnsha
161 1.1 rearnsha int
162 1.9 dsl ifpga_bs_map(void *t, bus_addr_t bpa, bus_size_t size, int cacheable, bus_space_handle_t *bshp)
163 1.1 rearnsha {
164 1.1 rearnsha /* The cookie is the base address for the I/O area */
165 1.1 rearnsha *bshp = bpa + (bus_addr_t)t;
166 1.1 rearnsha return 0;
167 1.1 rearnsha }
168 1.1 rearnsha
169 1.1 rearnsha int
170 1.9 dsl ifpga_mem_bs_map(void *t, bus_addr_t bpa, bus_size_t size, int cacheable, bus_space_handle_t *bshp)
171 1.1 rearnsha {
172 1.1 rearnsha bus_addr_t startpa, endpa;
173 1.1 rearnsha vaddr_t va;
174 1.1 rearnsha
175 1.1 rearnsha /* Round the allocation to page boundries */
176 1.1 rearnsha startpa = trunc_page(bpa);
177 1.1 rearnsha endpa = round_page(bpa + size);
178 1.1 rearnsha
179 1.1 rearnsha /* Get some VM. */
180 1.8 yamt va = uvm_km_alloc(kernel_map, endpa - startpa, 0,
181 1.8 yamt UVM_KMF_VAONLY | UVM_KMF_NOWAIT);
182 1.1 rearnsha if (va == 0)
183 1.1 rearnsha return ENOMEM;
184 1.1 rearnsha
185 1.1 rearnsha /* Store the bus space handle */
186 1.1 rearnsha *bshp = va + (bpa & PGOFSET);
187 1.1 rearnsha
188 1.1 rearnsha /* Now map the pages */
189 1.1 rearnsha /* The cookie is the physical base address for the I/O area */
190 1.1 rearnsha while (startpa < endpa) {
191 1.1 rearnsha /* XXX pmap_kenter_pa maps pages cacheable -- not what
192 1.1 rearnsha we want. */
193 1.1 rearnsha pmap_enter(pmap_kernel(), va, (bus_addr_t)t + startpa,
194 1.1 rearnsha VM_PROT_READ | VM_PROT_WRITE, 0);
195 1.5 thorpej va += PAGE_SIZE;
196 1.5 thorpej startpa += PAGE_SIZE;
197 1.1 rearnsha }
198 1.1 rearnsha pmap_update(pmap_kernel());
199 1.1 rearnsha
200 1.1 rearnsha return 0;
201 1.1 rearnsha }
202 1.1 rearnsha
203 1.1 rearnsha int
204 1.1 rearnsha ifpga_bs_alloc(t, rstart, rend, size, alignment, boundary, cacheable,
205 1.1 rearnsha bpap, bshp)
206 1.1 rearnsha void *t;
207 1.1 rearnsha bus_addr_t rstart, rend;
208 1.1 rearnsha bus_size_t size, alignment, boundary;
209 1.1 rearnsha int cacheable;
210 1.1 rearnsha bus_addr_t *bpap;
211 1.1 rearnsha bus_space_handle_t *bshp;
212 1.1 rearnsha {
213 1.3 provos panic("ifpga_alloc(): Help!");
214 1.1 rearnsha }
215 1.1 rearnsha
216 1.1 rearnsha
217 1.1 rearnsha void
218 1.9 dsl ifpga_bs_unmap(void *t, bus_space_handle_t bsh, bus_size_t size)
219 1.1 rearnsha {
220 1.1 rearnsha /* Nothing to do for an io map. */
221 1.1 rearnsha }
222 1.1 rearnsha
223 1.1 rearnsha void
224 1.9 dsl ifpga_mem_bs_unmap(void *t, bus_space_handle_t bsh, bus_size_t size)
225 1.1 rearnsha {
226 1.1 rearnsha vaddr_t startva, endva;
227 1.1 rearnsha
228 1.1 rearnsha startva = trunc_page(bsh);
229 1.1 rearnsha endva = round_page(bsh + size);
230 1.1 rearnsha
231 1.7 yamt pmap_remove(pmap_kernel(), startva, endva);
232 1.7 yamt pmap_update(pmap_kernel());
233 1.7 yamt uvm_km_free(kernel_map, startva, endva - startva, UVM_KMF_VAONLY);
234 1.1 rearnsha }
235 1.1 rearnsha
236 1.1 rearnsha void
237 1.9 dsl ifpga_bs_free(void *t, bus_space_handle_t bsh, bus_size_t size)
238 1.1 rearnsha {
239 1.1 rearnsha
240 1.3 provos panic("ifpga_free(): Help!");
241 1.1 rearnsha /* ifpga_bs_unmap() does all that we need to do. */
242 1.1 rearnsha /* ifpga_bs_unmap(t, bsh, size);*/
243 1.1 rearnsha }
244 1.1 rearnsha
245 1.1 rearnsha int
246 1.9 dsl ifpga_bs_subregion(void *t, bus_space_handle_t bsh, bus_size_t offset, bus_size_t size, bus_space_handle_t *nbshp)
247 1.1 rearnsha {
248 1.1 rearnsha
249 1.1 rearnsha *nbshp = bsh + (offset << ((int)t));
250 1.1 rearnsha return (0);
251 1.1 rearnsha }
252 1.1 rearnsha
253 1.1 rearnsha void *
254 1.9 dsl ifpga_bs_vaddr(void *t, bus_space_handle_t bsh)
255 1.1 rearnsha {
256 1.1 rearnsha
257 1.1 rearnsha return ((void *)bsh);
258 1.1 rearnsha }
259 1.1 rearnsha
260 1.1 rearnsha void
261 1.9 dsl ifpga_bs_barrier(void *t, bus_space_handle_t bsh, bus_size_t offset, bus_size_t len, int flags)
262 1.1 rearnsha {
263 1.1 rearnsha }
264