1 1.8 rin /* $NetBSD: alpha_pci_io.c,v 1.8 2017/02/20 15:23:43 rin Exp $ */ 2 1.1 thorpej 3 1.1 thorpej /*- 4 1.1 thorpej * Copyright (c) 2000 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 /* 33 1.1 thorpej * Support for x86-style programmed I/O to PCI/EISA/ISA I/O space. This 34 1.1 thorpej * is currently used to provide such support for XFree86. In a perfect 35 1.1 thorpej * world, this would go away in favor of a real bus space mapping framework. 36 1.1 thorpej */ 37 1.1 thorpej 38 1.1 thorpej #include <sys/param.h> 39 1.1 thorpej 40 1.7 he #include <machine/alpha_cpu.h> 41 1.1 thorpej #include <machine/sysarch.h> 42 1.1 thorpej #include <machine/pio.h> 43 1.1 thorpej 44 1.1 thorpej #include <err.h> 45 1.1 thorpej #include <stdlib.h> 46 1.1 thorpej 47 1.1 thorpej struct alpha_bus_window *alpha_pci_io_windows; 48 1.1 thorpej int alpha_pci_io_window_count; 49 1.1 thorpej 50 1.2 thorpej uint8_t alpha_pci_io_swiz_inb(bus_addr_t); 51 1.2 thorpej uint16_t alpha_pci_io_swiz_inw(bus_addr_t); 52 1.2 thorpej uint32_t alpha_pci_io_swiz_inl(bus_addr_t); 53 1.2 thorpej void alpha_pci_io_swiz_outb(bus_addr_t, uint8_t); 54 1.2 thorpej void alpha_pci_io_swiz_outw(bus_addr_t, uint16_t); 55 1.2 thorpej void alpha_pci_io_swiz_outl(bus_addr_t, uint32_t); 56 1.1 thorpej 57 1.1 thorpej const struct alpha_pci_io_ops alpha_pci_io_swiz_ops = { 58 1.1 thorpej alpha_pci_io_swiz_inb, 59 1.1 thorpej alpha_pci_io_swiz_inw, 60 1.1 thorpej alpha_pci_io_swiz_inl, 61 1.1 thorpej alpha_pci_io_swiz_outb, 62 1.1 thorpej alpha_pci_io_swiz_outw, 63 1.1 thorpej alpha_pci_io_swiz_outl, 64 1.1 thorpej }; 65 1.1 thorpej 66 1.2 thorpej uint8_t alpha_pci_io_bwx_inb(bus_addr_t); 67 1.2 thorpej uint16_t alpha_pci_io_bwx_inw(bus_addr_t); 68 1.2 thorpej uint32_t alpha_pci_io_bwx_inl(bus_addr_t); 69 1.2 thorpej void alpha_pci_io_bwx_outb(bus_addr_t, uint8_t); 70 1.2 thorpej void alpha_pci_io_bwx_outw(bus_addr_t, uint16_t); 71 1.2 thorpej void alpha_pci_io_bwx_outl(bus_addr_t, uint32_t); 72 1.1 thorpej 73 1.1 thorpej const struct alpha_pci_io_ops alpha_pci_io_bwx_ops = { 74 1.1 thorpej alpha_pci_io_bwx_inb, 75 1.1 thorpej alpha_pci_io_bwx_inw, 76 1.1 thorpej alpha_pci_io_bwx_inl, 77 1.1 thorpej alpha_pci_io_bwx_outb, 78 1.1 thorpej alpha_pci_io_bwx_outw, 79 1.1 thorpej alpha_pci_io_bwx_outl, 80 1.1 thorpej }; 81 1.1 thorpej 82 1.1 thorpej const struct alpha_pci_io_ops *alpha_pci_io_switch; 83 1.1 thorpej 84 1.1 thorpej int 85 1.2 thorpej alpha_pci_io_enable(int onoff) 86 1.1 thorpej { 87 1.1 thorpej struct alpha_bus_window *abw; 88 1.1 thorpej int i, count; 89 1.1 thorpej 90 1.1 thorpej if (onoff == 0 && alpha_pci_io_windows != NULL) { 91 1.1 thorpej for (i = 0; i < alpha_pci_io_window_count; i++) 92 1.1 thorpej alpha_bus_unmapwindow(&alpha_pci_io_windows[i]); 93 1.1 thorpej free(alpha_pci_io_windows); 94 1.1 thorpej alpha_pci_io_windows = NULL; 95 1.1 thorpej alpha_pci_io_window_count = 0; 96 1.1 thorpej alpha_pci_io_switch = NULL; 97 1.1 thorpej return (0); 98 1.1 thorpej } else if (onoff == 0) 99 1.1 thorpej return (0); 100 1.1 thorpej else if (alpha_pci_io_windows != NULL) 101 1.1 thorpej return (0); 102 1.1 thorpej 103 1.1 thorpej count = alpha_bus_getwindows(ALPHA_BUS_TYPE_PCI_IO, &abw); 104 1.1 thorpej if (count <= 0) 105 1.1 thorpej return (-1); 106 1.1 thorpej 107 1.1 thorpej for (i = 0; i < count; i++) { 108 1.1 thorpej if (alpha_bus_mapwindow(&abw[i]) == -1) { 109 1.1 thorpej free(abw); 110 1.1 thorpej return (-1); 111 1.1 thorpej } 112 1.1 thorpej } 113 1.1 thorpej 114 1.1 thorpej alpha_pci_io_windows = abw; 115 1.1 thorpej alpha_pci_io_window_count = count; 116 1.1 thorpej 117 1.1 thorpej if (abw->abw_abst.abst_flags & ABST_BWX) 118 1.1 thorpej alpha_pci_io_switch = &alpha_pci_io_bwx_ops; 119 1.1 thorpej else 120 1.1 thorpej alpha_pci_io_switch = &alpha_pci_io_swiz_ops; 121 1.1 thorpej 122 1.1 thorpej return (0); 123 1.1 thorpej } 124 1.1 thorpej 125 1.3 perry static inline struct alpha_bus_window * 126 1.2 thorpej alpha_pci_io_findwindow(bus_addr_t ioaddr) 127 1.1 thorpej { 128 1.1 thorpej struct alpha_bus_window *abw; 129 1.1 thorpej int i; 130 1.1 thorpej 131 1.1 thorpej /* XXX Cache the last hit? */ 132 1.1 thorpej 133 1.1 thorpej for (i = 0; i < alpha_pci_io_window_count; i++) { 134 1.1 thorpej abw = &alpha_pci_io_windows[i]; 135 1.1 thorpej if (ioaddr >= abw->abw_abst.abst_bus_start && 136 1.1 thorpej ioaddr <= abw->abw_abst.abst_bus_end) 137 1.1 thorpej return (abw); 138 1.1 thorpej } 139 1.1 thorpej 140 1.1 thorpej warnx("alpha_pci_io_findwindow: no window for 0x%lx, ABORTING!", 141 1.1 thorpej (u_long) ioaddr); 142 1.1 thorpej abort(); 143 1.2 thorpej /* NOTREACHED */ 144 1.1 thorpej } 145 1.1 thorpej 146 1.3 perry static inline uint32_t * 147 1.2 thorpej alpha_pci_io_swiz(bus_addr_t ioaddr, int size) 148 1.1 thorpej { 149 1.1 thorpej struct alpha_bus_window *abw = alpha_pci_io_findwindow(ioaddr); 150 1.2 thorpej uint32_t *port; 151 1.1 thorpej 152 1.2 thorpej /* LINTED */ 153 1.4 mrg port = (uint32_t *) ((char *)abw->abw_addr + 154 1.1 thorpej (((ioaddr - abw->abw_abst.abst_bus_start) << 155 1.1 thorpej abw->abw_abst.abst_addr_shift) | 156 1.1 thorpej (size << abw->abw_abst.abst_size_shift))); 157 1.1 thorpej 158 1.1 thorpej return (port); 159 1.1 thorpej } 160 1.1 thorpej 161 1.2 thorpej uint8_t 162 1.2 thorpej alpha_pci_io_swiz_inb(bus_addr_t ioaddr) 163 1.1 thorpej { 164 1.2 thorpej uint32_t *port = alpha_pci_io_swiz(ioaddr, 0); 165 1.2 thorpej bus_addr_t offset = ioaddr & 3; 166 1.1 thorpej 167 1.1 thorpej alpha_mb(); 168 1.1 thorpej 169 1.1 thorpej return ((*port >> (8 * offset)) & 0xff); 170 1.1 thorpej } 171 1.1 thorpej 172 1.2 thorpej uint16_t 173 1.2 thorpej alpha_pci_io_swiz_inw(bus_addr_t ioaddr) 174 1.1 thorpej { 175 1.2 thorpej uint32_t *port = alpha_pci_io_swiz(ioaddr, 1); 176 1.2 thorpej bus_addr_t offset = ioaddr & 3; 177 1.1 thorpej 178 1.1 thorpej alpha_mb(); 179 1.1 thorpej 180 1.1 thorpej return ((*port >> (8 * offset)) & 0xffff); 181 1.1 thorpej } 182 1.1 thorpej 183 1.2 thorpej uint32_t 184 1.2 thorpej alpha_pci_io_swiz_inl(bus_addr_t ioaddr) 185 1.1 thorpej { 186 1.2 thorpej uint32_t *port = alpha_pci_io_swiz(ioaddr, 3); 187 1.1 thorpej 188 1.1 thorpej alpha_mb(); 189 1.1 thorpej 190 1.1 thorpej return (*port); 191 1.1 thorpej } 192 1.1 thorpej 193 1.1 thorpej void 194 1.2 thorpej alpha_pci_io_swiz_outb(bus_addr_t ioaddr, uint8_t val) 195 1.2 thorpej { 196 1.2 thorpej uint32_t *port = alpha_pci_io_swiz(ioaddr, 0); 197 1.2 thorpej bus_addr_t offset = ioaddr & 3; 198 1.6 he uint32_t nval = ((uint32_t)val) << (uint32_t)(8 * offset); 199 1.1 thorpej 200 1.1 thorpej *port = nval; 201 1.1 thorpej alpha_mb(); 202 1.1 thorpej } 203 1.1 thorpej 204 1.1 thorpej void 205 1.2 thorpej alpha_pci_io_swiz_outw(bus_addr_t ioaddr, uint16_t val) 206 1.2 thorpej { 207 1.2 thorpej uint32_t *port = alpha_pci_io_swiz(ioaddr, 1); 208 1.2 thorpej bus_addr_t offset = ioaddr & 3; 209 1.6 he uint32_t nval = ((uint32_t)val) << (uint32_t)(8 * offset); 210 1.1 thorpej 211 1.1 thorpej *port = nval; 212 1.1 thorpej alpha_mb(); 213 1.1 thorpej } 214 1.1 thorpej 215 1.1 thorpej void 216 1.2 thorpej alpha_pci_io_swiz_outl(bus_addr_t ioaddr, uint32_t val) 217 1.1 thorpej { 218 1.2 thorpej uint32_t *port = alpha_pci_io_swiz(ioaddr, 3); 219 1.1 thorpej 220 1.1 thorpej *port = val; 221 1.1 thorpej alpha_mb(); 222 1.1 thorpej } 223 1.1 thorpej 224 1.1 thorpej /* 225 1.1 thorpej * The following functions are used only on EV56 and greater CPUs, 226 1.1 thorpej * and the assembler requires going to EV56 mode in order to emit 227 1.1 thorpej * these instructions. 228 1.1 thorpej */ 229 1.1 thorpej __asm(".arch ev56"); 230 1.8 rin #include <machine/bwx.h> 231 1.1 thorpej 232 1.2 thorpej uint8_t 233 1.2 thorpej alpha_pci_io_bwx_inb(bus_addr_t ioaddr) 234 1.1 thorpej { 235 1.1 thorpej struct alpha_bus_window *abw = alpha_pci_io_findwindow(ioaddr); 236 1.4 mrg uint8_t *port = (uint8_t *) ((char *)abw->abw_addr + 237 1.1 thorpej (ioaddr - abw->abw_abst.abst_bus_start)); 238 1.1 thorpej 239 1.1 thorpej alpha_mb(); 240 1.1 thorpej 241 1.1 thorpej return (alpha_ldbu(port)); 242 1.1 thorpej } 243 1.1 thorpej 244 1.2 thorpej uint16_t 245 1.2 thorpej alpha_pci_io_bwx_inw(bus_addr_t ioaddr) 246 1.1 thorpej { 247 1.1 thorpej struct alpha_bus_window *abw = alpha_pci_io_findwindow(ioaddr); 248 1.2 thorpej /* LINTED */ 249 1.4 mrg uint16_t *port = (uint16_t *) ((char *)abw->abw_addr + 250 1.1 thorpej (ioaddr - abw->abw_abst.abst_bus_start)); 251 1.1 thorpej 252 1.1 thorpej alpha_mb(); 253 1.1 thorpej 254 1.1 thorpej return (alpha_ldwu(port)); 255 1.1 thorpej } 256 1.1 thorpej 257 1.2 thorpej uint32_t 258 1.2 thorpej alpha_pci_io_bwx_inl(bus_addr_t ioaddr) 259 1.1 thorpej { 260 1.1 thorpej struct alpha_bus_window *abw = alpha_pci_io_findwindow(ioaddr); 261 1.2 thorpej /* LINTED */ 262 1.4 mrg uint32_t *port = (uint32_t *) ((char *)abw->abw_addr + 263 1.1 thorpej (ioaddr - abw->abw_abst.abst_bus_start)); 264 1.1 thorpej 265 1.1 thorpej alpha_mb(); 266 1.1 thorpej 267 1.1 thorpej return (*port); 268 1.1 thorpej } 269 1.1 thorpej 270 1.1 thorpej void 271 1.2 thorpej alpha_pci_io_bwx_outb(bus_addr_t ioaddr, uint8_t val) 272 1.1 thorpej { 273 1.1 thorpej struct alpha_bus_window *abw = alpha_pci_io_findwindow(ioaddr); 274 1.4 mrg uint8_t *port = (uint8_t *) ((char *)abw->abw_addr + 275 1.1 thorpej (ioaddr - abw->abw_abst.abst_bus_start)); 276 1.1 thorpej 277 1.1 thorpej alpha_stb(port, val); 278 1.1 thorpej alpha_mb(); 279 1.1 thorpej } 280 1.1 thorpej 281 1.1 thorpej void 282 1.2 thorpej alpha_pci_io_bwx_outw(bus_addr_t ioaddr, uint16_t val) 283 1.1 thorpej { 284 1.1 thorpej struct alpha_bus_window *abw = alpha_pci_io_findwindow(ioaddr); 285 1.2 thorpej /* LINTED */ 286 1.4 mrg uint16_t *port = (uint16_t *) ((char *)abw->abw_addr + 287 1.1 thorpej (ioaddr - abw->abw_abst.abst_bus_start)); 288 1.1 thorpej 289 1.1 thorpej alpha_stw(port, val); 290 1.1 thorpej alpha_mb(); 291 1.1 thorpej } 292 1.1 thorpej 293 1.1 thorpej void 294 1.2 thorpej alpha_pci_io_bwx_outl(bus_addr_t ioaddr, uint32_t val) 295 1.1 thorpej { 296 1.1 thorpej struct alpha_bus_window *abw = alpha_pci_io_findwindow(ioaddr); 297 1.2 thorpej /* LINTED */ 298 1.4 mrg uint32_t *port = (uint32_t *) ((char *)abw->abw_addr + 299 1.1 thorpej (ioaddr - abw->abw_abst.abst_bus_start)); 300 1.1 thorpej 301 1.1 thorpej *port = val; 302 1.1 thorpej alpha_mb(); 303 1.1 thorpej } 304