jensenio_bus_intio.c revision 1.6 1 1.6 thorpej /* $NetBSD: jensenio_bus_intio.c,v 1.6 2021/07/04 22:42:35 thorpej Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*-
4 1.1 thorpej * Copyright (c) 1999 The NetBSD Foundation, Inc.
5 1.1 thorpej * All rights reserved.
6 1.1 thorpej *
7 1.1 thorpej * This code is derived from software contributed to The NetBSD Foundation
8 1.1 thorpej * by Jason R. Thorpe.
9 1.1 thorpej *
10 1.1 thorpej * Redistribution and use in source and binary forms, with or without
11 1.1 thorpej * modification, are permitted provided that the following conditions
12 1.1 thorpej * are met:
13 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
14 1.1 thorpej * notice, this list of conditions and the following disclaimer.
15 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
17 1.1 thorpej * documentation and/or other materials provided with the distribution.
18 1.1 thorpej *
19 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 thorpej * POSSIBILITY OF SUCH DAMAGE.
30 1.1 thorpej */
31 1.1 thorpej
32 1.1 thorpej #include <sys/cdefs.h> /* RCS ID & Copyright macro defns */
33 1.1 thorpej
34 1.6 thorpej __KERNEL_RCSID(0, "$NetBSD: jensenio_bus_intio.c,v 1.6 2021/07/04 22:42:35 thorpej Exp $");
35 1.1 thorpej
36 1.1 thorpej #include <sys/param.h>
37 1.1 thorpej #include <sys/systm.h>
38 1.1 thorpej #include <sys/device.h>
39 1.1 thorpej #include <sys/extent.h>
40 1.1 thorpej
41 1.3 dyoung #include <sys/bus.h>
42 1.1 thorpej
43 1.1 thorpej #include <dev/eisa/eisavar.h>
44 1.1 thorpej
45 1.1 thorpej #include <dev/isa/isavar.h>
46 1.1 thorpej
47 1.1 thorpej #include <alpha/jensenio/jensenioreg.h>
48 1.1 thorpej #include <alpha/jensenio/jenseniovar.h>
49 1.1 thorpej
50 1.1 thorpej /* mapping/unmapping */
51 1.1 thorpej int jensenio_intio_map(void *, bus_addr_t, bus_size_t, int,
52 1.1 thorpej bus_space_handle_t *, int);
53 1.1 thorpej void jensenio_intio_unmap(void *, bus_space_handle_t,
54 1.1 thorpej bus_size_t, int);
55 1.1 thorpej int jensenio_intio_subregion(void *, bus_space_handle_t,
56 1.1 thorpej bus_size_t, bus_size_t, bus_space_handle_t *);
57 1.1 thorpej
58 1.1 thorpej /* allocation/deallocation */
59 1.1 thorpej /* Not supported for Internal space */
60 1.1 thorpej
61 1.1 thorpej /* barrier */
62 1.4 chs static inline void jensenio_intio_barrier(void *, bus_space_handle_t,
63 1.1 thorpej bus_size_t, bus_size_t, int);
64 1.1 thorpej
65 1.1 thorpej /* read (single) */
66 1.5 matt static inline uint8_t jensenio_intio_read_1(void *, bus_space_handle_t, bus_size_t);
67 1.1 thorpej
68 1.1 thorpej /* read multiple */
69 1.1 thorpej void jensenio_intio_read_multi_1(void *, bus_space_handle_t,
70 1.5 matt bus_size_t, uint8_t *, bus_size_t);
71 1.1 thorpej
72 1.1 thorpej /* read region */
73 1.1 thorpej /* Not supported for Internal space */
74 1.1 thorpej
75 1.1 thorpej /* write (single) */
76 1.4 chs static inline void jensenio_intio_write_1(void *, bus_space_handle_t,
77 1.5 matt bus_size_t, uint8_t);
78 1.1 thorpej
79 1.1 thorpej /* write multiple */
80 1.1 thorpej void jensenio_intio_write_multi_1(void *, bus_space_handle_t,
81 1.5 matt bus_size_t, const uint8_t *, bus_size_t);
82 1.1 thorpej
83 1.1 thorpej /* write region */
84 1.1 thorpej /* Not supported for Internal space */
85 1.1 thorpej
86 1.1 thorpej /* set multiple */
87 1.1 thorpej void jensenio_intio_set_multi_1(void *, bus_space_handle_t,
88 1.5 matt bus_size_t, uint8_t, bus_size_t);
89 1.1 thorpej
90 1.1 thorpej /* set region */
91 1.1 thorpej /* Not supported for Internal space */
92 1.1 thorpej
93 1.1 thorpej /* copy */
94 1.1 thorpej /* Not supported for Internal space */
95 1.1 thorpej
96 1.1 thorpej void
97 1.1 thorpej jensenio_bus_intio_init(bus_space_tag_t t, void *v)
98 1.1 thorpej {
99 1.1 thorpej
100 1.1 thorpej /*
101 1.1 thorpej * Initialize the bus space tag.
102 1.1 thorpej */
103 1.1 thorpej
104 1.1 thorpej memset(t, 0, sizeof(*t));
105 1.1 thorpej
106 1.1 thorpej /* cookie */
107 1.1 thorpej t->abs_cookie = v;
108 1.1 thorpej
109 1.1 thorpej /* mapping/unmapping */
110 1.1 thorpej t->abs_map = jensenio_intio_map;
111 1.1 thorpej t->abs_unmap = jensenio_intio_unmap;
112 1.1 thorpej t->abs_subregion = jensenio_intio_subregion;
113 1.1 thorpej
114 1.1 thorpej /* barrier */
115 1.1 thorpej t->abs_barrier = jensenio_intio_barrier;
116 1.1 thorpej
117 1.1 thorpej /* read (single) */
118 1.1 thorpej t->abs_r_1 = jensenio_intio_read_1;
119 1.1 thorpej
120 1.1 thorpej /* read multiple */
121 1.1 thorpej t->abs_rm_1 = jensenio_intio_read_multi_1;
122 1.1 thorpej
123 1.1 thorpej /* write (single) */
124 1.1 thorpej t->abs_w_1 = jensenio_intio_write_1;
125 1.1 thorpej
126 1.1 thorpej /* write multiple */
127 1.1 thorpej t->abs_wm_1 = jensenio_intio_write_multi_1;
128 1.1 thorpej
129 1.1 thorpej /* set multiple */
130 1.1 thorpej t->abs_sm_1 = jensenio_intio_set_multi_1;
131 1.1 thorpej
132 1.1 thorpej /*
133 1.1 thorpej * Extent map is already set up.
134 1.1 thorpej */
135 1.1 thorpej }
136 1.1 thorpej
137 1.1 thorpej int
138 1.1 thorpej jensenio_intio_map(void *v, bus_addr_t ioaddr, bus_size_t iosize, int flags,
139 1.1 thorpej bus_space_handle_t *iohp, int acct)
140 1.1 thorpej {
141 1.1 thorpej struct jensenio_config *jcp = v;
142 1.1 thorpej int linear = flags & BUS_SPACE_MAP_LINEAR;
143 1.1 thorpej int error;
144 1.1 thorpej
145 1.1 thorpej /*
146 1.1 thorpej * Can't map i/o space linearly.
147 1.1 thorpej */
148 1.1 thorpej if (linear)
149 1.1 thorpej return (EOPNOTSUPP);
150 1.1 thorpej
151 1.1 thorpej if (acct) {
152 1.1 thorpej #ifdef EXTENT_DEBUG
153 1.1 thorpej printf("intio: allocating 0x%lx to 0x%lx\n", ioaddr,
154 1.1 thorpej ioaddr + iosize - 1);
155 1.1 thorpej #endif
156 1.1 thorpej error = extent_alloc_region(jcp->jc_io_ex, ioaddr, iosize,
157 1.1 thorpej EX_NOWAIT | (jcp->jc_mallocsafe ? EX_MALLOCOK : 0));
158 1.1 thorpej if (error) {
159 1.1 thorpej #ifdef EXTENT_DEBUG
160 1.1 thorpej printf("intio: allocation failed (%d)\n", error);
161 1.1 thorpej extent_print(jcp->jc_io_ex);
162 1.1 thorpej #endif
163 1.1 thorpej return (error);
164 1.1 thorpej }
165 1.1 thorpej }
166 1.1 thorpej
167 1.1 thorpej *iohp = ALPHA_PHYS_TO_K0SEG((ioaddr << 9) + JENSEN_VL82C106);
168 1.1 thorpej return (0);
169 1.1 thorpej }
170 1.1 thorpej
171 1.1 thorpej void
172 1.1 thorpej jensenio_intio_unmap(void *v, bus_space_handle_t ioh, bus_size_t iosize,
173 1.1 thorpej int acct)
174 1.1 thorpej {
175 1.1 thorpej struct jensenio_config *jcp = v;
176 1.1 thorpej bus_addr_t ioaddr;
177 1.1 thorpej int error;
178 1.1 thorpej
179 1.1 thorpej if (acct == 0)
180 1.1 thorpej return;
181 1.1 thorpej
182 1.1 thorpej #ifdef EXTENT_DEBUG
183 1.1 thorpej printf("intio: freeing handle 0x%lx for 0x%lx\n", ioh, iosize);
184 1.1 thorpej #endif
185 1.1 thorpej
186 1.1 thorpej ioh = ALPHA_K0SEG_TO_PHYS(ioh);
187 1.1 thorpej
188 1.1 thorpej ioaddr = (ioh - JENSEN_VL82C106) >> 9;
189 1.1 thorpej
190 1.1 thorpej #ifdef EXTENT_DEBUG
191 1.1 thorpej printf("intio: freeing 0x%lx to 0x%lx\n", ioaddr, ioaddr + iosize - 1);
192 1.1 thorpej #endif
193 1.1 thorpej error = extent_free(jcp->jc_io_ex, ioaddr, iosize,
194 1.1 thorpej EX_NOWAIT | (jcp->jc_mallocsafe ? EX_MALLOCOK : 0));
195 1.1 thorpej if (error) {
196 1.1 thorpej printf("WARNING: could not unmap 0x%lx-0x%lx (error %d)\n",
197 1.1 thorpej ioaddr, ioaddr + iosize - 1, error);
198 1.1 thorpej #ifdef EXTENT_DEBUG
199 1.1 thorpej extent_print(jcp->jc_io_ex);
200 1.1 thorpej #endif
201 1.1 thorpej }
202 1.1 thorpej }
203 1.1 thorpej
204 1.1 thorpej int
205 1.1 thorpej jensenio_intio_subregion(void *v, bus_space_handle_t ioh, bus_size_t offset,
206 1.1 thorpej bus_size_t size, bus_space_handle_t *nioh)
207 1.1 thorpej {
208 1.1 thorpej
209 1.1 thorpej *nioh = ioh + (offset << 9);
210 1.1 thorpej return (0);
211 1.1 thorpej }
212 1.1 thorpej
213 1.4 chs static inline void
214 1.1 thorpej jensenio_intio_barrier(void *v, bus_space_handle_t h, bus_size_t o,
215 1.1 thorpej bus_size_t l, int f)
216 1.1 thorpej {
217 1.1 thorpej
218 1.1 thorpej if ((f & BUS_SPACE_BARRIER_READ) != 0)
219 1.1 thorpej alpha_mb();
220 1.1 thorpej else if ((f & BUS_SPACE_BARRIER_WRITE) != 0)
221 1.1 thorpej alpha_wmb();
222 1.1 thorpej }
223 1.1 thorpej
224 1.5 matt static inline uint8_t
225 1.1 thorpej jensenio_intio_read_1(void *v, bus_space_handle_t ioh, bus_size_t off)
226 1.1 thorpej {
227 1.5 matt register uint32_t *port;
228 1.1 thorpej
229 1.1 thorpej alpha_mb();
230 1.1 thorpej
231 1.5 matt port = (uint32_t *)(ioh + (off << 9));
232 1.1 thorpej return (*port & 0xff);
233 1.1 thorpej }
234 1.1 thorpej
235 1.1 thorpej void
236 1.1 thorpej jensenio_intio_read_multi_1(void *v, bus_space_handle_t h, bus_size_t o,
237 1.5 matt uint8_t *a, bus_size_t c)
238 1.1 thorpej {
239 1.1 thorpej
240 1.1 thorpej while (c-- > 0) {
241 1.1 thorpej jensenio_intio_barrier(v, h, o, sizeof *a,
242 1.1 thorpej BUS_SPACE_BARRIER_READ);
243 1.1 thorpej *a++ = jensenio_intio_read_1(v, h, o);
244 1.1 thorpej }
245 1.1 thorpej }
246 1.1 thorpej
247 1.4 chs static inline void
248 1.1 thorpej jensenio_intio_write_1(void *v, bus_space_handle_t ioh, bus_size_t off,
249 1.5 matt uint8_t val)
250 1.1 thorpej {
251 1.5 matt register uint32_t *port;
252 1.1 thorpej
253 1.5 matt port = (uint32_t *)(ioh + (off << 9));
254 1.1 thorpej *port = val;
255 1.1 thorpej alpha_mb();
256 1.1 thorpej }
257 1.1 thorpej
258 1.1 thorpej void
259 1.1 thorpej jensenio_intio_write_multi_1(void *v, bus_space_handle_t h, bus_size_t o,
260 1.5 matt const uint8_t *a, bus_size_t c)
261 1.1 thorpej {
262 1.1 thorpej
263 1.1 thorpej while (c-- > 0) {
264 1.1 thorpej jensenio_intio_write_1(v, h, o, *a++);
265 1.1 thorpej jensenio_intio_barrier(v, h, o, sizeof *a,
266 1.1 thorpej BUS_SPACE_BARRIER_WRITE);
267 1.1 thorpej }
268 1.1 thorpej }
269 1.1 thorpej
270 1.1 thorpej void
271 1.1 thorpej jensenio_intio_set_multi_1(void *v, bus_space_handle_t h, bus_size_t o,
272 1.5 matt uint8_t val, bus_size_t c)
273 1.1 thorpej {
274 1.1 thorpej
275 1.1 thorpej while (c-- > 0) {
276 1.1 thorpej jensenio_intio_write_1(v, h, o, val);
277 1.1 thorpej jensenio_intio_barrier(v, h, o, sizeof val,
278 1.1 thorpej BUS_SPACE_BARRIER_WRITE);
279 1.1 thorpej }
280 1.1 thorpej }
281