4 * Copyright (c) 2007 David Young. All rights reserved.
6 * Redistribution and use in source and binary forms, with or
7 * without modification, are permitted provided that the following
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
12 * copyright notice, this list of conditions and the following
13 * disclaimer in the documentation and/or other materials provided
14 * with the distribution.
15 * 3. The name of the author may not be used to endorse or promote
16 * products derived from this software without specific prior
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY
20 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
24 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
26 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
28 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: pb1000_obio.c,v 1.8 2005/08/26 13:19:35 drochner Exp $");
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/device.h>
39 #include <machine/bus.h>
41 #include <mips/cache.h>
42 #include <mips/cpuregs.h>
44 #include <mips/adm5120/include/adm5120reg.h>
45 #include <mips/adm5120/include/adm5120var.h>
46 #include <mips/adm5120/include/adm5120_mainbusvar.h>
47 #include <mips/adm5120/include/adm5120_extiovar.h>
51 #define CFIO_DPRINTF(__fmt, ...) \
54 printf((__fmt), __VA_ARGS__); \
55 } while (/*CONSTCOND*/0)
56 #else /* !CFIO_DEBUG */
57 #define CFIO_DPRINTF(__fmt, ...) do { } while (/*CONSTCOND*/0)
58 #endif /* CFIO_DEBUG */
61 bus_space_handle_t ch_handle
[2];
66 static int cf_handle_alloc(int);
67 static void cf_handle_free(int);
68 static void cf_bs_unmap(void *, bus_space_handle_t
, bus_size_t
, int);
69 static int cf_bs_map(void *, bus_addr_t
, bus_size_t
, int,
70 bus_space_handle_t
*, int);
71 static int cf_bs_subregion(void *, bus_space_handle_t
, bus_size_t
,
72 bus_size_t
, bus_space_handle_t
*);
73 static int cf_bs_translate(void *, bus_addr_t
, bus_size_t
, int,
74 struct mips_bus_space_translation
*);
75 static int cf_bs_get_window(void *, int, struct mips_bus_space_translation
*);
76 static int cf_bs_translate(void *, bus_addr_t
, bus_size_t
, int,
77 struct mips_bus_space_translation
*);
78 static int cf_bs_get_window(void *, int, struct mips_bus_space_translation
*);
79 static int cf_bs_alloc(void *, bus_addr_t
, bus_addr_t
, bus_size_t
, bus_size_t
,
80 bus_size_t
, int, bus_addr_t
*, bus_space_handle_t
*);
81 static void cf_bs_free(void *, bus_space_handle_t
, bus_size_t size
);
82 static void *cf_bs_vaddr(void *, bus_space_handle_t
);
83 static paddr_t
cf_bs_mmap(void *, bus_addr_t
, off_t off
, int prot
, int);
84 static void cf_bs_barrier(void *, bus_space_handle_t
, bus_size_t
,
85 bus_size_t length
, int);
86 static uint8_t cf_bs_r_1(void *, bus_space_handle_t
, bus_size_t offset
);
87 static void cf_bs_rm_1(void *, bus_space_handle_t
, bus_size_t
,
88 uint8_t *, bus_size_t
);
89 static void cf_bs_w_1(void *, bus_space_handle_t
, bus_size_t
, uint8_t val
);
90 static void cf_bs_wm_1(void *, bus_space_handle_t
, bus_size_t
,
91 const uint8_t *, bus_size_t
);
92 static uint8_t cf_bs_rs_1(void *, bus_space_handle_t
, bus_size_t offset
);
93 static void cf_bs_rms_1(void *, bus_space_handle_t
, bus_size_t
,
94 uint8_t *, bus_size_t
);
95 static void cf_bs_ws_1(void *, bus_space_handle_t
, bus_size_t
, uint8_t val
);
96 static void cf_bs_wms_1(void *, bus_space_handle_t
, bus_size_t
, const uint8_t *,
98 static void cf_bs_sm_1(void *, bus_space_handle_t
, bus_size_t
, uint8_t,
101 static const struct mips_bus_space cfio_space
= {
105 /* mapping/unmapping */
107 .bs_unmap
= cf_bs_unmap
,
108 .bs_subregion
= cf_bs_subregion
,
110 .bs_translate
= cf_bs_translate
,
111 .bs_get_window
= cf_bs_get_window
,
112 .bs_alloc
= cf_bs_alloc
,
113 .bs_free
= cf_bs_free
,
114 .bs_vaddr
= cf_bs_vaddr
,
115 .bs_mmap
= cf_bs_mmap
,
116 .bs_barrier
= cf_bs_barrier
,
121 .bs_rm_1
= cf_bs_rm_1
,
134 .bs_wm_1
= cf_bs_wm_1
,
144 /* read (single) stream */
145 .bs_rs_1
= cf_bs_rs_1
,
150 /* read multiple stream */
151 .bs_rms_1
= cf_bs_rms_1
,
156 /* read region stream */
162 /* write (single) stream */
163 .bs_ws_1
= cf_bs_ws_1
,
168 /* write multiple stream */
169 .bs_wms_1
= cf_bs_wms_1
,
174 /* write region stream */
181 .bs_sm_1
= cf_bs_sm_1
,
200 cfio_bus_mem_init(bus_space_tag_t cfio
, bus_space_tag_t extio
)
203 cfio
->bs_cookie
= (void *)extio
;
207 cf_bs_unmap(void *cookie
, bus_space_handle_t bh
, bus_size_t size
, int acct
)
210 bus_space_unmap((bus_space_tag_t
)cookie
,
211 cf_handles
[bh
].ch_handle
[1], size
);
212 bus_space_unmap((bus_space_tag_t
)cookie
,
213 cf_handles
[bh
].ch_handle
[0], size
);
218 cf_handle_free(int which
)
222 KASSERT(cf_handles
[which
].ch_inuse
);
223 for (i
= 0; i
< __arraycount(cf_handles
); i
++) {
224 for (parent
= cf_handles
[i
].ch_parent
; parent
!= -1;
225 parent
= cf_handles
[parent
].ch_parent
) {
226 if (parent
== which
) {
227 CFIO_DPRINTF("%s: free %d (ancestor %d)\n",
229 cf_handles
[i
].ch_inuse
= 0;
234 cf_handles
[which
].ch_inuse
= 0;
235 for (i
= 0; i
< __arraycount(cf_handles
); i
++) {
236 if (!cf_handles
[i
].ch_inuse
)
237 cf_handles
[i
].ch_parent
= -1;
242 cf_handle_alloc(int parent
)
246 for (i
= 0; i
< __arraycount(cf_handles
) && cf_handles
[i
].ch_inuse
; i
++)
248 if (i
>= __arraycount(cf_handles
))
250 cf_handles
[i
].ch_inuse
= 1;
251 cf_handles
[i
].ch_parent
= parent
;
256 cf_bs_map(void *cookie
, bus_addr_t addr
, bus_size_t size
, int flags
,
257 bus_space_handle_t
*bhp
, int acct
)
263 if ((bh
= cf_handle_alloc(-1)) == -1)
266 rc
= bus_space_map((bus_space_tag_t
)cookie
,
267 addr
+ ADM5120_BASE_EXTIO1
- ADM5120_BASE_EXTIO0
, size
, flags
,
268 &cf_handles
[bh
].ch_handle
[1]);
272 rc
= bus_space_map((bus_space_tag_t
)cookie
, addr
, size
, flags
,
273 &cf_handles
[bh
].ch_handle
[0]);
275 bus_space_unmap((bus_space_tag_t
)cookie
,
276 cf_handles
[bh
].ch_handle
[1], size
);
285 cf_bs_subregion(void *cookie
, bus_space_handle_t h
, bus_size_t offset
,
286 bus_size_t size
, bus_space_handle_t
*bhp
)
290 if ((bh
= cf_handle_alloc(h
)) == -1)
293 rc
= bus_space_subregion((bus_space_tag_t
)cookie
,
294 cf_handles
[h
].ch_handle
[1], offset
, size
,
295 &cf_handles
[bh
].ch_handle
[1]);
299 rc
= bus_space_subregion((bus_space_tag_t
)cookie
,
300 cf_handles
[h
].ch_handle
[0], offset
, size
,
301 &cf_handles
[bh
].ch_handle
[0]);
311 cf_bs_translate(void *cookie
, bus_addr_t addr
, bus_size_t len
, int flags
,
312 struct mips_bus_space_translation
*mbst
)
314 panic("%s: not implemented\n", __func__
);
318 cf_bs_get_window(void *cookie
, int window
,
319 struct mips_bus_space_translation
*mbst
)
321 panic("%s: not implemented\n", __func__
);
325 cf_bs_alloc(void *cookie
, bus_addr_t reg_start
,
326 bus_addr_t reg_end
, bus_size_t size
, bus_size_t alignment
,
327 bus_size_t boundary
, int flags
, bus_addr_t
*addrp
,
328 bus_space_handle_t
*bhp
)
333 if ((bh
= cf_handle_alloc(-1)) == -1)
336 rc
= bus_space_alloc((bus_space_tag_t
)cookie
,
337 reg_start
+ ADM5120_BASE_EXTIO1
- ADM5120_BASE_EXTIO0
,
338 reg_end
+ ADM5120_BASE_EXTIO1
- ADM5120_BASE_EXTIO0
,
339 size
, alignment
, boundary
, flags
, &tmp
,
340 &cf_handles
[bh
].ch_handle
[1]);
344 rc
= bus_space_alloc((bus_space_tag_t
)cookie
, reg_start
, reg_end
, size
,
345 alignment
, boundary
, flags
, addrp
, &cf_handles
[bh
].ch_handle
[0]);
347 bus_space_free((bus_space_tag_t
)cookie
,
348 cf_handles
[bh
].ch_handle
[1], size
);
357 cf_bs_free(void *cookie
, bus_space_handle_t bh
, bus_size_t size
)
359 bus_space_free((bus_space_tag_t
)cookie
, cf_handles
[bh
].ch_handle
[0],
361 bus_space_free((bus_space_tag_t
)cookie
, cf_handles
[bh
].ch_handle
[1],
367 cf_bs_vaddr(void *cookie
, bus_space_handle_t bh
)
369 panic("%s: not implemented", __func__
);
373 cf_bs_mmap(void *cookie
, bus_addr_t addr
, off_t off
, int prot
, int flags
)
375 panic("%s: not implemented", __func__
);
379 cf_bs_barrier(void *cookie
, bus_space_handle_t bh
, bus_size_t offset
,
380 bus_size_t length
, int flags
)
382 bus_space_barrier((bus_space_tag_t
)cookie
, cf_handles
[bh
].ch_handle
[0],
383 offset
, length
, flags
);
384 bus_space_barrier((bus_space_tag_t
)cookie
, cf_handles
[bh
].ch_handle
[1],
385 offset
, length
, flags
);
390 cf_bs_r_1(void *cookie
, bus_space_handle_t bh
, bus_size_t offset
)
392 (void)bus_space_read_1((bus_space_tag_t
)cookie
,
393 cf_handles
[bh
].ch_handle
[0], offset
);
394 return bus_space_read_1((bus_space_tag_t
)cookie
,
395 cf_handles
[bh
].ch_handle
[1], offset
);
400 cf_bs_rm_1(void *cookie
, bus_space_handle_t bh
, bus_size_t offset
,
401 uint8_t *datap
, bus_size_t count
)
404 *datap
++ = cf_bs_r_1(cookie
, bh
, offset
);
409 cf_bs_w_1(void *cookie
, bus_space_handle_t bh
, bus_size_t offset
, uint8_t val
)
411 bus_space_write_1((bus_space_tag_t
)cookie
, cf_handles
[bh
].ch_handle
[0],
413 bus_space_write_1((bus_space_tag_t
)cookie
, cf_handles
[bh
].ch_handle
[1],
419 cf_bs_wm_1(void *cookie
, bus_space_handle_t bh
, bus_size_t offset
,
420 const uint8_t *datap
, bus_size_t count
)
423 cf_bs_w_1(cookie
, bh
, offset
, *datap
++);
426 /* read (single) stream */
428 cf_bs_rs_1(void *cookie
, bus_space_handle_t bh
, bus_size_t offset
)
430 (void)bus_space_read_stream_1((bus_space_tag_t
)cookie
,
431 cf_handles
[bh
].ch_handle
[0], offset
);
432 return bus_space_read_stream_1((bus_space_tag_t
)cookie
,
433 cf_handles
[bh
].ch_handle
[1], offset
);
436 /* read multiple stream */
438 cf_bs_rms_1(void *cookie
, bus_space_handle_t bh
, bus_size_t offset
,
439 uint8_t *datap
, bus_size_t count
)
442 *datap
++ = cf_bs_rs_1(cookie
, bh
, offset
);
445 /* write (single) stream */
447 cf_bs_ws_1(void *cookie
, bus_space_handle_t bh
, bus_size_t offset
, uint8_t val
)
449 bus_space_write_stream_1((bus_space_tag_t
)cookie
,
450 cf_handles
[bh
].ch_handle
[0], offset
, val
);
451 bus_space_write_stream_1((bus_space_tag_t
)cookie
,
452 cf_handles
[bh
].ch_handle
[1], offset
, val
);
455 /* write multiple stream */
457 cf_bs_wms_1(void *cookie
, bus_space_handle_t bh
, bus_size_t offset
,
458 const uint8_t *datap
, bus_size_t count
)
461 cf_bs_ws_1(cookie
, bh
, offset
, *datap
++);
466 cf_bs_sm_1(void *cookie
, bus_space_handle_t bh
, bus_size_t offset
,
467 uint8_t value
, bus_size_t count
)
470 cf_bs_w_1(cookie
, bh
, offset
, value
);