1 /* $NetBSD: clps711x_space.c,v 1.2 2018/03/16 17:56:31 ryo 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: clps711x_space.c,v 1.2 2018/03/16 17:56:31 ryo Exp $");
31
32 /*
33 * bus_space I/O functions for clps711x
34 */
35
36 #include <sys/param.h>
37 #include <sys/bus.h>
38 #include <sys/systm.h>
39
40 #include <uvm/uvm_extern.h>
41
42 #include <machine/pmap.h>
43
44 /* Proto types for all the bus_space structure functions */
45 bs_protos(clps711x);
46 bs_protos(generic);
47 bs_protos(bs_notimpl);
48
49 struct bus_space clps711x_bs_tag = {
50 /* cookie */
51 .bs_cookie = (void *) 0,
52
53 /* mapping/unmapping */
54 .bs_map = clps711x_bs_map,
55 .bs_unmap = clps711x_bs_unmap,
56 .bs_subregion = clps711x_bs_subregion,
57
58 /* allocation/deallocation */
59 .bs_alloc = clps711x_bs_alloc,
60 .bs_free = clps711x_bs_free,
61
62 /* get kernel virtual address */
63 .bs_vaddr = clps711x_bs_vaddr,
64
65 /* mmap bus space for userland */
66 .bs_mmap = clps711x_bs_mmap,
67
68 /* barrier */
69 .bs_barrier = clps711x_bs_barrier,
70
71 /* read (single) */
72 .bs_r_1 = generic_bs_r_1,
73 .bs_r_2 = bs_notimpl_bs_r_2, /* XXXX */
74 .bs_r_4 = generic_bs_r_4,
75 .bs_r_8 = bs_notimpl_bs_r_8,
76
77 /* read multiple */
78 .bs_rm_1 = generic_bs_rm_1,
79 .bs_rm_2 = bs_notimpl_bs_rm_2, /* XXXX */
80 .bs_rm_4 = generic_bs_rm_4,
81 .bs_rm_8 = bs_notimpl_bs_rm_8,
82
83 /* read region */
84 .bs_rr_1 = generic_bs_rr_1,
85 .bs_rr_2 = bs_notimpl_bs_rr_2, /* XXXX */
86 .bs_rr_4 = generic_bs_rr_4,
87 .bs_rr_8 = bs_notimpl_bs_rr_8,
88
89 /* write (single) */
90 .bs_w_1 = generic_bs_w_1,
91 .bs_w_2 = bs_notimpl_bs_w_2, /* XXXX */
92 .bs_w_4 = generic_bs_w_4,
93 .bs_w_8 = bs_notimpl_bs_w_8,
94
95 /* write multiple */
96 .bs_wm_1 = generic_bs_wm_1,
97 .bs_wm_2 = bs_notimpl_bs_wm_2, /* XXXX */
98 .bs_wm_4 = generic_bs_wm_4,
99 .bs_wm_8 = bs_notimpl_bs_wm_8,
100
101 /* write region */
102 .bs_wr_1 = generic_bs_wr_1,
103 .bs_wr_2 = bs_notimpl_bs_wr_2, /* XXXX */
104 .bs_wr_4 = generic_bs_wr_4,
105 .bs_wr_8 = bs_notimpl_bs_wr_8,
106
107 /* set multiple */
108 .bs_sm_1 = bs_notimpl_bs_sm_1,
109 .bs_sm_2 = bs_notimpl_bs_sm_2,
110 .bs_sm_4 = bs_notimpl_bs_sm_4,
111 .bs_sm_8 = bs_notimpl_bs_sm_8,
112
113 /* set region */
114 .bs_sr_1 = bs_notimpl_bs_sr_1,
115 .bs_sr_2 = bs_notimpl_bs_sr_2, /* XXXX */
116 .bs_sr_4 = generic_bs_sr_4,
117 .bs_sr_8 = bs_notimpl_bs_sr_8,
118
119 /* copy */
120 .bs_c_1 = bs_notimpl_bs_c_1,
121 .bs_c_2 = bs_notimpl_bs_c_2, /* XXXX */
122 .bs_c_4 = bs_notimpl_bs_c_4,
123 .bs_c_8 = bs_notimpl_bs_c_8,
124 };
125
126 int
clps711x_bs_map(void * t,bus_addr_t bpa,bus_size_t size,int cacheable,bus_space_handle_t * bshp)127 clps711x_bs_map(void *t, bus_addr_t bpa, bus_size_t size,
128 int cacheable, bus_space_handle_t *bshp)
129 {
130 const struct pmap_devmap *pd;
131 paddr_t startpa, endpa, pa, offset;
132 vaddr_t va;
133
134 if ((pd = pmap_devmap_find_pa(bpa, size)) != NULL) {
135 /* Device was statically mapped. */
136 *bshp = pd->pd_va + (bpa - pd->pd_pa);
137 return 0;
138 }
139
140 endpa = round_page(bpa + size);
141 offset = bpa & PAGE_MASK;
142 startpa = trunc_page(bpa);
143
144 /* Get some VM. */
145 va = uvm_km_alloc(kernel_map, endpa - startpa, 0,
146 UVM_KMF_VAONLY | UVM_KMF_NOWAIT);
147 if (va == 0)
148 return ENOMEM;
149
150 /* Store the bus space handle */
151 *bshp = va + offset;
152
153 /* Now map the pages */
154 for (pa = startpa; pa < endpa; pa += PAGE_SIZE, va += PAGE_SIZE) {
155 pmap_enter(pmap_kernel(), va, pa,
156 VM_PROT_READ | VM_PROT_WRITE,
157 VM_PROT_READ | VM_PROT_WRITE | PMAP_WIRED);
158 }
159 pmap_update(pmap_kernel());
160
161 return 0;
162 }
163
164 void
clps711x_bs_unmap(void * t,bus_space_handle_t bsh,bus_size_t size)165 clps711x_bs_unmap(void *t, bus_space_handle_t bsh, bus_size_t size)
166 {
167 vaddr_t va, endva;
168
169 if (pmap_devmap_find_va(bsh, size) != NULL)
170 /* Device was statically mapped; nothing to do. */
171 return;
172
173 endva = round_page(bsh + size);
174 va = trunc_page(bsh);
175
176 pmap_remove(pmap_kernel(), va, endva);
177 pmap_update(pmap_kernel());
178 uvm_km_free(kernel_map, va, endva - va, UVM_KMF_VAONLY);
179 }
180
181 int
clps711x_bs_alloc(void * t,bus_addr_t rstart,bus_addr_t rend,bus_size_t size,bus_size_t alignment,bus_size_t boundary,int cacheable,bus_addr_t * bpap,bus_space_handle_t * bshp)182 clps711x_bs_alloc(void *t, bus_addr_t rstart, bus_addr_t rend, bus_size_t size,
183 bus_size_t alignment, bus_size_t boundary, int cacheable,
184 bus_addr_t *bpap, bus_space_handle_t *bshp)
185 {
186
187 panic("clps711x_bs_alloc(): not implemented\n");
188 }
189
190 void
clps711x_bs_free(void * t,bus_space_handle_t bsh,bus_size_t size)191 clps711x_bs_free(void *t, bus_space_handle_t bsh, bus_size_t size)
192 {
193
194 panic("clps711x_bs_free(): not implemented\n");
195 }
196
197 int
clps711x_bs_subregion(void * t,bus_space_handle_t bsh,bus_size_t offset,bus_size_t size,bus_space_handle_t * nbshp)198 clps711x_bs_subregion(void *t, bus_space_handle_t bsh, bus_size_t offset,
199 bus_size_t size, bus_space_handle_t *nbshp)
200 {
201
202 *nbshp = bsh + offset;
203 return 0;
204 }
205
206 void *
clps711x_bs_vaddr(void * t,bus_space_handle_t bsh)207 clps711x_bs_vaddr(void *t, bus_space_handle_t bsh)
208 {
209
210 return (void *)bsh;
211 }
212
213 paddr_t
clps711x_bs_mmap(void * t,bus_addr_t addr,off_t off,int prot,int flags)214 clps711x_bs_mmap(void *t, bus_addr_t addr, off_t off, int prot, int flags)
215 {
216 /* Not supported. */
217 return -1;
218 }
219
220 void
clps711x_bs_barrier(void * t,bus_space_handle_t bsh,bus_size_t offset,bus_size_t len,int flags)221 clps711x_bs_barrier(void *t, bus_space_handle_t bsh, bus_size_t offset,
222 bus_size_t len, int flags)
223 {
224 /* Nothing */
225 }
226