xref: /freebsd/tools/bus_space/C/lang.c (revision 02e9120893770924227138ba49df1edb3896112a)
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/types.h>
28 #include <errno.h>
29 
30 #include "bus.h"
31 #include "busdma.h"
32 #include "libbus.h"
33 
34 int16_t
35 bus_read_1(int rid, long ofs)
36 {
37 	uint8_t val;
38 
39 	return ((!bs_read(rid, ofs, &val, sizeof(val))) ? -1 : (int)val);
40 }
41 
42 int32_t
43 bus_read_2(int rid, long ofs)
44 {
45 	uint16_t val;
46 
47 	return ((!bs_read(rid, ofs, &val, sizeof(val))) ? -1 : (int)val);
48 }
49 
50 int64_t
51 bus_read_4(int rid, long ofs)
52 {
53 	uint32_t val;
54 
55 	return ((!bs_read(rid, ofs, &val, sizeof(val))) ? -1 : (int64_t)val);
56 }
57 
58 int
59 bus_write_1(int rid, long ofs, uint8_t val)
60 {
61 
62 	return ((!bs_write(rid, ofs, &val, sizeof(val))) ? errno : 0);
63 }
64 
65 int
66 bus_write_2(int rid, long ofs, uint16_t val)
67 {
68 
69 	return ((!bs_write(rid, ofs, &val, sizeof(val))) ? errno : 0);
70 }
71 
72 int
73 bus_write_4(int rid, long ofs, uint32_t val)
74 {
75 
76 	return ((!bs_write(rid, ofs, &val, sizeof(val))) ? errno : 0);
77 }
78 
79 int
80 bus_map(const char *dev, const char *resource)
81 {
82 
83 	return (bs_map(dev, resource));
84 }
85 
86 int
87 bus_unmap(int rid)
88 {
89 
90 	return ((!bs_unmap(rid)) ? errno : 0);
91 }
92 
93 int
94 bus_subregion(int rid, long ofs, long sz)
95 {
96 
97 	return (bs_subregion(rid, ofs, sz));
98 }
99 
100 int
101 busdma_tag_create(const char *dev, bus_addr_t align, bus_addr_t bndry,
102     bus_addr_t maxaddr, bus_size_t maxsz, u_int nsegs, bus_size_t maxsegsz,
103     u_int datarate, u_int flags, busdma_tag_t *out_p)
104 {
105 	int res;
106 
107 	res = bd_tag_create(dev, align, bndry, maxaddr, maxsz, nsegs, maxsegsz,
108 	    datarate, flags);
109 	if (res == -1)
110 		return (errno);
111 	*out_p = res;
112 	return (0);
113 }
114 
115 int
116 busdma_tag_derive(busdma_tag_t tag, bus_addr_t align, bus_addr_t bndry,
117     bus_addr_t maxaddr, bus_size_t maxsz, u_int nsegs, bus_size_t maxsegsz,
118     u_int datarate, u_int flags, busdma_tag_t *out_p)
119 {
120 	int res;
121 
122 	res = bd_tag_derive(tag, align, bndry, maxaddr, maxsz, nsegs, maxsegsz,
123 	    datarate, flags);
124 	if (res == -1)
125 		return (errno);
126 	*out_p = res;
127 	return (0);
128 }
129 
130 int
131 busdma_tag_destroy(busdma_tag_t tag)
132 {
133 
134 	return (bd_tag_destroy(tag));
135 }
136 
137 int
138 busdma_mem_alloc(busdma_tag_t tag, u_int flags, busdma_md_t *out_p)
139 {
140 	int res;
141 
142 	res = bd_mem_alloc(tag, flags);
143 	if (res == -1)
144 		return (errno);
145 	*out_p = res;
146 	return (0);
147 }
148 
149 int
150 busdma_mem_free(busdma_md_t md)
151 {
152 
153 	return (bd_mem_free(md));
154 }
155 
156 int
157 busdma_md_create(busdma_tag_t tag, u_int flags, busdma_md_t *out_p)
158 {
159 	int res;
160 
161 	res = bd_md_create(tag, flags);
162 	if (res == -1)
163 		return (errno);
164 	*out_p = res;
165 	return (0);
166 }
167 
168 int
169 busdma_md_destroy(busdma_md_t md)
170 {
171 
172 	return (bd_md_destroy(md));
173 }
174 
175 int
176 busdma_md_load(busdma_md_t md, void *buf, size_t len, u_int flags)
177 {
178 
179 	return (bd_md_load(md, buf, len, flags));
180 }
181 
182 int
183 busdma_md_unload(busdma_md_t md)
184 {
185 
186 	return (bd_md_unload(md));
187 }
188 
189 busdma_seg_t
190 busdma_md_first_seg(busdma_md_t md, int space)
191 {
192 	busdma_seg_t seg;
193 
194 	seg = bd_md_first_seg(md, space);
195 	return (seg);
196 }
197 
198 busdma_seg_t
199 busdma_md_next_seg(busdma_md_t md, busdma_seg_t seg)
200 {
201 
202 	seg = bd_md_next_seg(md, seg);
203 	return (seg);
204 }
205 
206 bus_addr_t
207 busdma_seg_get_addr(busdma_seg_t seg)
208 {
209 	u_long addr;
210 	int error;
211 
212 	error = bd_seg_get_addr(seg, &addr);
213 	return ((error) ? ~0UL : addr);
214 }
215 
216 bus_size_t
217 busdma_seg_get_size(busdma_seg_t seg)
218 {
219 	u_long size;
220 	int error;
221 
222 	error = bd_seg_get_size(seg, &size);
223 	return ((error) ? ~0UL : size);
224 }
225 
226 int
227 busdma_sync(busdma_md_t md, int op)
228 {
229 
230 	return (bd_sync(md, op, 0UL, ~0UL));
231 }
232 
233 int
234 busdma_sync_range(busdma_md_t md, int op, bus_size_t ofs, bus_size_t len)
235 {
236 
237 	return (bd_sync(md, op, ofs, len));
238 }
239