xref: /netbsd-src/sys/arch/hp300/hp300/bus_space.c (revision 60ab2ca5c0570c0013b39de285ddaa91fe27d029)
1 /*	$NetBSD: bus_space.c,v 1.17 2008/04/28 20:23:19 martin Exp $	*/
2 
3 /*-
4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jason R. Thorpe.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  * Implementation of bus_space mapping for the hp300.
34  */
35 
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: bus_space.c,v 1.17 2008/04/28 20:23:19 martin Exp $");
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/extent.h>
42 
43 #include <machine/autoconf.h>
44 #include <machine/bus.h>
45 
46 #include <uvm/uvm_extern.h>
47 
48 extern int *nofault;
49 
50 /* ARGSUSED */
51 int
52 bus_space_map(bus_space_tag_t t, bus_addr_t bpa, bus_size_t size, int flags,
53     bus_space_handle_t *bshp)
54 {
55 	vaddr_t kva;
56 	vsize_t offset;
57 	int error;
58 
59 	if (t->bustype == HP300_BUS_SPACE_INTIO) {
60 		/*
61 		 * Intio space is direct-mapped in pmap_bootstrap(); just
62 		 * do the translation.
63 		 */
64 		*bshp = (bus_space_handle_t)IIOV(INTIOBASE + bpa);
65 		return 0;
66 	}
67 
68 	if (t->bustype != HP300_BUS_SPACE_DIO)
69 		panic("bus_space_map: bad space tag");
70 
71 	/*
72 	 * Allocate virtual address space from the extio extent map.
73 	 */
74 	offset = m68k_page_offset(bpa);
75 	size = m68k_round_page(offset + size);
76 	error = extent_alloc(extio_ex, size, PAGE_SIZE, 0,
77 	    EX_FAST | EX_NOWAIT | (extio_ex_malloc_safe ? EX_MALLOCOK : 0),
78 	    &kva);
79 	if (error)
80 		return error;
81 
82 	/*
83 	 * Map the range.  The range is always cache-inhibited on the hp300.
84 	 */
85 	physaccess((void *)kva, (void *)bpa, size, PG_RW|PG_CI);
86 
87 	/*
88 	 * All done.
89 	 */
90 	*bshp = (bus_space_handle_t)(kva + offset);
91 	return 0;
92 }
93 
94 /* ARGSUSED */
95 int
96 bus_space_alloc(bus_space_tag_t t, bus_addr_t rstart, bus_addr_t rend,
97     bus_size_t size, bus_size_t alignment, bus_size_t boundary, int flags,
98     bus_addr_t *bpap, bus_space_handle_t *bshp)
99 {
100 
101 	/*
102 	 * Not meaningful on any currently-supported hp300 bus.
103 	 */
104 	return EINVAL;
105 }
106 
107 /* ARGSUSED */
108 void
109 bus_space_free(bus_space_tag_t t, bus_space_handle_t bsh, bus_size_t size)
110 {
111 
112 	/*
113 	 * Not meaningful on any currently-supported hp300 bus.
114 	 */
115 	panic("bus_space_free: shouldn't be here");
116 }
117 
118 void
119 bus_space_unmap(bus_space_tag_t t, bus_space_handle_t bsh, bus_size_t size)
120 {
121 	vaddr_t kva;
122 	vsize_t offset;
123 
124 	if (t->bustype == HP300_BUS_SPACE_INTIO) {
125 		/*
126 		 * Intio space is direct-mapped in pmap_bootstrap(); nothing
127 		 * to do
128 		 */
129 		return;
130 	}
131 
132 	if (t->bustype != HP300_BUS_SPACE_DIO)
133 		panic("bus_space_map: bad space tag");
134 
135 	kva = m68k_trunc_page(bsh);
136 	offset = m68k_page_offset(bsh);
137 	size = m68k_round_page(offset + size);
138 
139 #ifdef DIAGNOSTIC
140 	if (bsh < (vaddr_t)extiobase ||
141 	    bsh >= ((vaddr_t)extiobase + ptoa(EIOMAPSIZE)))
142 		panic("bus_space_unmap: bad bus space handle");
143 #endif
144 
145 	/*
146 	 * Unmap the range.
147 	 */
148 	physunaccess((void *)kva, size);
149 
150 	/*
151 	 * Free it from the extio extent map.
152 	 */
153 	if (extent_free(extio_ex, kva, size,
154 	    EX_NOWAIT | (extio_ex_malloc_safe ? EX_MALLOCOK : 0)))
155 		printf("bus_space_unmap: kva 0x%lx size 0x%lx: "
156 		    "can't free region\n", (u_long) bsh, size);
157 }
158 
159 /* ARGSUSED */
160 int
161 bus_space_subregion(bus_space_tag_t t, bus_space_handle_t bsh,
162     bus_size_t offset, bus_size_t size, bus_space_handle_t *nbshp)
163 {
164 
165 	*nbshp = bsh + offset;
166 	return 0;
167 }
168 
169 /* ARGSUSED */
170 int
171 hp300_bus_space_probe(bus_space_tag_t t, bus_space_handle_t bsh,
172     bus_size_t offset, int sz)
173 {
174 	label_t faultbuf;
175 	int i;
176 
177 	nofault = (int *)&faultbuf;
178 	if (setjmp((label_t *)nofault)) {
179 		nofault = NULL;
180 		return 0;
181 	}
182 
183 	switch (sz) {
184 	case 1:
185 		i = bus_space_read_1(t, bsh, offset);
186 		break;
187 
188 	case 2:
189 		i = bus_space_read_2(t, bsh, offset);
190 		break;
191 
192 	case 4:
193 		i = bus_space_read_4(t, bsh, offset);
194 		break;
195 
196 	default:
197 		panic("bus_space_probe: unupported data size %d", sz);
198 		/* NOTREACHED */
199 	}
200 
201 	nofault = NULL;
202 	return 1;
203 }
204