1/*	$NetBSD: s3c2xx0_space.c,v 1.7 2005/11/24 13:08:32 yamt Exp $ */
2
3/*
4 * Copyright (c) 2002 Fujitsu Component Limited
5 * Copyright (c) 2002 Genetec Corporation
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of The Fujitsu Component Limited nor the name of
17 *    Genetec corporation may not be used to endorse or promote products
18 *    derived from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY FUJITSU COMPONENT LIMITED AND GENETEC
21 * CORPORATION ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
22 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 * DISCLAIMED.  IN NO EVENT SHALL FUJITSU COMPONENT LIMITED OR GENETEC
25 * CORPORATION BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
28 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34/* derived from sa11x0_io.c */
35
36/*
37 * Copyright (c) 1997 Mark Brinicombe.
38 * Copyright (c) 1997 Causality Limited.
39 * All rights reserved.
40 *
41 * This code is derived from software contributed to The NetBSD Foundation
42 * by Ichiro FUKUHARA.
43 *
44 * Redistribution and use in source and binary forms, with or without
45 * modification, are permitted provided that the following conditions
46 * are met:
47 * 1. Redistributions of source code must retain the above copyright
48 *    notice, this list of conditions and the following disclaimer.
49 * 2. Redistributions in binary form must reproduce the above copyright
50 *    notice, this list of conditions and the following disclaimer in the
51 *    documentation and/or other materials provided with the distribution.
52 * 3. All advertising materials mentioning features or use of this software
53 *    must display the following acknowledgement:
54 *	This product includes software developed by Mark Brinicombe.
55 * 4. The name of the company nor the name of the author may be used to
56 *    endorse or promote products derived from this software without specific
57 *    prior written permission.
58 *
59 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
60 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
61 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
62 * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
63 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
64 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
65 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69 * SUCH DAMAGE.
70 */
71
72/*
73 * bus_space functions for Samsung S3C2800/2400/2410.
74 */
75
76#include <sys/cdefs.h>
77__FBSDID("$FreeBSD$");
78
79#include <sys/param.h>
80#include <sys/systm.h>
81#include <sys/bus.h>
82
83#include <vm/vm.h>
84#include <vm/vm_kern.h>
85#include <vm/pmap.h>
86#include <vm/vm_page.h>
87#include <vm/vm_extern.h>
88
89#include <machine/bus.h>
90
91/* Prototypes for all the bus_space structure functions */
92bs_protos(s3c2xx0);
93bs_protos(generic);
94bs_protos(generic_armv4);
95
96struct bus_space s3c2xx0_bs_tag = {
97	/* cookie */
98	(void *) 0,
99
100	/* mapping/unmapping */
101	s3c2xx0_bs_map,
102	s3c2xx0_bs_unmap,
103	s3c2xx0_bs_subregion,
104
105	/* allocation/deallocation */
106	s3c2xx0_bs_alloc,	/* not implemented */
107	s3c2xx0_bs_free,	/* not implemented */
108
109	/* barrier */
110	s3c2xx0_bs_barrier,
111
112	/* read (single) */
113	generic_bs_r_1,
114	generic_armv4_bs_r_2,
115	generic_bs_r_4,
116	NULL,
117
118	/* read multiple */
119	generic_bs_rm_1,
120	generic_armv4_bs_rm_2,
121	generic_bs_rm_4,
122	NULL,
123
124	/* read region */
125	generic_bs_rr_1,
126	generic_armv4_bs_rr_2,
127	generic_bs_rr_4,
128	NULL,
129
130	/* write (single) */
131	generic_bs_w_1,
132	generic_armv4_bs_w_2,
133	generic_bs_w_4,
134	NULL,
135
136	/* write multiple */
137	generic_bs_wm_1,
138	generic_armv4_bs_wm_2,
139	generic_bs_wm_4,
140	NULL,
141
142	/* write region */
143	generic_bs_wr_1,
144	generic_armv4_bs_wr_2,
145	generic_bs_wr_4,
146	NULL,
147
148	/* set multiple */
149	NULL,
150	NULL,
151	NULL,
152	NULL,
153
154	/* set region */
155	generic_bs_sr_1,
156	generic_armv4_bs_sr_2,
157	NULL,
158	NULL,
159
160	/* copy */
161	NULL,
162	generic_armv4_bs_c_2,
163	NULL,
164	NULL,
165};
166
167int
168s3c2xx0_bs_map(void *t, bus_addr_t bpa, bus_size_t size,
169	       int flag, bus_space_handle_t * bshp)
170{
171	u_long startpa, endpa, pa;
172	vm_offset_t va;
173	pt_entry_t *pte;
174	const struct pmap_devmap *pd;
175
176	if ((pd = pmap_devmap_find_pa(bpa, size)) != NULL) {
177		/* Device was statically mapped. */
178		*bshp = pd->pd_va + (bpa - pd->pd_pa);
179		return 0;
180	}
181
182	startpa = trunc_page(bpa);
183	endpa = round_page(bpa + size);
184
185	va = kva_alloc(endpa - startpa);
186	if (!va)
187		return (ENOMEM);
188
189	*bshp = (bus_space_handle_t) (va + (bpa - startpa));
190
191	for (pa = startpa; pa < endpa; pa += PAGE_SIZE, va += PAGE_SIZE) {
192		pmap_kenter(va, pa);
193		pte = vtopte(va);
194		if ((flag & BUS_SPACE_MAP_CACHEABLE) == 0)
195			*pte &= ~L2_S_CACHE_MASK;
196	}
197	return (0);
198}
199
200void
201s3c2xx0_bs_unmap(void *t, bus_space_handle_t h, bus_size_t size)
202{
203	vm_offset_t va, endva, origva;
204
205	if (pmap_devmap_find_va((vm_offset_t)h, size) != NULL) {
206		/* Device was statically mapped; nothing to do. */
207		return;
208	}
209
210	endva = round_page((vm_offset_t)h + size);
211	origva = va = trunc_page((vm_offset_t)h);
212
213	while (va < endva) {
214		pmap_kremove(va);
215		va += PAGE_SIZE;
216	}
217	kva_free(origva, endva - origva);
218}
219
220int
221s3c2xx0_bs_subregion(void *t, bus_space_handle_t bsh, bus_size_t offset,
222		     bus_size_t size, bus_space_handle_t * nbshp)
223{
224
225	*nbshp = bsh + offset;
226	return (0);
227}
228
229void
230s3c2xx0_bs_barrier(void *t, bus_space_handle_t bsh, bus_size_t offset,
231		   bus_size_t len, int flags)
232{
233
234	/* Nothing to do. */
235}
236
237int
238s3c2xx0_bs_alloc(void *t, bus_addr_t rstart, bus_addr_t rend,
239		 bus_size_t size, bus_size_t alignment, bus_size_t boundary,
240    		 int flags, bus_addr_t * bpap, bus_space_handle_t * bshp)
241{
242
243	panic("s3c2xx0_io_bs_alloc(): not implemented\n");
244}
245
246void
247s3c2xx0_bs_free(void *t, bus_space_handle_t bsh, bus_size_t size)
248{
249	panic("s3c2xx0_io_bs_free(): not implemented\n");
250}
251