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