1 /* $NetBSD: gemini_pci.c,v 1.15 2013/08/18 15:58:19 matt Exp $ */ 2 3 /* adapted from: 4 * NetBSD: i80312_pci.c,v 1.9 2005/12/11 12:16:51 christos Exp 5 */ 6 7 /* 8 * Copyright (c) 2001 Wasabi Systems, Inc. 9 * All rights reserved. 10 * 11 * Written by Jason R. Thorpe for Wasabi Systems, Inc. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 3. All advertising materials mentioning features or use of this software 22 * must display the following acknowledgement: 23 * This product includes software developed for the NetBSD Project by 24 * Wasabi Systems, Inc. 25 * 4. The name of Wasabi Systems, Inc. may not be used to endorse 26 * or promote products derived from this software without specific prior 27 * written permission. 28 * 29 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND 30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 31 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 32 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC 33 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 34 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 35 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 36 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 37 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 39 * POSSIBILITY OF SUCH DAMAGE. 40 */ 41 42 /* 43 * PCI configuration support for i80312 Companion I/O chip. 44 */ 45 46 #include <sys/cdefs.h> 47 __KERNEL_RCSID(0, "$NetBSD: gemini_pci.c,v 1.15 2013/08/18 15:58:19 matt Exp $"); 48 49 #include "opt_gemini.h" 50 #include "opt_pci.h" 51 #include "pci.h" 52 53 #include <sys/param.h> 54 #include <sys/systm.h> 55 #include <sys/device.h> 56 #include <sys/extent.h> 57 #include <sys/malloc.h> 58 #include <sys/bus.h> 59 #include <sys/intr.h> 60 61 #include <uvm/uvm_extern.h> 62 63 #include <dev/pci/pcivar.h> 64 #include <dev/pci/pcidevs.h> 65 #include <dev/pci/pciconf.h> 66 67 #include <arm/locore.h> 68 69 #include <arm/pic/picvar.h> 70 71 #include <arm/gemini/gemini_reg.h> 72 #include <arm/gemini/gemini_pcivar.h> 73 #include <arm/gemini/gemini_obiovar.h> 74 75 void gemini_pci_attach_hook(device_t, device_t, 76 struct pcibus_attach_args *); 77 int gemini_pci_bus_maxdevs(void *, int); 78 pcitag_t gemini_pci_make_tag(void *, int, int, int); 79 void gemini_pci_decompose_tag(void *, pcitag_t, int *, int *, 80 int *); 81 pcireg_t gemini_pci_conf_read(void *, pcitag_t, int); 82 void gemini_pci_conf_write(void *, pcitag_t, int, pcireg_t); 83 int gemini_pci_conf_hook(void *, int, int, int, pcireg_t); 84 void gemini_pci_conf_interrupt(void *, int, int, int, int, int *); 85 86 int gemini_pci_intr_map(const struct pci_attach_args *, 87 pci_intr_handle_t *); 88 const char *gemini_pci_intr_string(void *, pci_intr_handle_t); 89 const struct evcnt *gemini_pci_intr_evcnt(void *, pci_intr_handle_t); 90 void *gemini_pci_intr_establish(void *, pci_intr_handle_t, 91 int, int (*)(void *), void *); 92 void gemini_pci_intr_disestablish(void *, void *); 93 int gemini_pci_intr_handler(void *v); 94 95 #define PCI_CONF_LOCK(s) (s) = disable_interrupts(I32_bit) 96 #define PCI_CONF_UNLOCK(s) restore_interrupts((s)) 97 98 struct gemini_pci_intrq { 99 SIMPLEQ_ENTRY(gemini_pci_intrq) iq_q; 100 int (*iq_func)(void *); 101 void *iq_arg; 102 void *iq_ih; 103 }; 104 105 static SIMPLEQ_HEAD(, gemini_pci_intrq) gemini_pci_intrq = 106 SIMPLEQ_HEAD_INITIALIZER(gemini_pci_intrq); 107 108 static inline int 109 gemini_pci_intrq_empty(void) 110 { 111 return SIMPLEQ_EMPTY(&gemini_pci_intrq); 112 } 113 114 static inline void * 115 gemini_pci_intrq_insert(void *ih, int (*func)(void *), void *arg) 116 { 117 struct gemini_pci_intrq *iqp; 118 119 iqp = malloc(sizeof(*iqp), M_DEVBUF, M_NOWAIT|M_ZERO); 120 if (iqp == NULL) { 121 printf("gemini_pci_intrq_insert: malloc failed\n"); 122 return NULL; 123 } 124 125 iqp->iq_func = func; 126 iqp->iq_arg = arg; 127 iqp->iq_ih = ih; 128 SIMPLEQ_INSERT_TAIL(&gemini_pci_intrq, iqp, iq_q); 129 130 return (void *)iqp; 131 } 132 133 static inline void 134 gemini_pci_intrq_remove(void *cookie) 135 { 136 struct gemini_pci_intrq *iqp; 137 138 SIMPLEQ_FOREACH(iqp, &gemini_pci_intrq, iq_q) { 139 if ((void *)iqp == cookie) { 140 SIMPLEQ_REMOVE(&gemini_pci_intrq, 141 iqp, gemini_pci_intrq, iq_q); 142 free(iqp, M_DEVBUF); 143 return; 144 } 145 } 146 } 147 148 static inline int 149 gemini_pci_intrq_dispatch(void) 150 { 151 struct gemini_pci_intrq *iqp; 152 153 SIMPLEQ_FOREACH(iqp, &gemini_pci_intrq, iq_q) { 154 (*iqp->iq_func)(iqp->iq_arg); 155 } 156 157 return 1; 158 } 159 160 void 161 gemini_pci_init(pci_chipset_tag_t pc, void *cookie) 162 { 163 #if NPCI > 0 && defined(PCI_NETBSD_CONFIGURE) 164 struct obio_softc *sc = cookie; 165 struct extent *ioext, *memext; 166 #endif 167 168 pc->pc_conf_v = cookie; 169 pc->pc_attach_hook = gemini_pci_attach_hook; 170 pc->pc_bus_maxdevs = gemini_pci_bus_maxdevs; 171 pc->pc_make_tag = gemini_pci_make_tag; 172 pc->pc_decompose_tag = gemini_pci_decompose_tag; 173 pc->pc_conf_read = gemini_pci_conf_read; 174 pc->pc_conf_write = gemini_pci_conf_write; 175 176 pc->pc_intr_v = cookie; 177 pc->pc_intr_map = gemini_pci_intr_map; 178 pc->pc_intr_string = gemini_pci_intr_string; 179 pc->pc_intr_evcnt = gemini_pci_intr_evcnt; 180 pc->pc_intr_establish = gemini_pci_intr_establish; 181 pc->pc_intr_disestablish = gemini_pci_intr_disestablish; 182 183 pc->pc_conf_hook = gemini_pci_conf_hook; 184 pc->pc_conf_interrupt = gemini_pci_conf_interrupt; 185 186 /* 187 * initialize copy of CFG_CMD 188 */ 189 sc->sc_pci_chipset.pc_cfg_cmd = 190 gemini_pci_conf_read(sc, 0, GEMINI_PCI_CFG_CMD); 191 192 #if NPCI > 0 && defined(PCI_NETBSD_CONFIGURE) 193 /* 194 * Configure the PCI bus. 195 * 196 * XXX We need to revisit this. We only configure the Secondary 197 * bus (and its children). The bus configure code needs changes 198 * to support how the busses are arranged on this chip. We also 199 * need to only configure devices in the private device space on 200 * the Secondary bus. 201 */ 202 203 aprint_normal("%s: configuring Secondary PCI bus\n", 204 device_xname(sc->sc_dev)); 205 206 /* 207 * XXX PCI IO addr should be inherited ? 208 */ 209 ioext = extent_create("pciio", 210 GEMINI_PCIIO_BASE, 211 GEMINI_PCIIO_BASE + GEMINI_PCIIO_SIZE - 1, 212 NULL, 0, EX_NOWAIT); 213 214 /* 215 * XXX PCI mem addr should be inherited ? 216 */ 217 memext = extent_create("pcimem", 218 GEMINI_PCIMEM_BASE, 219 GEMINI_PCIMEM_BASE + GEMINI_PCIMEM_SIZE - 1, 220 NULL, 0, EX_NOWAIT); 221 222 pci_configure_bus(pc, ioext, memext, NULL, 0, arm_dcache_align); 223 224 gemini_pci_conf_write(sc, 0, GEMINI_PCI_CFG_REG_MEM1, 225 PCI_CFG_REG_MEM_BASE((GEMINI_DRAM_BASE + (GEMINI_BUSBASE * 1024 * 1024))) 226 | gemini_pci_cfg_reg_mem_size(MEMSIZE * 1024 * 1024)); 227 228 extent_destroy(ioext); 229 extent_destroy(memext); 230 #endif 231 } 232 233 void 234 gemini_pci_conf_interrupt(void *v, int a, int b, int c, int d, int *p) 235 { 236 } 237 238 int 239 gemini_pci_conf_hook(void *v, int bus, int device, int function, pcireg_t id) 240 { 241 int rv; 242 243 rv = PCI_CONF_ALL; 244 245 return rv; 246 } 247 248 void 249 gemini_pci_attach_hook(device_t parent, device_t self, 250 struct pcibus_attach_args *pba) 251 { 252 /* Nothing to do. */ 253 } 254 255 int 256 gemini_pci_bus_maxdevs(void *v, int busno) 257 { 258 return (32); 259 } 260 261 pcitag_t 262 gemini_pci_make_tag(void *v, int b, int d, int f) 263 { 264 return ((b << 16) | (d << 11) | (f << 8)); 265 } 266 267 void 268 gemini_pci_decompose_tag(void *v, pcitag_t tag, int *bp, int *dp, int *fp) 269 { 270 if (bp != NULL) 271 *bp = (tag >> 16) & 0xff; 272 if (dp != NULL) 273 *dp = (tag >> 11) & 0x1f; 274 if (fp != NULL) 275 *fp = (tag >> 8) & 0x7; 276 } 277 278 struct pciconf_state { 279 uint32_t ps_addr_val; 280 int ps_b, ps_d, ps_f; 281 }; 282 283 static int 284 gemini_pci_conf_setup(struct obio_softc *sc, pcitag_t tag, int offset, 285 struct pciconf_state *ps) 286 { 287 gemini_pci_decompose_tag(sc, tag, &ps->ps_b, &ps->ps_d, &ps->ps_f); 288 289 ps->ps_addr_val = 290 PCI_CFG_CMD_ENB 291 | PCI_CFG_CMD_BUSn(ps->ps_b) 292 | PCI_CFG_CMD_DEVn(ps->ps_d) 293 | PCI_CFG_CMD_FUNCn(ps->ps_f) 294 | PCI_CFG_CMD_REGn(offset); 295 296 return (0); 297 } 298 299 pcireg_t 300 gemini_pci_conf_read(void *v, pcitag_t tag, int offset) 301 { 302 struct obio_softc *sc = v; 303 struct pciconf_state ps; 304 vaddr_t va; 305 pcireg_t rv; 306 u_int s; 307 308 if (gemini_pci_conf_setup(sc, tag, offset, &ps)) 309 return ((pcireg_t) -1); 310 311 PCI_CONF_LOCK(s); 312 313 if (sc->sc_pci_chipset.pc_cfg_cmd != ps.ps_addr_val) { 314 sc->sc_pci_chipset.pc_cfg_cmd = ps.ps_addr_val; 315 bus_space_write_4(sc->sc_iot, sc->sc_pcicfg_ioh, 316 GEMINI_PCI_CFG_CMD, ps.ps_addr_val); 317 } 318 319 va = (vaddr_t) bus_space_vaddr(sc->sc_iot, sc->sc_pcicfg_ioh); 320 if (badaddr_read((void *) (va + GEMINI_PCI_CFG_DATA), sizeof(rv), &rv)) { 321 /* 322 * XXX Clear the Master Abort 323 */ 324 #if 1 325 printf("conf_read: %d/%d/%d bad address\n", 326 ps.ps_b, ps.ps_d, ps.ps_f); 327 #endif 328 rv = (pcireg_t) -1; 329 } 330 331 PCI_CONF_UNLOCK(s); 332 333 return (rv); 334 } 335 336 void 337 gemini_pci_conf_write(void *v, pcitag_t tag, int offset, pcireg_t val) 338 { 339 struct obio_softc *sc = v; 340 struct pciconf_state ps; 341 u_int s; 342 343 if (gemini_pci_conf_setup(sc, tag, offset, &ps)) 344 return; 345 346 PCI_CONF_LOCK(s); 347 348 if (sc->sc_pci_chipset.pc_cfg_cmd != ps.ps_addr_val) { 349 sc->sc_pci_chipset.pc_cfg_cmd = ps.ps_addr_val; 350 bus_space_write_4(sc->sc_iot, sc->sc_pcicfg_ioh, 351 GEMINI_PCI_CFG_CMD, ps.ps_addr_val); 352 } 353 354 bus_space_write_4(sc->sc_iot, sc->sc_pcicfg_ioh, 355 GEMINI_PCI_CFG_DATA, val); 356 357 PCI_CONF_UNLOCK(s); 358 } 359 360 int 361 gemini_pci_intr_map(const struct pci_attach_args *pa, pci_intr_handle_t *ihp) 362 { 363 int irq; 364 365 irq = 8; 366 367 *ihp = irq; 368 return 0; 369 } 370 371 const char * 372 gemini_pci_intr_string(void *v, pci_intr_handle_t ih) 373 { 374 const char *name = "pci"; 375 376 return (name); 377 } 378 379 const struct evcnt * 380 gemini_pci_intr_evcnt(void *v, pci_intr_handle_t ih) 381 { 382 return NULL; 383 } 384 385 void * 386 gemini_pci_intr_establish(void *v, pci_intr_handle_t pci_ih, int ipl, 387 int (*func)(void *), void *arg) 388 { 389 pcireg_t r; 390 void *ih=NULL; 391 int irq; 392 void *cookie; 393 394 irq = (int)pci_ih; 395 396 r = gemini_pci_conf_read(v, 0, GEMINI_PCI_CFG_REG_CTL2); 397 r |= CFG_REG_CTL2_INTMASK_INT_ABCD; 398 gemini_pci_conf_write(v, 0, GEMINI_PCI_CFG_REG_CTL2, r); 399 400 if (gemini_pci_intrq_empty()) 401 ih = intr_establish(irq, ipl, IST_LEVEL_HIGH, 402 gemini_pci_intr_handler, v); 403 404 cookie = gemini_pci_intrq_insert(ih, func, arg); 405 if (cookie == NULL) { 406 if (gemini_pci_intrq_empty()) 407 intr_disestablish(ih); 408 } 409 410 return cookie; 411 } 412 413 void 414 gemini_pci_intr_disestablish(void *v, void *cookie) 415 { 416 pcireg_t r; 417 struct gemini_pci_intrq *iqp = (struct gemini_pci_intrq *)cookie; 418 void *ih = iqp->iq_ih; 419 420 gemini_pci_intrq_remove(cookie); 421 if (gemini_pci_intrq_empty()) { 422 r = gemini_pci_conf_read(v, 0, GEMINI_PCI_CFG_REG_CTL2); 423 r &= ~CFG_REG_CTL2_INTMASK_INT_ABCD; 424 gemini_pci_conf_write(v, 0, GEMINI_PCI_CFG_REG_CTL2, r); 425 intr_disestablish(ih); 426 } 427 } 428 429 int 430 gemini_pci_intr_handler(void *v) 431 { 432 pcireg_t r; 433 int rv; 434 435 /* 436 * dispatch PCI device interrupt handlers 437 */ 438 rv = gemini_pci_intrq_dispatch(); 439 440 /* 441 * ack Gemini PCI interrupts 442 */ 443 r = gemini_pci_conf_read(v, 0, GEMINI_PCI_CFG_REG_CTL2); 444 gemini_pci_conf_write(v, 0, GEMINI_PCI_CFG_REG_CTL2, r); 445 446 return rv; 447 } 448