1 /* $NetBSD: vlpci.c,v 1.10 2021/01/27 03:10:21 thorpej Exp $ */ 2 3 /* 4 * Copyright (c) 2017 Jonathan A. Kollasch 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 20 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 21 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 23 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 25 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 26 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __KERNEL_RCSID(0, "$NetBSD: vlpci.c,v 1.10 2021/01/27 03:10:21 thorpej Exp $"); 31 32 #include "opt_pci.h" 33 #include "pci.h" 34 35 #include <sys/param.h> 36 #include <sys/bus.h> 37 #include <sys/device.h> 38 #include <sys/extent.h> 39 #include <sys/mutex.h> 40 #include <uvm/uvm.h> 41 #include <machine/pio.h> 42 #include <machine/pmap.h> 43 #include <machine/ofw.h> 44 45 #include <dev/isa/isavar.h> 46 47 #include <dev/ofw/openfirm.h> 48 49 #include <dev/pci/pcivar.h> 50 #include <dev/pci/pciconf.h> 51 #include <arm/pci_machdep.h> 52 53 #include <shark/ofw/vlpci.h> 54 55 #define VLPCI_ADDON_DEV_NO 6 56 #define VLPCI_IRQ 10 57 58 #define VLPCI_PCI_MEM_BASE 0x02000000 59 #define VLPCI_PCI_MEM_SZ 1048576 60 61 #define VLPCI_VL_MEM_BASE 0x08000000 62 #define VLPCI_VL_MEM_SZ 4194304 63 64 static int vlpci_match(device_t, struct cfdata *, void *); 65 static void vlpci_attach(device_t, device_t, void *); 66 67 static void vlpci_pc_attach_hook(device_t, device_t, 68 struct pcibus_attach_args *); 69 static int vlpci_pc_bus_maxdevs(void *, int); 70 static pcitag_t vlpci_pc_make_tag(void *, int, int, int); 71 static void vlpci_pc_decompose_tag(void *, pcitag_t, int *, int *, int *); 72 static pcireg_t vlpci_pc_conf_read(void *, pcitag_t, int); 73 static void vlpci_pc_conf_write(void *, pcitag_t, int, pcireg_t); 74 75 static int vlpci_pc_intr_map(const struct pci_attach_args *, 76 pci_intr_handle_t *); 77 static const char * vlpci_pc_intr_string(void *, pci_intr_handle_t, char *, 78 size_t); 79 static const struct evcnt * vlpci_pc_intr_evcnt(void *, pci_intr_handle_t); 80 static void * vlpci_pc_intr_establish(void *, pci_intr_handle_t, int, 81 int (*)(void *), void *, const char *); 82 static void vlpci_pc_intr_disestablish(void *, void *); 83 84 #ifdef __HAVE_PCI_CONF_HOOK 85 static int vlpci_pc_conf_hook(void *, int, int, int, pcireg_t); 86 #endif 87 static void vlpci_pc_conf_interrupt(void *, int, int, int, int, int *); 88 89 struct vlpci_softc { 90 device_t sc_dev; 91 kmutex_t sc_lock; 92 bus_space_handle_t sc_conf_ioh; 93 bus_space_handle_t sc_reg_ioh; 94 struct arm32_pci_chipset sc_pc; 95 }; 96 97 CFATTACH_DECL_NEW(vlpci, sizeof(struct vlpci_softc), 98 vlpci_match, vlpci_attach, NULL, NULL); 99 100 static const struct device_compatible_entry compat_data[] = { 101 { .compat = "via,vt82c505" }, 102 DEVICE_COMPAT_EOL 103 }; 104 105 vaddr_t vlpci_mem_vaddr = 0; 106 paddr_t vlpci_mem_paddr; 107 struct bus_space vlpci_memt; 108 109 static void 110 regwrite_1(struct vlpci_softc * const sc, uint8_t off, uint8_t val) 111 { 112 113 mutex_spin_enter(&sc->sc_lock); 114 bus_space_write_1(&isa_io_bs_tag, sc->sc_reg_ioh, VLPCI_INTREG_IDX_OFF, 115 off); 116 bus_space_write_1(&isa_io_bs_tag, sc->sc_reg_ioh, VLPCI_INTREG_DATA_OFF, 117 val); 118 mutex_spin_exit(&sc->sc_lock); 119 } 120 121 static uint8_t 122 regread_1(struct vlpci_softc * const sc, uint8_t off) 123 { 124 uint8_t reg; 125 126 mutex_spin_enter(&sc->sc_lock); 127 bus_space_write_1(&isa_io_bs_tag, sc->sc_reg_ioh, VLPCI_INTREG_IDX_OFF, 128 off); 129 reg = bus_space_read_1(&isa_io_bs_tag, sc->sc_reg_ioh, 130 VLPCI_INTREG_DATA_OFF); 131 mutex_spin_exit(&sc->sc_lock); 132 return reg; 133 } 134 135 static void 136 vlpci_dump_window(struct vlpci_softc *sc, int num) 137 { 138 int regaddr = VLPCI_PCI_WND_HIADDR_REG(num); 139 uint32_t addr, size; 140 uint8_t attr; 141 142 addr = regread_1(sc, regaddr) << 24; 143 addr |= regread_1(sc, regaddr + 1) << 16; 144 attr = regread_1(sc, regaddr + 2); 145 size = 0x00010000 << __SHIFTOUT(attr, VLPCI_PCI_WND_ATTR_SZ); 146 printf("memory window #%d at %08x size %08x flags %x\n", num, addr, 147 size, attr); 148 } 149 150 static int 151 vlpci_map(void *t, bus_addr_t bpa, bus_size_t size, int cacheable, 152 bus_space_handle_t *bshp) 153 { 154 155 *bshp = vlpci_mem_vaddr - VLPCI_PCI_MEM_BASE + bpa; 156 printf("%s: %08lx -> %08lx\n", __func__, bpa, *bshp); 157 return(0); 158 } 159 160 static paddr_t 161 vlpci_mmap(void *cookie, bus_addr_t addr, off_t off, int prot, 162 int flags) 163 { 164 paddr_t ret; 165 166 ret = vlpci_mem_paddr + addr + off; 167 168 if (flags & BUS_SPACE_MAP_PREFETCHABLE) 169 return (arm_btop(ret) | ARM32_MMAP_WRITECOMBINE); 170 else 171 return arm_btop(ret); 172 } 173 174 static void 175 vlpci_steer_irq(struct vlpci_softc * const sc) 176 { 177 const unsigned int_ctl[] = { 178 VLPCI_INT_CTL_INTA, VLPCI_INT_CTL_INTB, 179 VLPCI_INT_CTL_INTC, VLPCI_INT_CTL_INTD 180 }; 181 uint8_t val; 182 183 for (size_t i = 0; i < __arraycount(int_ctl); i++) { 184 val = regread_1(sc, VLPCI_INT_CTL_REG(int_ctl[i])); 185 val &= ~VLPCI_INT_CTL_INT2IRQ(int_ctl[i]); 186 val |= VLPCI_INT_CTL_ENA(int_ctl[i]); 187 val |= __SHIFTIN(VLPCI_INT_CTL_IRQ(VLPCI_IRQ), 188 VLPCI_INT_CTL_INT2IRQ(int_ctl[i])); 189 regwrite_1(sc, VLPCI_INT_CTL_REG(int_ctl[i]), val); 190 } 191 } 192 193 static int 194 vlpci_match(device_t parent, struct cfdata *match, void *aux) 195 { 196 struct ofbus_attach_args * const oba = aux; 197 198 /* beat generic ofbus */ 199 return of_compatible_match(oba->oba_phandle, compat_data) * 2; 200 } 201 202 static void 203 vlpci_attach(device_t parent, device_t self, void *aux) 204 { 205 struct vlpci_softc * const sc = device_private(self); 206 pci_chipset_tag_t const pc = &sc->sc_pc; 207 struct pcibus_attach_args pba; 208 pcitag_t tag; 209 pcireg_t cmd; 210 211 aprint_normal("\n"); 212 213 sc->sc_dev = self; 214 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_HIGH); 215 memset(&pba, 0, sizeof(pba)); 216 217 if (bus_space_map(&isa_io_bs_tag, VLPCI_INTREG_BASE, VLPCI_INTREG_SZ, 218 0, &sc->sc_reg_ioh) != 0) { 219 aprint_error_dev(self, "failed to map internal reg port\n"); 220 return; 221 } 222 if (bus_space_map(&isa_io_bs_tag, VLPCI_CFGREG_BASE, VLPCI_CFGREG_SZ, 223 0, &sc->sc_conf_ioh) != 0) { 224 aprint_error_dev(self, "failed to map configuration port\n"); 225 return; 226 } 227 228 /* Enable VLB/PCI bridge */ 229 regwrite_1(sc, VLPCI_MISC_1_REG, VLPCI_MISC_1_LOCAL_PIN | 230 VLPCI_MISC_1_COMPAT_ISA_BOFF); 231 regwrite_1(sc, VLPCI_MISC_CTL_REG, __SHIFTIN(VLPCI_MISC_CTL_HIADDR_DIS, 232 VLPCI_MISC_CTL_HIADDR) | VLPCI_MISC_CTL_IOCHCK_PIN); 233 regwrite_1(sc, VLPCI_CFG_MISC_CTL_REG, 234 __SHIFTIN(VLPCI_CFG_MISC_CTL_INT_CTL_CONV, 235 VLPCI_CFG_MISC_CTL_INT_CTL) | VLPCI_CFG_MISC_CTL_LREQI_LGNTO_PIN); 236 regwrite_1(sc, VLPCI_IRQ_MODE_REG, 0x00); /* don't do per-INTx conversions */ 237 vlpci_steer_irq(sc); 238 /* 239 * XXX 240 * set memory size to 255MB, so the bridge knows which cycles go to RAM 241 * shark's RAM is in the upper half of the lower 256MB, part of the 242 * lower half is occupied by the graphics chip 243 * ... or that's the theory. OF puts PCI BARS at 0x02000000 which 244 * overlaps with when we do this and pci memory access doesn't work. 245 */ 246 regwrite_1(sc, VLPCI_OBD_MEM_SZ_REG, 1); 247 248 regwrite_1(sc, VLPCI_BUF_CTL_REG, VLPCI_BUF_CTL_PCI_DYN_ACC_DEC); 249 regwrite_1(sc, VLPCI_VL_TIM_REG, VLPCI_VL_TIM_OBD_MEM_1ST_DAT); 250 printf("reg 0x83 %02x\n", regread_1(sc, VLPCI_VL_TIM_REG)); 251 252 #if 1 253 /* program window #0 to 0x08000000 */ 254 regwrite_1(sc, VLPCI_PCI_WND_HIADDR_REG(VLPCI_PCI_WND_NO_1), 255 VLPCI_PCI_WND_HIADDR_MEM(VLPCI_VL_MEM_BASE)); 256 regwrite_1(sc, VLPCI_PCI_WND_LOADDR_REG(VLPCI_PCI_WND_NO_1), 257 VLPCI_PCI_WND_LOADDR_MEM(VLPCI_VL_MEM_BASE)); 258 regwrite_1(sc, VLPCI_PCI_WND_ATTR_REG(VLPCI_PCI_WND_NO_1), 259 VLPCI_PCI_WND_ATTR_VL | 260 __SHIFTIN(VLPCI_PCI_WND_ATTR_SZ_MEM(VLPCI_VL_MEM_SZ), 261 VLPCI_PCI_WND_ATTR_SZ)); 262 #else 263 regwrite_1(sc, VLPCI_PCI_WND_HIADDR_REG(VLPCI_PCI_WND_NO_1), 0x00); 264 regwrite_1(sc, VLPCI_PCI_WND_LOADDR_REG(VLPCI_PCI_WND_NO_1), 0x00); 265 regwrite_1(sc, VLPCI_PCI_WND_ATTR_REG(VLPCI_PCI_WND_NO_1), 0x00); 266 #endif 267 268 vlpci_mem_paddr = VLPCI_PCI_MEM_BASE; /* get from OF! */ 269 270 /* 271 * we map in 1MB at 0x02000000, so program window #1 accordingly 272 */ 273 regwrite_1(sc, VLPCI_PCI_WND_HIADDR_REG(VLPCI_PCI_WND_NO_2), 274 VLPCI_PCI_WND_HIADDR_MEM(vlpci_mem_paddr)); 275 regwrite_1(sc, VLPCI_PCI_WND_LOADDR_REG(VLPCI_PCI_WND_NO_2), 276 VLPCI_PCI_WND_LOADDR_MEM(vlpci_mem_paddr)); 277 regwrite_1(sc, VLPCI_PCI_WND_ATTR_REG(VLPCI_PCI_WND_NO_2), 278 VLPCI_PCI_WND_ATTR_PCI | 279 __SHIFTIN(VLPCI_PCI_WND_ATTR_SZ_MEM(VLPCI_PCI_MEM_SZ), 280 VLPCI_PCI_WND_ATTR_SZ)); 281 282 /* now map in some of the memory space */ 283 printf("vlpci_mem_vaddr %08lx\n", vlpci_mem_vaddr); 284 memcpy(&vlpci_memt, &isa_io_bs_tag, sizeof(struct bus_space)); 285 vlpci_memt.bs_cookie = (void *)vlpci_mem_vaddr; 286 vlpci_memt.bs_map = vlpci_map; 287 vlpci_memt.bs_mmap = vlpci_mmap; 288 289 pc->pc_conf_v = sc; 290 pc->pc_attach_hook = vlpci_pc_attach_hook; 291 pc->pc_bus_maxdevs = vlpci_pc_bus_maxdevs; 292 pc->pc_make_tag = vlpci_pc_make_tag; 293 pc->pc_decompose_tag = vlpci_pc_decompose_tag; 294 pc->pc_conf_read = vlpci_pc_conf_read; 295 pc->pc_conf_write = vlpci_pc_conf_write; 296 297 pc->pc_intr_v = sc; 298 pc->pc_intr_map = vlpci_pc_intr_map; 299 pc->pc_intr_string = vlpci_pc_intr_string; 300 pc->pc_intr_evcnt = vlpci_pc_intr_evcnt; 301 pc->pc_intr_establish = vlpci_pc_intr_establish; 302 pc->pc_intr_disestablish = vlpci_pc_intr_disestablish; 303 304 #ifdef __HAVE_PCI_CONF_HOOK 305 pc->pc_conf_hook = vlpci_pc_conf_hook; 306 #endif 307 pc->pc_conf_interrupt = vlpci_pc_conf_interrupt; 308 309 /* try to assure IO space is enabled on the default device-function */ 310 tag = vlpci_pc_make_tag(sc, 0, VLPCI_ADDON_DEV_NO, 0); 311 cmd = vlpci_pc_conf_read(sc, tag, PCI_COMMAND_STATUS_REG); 312 vlpci_pc_conf_write(sc, tag, PCI_COMMAND_STATUS_REG, 313 cmd | PCI_COMMAND_IO_ENABLE); 314 315 pba.pba_flags = PCI_FLAGS_IO_OKAY | PCI_FLAGS_MEM_OKAY; 316 pba.pba_iot = &isa_io_bs_tag; 317 pba.pba_memt = &vlpci_memt; 318 pba.pba_dmat = &isa_bus_dma_tag; 319 pba.pba_pc = &sc->sc_pc; 320 pba.pba_bus = 0; 321 322 printf("dma %lx %lx, %lx\n", isa_bus_dma_tag._ranges[0].dr_sysbase, 323 isa_bus_dma_tag._ranges[0].dr_busbase, 324 isa_bus_dma_tag._ranges[0].dr_len); 325 326 vlpci_dump_window(sc, VLPCI_PCI_WND_NO_1); 327 vlpci_dump_window(sc, VLPCI_PCI_WND_NO_2); 328 vlpci_dump_window(sc, VLPCI_PCI_WND_NO_3); 329 330 config_found_ia(self, "pcibus", &pba, pcibusprint); 331 } 332 333 static void 334 vlpci_pc_attach_hook(device_t parent, device_t self, 335 struct pcibus_attach_args *pba) 336 { 337 } 338 339 static int 340 vlpci_pc_bus_maxdevs(void *v, int busno) 341 { 342 343 return busno == 0 ? 32 : 0; 344 } 345 346 static pcitag_t 347 vlpci_pc_make_tag(void *v, int b, int d, int f) 348 { 349 350 return (b << 16) | (d << 11) | (f << 8); 351 } 352 353 static void 354 vlpci_pc_decompose_tag(void *v, pcitag_t tag, int *bp, int *dp, int *fp) 355 { 356 357 if (bp) 358 *bp = (tag >> 16) & 0xff; 359 if (dp) 360 *dp = (tag >> 11) & 0x1f; 361 if (fp) 362 *fp = (tag >> 8) & 0x7; 363 } 364 365 static pcireg_t 366 vlpci_pc_conf_read(void *v, pcitag_t tag, int offset) 367 { 368 struct vlpci_softc * const sc = v; 369 pcireg_t ret; 370 371 KASSERT((offset & 3) == 0); 372 373 if (offset >= PCI_CONF_SIZE) 374 return 0xffffffff; 375 376 mutex_spin_enter(&sc->sc_lock); 377 bus_space_write_4(&isa_io_bs_tag, sc->sc_conf_ioh, 378 VLPCI_CFGREG_ADDR_OFF, 0x80000000UL|tag|offset); 379 ret = bus_space_read_4(&isa_io_bs_tag, sc->sc_conf_ioh, 380 VLPCI_CFGREG_DATA_OFF); 381 mutex_spin_exit(&sc->sc_lock); 382 383 #if 0 384 device_printf(sc->sc_dev, "%s tag %x offset %x ret %x\n", 385 __func__, (unsigned int)tag, offset, ret); 386 #endif 387 388 return ret; 389 } 390 391 static void 392 vlpci_pc_conf_write(void *v, pcitag_t tag, int offset, pcireg_t val) 393 { 394 struct vlpci_softc * const sc = v; 395 396 KASSERT((offset & 3) == 0); 397 398 if (offset >= PCI_CONF_SIZE) 399 return; 400 401 #if 0 402 device_printf(sc->sc_dev, "%s tag %x offset %x val %x\n", 403 __func__, (unsigned int)tag, offset, val); 404 #endif 405 406 mutex_spin_enter(&sc->sc_lock); 407 bus_space_write_4(&isa_io_bs_tag, sc->sc_conf_ioh, 408 VLPCI_CFGREG_ADDR_OFF, 0x80000000UL|tag|offset); 409 bus_space_write_4(&isa_io_bs_tag, sc->sc_conf_ioh, 410 VLPCI_CFGREG_DATA_OFF, val); 411 mutex_spin_exit(&sc->sc_lock); 412 } 413 414 static int 415 vlpci_pc_intr_map(const struct pci_attach_args *pa, pci_intr_handle_t *ih) 416 { 417 418 switch (pa->pa_intrpin) { 419 default: 420 case 0: 421 return EINVAL; 422 case 1: 423 case 2: 424 case 3: 425 case 4: 426 *ih = VLPCI_IRQ; 427 return 0; 428 } 429 } 430 431 static const char * 432 vlpci_pc_intr_string(void *v, pci_intr_handle_t ih, char *buf, size_t len) 433 { 434 435 if (ih == PCI_INTERRUPT_PIN_NONE) 436 return NULL; 437 snprintf(buf, len, "irq %llu", ih); 438 return buf; 439 } 440 441 static const struct evcnt * 442 vlpci_pc_intr_evcnt(void *v, pci_intr_handle_t ih) 443 { 444 445 return NULL; 446 } 447 448 static void * 449 vlpci_pc_intr_establish(void *v, pci_intr_handle_t pih, int ipl, 450 int (*callback)(void *), void *arg, const char *foo) 451 { 452 453 if (pih == 0) 454 return NULL; 455 456 return isa_intr_establish(NULL, pih, IST_LEVEL, ipl, callback, arg); 457 } 458 459 static void 460 vlpci_pc_intr_disestablish(void *v, void *w) 461 { 462 463 return isa_intr_disestablish(NULL, v); 464 } 465 466 #ifdef __HAVE_PCI_CONF_HOOK 467 static int 468 vlpci_pc_conf_hook(void *v, int b, int d, int f, pcireg_t id) 469 { 470 471 return PCI_CONF_DEFAULT /*& ~PCI_CONF_ENABLE_BM*/; 472 } 473 #endif 474 475 static void 476 vlpci_pc_conf_interrupt(void *v, int bus, int dev, int ipin, int swiz, 477 int *ilinep) 478 { 479 480 *ilinep = 0xff; /* XXX */ 481 } 482