alpha_pci_io.c revision 1.8
1/* $NetBSD: alpha_pci_io.c,v 1.8 2017/02/20 15:23:43 rin 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/sysarch.h> 42#include <machine/pio.h> 43 44#include <err.h> 45#include <stdlib.h> 46 47struct alpha_bus_window *alpha_pci_io_windows; 48int alpha_pci_io_window_count; 49 50uint8_t alpha_pci_io_swiz_inb(bus_addr_t); 51uint16_t alpha_pci_io_swiz_inw(bus_addr_t); 52uint32_t alpha_pci_io_swiz_inl(bus_addr_t); 53void alpha_pci_io_swiz_outb(bus_addr_t, uint8_t); 54void alpha_pci_io_swiz_outw(bus_addr_t, uint16_t); 55void alpha_pci_io_swiz_outl(bus_addr_t, uint32_t); 56 57const struct alpha_pci_io_ops alpha_pci_io_swiz_ops = { 58 alpha_pci_io_swiz_inb, 59 alpha_pci_io_swiz_inw, 60 alpha_pci_io_swiz_inl, 61 alpha_pci_io_swiz_outb, 62 alpha_pci_io_swiz_outw, 63 alpha_pci_io_swiz_outl, 64}; 65 66uint8_t alpha_pci_io_bwx_inb(bus_addr_t); 67uint16_t alpha_pci_io_bwx_inw(bus_addr_t); 68uint32_t alpha_pci_io_bwx_inl(bus_addr_t); 69void alpha_pci_io_bwx_outb(bus_addr_t, uint8_t); 70void alpha_pci_io_bwx_outw(bus_addr_t, uint16_t); 71void alpha_pci_io_bwx_outl(bus_addr_t, uint32_t); 72 73const struct alpha_pci_io_ops alpha_pci_io_bwx_ops = { 74 alpha_pci_io_bwx_inb, 75 alpha_pci_io_bwx_inw, 76 alpha_pci_io_bwx_inl, 77 alpha_pci_io_bwx_outb, 78 alpha_pci_io_bwx_outw, 79 alpha_pci_io_bwx_outl, 80}; 81 82const struct alpha_pci_io_ops *alpha_pci_io_switch; 83 84int 85alpha_pci_io_enable(int onoff) 86{ 87 struct alpha_bus_window *abw; 88 int i, count; 89 90 if (onoff == 0 && alpha_pci_io_windows != NULL) { 91 for (i = 0; i < alpha_pci_io_window_count; i++) 92 alpha_bus_unmapwindow(&alpha_pci_io_windows[i]); 93 free(alpha_pci_io_windows); 94 alpha_pci_io_windows = NULL; 95 alpha_pci_io_window_count = 0; 96 alpha_pci_io_switch = NULL; 97 return (0); 98 } else if (onoff == 0) 99 return (0); 100 else if (alpha_pci_io_windows != NULL) 101 return (0); 102 103 count = alpha_bus_getwindows(ALPHA_BUS_TYPE_PCI_IO, &abw); 104 if (count <= 0) 105 return (-1); 106 107 for (i = 0; i < count; i++) { 108 if (alpha_bus_mapwindow(&abw[i]) == -1) { 109 free(abw); 110 return (-1); 111 } 112 } 113 114 alpha_pci_io_windows = abw; 115 alpha_pci_io_window_count = count; 116 117 if (abw->abw_abst.abst_flags & ABST_BWX) 118 alpha_pci_io_switch = &alpha_pci_io_bwx_ops; 119 else 120 alpha_pci_io_switch = &alpha_pci_io_swiz_ops; 121 122 return (0); 123} 124 125static inline struct alpha_bus_window * 126alpha_pci_io_findwindow(bus_addr_t ioaddr) 127{ 128 struct alpha_bus_window *abw; 129 int i; 130 131 /* XXX Cache the last hit? */ 132 133 for (i = 0; i < alpha_pci_io_window_count; i++) { 134 abw = &alpha_pci_io_windows[i]; 135 if (ioaddr >= abw->abw_abst.abst_bus_start && 136 ioaddr <= abw->abw_abst.abst_bus_end) 137 return (abw); 138 } 139 140 warnx("alpha_pci_io_findwindow: no window for 0x%lx, ABORTING!", 141 (u_long) ioaddr); 142 abort(); 143 /* NOTREACHED */ 144} 145 146static inline uint32_t * 147alpha_pci_io_swiz(bus_addr_t ioaddr, int size) 148{ 149 struct alpha_bus_window *abw = alpha_pci_io_findwindow(ioaddr); 150 uint32_t *port; 151 152 /* LINTED */ 153 port = (uint32_t *) ((char *)abw->abw_addr + 154 (((ioaddr - abw->abw_abst.abst_bus_start) << 155 abw->abw_abst.abst_addr_shift) | 156 (size << abw->abw_abst.abst_size_shift))); 157 158 return (port); 159} 160 161uint8_t 162alpha_pci_io_swiz_inb(bus_addr_t ioaddr) 163{ 164 uint32_t *port = alpha_pci_io_swiz(ioaddr, 0); 165 bus_addr_t offset = ioaddr & 3; 166 167 alpha_mb(); 168 169 return ((*port >> (8 * offset)) & 0xff); 170} 171 172uint16_t 173alpha_pci_io_swiz_inw(bus_addr_t ioaddr) 174{ 175 uint32_t *port = alpha_pci_io_swiz(ioaddr, 1); 176 bus_addr_t offset = ioaddr & 3; 177 178 alpha_mb(); 179 180 return ((*port >> (8 * offset)) & 0xffff); 181} 182 183uint32_t 184alpha_pci_io_swiz_inl(bus_addr_t ioaddr) 185{ 186 uint32_t *port = alpha_pci_io_swiz(ioaddr, 3); 187 188 alpha_mb(); 189 190 return (*port); 191} 192 193void 194alpha_pci_io_swiz_outb(bus_addr_t ioaddr, uint8_t val) 195{ 196 uint32_t *port = alpha_pci_io_swiz(ioaddr, 0); 197 bus_addr_t offset = ioaddr & 3; 198 uint32_t nval = ((uint32_t)val) << (uint32_t)(8 * offset); 199 200 *port = nval; 201 alpha_mb(); 202} 203 204void 205alpha_pci_io_swiz_outw(bus_addr_t ioaddr, uint16_t val) 206{ 207 uint32_t *port = alpha_pci_io_swiz(ioaddr, 1); 208 bus_addr_t offset = ioaddr & 3; 209 uint32_t nval = ((uint32_t)val) << (uint32_t)(8 * offset); 210 211 *port = nval; 212 alpha_mb(); 213} 214 215void 216alpha_pci_io_swiz_outl(bus_addr_t ioaddr, uint32_t val) 217{ 218 uint32_t *port = alpha_pci_io_swiz(ioaddr, 3); 219 220 *port = val; 221 alpha_mb(); 222} 223 224/* 225 * The following functions are used only on EV56 and greater CPUs, 226 * and the assembler requires going to EV56 mode in order to emit 227 * these instructions. 228 */ 229__asm(".arch ev56"); 230#include <machine/bwx.h> 231 232uint8_t 233alpha_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 244uint16_t 245alpha_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 257uint32_t 258alpha_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 270void 271alpha_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 281void 282alpha_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 293void 294alpha_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