xref: /netbsd-src/lib/libarch/alpha/alpha_pci_io.c (revision 6a493d6bc668897c91594964a732d38505b70cbb)
1 /*	$NetBSD: alpha_pci_io.c,v 1.7 2013/01/09 08:49:44 he Exp $	*/
2 
3 /*-
4  * Copyright (c) 2000 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jason R. Thorpe.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  * Support for x86-style programmed I/O to PCI/EISA/ISA I/O space.  This
34  * is currently used to provide such support for XFree86.  In a perfect
35  * world, this would go away in favor of a real bus space mapping framework.
36  */
37 
38 #include <sys/param.h>
39 
40 #include <machine/alpha_cpu.h>
41 #include <machine/bwx.h>
42 #include <machine/sysarch.h>
43 #include <machine/pio.h>
44 
45 #include <err.h>
46 #include <stdlib.h>
47 
48 struct alpha_bus_window *alpha_pci_io_windows;
49 int alpha_pci_io_window_count;
50 
51 uint8_t		alpha_pci_io_swiz_inb(bus_addr_t);
52 uint16_t	alpha_pci_io_swiz_inw(bus_addr_t);
53 uint32_t	alpha_pci_io_swiz_inl(bus_addr_t);
54 void		alpha_pci_io_swiz_outb(bus_addr_t, uint8_t);
55 void		alpha_pci_io_swiz_outw(bus_addr_t, uint16_t);
56 void		alpha_pci_io_swiz_outl(bus_addr_t, uint32_t);
57 
58 const struct alpha_pci_io_ops alpha_pci_io_swiz_ops = {
59 	alpha_pci_io_swiz_inb,
60 	alpha_pci_io_swiz_inw,
61 	alpha_pci_io_swiz_inl,
62 	alpha_pci_io_swiz_outb,
63 	alpha_pci_io_swiz_outw,
64 	alpha_pci_io_swiz_outl,
65 };
66 
67 uint8_t		alpha_pci_io_bwx_inb(bus_addr_t);
68 uint16_t	alpha_pci_io_bwx_inw(bus_addr_t);
69 uint32_t	alpha_pci_io_bwx_inl(bus_addr_t);
70 void		alpha_pci_io_bwx_outb(bus_addr_t, uint8_t);
71 void		alpha_pci_io_bwx_outw(bus_addr_t, uint16_t);
72 void		alpha_pci_io_bwx_outl(bus_addr_t, uint32_t);
73 
74 const struct alpha_pci_io_ops alpha_pci_io_bwx_ops = {
75 	alpha_pci_io_bwx_inb,
76 	alpha_pci_io_bwx_inw,
77 	alpha_pci_io_bwx_inl,
78 	alpha_pci_io_bwx_outb,
79 	alpha_pci_io_bwx_outw,
80 	alpha_pci_io_bwx_outl,
81 };
82 
83 const struct alpha_pci_io_ops *alpha_pci_io_switch;
84 
85 int
86 alpha_pci_io_enable(int onoff)
87 {
88 	struct alpha_bus_window *abw;
89 	int i, count;
90 
91 	if (onoff == 0 && alpha_pci_io_windows != NULL) {
92 		for (i = 0; i < alpha_pci_io_window_count; i++)
93 			alpha_bus_unmapwindow(&alpha_pci_io_windows[i]);
94 		free(alpha_pci_io_windows);
95 		alpha_pci_io_windows = NULL;
96 		alpha_pci_io_window_count = 0;
97 		alpha_pci_io_switch = NULL;
98 		return (0);
99 	} else if (onoff == 0)
100 		return (0);
101 	else if (alpha_pci_io_windows != NULL)
102 		return (0);
103 
104 	count = alpha_bus_getwindows(ALPHA_BUS_TYPE_PCI_IO, &abw);
105 	if (count <= 0)
106 		return (-1);
107 
108 	for (i = 0; i < count; i++) {
109 		if (alpha_bus_mapwindow(&abw[i]) == -1) {
110 			free(abw);
111 			return (-1);
112 		}
113 	}
114 
115 	alpha_pci_io_windows = abw;
116 	alpha_pci_io_window_count = count;
117 
118 	if (abw->abw_abst.abst_flags & ABST_BWX)
119 		alpha_pci_io_switch = &alpha_pci_io_bwx_ops;
120 	else
121 		alpha_pci_io_switch = &alpha_pci_io_swiz_ops;
122 
123 	return (0);
124 }
125 
126 static inline struct alpha_bus_window *
127 alpha_pci_io_findwindow(bus_addr_t ioaddr)
128 {
129 	struct alpha_bus_window *abw;
130 	int i;
131 
132 	/* XXX Cache the last hit? */
133 
134 	for (i = 0; i < alpha_pci_io_window_count; i++) {
135 		abw = &alpha_pci_io_windows[i];
136 		if (ioaddr >= abw->abw_abst.abst_bus_start &&
137 		    ioaddr <= abw->abw_abst.abst_bus_end)
138 			return (abw);
139 	}
140 
141 	warnx("alpha_pci_io_findwindow: no window for 0x%lx, ABORTING!",
142 	    (u_long) ioaddr);
143 	abort();
144 	/* NOTREACHED */
145 }
146 
147 static inline uint32_t *
148 alpha_pci_io_swiz(bus_addr_t ioaddr, int size)
149 {
150 	struct alpha_bus_window *abw = alpha_pci_io_findwindow(ioaddr);
151 	uint32_t *port;
152 
153 	/* LINTED */
154 	port = (uint32_t *) ((char *)abw->abw_addr +
155 	    (((ioaddr - abw->abw_abst.abst_bus_start) <<
156 	      abw->abw_abst.abst_addr_shift) |
157 	     (size << abw->abw_abst.abst_size_shift)));
158 
159 	return (port);
160 }
161 
162 uint8_t
163 alpha_pci_io_swiz_inb(bus_addr_t ioaddr)
164 {
165 	uint32_t *port = alpha_pci_io_swiz(ioaddr, 0);
166 	bus_addr_t offset = ioaddr & 3;
167 
168 	alpha_mb();
169 
170 	return ((*port >> (8 * offset)) & 0xff);
171 }
172 
173 uint16_t
174 alpha_pci_io_swiz_inw(bus_addr_t ioaddr)
175 {
176 	uint32_t *port = alpha_pci_io_swiz(ioaddr, 1);
177 	bus_addr_t offset = ioaddr & 3;
178 
179 	alpha_mb();
180 
181 	return ((*port >> (8 * offset)) & 0xffff);
182 }
183 
184 uint32_t
185 alpha_pci_io_swiz_inl(bus_addr_t ioaddr)
186 {
187 	uint32_t *port = alpha_pci_io_swiz(ioaddr, 3);
188 
189 	alpha_mb();
190 
191 	return (*port);
192 }
193 
194 void
195 alpha_pci_io_swiz_outb(bus_addr_t ioaddr, uint8_t val)
196 {
197 	uint32_t *port = alpha_pci_io_swiz(ioaddr, 0);
198 	bus_addr_t offset = ioaddr & 3;
199 	uint32_t nval = ((uint32_t)val) << (uint32_t)(8 * offset);
200 
201 	*port = nval;
202 	alpha_mb();
203 }
204 
205 void
206 alpha_pci_io_swiz_outw(bus_addr_t ioaddr, uint16_t val)
207 {
208 	uint32_t *port = alpha_pci_io_swiz(ioaddr, 1);
209 	bus_addr_t offset = ioaddr & 3;
210 	uint32_t nval = ((uint32_t)val) << (uint32_t)(8 * offset);
211 
212 	*port = nval;
213 	alpha_mb();
214 }
215 
216 void
217 alpha_pci_io_swiz_outl(bus_addr_t ioaddr, uint32_t val)
218 {
219 	uint32_t *port = alpha_pci_io_swiz(ioaddr, 3);
220 
221 	*port = val;
222 	alpha_mb();
223 }
224 
225 /*
226  * The following functions are used only on EV56 and greater CPUs,
227  * and the assembler requires going to EV56 mode in order to emit
228  * these instructions.
229  */
230 __asm(".arch ev56");
231 
232 uint8_t
233 alpha_pci_io_bwx_inb(bus_addr_t ioaddr)
234 {
235 	struct alpha_bus_window *abw = alpha_pci_io_findwindow(ioaddr);
236 	uint8_t *port = (uint8_t *) ((char *)abw->abw_addr +
237 	    (ioaddr - abw->abw_abst.abst_bus_start));
238 
239 	alpha_mb();
240 
241 	return (alpha_ldbu(port));
242 }
243 
244 uint16_t
245 alpha_pci_io_bwx_inw(bus_addr_t ioaddr)
246 {
247 	struct alpha_bus_window *abw = alpha_pci_io_findwindow(ioaddr);
248 	/* LINTED */
249 	uint16_t *port = (uint16_t *) ((char *)abw->abw_addr +
250 	    (ioaddr - abw->abw_abst.abst_bus_start));
251 
252 	alpha_mb();
253 
254 	return (alpha_ldwu(port));
255 }
256 
257 uint32_t
258 alpha_pci_io_bwx_inl(bus_addr_t ioaddr)
259 {
260 	struct alpha_bus_window *abw = alpha_pci_io_findwindow(ioaddr);
261 	/* LINTED */
262 	uint32_t *port = (uint32_t *) ((char *)abw->abw_addr +
263 	    (ioaddr - abw->abw_abst.abst_bus_start));
264 
265 	alpha_mb();
266 
267 	return (*port);
268 }
269 
270 void
271 alpha_pci_io_bwx_outb(bus_addr_t ioaddr, uint8_t val)
272 {
273 	struct alpha_bus_window *abw = alpha_pci_io_findwindow(ioaddr);
274 	uint8_t *port = (uint8_t *) ((char *)abw->abw_addr +
275 	    (ioaddr - abw->abw_abst.abst_bus_start));
276 
277 	alpha_stb(port, val);
278 	alpha_mb();
279 }
280 
281 void
282 alpha_pci_io_bwx_outw(bus_addr_t ioaddr, uint16_t val)
283 {
284 	struct alpha_bus_window *abw = alpha_pci_io_findwindow(ioaddr);
285 	/* LINTED */
286 	uint16_t *port = (uint16_t *) ((char *)abw->abw_addr +
287 	    (ioaddr - abw->abw_abst.abst_bus_start));
288 
289 	alpha_stw(port, val);
290 	alpha_mb();
291 }
292 
293 void
294 alpha_pci_io_bwx_outl(bus_addr_t ioaddr, uint32_t val)
295 {
296 	struct alpha_bus_window *abw = alpha_pci_io_findwindow(ioaddr);
297 	/* LINTED */
298 	uint32_t *port = (uint32_t *) ((char *)abw->abw_addr +
299 	    (ioaddr - abw->abw_abst.abst_bus_start));
300 
301 	*port = val;
302 	alpha_mb();
303 }
304