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