bus_space.c revision 1.2 1 /* $NetBSD: bus_space.c,v 1.2 2006/06/01 14:18:41 tsutsui Exp $ */
2
3 /*-
4 * Copyright (c) 2001, 2004, 2005 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the NetBSD
18 * Foundation, Inc. and its contributors.
19 * 4. Neither the name of The NetBSD Foundation nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
24 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
27 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: bus_space.c,v 1.2 2006/06/01 14:18:41 tsutsui Exp $");
38
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/malloc.h>
42 #include <sys/extent.h>
43
44 #define _EWS4800MIPS_BUS_SPACE_PRIVATE
45 #include <machine/bus.h>
46 #include <machine/sbdvar.h>
47
48 #ifdef BUS_SPACE_DEBUG
49 int bus_space_debug = 0;
50 #define DPRINTF(fmt, args...) \
51 if (bus_space_debug) \
52 printf("%s: " fmt, __FUNCTION__ , ##args)
53 #define DPRINTFN(n, arg) \
54 if (bus_space_debug > (n)) \
55 printf("%s: " fmt, __FUNCTION__ , ##args)
56 #else
57 #define DPRINTF(arg...) ((void)0)
58 #define DPRINTFN(n, arg...) ((void)0)
59 #endif
60
61 #define VADDR(h, o) (h + o)
62 _BUS_SPACE_READ(_default, 1, 8)
63 _BUS_SPACE_READ(_default, 2, 16)
64 _BUS_SPACE_READ(_default, 4, 32)
65 _BUS_SPACE_READ(_default, 8, 64)
66 _BUS_SPACE_READ_MULTI(_default, 1, 8)
67 _BUS_SPACE_READ_MULTI(_default, 2, 16)
68 _BUS_SPACE_READ_MULTI(_default, 4, 32)
69 _BUS_SPACE_READ_MULTI(_default, 8, 64)
70 _BUS_SPACE_READ_REGION(_default, 1, 8)
71 _BUS_SPACE_READ_REGION(_default, 2, 16)
72 _BUS_SPACE_READ_REGION(_default, 4, 32)
73 _BUS_SPACE_READ_REGION(_default, 8, 64)
74 _BUS_SPACE_WRITE(_default, 1, 8)
75 _BUS_SPACE_WRITE(_default, 2, 16)
76 _BUS_SPACE_WRITE(_default, 4, 32)
77 _BUS_SPACE_WRITE(_default, 8, 64)
78 _BUS_SPACE_WRITE_MULTI(_default, 1, 8)
79 _BUS_SPACE_WRITE_MULTI(_default, 2, 16)
80 _BUS_SPACE_WRITE_MULTI(_default, 4, 32)
81 _BUS_SPACE_WRITE_MULTI(_default, 8, 64)
82 _BUS_SPACE_WRITE_REGION(_default, 1, 8)
83 _BUS_SPACE_WRITE_REGION(_default, 2, 16)
84 _BUS_SPACE_WRITE_REGION(_default, 4, 32)
85 _BUS_SPACE_WRITE_REGION(_default, 8, 64)
86 _BUS_SPACE_SET_MULTI(_default, 1, 8)
87 _BUS_SPACE_SET_MULTI(_default, 2, 16)
88 _BUS_SPACE_SET_MULTI(_default, 4, 32)
89 _BUS_SPACE_SET_MULTI(_default, 8, 64)
90 _BUS_SPACE_SET_REGION(_default, 1, 8)
91 _BUS_SPACE_SET_REGION(_default, 2, 16)
92 _BUS_SPACE_SET_REGION(_default, 4, 32)
93 _BUS_SPACE_SET_REGION(_default, 8, 64)
94 _BUS_SPACE_COPY_REGION(_default, 1, 8)
95 _BUS_SPACE_COPY_REGION(_default, 2, 16)
96 _BUS_SPACE_COPY_REGION(_default, 4, 32)
97 _BUS_SPACE_COPY_REGION(_default, 8, 64)
98 #undef VADDR
99
100 static int _default_map(void *, bus_addr_t, bus_size_t, int,
101 bus_space_handle_t *);
102 static void _default_unmap(void *, bus_space_handle_t, bus_size_t);
103 static int _default_subregion(void *, bus_space_handle_t, bus_size_t,
104 bus_size_t, bus_space_handle_t *);
105 static int _default_alloc(void *, bus_addr_t, bus_addr_t, bus_size_t,
106 bus_size_t, bus_size_t, int, bus_addr_t *, bus_space_handle_t *);
107 static void _default_free(void *, bus_space_handle_t, bus_size_t);
108 static void *_default_vaddr(void *, bus_space_handle_t);
109
110 static const struct ews4800mips_bus_space _default_bus_space = {
111 .ebs_map = _default_map,
112 .ebs_unmap = _default_unmap,
113 .ebs_subregion = _default_subregion,
114 .ebs_alloc = _default_alloc,
115 .ebs_free = _default_free,
116 .ebs_vaddr = _default_vaddr,
117 .ebs_r_1 = _default_read_1,
118 .ebs_r_2 = _default_read_2,
119 .ebs_r_4 = _default_read_4,
120 .ebs_r_8 = _default_read_8,
121 .ebs_rm_1 = _default_read_multi_1,
122 .ebs_rm_2 = _default_read_multi_2,
123 .ebs_rm_4 = _default_read_multi_4,
124 .ebs_rm_8 = _default_read_multi_8,
125 .ebs_rr_1 = _default_read_region_1,
126 .ebs_rr_2 = _default_read_region_2,
127 .ebs_rr_4 = _default_read_region_4,
128 .ebs_rr_8 = _default_read_region_8,
129 .ebs_w_1 = _default_write_1,
130 .ebs_w_2 = _default_write_2,
131 .ebs_w_4 = _default_write_4,
132 .ebs_w_8 = _default_write_8,
133 .ebs_wm_1 = _default_write_multi_1,
134 .ebs_wm_2 = _default_write_multi_2,
135 .ebs_wm_4 = _default_write_multi_4,
136 .ebs_wm_8 = _default_write_multi_8,
137 .ebs_wr_1 = _default_write_region_1,
138 .ebs_wr_2 = _default_write_region_2,
139 .ebs_wr_4 = _default_write_region_4,
140 .ebs_wr_8 = _default_write_region_8,
141 .ebs_sm_1 = _default_set_multi_1,
142 .ebs_sm_2 = _default_set_multi_2,
143 .ebs_sm_4 = _default_set_multi_4,
144 .ebs_sm_8 = _default_set_multi_8,
145 .ebs_sr_1 = _default_set_region_1,
146 .ebs_sr_2 = _default_set_region_2,
147 .ebs_sr_4 = _default_set_region_4,
148 .ebs_sr_8 = _default_set_region_8,
149 .ebs_c_1 = _default_copy_region_1,
150 .ebs_c_2 = _default_copy_region_2,
151 .ebs_c_4 = _default_copy_region_4,
152 .ebs_c_8 = _default_copy_region_8
153 };
154
155 /* create default bus_space_tag */
156 int
157 bus_space_create(bus_space_tag_t t, const char *name,
158 bus_addr_t addr, bus_size_t size)
159 {
160 struct ews4800mips_bus_space *ebs = t;
161
162 if (ebs == NULL)
163 return EFAULT;
164
165 /* set default method */
166 *ebs = _default_bus_space; /* structure assignment */
167 ebs->ebs_cookie = ebs;
168
169 /* set access region */
170 if (size == 0) {
171 /* no extent */
172 ebs->ebs_base_addr = addr;
173 ebs->ebs_size = size;
174 } else {
175 ebs->ebs_extent = extent_create(name, addr, addr + size - 1,
176 M_DEVBUF, 0, 0, EX_NOWAIT);
177 if (ebs->ebs_extent == NULL) {
178 panic("%s:: unable to create bus_space for "
179 "0x%08lx-%#lx", __FUNCTION__, addr, size);
180 }
181 }
182
183 return 0;
184 }
185
186 void
187 bus_space_destroy(bus_space_tag_t t)
188 {
189 struct ews4800mips_bus_space *ebs = t;
190 struct extent *ex = ebs->ebs_extent;
191
192 if (ex != NULL)
193 extent_destroy(ex);
194 }
195
196 void
197 _bus_space_invalid_access(void)
198 {
199
200 panic("invalid bus space access.");
201 }
202
203 /* default bus_space tag */
204 static int
205 _default_map(void *t, bus_addr_t bpa, bus_size_t size, int flags,
206 bus_space_handle_t *bshp)
207 {
208 struct ews4800mips_bus_space *ebs = t;
209 struct extent *ex = ebs->ebs_extent;
210 int error;
211
212 if (ex == NULL) {
213 if (bpa + size > ebs->ebs_size)
214 return EFAULT;
215 *bshp = (bus_space_handle_t)(ebs->ebs_base_addr + bpa);
216 return 0;
217 }
218
219 bpa += ex->ex_start;
220 error = extent_alloc_region(ex, bpa, size, EX_NOWAIT | EX_MALLOCOK);
221
222 if (error) {
223 DPRINTF("failed.\n");
224 return error;
225 }
226
227 *bshp = (bus_space_handle_t)bpa;
228
229 DPRINTF("success.\n");
230
231 return 0;
232 }
233
234 static int
235 _default_subregion(void *t, bus_space_handle_t bsh,
236 bus_size_t offset, bus_size_t size, bus_space_handle_t *nbshp)
237 {
238
239 *nbshp = bsh + offset;
240
241 return 0;
242 }
243
244 static int
245 _default_alloc(void *t, bus_addr_t rstart, bus_addr_t rend,
246 bus_size_t size, bus_size_t alignment, bus_size_t boundary,
247 int flags, bus_addr_t *bpap, bus_space_handle_t *bshp)
248 {
249 struct ews4800mips_bus_space *ebs = t;
250 struct extent *ex = ebs->ebs_extent;
251 u_long bpa, base;
252 int error;
253
254 if (ex == NULL) {
255 if (rend > ebs->ebs_size)
256 return EFAULT;
257 *bshp = *bpap = rstart + ebs->ebs_base_addr;
258 return 0;
259 }
260
261 base = ex->ex_start;
262
263 error = extent_alloc_subregion(ex, rstart + base, rend + base, size,
264 alignment, boundary,
265 EX_FAST | EX_NOWAIT | EX_MALLOCOK,
266 &bpa);
267
268 if (error) {
269 DPRINTF("failed.\n");
270 return error;
271 }
272
273 *bshp = (bus_space_handle_t)bpa;
274
275 if (bpap)
276 *bpap = bpa;
277
278 DPRINTF("success.\n");
279
280 return 0;
281 }
282
283 static void
284 _default_free(void *t, bus_space_handle_t bsh, bus_size_t size)
285 {
286 struct ews4800mips_bus_space *ebs = t;
287 struct extent *ex = ebs->ebs_extent;
288
289 if (ex != NULL)
290 _default_unmap(t, bsh, size);
291 }
292
293 static void
294 _default_unmap(void *t, bus_space_handle_t bsh, bus_size_t size)
295 {
296 struct ews4800mips_bus_space *ebs = t;
297 struct extent *ex = ebs->ebs_extent;
298 int error;
299
300 if (ex == NULL)
301 return;
302
303 error = extent_free(ex, bsh, size, EX_NOWAIT);
304
305 if (error) {
306 DPRINTF("%#lx-%#lx of %s space lost\n", bsh, bsh + size,
307 ex->ex_name);
308 }
309 }
310
311 void *
312 _default_vaddr(void *t, bus_space_handle_t h)
313 {
314
315 return (void *)h;
316 }
317