1*c5a673c1Sskrll /* $NetBSD: obio_space.c,v 1.10 2023/04/21 14:58:34 skrll Exp $ */
2fd5a840fSthorpej
3fd5a840fSthorpej /*
405d2fb43Sthorpej * Copyright (c) 2001, 2002, 2003 Wasabi Systems, Inc.
5fd5a840fSthorpej * All rights reserved.
6fd5a840fSthorpej *
7fd5a840fSthorpej * Written by Jason R. Thorpe for Wasabi Systems, Inc.
8fd5a840fSthorpej *
9fd5a840fSthorpej * Redistribution and use in source and binary forms, with or without
10fd5a840fSthorpej * modification, are permitted provided that the following conditions
11fd5a840fSthorpej * are met:
12fd5a840fSthorpej * 1. Redistributions of source code must retain the above copyright
13fd5a840fSthorpej * notice, this list of conditions and the following disclaimer.
14fd5a840fSthorpej * 2. Redistributions in binary form must reproduce the above copyright
15fd5a840fSthorpej * notice, this list of conditions and the following disclaimer in the
16fd5a840fSthorpej * documentation and/or other materials provided with the distribution.
17fd5a840fSthorpej * 3. All advertising materials mentioning features or use of this software
18fd5a840fSthorpej * must display the following acknowledgement:
19fd5a840fSthorpej * This product includes software developed for the NetBSD Project by
20fd5a840fSthorpej * Wasabi Systems, Inc.
21fd5a840fSthorpej * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22fd5a840fSthorpej * or promote products derived from this software without specific prior
23fd5a840fSthorpej * written permission.
24fd5a840fSthorpej *
25fd5a840fSthorpej * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26fd5a840fSthorpej * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27fd5a840fSthorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28fd5a840fSthorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29fd5a840fSthorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30fd5a840fSthorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31fd5a840fSthorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32fd5a840fSthorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33fd5a840fSthorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34fd5a840fSthorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35fd5a840fSthorpej * POSSIBILITY OF SUCH DAMAGE.
36fd5a840fSthorpej */
37fd5a840fSthorpej
38fd5a840fSthorpej /*
39fd5a840fSthorpej * bus_space functions for ADI BRH on-board devices
40fd5a840fSthorpej */
41fd5a840fSthorpej
4208716eaeSlukem #include <sys/cdefs.h>
43*c5a673c1Sskrll __KERNEL_RCSID(0, "$NetBSD: obio_space.c,v 1.10 2023/04/21 14:58:34 skrll Exp $");
4408716eaeSlukem
45fd5a840fSthorpej #include <sys/param.h>
46fd5a840fSthorpej #include <sys/systm.h>
47fd5a840fSthorpej
48fd5a840fSthorpej #include <uvm/uvm_extern.h>
49fd5a840fSthorpej
50fea15f47Sdyoung #include <sys/bus.h>
51fd5a840fSthorpej
52fd5a840fSthorpej /* Prototypes for all the bus_space structure functions */
53fd5a840fSthorpej bs_protos(obio);
54fd5a840fSthorpej bs_protos(generic);
55fd5a840fSthorpej bs_protos(generic_armv4);
56fd5a840fSthorpej bs_protos(bs_notimpl);
57fd5a840fSthorpej
58fd5a840fSthorpej /*
59fd5a840fSthorpej * The obio bus space tag. This is constant for all instances, so
60fd5a840fSthorpej * we never have to explicitly "create" it.
61fd5a840fSthorpej */
62fd5a840fSthorpej struct bus_space obio_bs_tag = {
63fd5a840fSthorpej /* cookie */
64509197b6Sryo .bs_cookie = (void *) 0,
65fd5a840fSthorpej
66fd5a840fSthorpej /* mapping/unmapping */
67509197b6Sryo .bs_map = obio_bs_map,
68509197b6Sryo .bs_unmap = obio_bs_unmap,
69509197b6Sryo .bs_subregion = obio_bs_subregion,
70fd5a840fSthorpej
71fd5a840fSthorpej /* allocation/deallocation */
72509197b6Sryo .bs_alloc = obio_bs_alloc,
73509197b6Sryo .bs_free = obio_bs_free,
74fd5a840fSthorpej
75fd5a840fSthorpej /* get kernel virtual address */
76509197b6Sryo .bs_vaddr = obio_bs_vaddr,
77fd5a840fSthorpej
78fd5a840fSthorpej /* mmap */
79509197b6Sryo .bs_mmap = bs_notimpl_bs_mmap,
80fd5a840fSthorpej
81fd5a840fSthorpej /* barrier */
82509197b6Sryo .bs_barrier = obio_bs_barrier,
83fd5a840fSthorpej
84fd5a840fSthorpej /* read (single) */
85509197b6Sryo .bs_r_1 = generic_bs_r_1,
86509197b6Sryo .bs_r_2 = generic_armv4_bs_r_2,
87509197b6Sryo .bs_r_4 = generic_bs_r_4,
88509197b6Sryo .bs_r_8 = bs_notimpl_bs_r_8,
89fd5a840fSthorpej
90fd5a840fSthorpej /* read multiple */
91509197b6Sryo .bs_rm_1 = generic_bs_rm_1,
92509197b6Sryo .bs_rm_2 = bs_notimpl_bs_rm_2,
93509197b6Sryo .bs_rm_4 = bs_notimpl_bs_rm_4,
94509197b6Sryo .bs_rm_8 = bs_notimpl_bs_rm_8,
95fd5a840fSthorpej
96fd5a840fSthorpej /* read region */
97509197b6Sryo .bs_rr_1 = generic_bs_rr_1,
98509197b6Sryo .bs_rr_2 = bs_notimpl_bs_rr_2,
99509197b6Sryo .bs_rr_4 = bs_notimpl_bs_rr_4,
100509197b6Sryo .bs_rr_8 = bs_notimpl_bs_rr_8,
101fd5a840fSthorpej
102fd5a840fSthorpej /* write (single) */
103509197b6Sryo .bs_w_1 = generic_bs_w_1,
104509197b6Sryo .bs_w_2 = generic_armv4_bs_w_2,
105509197b6Sryo .bs_w_4 = generic_bs_w_4,
106509197b6Sryo .bs_w_8 = bs_notimpl_bs_w_8,
107fd5a840fSthorpej
108fd5a840fSthorpej /* write multiple */
109509197b6Sryo .bs_wm_1 = generic_bs_wm_1,
110509197b6Sryo .bs_wm_2 = bs_notimpl_bs_wm_2,
111509197b6Sryo .bs_wm_4 = bs_notimpl_bs_wm_4,
112509197b6Sryo .bs_wm_8 = bs_notimpl_bs_wm_8,
113fd5a840fSthorpej
114fd5a840fSthorpej /* write region */
115509197b6Sryo .bs_wr_1 = bs_notimpl_bs_wr_1,
116509197b6Sryo .bs_wr_2 = bs_notimpl_bs_wr_2,
117509197b6Sryo .bs_wr_4 = bs_notimpl_bs_wr_4,
118509197b6Sryo .bs_wr_8 = bs_notimpl_bs_wr_8,
119fd5a840fSthorpej
120fd5a840fSthorpej /* set multiple */
121509197b6Sryo .bs_sm_1 = bs_notimpl_bs_sm_1,
122509197b6Sryo .bs_sm_2 = bs_notimpl_bs_sm_2,
123509197b6Sryo .bs_sm_4 = bs_notimpl_bs_sm_4,
124509197b6Sryo .bs_sm_8 = bs_notimpl_bs_sm_8,
125fd5a840fSthorpej
126fd5a840fSthorpej /* set region */
127509197b6Sryo .bs_sr_1 = bs_notimpl_bs_sr_1,
128509197b6Sryo .bs_sr_2 = bs_notimpl_bs_sr_2,
129509197b6Sryo .bs_sr_4 = bs_notimpl_bs_sr_4,
130509197b6Sryo .bs_sr_8 = bs_notimpl_bs_sr_8,
131fd5a840fSthorpej
132fd5a840fSthorpej /* copy */
133509197b6Sryo .bs_c_1 = bs_notimpl_bs_c_1,
134509197b6Sryo .bs_c_2 = bs_notimpl_bs_c_2,
135509197b6Sryo .bs_c_4 = bs_notimpl_bs_c_4,
136509197b6Sryo .bs_c_8 = bs_notimpl_bs_c_8,
137fd5a840fSthorpej };
138fd5a840fSthorpej
139fd5a840fSthorpej int
obio_bs_map(void * t,bus_addr_t bpa,bus_size_t size,int flag,bus_space_handle_t * bshp)1405803417aSmatt obio_bs_map(void *t, bus_addr_t bpa, bus_size_t size, int flag,
141fd5a840fSthorpej bus_space_handle_t *bshp)
142fd5a840fSthorpej {
14305d2fb43Sthorpej const struct pmap_devmap *pd;
14405d2fb43Sthorpej paddr_t startpa, endpa, pa, offset;
145fd5a840fSthorpej vaddr_t va;
146fd5a840fSthorpej
14705d2fb43Sthorpej if ((pd = pmap_devmap_find_pa(bpa, size)) != NULL) {
14805d2fb43Sthorpej /* Device was statically mapped. */
14905d2fb43Sthorpej *bshp = pd->pd_va + (bpa - pd->pd_pa);
15005d2fb43Sthorpej return (0);
15105d2fb43Sthorpej }
152fd5a840fSthorpej
153fd5a840fSthorpej endpa = round_page(bpa + size);
15405d2fb43Sthorpej offset = bpa & PAGE_MASK;
15505d2fb43Sthorpej startpa = trunc_page(bpa);
156fd5a840fSthorpej
157bc21da4cSyamt va = uvm_km_alloc(kernel_map, endpa - startpa, 0,
158bc21da4cSyamt UVM_KMF_VAONLY | UVM_KMF_NOWAIT);
15905d2fb43Sthorpej if (va == 0)
160fd5a840fSthorpej return ENOMEM;
161fd5a840fSthorpej
16205d2fb43Sthorpej *bshp = va + offset;
163fd5a840fSthorpej
1645803417aSmatt const int pmapflags =
1655803417aSmatt (flag & (BUS_SPACE_MAP_CACHEABLE|BUS_SPACE_MAP_PREFETCHABLE))
1665803417aSmatt ? 0
1675803417aSmatt : PMAP_NOCACHE;
1685803417aSmatt
169fd5a840fSthorpej for (pa = startpa; pa < endpa; pa += PAGE_SIZE, va += PAGE_SIZE) {
1705803417aSmatt pmap_kenter_pa(va, pa, VM_PROT_READ | VM_PROT_WRITE, pmapflags);
171fd5a840fSthorpej }
172fd5a840fSthorpej pmap_update(pmap_kernel());
173fd5a840fSthorpej
174fd5a840fSthorpej return (0);
175fd5a840fSthorpej }
176fd5a840fSthorpej
177fd5a840fSthorpej int
obio_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 flags,bus_addr_t * bpap,bus_space_handle_t * bshp)178fd5a840fSthorpej obio_bs_alloc(void *t, bus_addr_t rstart, bus_addr_t rend, bus_size_t size,
179fd5a840fSthorpej bus_size_t alignment, bus_size_t boundary, int flags, bus_addr_t *bpap,
180fd5a840fSthorpej bus_space_handle_t *bshp)
181fd5a840fSthorpej {
182fd5a840fSthorpej
183fd5a840fSthorpej panic("obio_bs_alloc(): not implemented\n");
184fd5a840fSthorpej }
185fd5a840fSthorpej
186fd5a840fSthorpej
187fd5a840fSthorpej void
obio_bs_unmap(void * t,bus_space_handle_t bsh,bus_size_t size)188fd5a840fSthorpej obio_bs_unmap(void *t, bus_space_handle_t bsh, bus_size_t size)
189fd5a840fSthorpej {
19005d2fb43Sthorpej vaddr_t va, endva;
191fd5a840fSthorpej
19205d2fb43Sthorpej if (pmap_devmap_find_va(bsh, size) != NULL) {
19305d2fb43Sthorpej /* Device was statically mapped; nothing to do. */
19405d2fb43Sthorpej return;
195fd5a840fSthorpej }
19605d2fb43Sthorpej
19705d2fb43Sthorpej endva = round_page(bsh + size);
19805d2fb43Sthorpej va = trunc_page(bsh);
19905d2fb43Sthorpej
20005d2fb43Sthorpej pmap_kremove(va, endva - va);
2016b2d8b66Syamt uvm_km_free(kernel_map, va, endva - va, UVM_KMF_VAONLY);
202fd5a840fSthorpej }
203fd5a840fSthorpej
204fd5a840fSthorpej void
obio_bs_free(void * t,bus_space_handle_t bsh,bus_size_t size)205fd5a840fSthorpej obio_bs_free(void *t, bus_space_handle_t bsh, bus_size_t size)
206fd5a840fSthorpej {
207fd5a840fSthorpej
208fd5a840fSthorpej panic("obio_bs_free(): not implemented\n");
209fd5a840fSthorpej }
210fd5a840fSthorpej
211fd5a840fSthorpej int
obio_bs_subregion(void * t,bus_space_handle_t bsh,bus_size_t offset,bus_size_t size,bus_space_handle_t * nbshp)212fd5a840fSthorpej obio_bs_subregion(void *t, bus_space_handle_t bsh, bus_size_t offset,
213fd5a840fSthorpej bus_size_t size, bus_space_handle_t *nbshp)
214fd5a840fSthorpej {
215fd5a840fSthorpej
216fd5a840fSthorpej *nbshp = bsh + offset;
217fd5a840fSthorpej return (0);
218fd5a840fSthorpej }
219fd5a840fSthorpej
220fd5a840fSthorpej void *
obio_bs_vaddr(void * t,bus_space_handle_t bsh)221fd5a840fSthorpej obio_bs_vaddr(void *t, bus_space_handle_t bsh)
222fd5a840fSthorpej {
223fd5a840fSthorpej
224fd5a840fSthorpej return ((void *)bsh);
225fd5a840fSthorpej }
226fd5a840fSthorpej
227fd5a840fSthorpej void
obio_bs_barrier(void * t,bus_space_handle_t bsh,bus_size_t offset,bus_size_t len,int flags)228fd5a840fSthorpej obio_bs_barrier(void *t, bus_space_handle_t bsh, bus_size_t offset,
229fd5a840fSthorpej bus_size_t len, int flags)
230fd5a840fSthorpej {
231fd5a840fSthorpej
232fd5a840fSthorpej /* Nothing to do. */
233fd5a840fSthorpej }
234