xref: /netbsd-src/sys/arch/evbarm/tsarm/isa/isa_io.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: isa_io.c,v 1.5 2007/03/04 05:59:45 christos Exp $	*/
2 
3 /*
4  * Copyright 1997
5  * Digital Equipment Corporation. All rights reserved.
6  *
7  * This software is furnished under license and may be used and
8  * copied only in accordance with the following terms and conditions.
9  * Subject to these conditions, you may download, copy, install,
10  * use, modify and distribute this software in source and/or binary
11  * form. No title or ownership is transferred hereby.
12  *
13  * 1) Any source code used, modified or distributed must reproduce
14  *    and retain this copyright notice and list of conditions as
15  *    they appear in the source file.
16  *
17  * 2) No right is granted to use any trade name, trademark, or logo of
18  *    Digital Equipment Corporation. Neither the "Digital Equipment
19  *    Corporation" name nor any trademark or logo of Digital Equipment
20  *    Corporation may be used to endorse or promote products derived
21  *    from this software without the prior written permission of
22  *    Digital Equipment Corporation.
23  *
24  * 3) This software is provided "AS-IS" and any express or implied
25  *    warranties, including but not limited to, any implied warranties
26  *    of merchantability, fitness for a particular purpose, or
27  *    non-infringement are disclaimed. In no event shall DIGITAL be
28  *    liable for any damages whatsoever, and in particular, DIGITAL
29  *    shall not be liable for special, indirect, consequential, or
30  *    incidental damages or damages for lost profits, loss of
31  *    revenue or loss of use, whether such damages arise in contract,
32  *    negligence, tort, under statute, in equity, at law or otherwise,
33  *    even if advised of the possibility of such damage.
34  */
35 
36 /*
37  * bus_space I/O functions for isa
38  */
39 
40 #include <sys/cdefs.h>
41 __KERNEL_RCSID(0, "$NetBSD: isa_io.c,v 1.5 2007/03/04 05:59:45 christos Exp $");
42 
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/malloc.h>
46 #include <sys/extent.h>
47 #include <machine/bus.h>
48 #include <machine/pio.h>
49 #include <machine/isa_machdep.h>
50 
51 /* Proto types for all the bus_space structure functions */
52 
53 bs_protos(isa);
54 bs_protos(bs_notimpl);
55 void isa_bs_mallocok(void);
56 
57 /*
58  * Declare the isa bus space tags
59  * The IO and MEM structs are identical, except for the cookies,
60  * which contain the address space bases.
61  */
62 
63 /*
64  * NOTE: ASSEMBLY LANGUAGE RELIES ON THE COOKIE -- THE FIRST MEMBER OF
65  *       THIS STRUCTURE -- TO BE THE VIRTUAL ADDRESS OF 16 BIT ISA/IO!
66  */
67 struct bus_space isa_io_bs_tag = {
68 	/* cookie */
69 	NULL, /* initialized below */
70 
71 	/* mapping/unmapping */
72 	isa_bs_map,
73 	isa_bs_unmap,
74 	isa_bs_subregion,
75 
76 	/* allocation/deallocation */
77 	isa_bs_alloc,
78 	isa_bs_free,
79 
80 	/* get kernel virtual address */
81 	isa_bs_vaddr,
82 
83 	/* mmap bus space for userland */
84 	bs_notimpl_bs_mmap,		/* XXX possible even? XXX */
85 
86 	/* barrier */
87 	isa_bs_barrier,
88 
89 	/* read (single) */
90 	isa_bs_r_1,
91 	isa_bs_r_2,
92 	isa_bs_r_4,
93 	bs_notimpl_bs_r_8,
94 
95 	/* read multiple */
96 	isa_bs_rm_1,
97 	isa_bs_rm_2,
98 	isa_bs_rm_4,
99 	bs_notimpl_bs_rm_8,
100 
101 	/* read region */
102 	isa_bs_rr_1,
103 	isa_bs_rr_2,
104 	isa_bs_rr_4,
105 	bs_notimpl_bs_rr_8,
106 
107 	/* write (single) */
108 	isa_bs_w_1,
109 	isa_bs_w_2,
110 	isa_bs_w_4,
111 	bs_notimpl_bs_w_8,
112 
113 	/* write multiple */
114 	isa_bs_wm_1,
115 	isa_bs_wm_2,
116 	isa_bs_wm_4,
117 	bs_notimpl_bs_wm_8,
118 
119 	/* write region */
120 	isa_bs_wr_1,
121 	isa_bs_wr_2,
122 	isa_bs_wr_4,
123 	bs_notimpl_bs_wr_8,
124 
125 	/* set multiple */
126 	bs_notimpl_bs_sm_1,
127 	bs_notimpl_bs_sm_2,
128 	bs_notimpl_bs_sm_4,
129 	bs_notimpl_bs_sm_8,
130 
131 	/* set region */
132 	bs_notimpl_bs_sr_1,
133 	isa_bs_sr_2,
134 	bs_notimpl_bs_sr_4,
135 	bs_notimpl_bs_sr_8,
136 
137 	/* copy */
138 	bs_notimpl_bs_c_1,
139 	bs_notimpl_bs_c_2,
140 	bs_notimpl_bs_c_4,
141 	bs_notimpl_bs_c_8,
142 };
143 
144 /*
145  * NOTE: ASSEMBLY LANGUAGE RELIES ON THE COOKIE -- THE FIRST MEMBER OF
146  *       THIS STRUCTURE -- TO BE THE VIRTUAL ADDRESS OF ISA/MEMORY!
147  */
148 struct bus_space isa_mem_bs_tag = {
149 	/* cookie */
150         NULL, /* initialized below */
151 
152 	/* mapping/unmapping */
153 	isa_bs_map,
154 	isa_bs_unmap,
155 	isa_bs_subregion,
156 
157 	/* allocation/deallocation */
158 	isa_bs_alloc,
159 	isa_bs_free,
160 
161 	/* get kernel virtual address */
162 	isa_bs_vaddr,
163 
164 	/* mmap bus space for userland */
165 	bs_notimpl_bs_mmap,		/* XXX open for now ... XXX */
166 
167 	/* barrier */
168 	isa_bs_barrier,
169 
170 	/* read (single) */
171 	isa_bs_r_1,
172 	isa_bs_r_2,
173 	isa_bs_r_4,
174 	bs_notimpl_bs_r_8,
175 
176 	/* read multiple */
177 	isa_bs_rm_1,
178 	isa_bs_rm_2,
179 	isa_bs_rm_4,
180 	bs_notimpl_bs_rm_8,
181 
182 	/* read region */
183 	isa_bs_rr_1,
184 	isa_bs_rr_2,
185 	isa_bs_rr_4,
186 	bs_notimpl_bs_rr_8,
187 
188 	/* write (single) */
189 	isa_bs_w_1,
190 	isa_bs_w_2,
191 	isa_bs_w_4,
192 	bs_notimpl_bs_w_8,
193 
194 	/* write multiple */
195 	isa_bs_wm_1,
196 	isa_bs_wm_2,
197 	isa_bs_wm_4,
198 	bs_notimpl_bs_wm_8,
199 
200 	/* write region */
201 	isa_bs_wr_1,
202 	isa_bs_wr_2,
203 	isa_bs_wr_4,
204 	bs_notimpl_bs_wr_8,
205 
206 	/* set multiple */
207 	bs_notimpl_bs_sm_1,
208 	bs_notimpl_bs_sm_2,
209 	bs_notimpl_bs_sm_4,
210 	bs_notimpl_bs_sm_8,
211 
212 	/* set region */
213 	bs_notimpl_bs_sr_1,
214 	isa_bs_sr_2,
215 	bs_notimpl_bs_sr_4,
216 	bs_notimpl_bs_sr_8,
217 
218 	/* copy */
219 	bs_notimpl_bs_c_1,
220 	bs_notimpl_bs_c_2,
221 	bs_notimpl_bs_c_4,
222 	bs_notimpl_bs_c_8,
223 };
224 
225 static long isaio_ex_storage[EXTENT_FIXED_STORAGE_SIZE(8) / sizeof(long)];
226 static long isamem_ex_storage[EXTENT_FIXED_STORAGE_SIZE(8) / sizeof(long)];
227 static int malloc_safe = 0;
228 struct extent	*isaio_ex;
229 struct extent	*isamem_ex;
230 
231 void
232 isa_bs_mallocok(void)
233 {
234 	malloc_safe = 1;
235 }
236 
237 /* bus space functions */
238 
239 void
240 isa_io_init(isa_io_addr, isa_mem_addr)
241 	vm_offset_t isa_io_addr;
242 	vm_offset_t isa_mem_addr;
243 {
244 	isa_io_bs_tag.bs_cookie = (void *)isa_io_addr;
245 	isa_mem_bs_tag.bs_cookie = (void *)isa_mem_addr;
246 
247 	isaio_ex = extent_create("isaio", 0x0, 0xffff, M_DEVBUF,
248 		(void *)isaio_ex_storage, sizeof(isaio_ex_storage),
249 		EX_NOWAIT|EX_NOCOALESCE);
250 	isamem_ex = extent_create("isamem", 0x0, 0xfffff, M_DEVBUF,
251 		(void *)isamem_ex_storage, sizeof(isamem_ex_storage),
252 		EX_NOWAIT|EX_NOCOALESCE);
253 	if (isaio_ex == NULL || isamem_ex == NULL)
254 		panic("isa_io_init(): can't alloc extent maps");
255 }
256 
257 /*
258  * break the abstraction: sometimes, other parts of the system
259  * (e.g. X servers) need to map ISA space directly.  use these
260  * functions sparingly!
261  */
262 vm_offset_t
263 isa_io_data_vaddr(void)
264 {
265 	return (vm_offset_t)isa_io_bs_tag.bs_cookie;
266 }
267 
268 vm_offset_t
269 isa_mem_data_vaddr(void)
270 {
271 	return (vm_offset_t)isa_mem_bs_tag.bs_cookie;
272 }
273 
274 int
275 isa_bs_map(t, bpa, size, cacheable, bshp)
276 	void *t;
277 	bus_addr_t bpa;
278 	bus_size_t size;
279 	int cacheable;
280 	bus_space_handle_t *bshp;
281 {
282 	struct extent *ex;
283 	int err;
284 
285 	if (t == isa_io_bs_tag.bs_cookie)
286 		ex = isaio_ex;
287 	else
288 		ex = isamem_ex;
289 
290 	err = extent_alloc_region(ex, bpa, size,
291 		EX_NOWAIT|(malloc_safe ? EX_MALLOCOK : 0));
292 	if (err)
293 		return err;
294 
295 	*bshp = bpa + (bus_addr_t)t;
296 	return(0);
297 }
298 
299 void
300 isa_bs_unmap(t, bsh, size)
301 	void *t;
302 	bus_space_handle_t bsh;
303 	bus_size_t size;
304 {
305 	isa_bs_free(t, bsh, size);
306 }
307 
308 int
309 isa_bs_subregion(t, bsh, offset, size, nbshp)
310 	void *t;
311 	bus_space_handle_t bsh;
312 	bus_size_t offset, size;
313 	bus_space_handle_t *nbshp;
314 {
315 	*nbshp = bsh + offset;
316 	return(0);
317 }
318 
319 int
320 isa_bs_alloc(t, rstart, rend, size, alignment, boundary, cacheable,
321     bpap, bshp)
322 	void *t;
323 	bus_addr_t rstart, rend;
324 	bus_size_t size, alignment, boundary;
325 	int cacheable;
326 	bus_addr_t *bpap;
327 	bus_space_handle_t *bshp;
328 {
329 	struct extent *ex;
330 	u_long bpa;
331 	int err;
332 
333 	if (t == isa_io_bs_tag.bs_cookie)
334 		ex = isaio_ex;
335 	else
336 		ex = isamem_ex;
337 
338 	err = extent_alloc_subregion(ex, rstart, rend, size, alignment,
339 		boundary, (EX_FAST|EX_NOWAIT|(malloc_safe ? EX_MALLOCOK : 0)),
340 		&bpa);
341 
342 	if (err)
343 		return err;
344 
345 	*bshp = *bpap = bpa + (bus_addr_t)t;
346 	return 0;
347 }
348 
349 void
350 isa_bs_free(t, bsh, size)
351 	void *t;
352 	bus_space_handle_t bsh;
353 	bus_size_t size;
354 {
355 	struct extent *ex;
356 
357 	if (t == isa_io_bs_tag.bs_cookie)
358 		ex = isaio_ex;
359 	else
360 		ex = isamem_ex;
361 
362 	extent_free(ex, bsh - (bus_addr_t)t, size,
363 		EX_NOWAIT|(malloc_safe ? EX_MALLOCOK : 0));
364 }
365 
366 void *
367 isa_bs_vaddr(t, bsh)
368 	void *t;
369 	bus_space_handle_t bsh;
370 {
371 
372 	return ((void *)bsh);
373 }
374 
375 void
376 isa_bs_barrier(t, bsh, offset, len, flags)
377 	void *t;
378 	bus_space_handle_t bsh;
379 	bus_size_t offset, len;
380 	int flags;
381 {
382 	/* just return */
383 }
384