1 /* $NetBSD: admpci.c,v 1.11 2014/06/28 10:25:16 skrll Exp $ */ 2 3 /*- 4 * Copyright (c) 2007 David Young. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or 7 * without modification, are permitted provided that the following 8 * conditions are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above 12 * copyright notice, this list of conditions and the following 13 * disclaimer in the documentation and/or other materials provided 14 * with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY 17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 18 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 19 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR 20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 21 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 23 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 25 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 26 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 27 * OF SUCH DAMAGE. 28 */ 29 /*- 30 * Copyright (c) 2006 Itronix Inc. 31 * All rights reserved. 32 * 33 * Written by Garrett D'Amore for Itronix Inc. 34 * 35 * Redistribution and use in source and binary forms, with or without 36 * modification, are permitted provided that the following conditions 37 * are met: 38 * 1. Redistributions of source code must retain the above copyright 39 * notice, this list of conditions and the following disclaimer. 40 * 2. Redistributions in binary form must reproduce the above copyright 41 * notice, this list of conditions and the following disclaimer in the 42 * documentation and/or other materials provided with the distribution. 43 * 3. The name of Itronix Inc. may not be used to endorse 44 * or promote products derived from this software without specific 45 * prior written permission. 46 * 47 * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND 48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 49 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 50 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY 51 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 52 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 53 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 54 * ON ANY THEORY OF LIABILITY, WHETHER IN 55 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 56 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 57 * POSSIBILITY OF SUCH DAMAGE. 58 */ 59 60 #include "opt_pci.h" 61 #include "pci.h" 62 63 #include <sys/cdefs.h> 64 __KERNEL_RCSID(0, "$NetBSD: admpci.c,v 1.11 2014/06/28 10:25:16 skrll Exp $"); 65 66 #include <sys/types.h> 67 #include <sys/bus.h> 68 #include <sys/cpu.h> 69 70 #include <sys/param.h> 71 #include <sys/time.h> 72 #include <sys/systm.h> 73 #include <sys/errno.h> 74 #include <sys/device.h> 75 #include <sys/malloc.h> 76 #include <sys/extent.h> 77 78 #include <uvm/uvm_extern.h> 79 80 #include <dev/pci/pcivar.h> 81 #include <dev/pci/pcireg.h> 82 #include <dev/pci/pciconf.h> 83 84 #ifdef PCI_NETBSD_CONFIGURE 85 #include <mips/cache.h> 86 #endif 87 88 #include <mips/pte.h> 89 90 #include <mips/adm5120/include/adm5120_mainbusvar.h> 91 #include <mips/adm5120/include/adm5120reg.h> 92 #include <mips/adm5120/include/adm5120var.h> 93 94 #ifdef ADMPCI_DEBUG 95 int admpci_debug = 1; 96 #define ADMPCI_DPRINTF(__fmt, ...) \ 97 do { \ 98 if (admpci_debug) \ 99 printf((__fmt), __VA_ARGS__); \ 100 } while (/*CONSTCOND*/0) 101 #else /* !ADMPCI_DEBUG */ 102 #define ADMPCI_DPRINTF(__fmt, ...) do { } while (/*CONSTCOND*/0) 103 #endif /* ADMPCI_DEBUG */ 104 105 #define ADMPCI_TAG_BUS_MASK __BITS(23, 16) 106 /* Bit 11 is reserved. It selects the AHB-PCI bridge. Let device 0 107 * be the bridge. For all other device numbers, let bit[11] == 0. 108 */ 109 #define ADMPCI_TAG_DEVICE_MASK __BITS(15, 11) 110 #define ADMPCI_TAG_DEVICE_SUBMASK __BITS(15, 12) 111 #define ADMPCI_TAG_DEVICE_BRIDGE __BIT(11) 112 #define ADMPCI_TAG_FUNCTION_MASK __BITS(10, 8) 113 #define ADMPCI_TAG_REGISTER_MASK __BITS(7, 0) 114 115 #define ADMPCI_MAX_DEVICE 116 117 struct admpci_softc { 118 device_t sc_dev; 119 struct mips_pci_chipset sc_pc; 120 121 bus_space_tag_t sc_memt; 122 bus_space_tag_t sc_iot; 123 124 bus_space_tag_t sc_conft; 125 bus_space_handle_t sc_addrh; 126 bus_space_handle_t sc_datah; 127 }; 128 129 int admpcimatch(device_t, cfdata_t, void *); 130 void admpciattach(device_t, device_t, void *); 131 132 #if NPCI > 0 133 static void admpci_attach_hook(device_t, device_t, 134 struct pcibus_attach_args *); 135 static int admpci_bus_maxdevs(void *, int); 136 static pcitag_t admpci_make_tag(void *, int, int, int); 137 static void admpci_decompose_tag(void *, pcitag_t, int *, int *, int *); 138 static pcireg_t admpci_conf_read(void *, pcitag_t, int); 139 static void admpci_conf_write(void *, pcitag_t, int, pcireg_t); 140 static const char *admpci_intr_string(void *, pci_intr_handle_t, char *, size_t); 141 static void admpci_conf_interrupt(void *, int, int, int, int, int *); 142 static void *admpci_intr_establish(void *, pci_intr_handle_t, int, 143 int (*)(void *), void *); 144 static void admpci_intr_disestablish(void *, void *); 145 static int admpci_intr_map(const struct pci_attach_args *, pci_intr_handle_t *); 146 147 #ifdef PCI_NETBSD_CONFIGURE 148 static struct extent *io_ex = NULL; 149 static struct extent *mem_ex = NULL; 150 #endif /* PCI_NETBSD_CONFIGURE */ 151 152 #endif /* NPCI > 0 */ 153 154 CFATTACH_DECL_NEW(admpci, sizeof(struct admpci_softc), 155 admpcimatch, admpciattach, NULL, NULL); 156 157 int admpci_found = 0; 158 159 /* 160 * Physical PCI addresses are 36-bits long, so we need to have 161 * adequate storage space for them. 162 */ 163 #if NPCI > 0 164 #if !defined(_MIPS_PADDR_T_64BIT) && !defined(_LP64) 165 #error "admpci requires 64 bit paddr_t!" 166 #endif 167 #endif 168 169 int 170 admpcimatch(device_t parent, cfdata_t match, void *aux) 171 { 172 struct mainbus_attach_args *ma = (struct mainbus_attach_args *)aux; 173 174 return !admpci_found && strcmp(ma->ma_name, "admpci") == 0; 175 } 176 177 void 178 admpciattach(device_t parent, device_t self, void *aux) 179 { 180 struct adm5120_config *admc = &adm5120_configuration; 181 struct admpci_softc *sc = device_private(self); 182 struct mainbus_attach_args *ma = (struct mainbus_attach_args *)aux; 183 #if NPCI > 0 184 u_long result; 185 struct pcibus_attach_args pba; 186 #endif 187 188 admpci_found = 1; 189 190 sc->sc_dev = self; 191 sc->sc_conft = ma->ma_obiot; 192 if (bus_space_map(sc->sc_conft, ADM5120_BASE_PCI_CONFDATA, 4, 0, 193 &sc->sc_datah) != 0) { 194 aprint_error( 195 ": unable to map PCI Configuration Data register\n"); 196 return; 197 } 198 if (bus_space_map(sc->sc_conft, ADM5120_BASE_PCI_CONFADDR, 4, 0, 199 &sc->sc_addrh) != 0) { 200 aprint_error( 201 ": unable to map PCI Configuration Address register\n"); 202 return; 203 } 204 205 aprint_normal(": ADM5120 Host-PCI Bridge, " 206 "data %"PRIxBSH" addr %"PRIxBSH", sc %p\n", 207 sc->sc_datah, sc->sc_addrh, sc); 208 209 #if NPCI > 0 210 sc->sc_memt = &admc->pcimem_space; 211 sc->sc_iot = &admc->pciio_space; 212 213 sc->sc_pc.pc_conf_v = sc; 214 sc->sc_pc.pc_attach_hook = admpci_attach_hook; 215 sc->sc_pc.pc_bus_maxdevs = admpci_bus_maxdevs; 216 sc->sc_pc.pc_make_tag = admpci_make_tag; 217 sc->sc_pc.pc_decompose_tag = admpci_decompose_tag; 218 sc->sc_pc.pc_conf_read = admpci_conf_read; 219 sc->sc_pc.pc_conf_write = admpci_conf_write; 220 221 sc->sc_pc.pc_intr_v = sc; 222 sc->sc_pc.pc_intr_map = admpci_intr_map; 223 sc->sc_pc.pc_intr_string = admpci_intr_string; 224 sc->sc_pc.pc_intr_establish = admpci_intr_establish; 225 sc->sc_pc.pc_intr_disestablish = admpci_intr_disestablish; 226 sc->sc_pc.pc_conf_interrupt = admpci_conf_interrupt; 227 228 #ifdef ADMPCI_DEBUG 229 pcitag_t tag = pci_make_tag(&sc->sc_pc, 0, 0, 0); 230 ADMPCI_DPRINTF("%s: BAR 0x10 0x%08x\n", __func__, 231 pci_conf_read(&sc->sc_pc, tag, PCI_MAPREG_START)); 232 #endif 233 234 #ifdef PCI_NETBSD_CONFIGURE 235 mem_ex = extent_create("pcimem", 236 ADM5120_BOTTOM, ADM5120_TOP, 237 NULL, 0, EX_WAITOK); 238 (void)extent_alloc_subregion(mem_ex, 239 ADM5120_BASE_SRAM1, ADM5120_BASE_PCI_MEM - 1, 240 ADM5120_BASE_PCI_MEM - ADM5120_BASE_SRAM1, 241 ADM5120_BASE_PCI_MEM - ADM5120_BASE_SRAM1, 242 0, EX_WAITOK, &result); 243 (void)extent_alloc_subregion(mem_ex, 244 ADM5120_BASE_PCI_IO, ADM5120_TOP, 245 ADM5120_TOP - ADM5120_BASE_PCI_IO + 1, 246 ADM5120_TOP - ADM5120_BASE_PCI_IO + 1, 247 0, EX_WAITOK, &result); 248 249 io_ex = extent_create("pciio", 250 ADM5120_BASE_PCI_IO, ADM5120_BASE_PCI_CONFADDR - 1, 251 NULL, 0, EX_WAITOK); 252 253 pci_configure_bus(&sc->sc_pc, 254 io_ex, mem_ex, NULL, 0, mips_cache_info.mci_dcache_align); 255 extent_destroy(mem_ex); 256 extent_destroy(io_ex); 257 #endif 258 259 pba.pba_iot = sc->sc_iot; 260 pba.pba_memt = sc->sc_memt; 261 /* XXX: review dma tag logic */ 262 pba.pba_dmat = ma->ma_dmat; 263 pba.pba_dmat64 = NULL; 264 pba.pba_pc = &sc->sc_pc; 265 pba.pba_flags = PCI_FLAGS_IO_OKAY | PCI_FLAGS_MEM_OKAY; 266 pba.pba_bus = 0; 267 pba.pba_bridgetag = NULL; 268 269 config_found_ia(self, "pcibus", &pba, pcibusprint); 270 #endif /* NPCI > 0 */ 271 } 272 273 #if NPCI > 0 274 275 void 276 admpci_attach_hook(device_t parent, device_t self, 277 struct pcibus_attach_args *pba) 278 { 279 } 280 281 /* There are at most four devices on bus 0. The ADM5120 has 282 * request/grant lines for 3 PCI devices: 1, 2, and 3. The host 283 * bridge is device 0. 284 */ 285 int 286 admpci_bus_maxdevs(void *v, int bus) 287 { 288 if (bus == 0) 289 return 4; 290 291 return 1 + __SHIFTOUT_MASK(ADMPCI_TAG_DEVICE_MASK); 292 } 293 294 pcitag_t 295 admpci_make_tag(void *v, int bus, int device, int function) 296 { 297 if (bus > __SHIFTOUT_MASK(ADMPCI_TAG_BUS_MASK) || 298 device > __SHIFTOUT_MASK(ADMPCI_TAG_DEVICE_MASK) || 299 function > __SHIFTOUT_MASK(ADMPCI_TAG_FUNCTION_MASK)) 300 panic("%s: bad request", __func__); 301 302 return __SHIFTIN(bus, ADMPCI_TAG_BUS_MASK) | 303 __SHIFTIN(device, ADMPCI_TAG_DEVICE_MASK) | 304 __SHIFTIN(function, ADMPCI_TAG_FUNCTION_MASK); 305 } 306 307 void 308 admpci_decompose_tag(void *v, pcitag_t tag, int *b, int *d, int *f) 309 { 310 int bus, device, function; 311 312 bus = __SHIFTOUT(tag, ADMPCI_TAG_BUS_MASK); 313 device = __SHIFTOUT(tag, ADMPCI_TAG_DEVICE_MASK); 314 function = __SHIFTOUT(tag, ADMPCI_TAG_FUNCTION_MASK); 315 316 if (b != NULL) 317 *b = bus; 318 if (d != NULL) 319 *d = device; 320 if (f != NULL) 321 *f = function; 322 } 323 324 static int 325 admpci_tag_to_addr(void *v, pcitag_t tag, int reg, bus_addr_t *addrp) 326 { 327 int bus, device, function; 328 329 KASSERT(addrp != NULL); 330 /* panics if tag is not well-formed */ 331 admpci_decompose_tag(v, tag, &bus, &device, &function); 332 if (reg > __SHIFTOUT_MASK(ADMPCI_TAG_REGISTER_MASK)) 333 panic("%s: bad register", __func__); 334 335 *addrp = 0x80000000 | tag | __SHIFTIN(reg, ADMPCI_TAG_REGISTER_MASK); 336 337 return 0; 338 } 339 340 static pcireg_t 341 admpci_conf_read(void *v, pcitag_t tag, int reg) 342 { 343 int s; 344 struct admpci_softc *sc = (struct admpci_softc *)v; 345 uint32_t data; 346 bus_addr_t addr; 347 348 ADMPCI_DPRINTF("%s: sc %p tag %lx reg %d\n", __func__, (void *)sc, tag, 349 reg); 350 351 if (admpci_tag_to_addr(v, tag, reg, &addr) == -1) 352 return 0xffffffff; 353 354 ADMPCI_DPRINTF("%s: sc_addrh %lx sc_datah %lx addr %lx\n", __func__, 355 sc->sc_addrh, sc->sc_datah, addr); 356 357 s = splhigh(); 358 bus_space_write_4(sc->sc_conft, sc->sc_addrh, 0, addr); 359 data = bus_space_read_4(sc->sc_conft, sc->sc_datah, 0); 360 splx(s); 361 362 ADMPCI_DPRINTF("%s: read 0x%" PRIx32 "\n", __func__, data); 363 return data; 364 } 365 366 void 367 admpci_conf_write(void *v, pcitag_t tag, int reg, pcireg_t data) 368 { 369 int s; 370 struct admpci_softc *sc = (struct admpci_softc *)v; 371 bus_addr_t addr; 372 373 ADMPCI_DPRINTF("%s: sc %p tag %lx reg %d\n", __func__, (void *)sc, tag, 374 reg); 375 376 if (admpci_tag_to_addr(v, tag, reg, &addr) == -1) 377 return; 378 379 ADMPCI_DPRINTF("%s: sc_addrh %lx sc_datah %lx addr %lx\n", __func__, 380 sc->sc_addrh, sc->sc_datah, addr); 381 382 s = splhigh(); 383 bus_space_write_4(sc->sc_conft, sc->sc_addrh, 0, addr); 384 bus_space_write_4(sc->sc_conft, sc->sc_datah, 0, data); 385 splx(s); 386 } 387 388 const char * 389 admpci_intr_string(void *v, pci_intr_handle_t ih, char *buf, size_t len) 390 { 391 (void)snprintf(buf, len, "irq %u", (unsigned)ih); 392 return buf; 393 } 394 395 void * 396 admpci_intr_establish(void *v, pci_intr_handle_t ih, int ipl, 397 int (*handler)(void *), void *arg) 398 { 399 return adm5120_intr_establish(ih, ipl, handler, arg); 400 } 401 402 void 403 admpci_intr_disestablish(void *v, void *cookie) 404 { 405 adm5120_intr_disestablish(cookie); 406 } 407 408 void 409 admpci_conf_interrupt(void *v, int bus, int dev, int ipin, int swiz, int *iline) 410 { 411 /* 412 * We let the machdep_pci_intr_map take care of IRQ routing. 413 * On some platforms the BIOS may have handled this properly, 414 * on others it might not have. For now we avoid clobbering 415 * the settings establishsed by the BIOS, so that they will be 416 * there if the platform logic is confident that it can rely 417 * on them. 418 */ 419 } 420 421 /* 422 * Map the bus 0 device numbers 1, 2, and 3 to IRQ 6, 7, and 8, 423 * respectively. 424 * 425 * XXX How to handle bridges? 426 */ 427 static int 428 admpci_intr_map(const struct pci_attach_args *pa, pci_intr_handle_t *ihp) 429 { 430 int bus, device, function; 431 432 admpci_decompose_tag(pa->pa_pc->pc_conf_v, pa->pa_tag, 433 &bus, &device, &function); 434 435 if (bus != 0 || device > 3) 436 return -1; 437 438 *ihp = (device - 1) + 6; 439 440 return 0; 441 } 442 #endif 443