xref: /freebsd/tools/bus_space/C/lang.c (revision edf8578117e8844e02c0121147f45e4609b30680)
1 /*-
2  * Copyright (c) 2014 Marcel Moolenaar
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 #include <sys/types.h>
29 #include <errno.h>
30 
31 #include "bus.h"
32 #include "busdma.h"
33 #include "libbus.h"
34 
35 int16_t
36 bus_read_1(int rid, long ofs)
37 {
38 	uint8_t val;
39 
40 	return ((!bs_read(rid, ofs, &val, sizeof(val))) ? -1 : (int)val);
41 }
42 
43 int32_t
44 bus_read_2(int rid, long ofs)
45 {
46 	uint16_t val;
47 
48 	return ((!bs_read(rid, ofs, &val, sizeof(val))) ? -1 : (int)val);
49 }
50 
51 int64_t
52 bus_read_4(int rid, long ofs)
53 {
54 	uint32_t val;
55 
56 	return ((!bs_read(rid, ofs, &val, sizeof(val))) ? -1 : (int64_t)val);
57 }
58 
59 int
60 bus_write_1(int rid, long ofs, uint8_t val)
61 {
62 
63 	return ((!bs_write(rid, ofs, &val, sizeof(val))) ? errno : 0);
64 }
65 
66 int
67 bus_write_2(int rid, long ofs, uint16_t val)
68 {
69 
70 	return ((!bs_write(rid, ofs, &val, sizeof(val))) ? errno : 0);
71 }
72 
73 int
74 bus_write_4(int rid, long ofs, uint32_t val)
75 {
76 
77 	return ((!bs_write(rid, ofs, &val, sizeof(val))) ? errno : 0);
78 }
79 
80 int
81 bus_map(const char *dev, const char *resource)
82 {
83 
84 	return (bs_map(dev, resource));
85 }
86 
87 int
88 bus_unmap(int rid)
89 {
90 
91 	return ((!bs_unmap(rid)) ? errno : 0);
92 }
93 
94 int
95 bus_subregion(int rid, long ofs, long sz)
96 {
97 
98 	return (bs_subregion(rid, ofs, sz));
99 }
100 
101 int
102 busdma_tag_create(const char *dev, bus_addr_t align, bus_addr_t bndry,
103     bus_addr_t maxaddr, bus_size_t maxsz, u_int nsegs, bus_size_t maxsegsz,
104     u_int datarate, u_int flags, busdma_tag_t *out_p)
105 {
106 	int res;
107 
108 	res = bd_tag_create(dev, align, bndry, maxaddr, maxsz, nsegs, maxsegsz,
109 	    datarate, flags);
110 	if (res == -1)
111 		return (errno);
112 	*out_p = res;
113 	return (0);
114 }
115 
116 int
117 busdma_tag_derive(busdma_tag_t tag, bus_addr_t align, bus_addr_t bndry,
118     bus_addr_t maxaddr, bus_size_t maxsz, u_int nsegs, bus_size_t maxsegsz,
119     u_int datarate, u_int flags, busdma_tag_t *out_p)
120 {
121 	int res;
122 
123 	res = bd_tag_derive(tag, align, bndry, maxaddr, maxsz, nsegs, maxsegsz,
124 	    datarate, flags);
125 	if (res == -1)
126 		return (errno);
127 	*out_p = res;
128 	return (0);
129 }
130 
131 int
132 busdma_tag_destroy(busdma_tag_t tag)
133 {
134 
135 	return (bd_tag_destroy(tag));
136 }
137 
138 int
139 busdma_mem_alloc(busdma_tag_t tag, u_int flags, busdma_md_t *out_p)
140 {
141 	int res;
142 
143 	res = bd_mem_alloc(tag, flags);
144 	if (res == -1)
145 		return (errno);
146 	*out_p = res;
147 	return (0);
148 }
149 
150 int
151 busdma_mem_free(busdma_md_t md)
152 {
153 
154 	return (bd_mem_free(md));
155 }
156 
157 int
158 busdma_md_create(busdma_tag_t tag, u_int flags, busdma_md_t *out_p)
159 {
160 	int res;
161 
162 	res = bd_md_create(tag, flags);
163 	if (res == -1)
164 		return (errno);
165 	*out_p = res;
166 	return (0);
167 }
168 
169 int
170 busdma_md_destroy(busdma_md_t md)
171 {
172 
173 	return (bd_md_destroy(md));
174 }
175 
176 int
177 busdma_md_load(busdma_md_t md, void *buf, size_t len, u_int flags)
178 {
179 
180 	return (bd_md_load(md, buf, len, flags));
181 }
182 
183 int
184 busdma_md_unload(busdma_md_t md)
185 {
186 
187 	return (bd_md_unload(md));
188 }
189 
190 busdma_seg_t
191 busdma_md_first_seg(busdma_md_t md, int space)
192 {
193 	busdma_seg_t seg;
194 
195 	seg = bd_md_first_seg(md, space);
196 	return (seg);
197 }
198 
199 busdma_seg_t
200 busdma_md_next_seg(busdma_md_t md, busdma_seg_t seg)
201 {
202 
203 	seg = bd_md_next_seg(md, seg);
204 	return (seg);
205 }
206 
207 bus_addr_t
208 busdma_seg_get_addr(busdma_seg_t seg)
209 {
210 	u_long addr;
211 	int error;
212 
213 	error = bd_seg_get_addr(seg, &addr);
214 	return ((error) ? ~0UL : addr);
215 }
216 
217 bus_size_t
218 busdma_seg_get_size(busdma_seg_t seg)
219 {
220 	u_long size;
221 	int error;
222 
223 	error = bd_seg_get_size(seg, &size);
224 	return ((error) ? ~0UL : size);
225 }
226 
227 int
228 busdma_sync(busdma_md_t md, int op)
229 {
230 
231 	return (bd_sync(md, op, 0UL, ~0UL));
232 }
233 
234 int
235 busdma_sync_range(busdma_md_t md, int op, bus_size_t ofs, bus_size_t len)
236 {
237 
238 	return (bd_sync(md, op, ofs, len));
239 }
240