1/*	$NetBSD: ep93xx_space.c,v 1.7 2021/11/21 08:25:26 skrll Exp $ */
2
3/*
4 * Copyright (c) 2004 Jesse Off
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 *
16 * THIS SOFTWARE IS PROVIDED BY ICHIRO FUKUHARA ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL ICHIRO FUKUHARA OR THE VOICES IN HIS HEAD BE LIABLE FOR
20 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#include <sys/cdefs.h>
30__KERNEL_RCSID(0, "$NetBSD: ep93xx_space.c,v 1.7 2021/11/21 08:25:26 skrll Exp $");
31
32/*
33 * bus_space I/O functions for ep93xx
34 */
35
36#include <sys/param.h>
37#include <sys/systm.h>
38#include <sys/queue.h>
39
40#include <uvm/uvm.h>
41
42#include <sys/bus.h>
43
44#include <arm/ep93xx/ep93xxreg.h>
45#include <arm/ep93xx/ep93xxvar.h>
46
47/* Proto types for all the bus_space structure functions */
48bs_protos(ep93xx);
49bs_protos(generic);
50bs_protos(generic_armv4);
51bs_protos(bs_notimpl);
52
53struct bus_space ep93xx_bs_tag = {
54	/* cookie */
55	.bs_cookie = (void *) 0,
56
57	/* mapping/unmapping */
58	.bs_map = ep93xx_bs_map,
59	.bs_unmap = ep93xx_bs_unmap,
60	.bs_subregion = ep93xx_bs_subregion,
61
62	/* allocation/deallocation */
63	.bs_alloc = ep93xx_bs_alloc,
64	.bs_free = ep93xx_bs_free,
65
66	/* get kernel virtual address */
67	.bs_vaddr = ep93xx_bs_vaddr,
68
69	/* mmap bus space for userland */
70	.bs_mmap = ep93xx_bs_mmap,
71
72	/* barrier */
73	.bs_barrier = ep93xx_bs_barrier,
74
75	/* read (single) */
76	.bs_r_1 = generic_bs_r_1,
77	.bs_r_2 = generic_armv4_bs_r_2,
78	.bs_r_4 = generic_bs_r_4,
79	.bs_r_8 = bs_notimpl_bs_r_8,
80
81	/* read multiple */
82	.bs_rm_1 = generic_bs_rm_1,
83	.bs_rm_2 = generic_armv4_bs_rm_2,
84	.bs_rm_4 = generic_bs_rm_4,
85	.bs_rm_8 = bs_notimpl_bs_rm_8,
86
87	/* read region */
88	.bs_rr_1 = generic_bs_rr_1,
89	.bs_rr_2 = generic_armv4_bs_rr_2,
90	.bs_rr_4 = generic_bs_rr_4,
91	.bs_rr_8 = bs_notimpl_bs_rr_8,
92
93	/* write (single) */
94	.bs_w_1 = generic_bs_w_1,
95	.bs_w_2 = generic_armv4_bs_w_2,
96	.bs_w_4 = generic_bs_w_4,
97	.bs_w_8 = bs_notimpl_bs_w_8,
98
99	/* write multiple */
100	.bs_wm_1 = generic_bs_wm_1,
101	.bs_wm_2 = generic_armv4_bs_wm_2,
102	.bs_wm_4 = generic_bs_wm_4,
103	.bs_wm_8 = bs_notimpl_bs_wm_8,
104
105	/* write region */
106	.bs_wr_1 = generic_bs_wr_1,
107	.bs_wr_2 = generic_armv4_bs_wr_2,
108	.bs_wr_4 = generic_bs_wr_4,
109	.bs_wr_8 = bs_notimpl_bs_wr_8,
110
111	/* set multiple */
112	.bs_sm_1 = bs_notimpl_bs_sm_1,
113	.bs_sm_2 = bs_notimpl_bs_sm_2,
114	.bs_sm_4 = bs_notimpl_bs_sm_4,
115	.bs_sm_8 = bs_notimpl_bs_sm_8,
116
117	/* set region */
118	.bs_sr_1 = bs_notimpl_bs_sr_1,
119	.bs_sr_2 = generic_armv4_bs_sr_2,
120	.bs_sr_4 = generic_bs_sr_4,
121	.bs_sr_8 = bs_notimpl_bs_sr_8,
122
123	/* copy */
124	.bs_c_1 = bs_notimpl_bs_c_1,
125	.bs_c_2 = generic_armv4_bs_c_2,
126	.bs_c_4 = bs_notimpl_bs_c_4,
127	.bs_c_8 = bs_notimpl_bs_c_8,
128};
129
130int
131ep93xx_bs_map(void *t, bus_addr_t bpa, bus_size_t size,
132	      int cacheable, bus_space_handle_t *bshp)
133{
134	const struct pmap_devmap	*pd;
135
136	paddr_t		startpa;
137        paddr_t		endpa;
138        paddr_t		pa;
139        paddr_t		offset;
140        vaddr_t		va;
141
142	if ((pd = pmap_devmap_find_pa(bpa, size)) != NULL) {
143		/* Device was statically mapped. */
144		*bshp = pd->pd_va + (bpa - pd->pd_pa);
145		return 0;
146	}
147
148	endpa = round_page(bpa + size);
149	offset = bpa & PAGE_MASK;
150	startpa = trunc_page(bpa);
151
152	/* Get some VM.  */
153	va = uvm_km_alloc(kernel_map, endpa - startpa, 0,
154	    UVM_KMF_VAONLY | UVM_KMF_NOWAIT);
155	if (va == 0)
156		return ENOMEM;
157
158	/* Store the bus space handle */
159	*bshp = va + offset;
160
161	/* Now map the pages */
162	for (pa = startpa; pa < endpa; pa += PAGE_SIZE, va += PAGE_SIZE) {
163		pmap_enter(pmap_kernel(), va, pa,
164		    VM_PROT_READ | VM_PROT_WRITE,
165		    VM_PROT_READ | VM_PROT_WRITE | PMAP_WIRED);
166	}
167	pmap_update(pmap_kernel());
168
169	return(0);
170}
171
172void
173ep93xx_bs_unmap(void *t, bus_space_handle_t bsh, bus_size_t size)
174{
175	vaddr_t	va;
176	vaddr_t	endva;
177
178	if (pmap_devmap_find_va(bsh, size) != NULL) {
179		/* Device was statically mapped; nothing to do. */
180		return;
181	}
182
183	endva = round_page(bsh + size);
184	va = trunc_page(bsh);
185
186	pmap_remove(pmap_kernel(), va, endva);
187	pmap_update(pmap_kernel());
188	uvm_km_free(kernel_map, va, endva - va, UVM_KMF_VAONLY);
189}
190
191int
192ep93xx_bs_alloc(void *t, bus_addr_t rstart, bus_addr_t rend,
193	bus_size_t size, bus_size_t alignment, bus_size_t boundary, int cacheable,
194	bus_addr_t *bpap, bus_space_handle_t *bshp)
195{
196	panic("ep93xx_bs_alloc(): not implemented\n");
197}
198
199void
200ep93xx_bs_free(void *t, bus_space_handle_t bsh, bus_size_t size)
201{
202	panic("ep93xx_bs_free(): not implemented\n");
203}
204
205int
206ep93xx_bs_subregion(void *t, bus_space_handle_t bsh, bus_size_t offset,
207	bus_size_t size, bus_space_handle_t *nbshp)
208{
209	*nbshp = bsh + offset;
210	return (0);
211}
212
213void *
214ep93xx_bs_vaddr(void *t, bus_space_handle_t bsh)
215{
216	return ((void *)bsh);
217}
218
219paddr_t
220ep93xx_bs_mmap(void *t, bus_addr_t addr, off_t off, int prot, int flags)
221{
222	/* Not supported. */
223	return (-1);
224}
225
226void
227ep93xx_bs_barrier(void *t, bus_space_handle_t bsh, bus_size_t offset,
228    bus_size_t len, int flags)
229{
230/* NULL */
231}
232/* End of ep93xx_space.c */
233