1 /* $NetBSD: empb.c,v 1.11 2015/10/02 05:22:49 msaitoh Exp $ */ 2 3 /*- 4 * Copyright (c) 2012 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Radoslaw Kujawa. 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 /* Elbox Mediator PCI bridge driver. Currently supports Mediator 1200 models.*/ 33 34 #include <sys/types.h> 35 #include <sys/param.h> 36 #include <sys/time.h> 37 #include <sys/systm.h> 38 #include <sys/errno.h> 39 #include <sys/device.h> 40 #include <sys/malloc.h> 41 #include <sys/extent.h> 42 #include <sys/kmem.h> 43 44 #include <uvm/uvm_extern.h> 45 46 #include <machine/bus.h> 47 #include <machine/cpu.h> 48 49 #include <amiga/dev/zbusvar.h> 50 #include <amiga/pci/empbreg.h> 51 #include <amiga/pci/empbvar.h> 52 #include <amiga/pci/emmemvar.h> 53 #include <amiga/pci/empmvar.h> 54 55 #include <dev/pci/pciconf.h> 56 57 #include "opt_pci.h" 58 59 /*#define EMPB_DEBUG 1 */ 60 61 #define PCI_CONF_LOCK(s) (s) = splhigh() 62 #define PCI_CONF_UNLOCK(s) splx((s)) 63 64 #define WINDOW_LOCK(s) (s) = splhigh() 65 #define WINDOW_UNLOCK(s) splx((s)) 66 67 static int empb_match(device_t, cfdata_t, void *); 68 static void empb_attach(device_t, device_t, void *); 69 static void empb_callback(device_t); 70 71 static void empb_empm_attach(struct empb_softc *sc); 72 static int empb_empm_print(void *aux, const char *pnp); 73 74 static void empb_find_mem(struct empb_softc *); 75 static void empb_switch_bridge(struct empb_softc *, uint8_t); 76 static void empb_intr_enable(struct empb_softc *); 77 78 pcireg_t empb_pci_conf_read(pci_chipset_tag_t, pcitag_t, int); 79 void empb_pci_conf_write(pci_chipset_tag_t, pcitag_t, int, pcireg_t); 80 int empb_pci_bus_maxdevs(pci_chipset_tag_t, int); 81 void empb_pci_attach_hook(device_t, device_t, 82 struct pcibus_attach_args *); 83 pcitag_t empb_pci_make_tag(pci_chipset_tag_t, int, int, int); 84 void empb_pci_decompose_tag(pci_chipset_tag_t, pcitag_t, 85 int *, int *, int *); 86 int empb_pci_intr_map(const struct pci_attach_args *, 87 pci_intr_handle_t *); 88 const struct evcnt * empb_pci_intr_evcnt(pci_chipset_tag_t, 89 pci_intr_handle_t); 90 int empb_pci_conf_hook(pci_chipset_tag_t, int, int, int, pcireg_t); 91 92 CFATTACH_DECL_NEW(empb, sizeof(struct empb_softc), 93 empb_match, empb_attach, NULL, NULL); 94 95 static int 96 empb_match(device_t parent, cfdata_t cf, void *aux) 97 { 98 struct zbus_args *zap; 99 100 zap = aux; 101 102 if (zap->manid != ZORRO_MANID_ELBOX) 103 return 0; 104 105 switch (zap->prodid) { 106 case ZORRO_PRODID_MED1K2: 107 case ZORRO_PRODID_MED1K2SX: 108 case ZORRO_PRODID_MED1K2LT2: 109 case ZORRO_PRODID_MED1K2LT4: 110 case ZORRO_PRODID_MED1K2TX: 111 case ZORRO_PRODID_MEDZIV: /* ZIV untested! */ 112 return 1; 113 } 114 115 return 0; 116 } 117 118 119 static void 120 empb_attach(device_t parent, device_t self, void *aux) 121 { 122 struct empb_softc *sc; 123 struct zbus_args *zap; 124 125 volatile char *ba; 126 127 zap = aux; 128 sc = device_private(self); 129 sc->sc_dev = self; 130 ba = zap->va; 131 132 sc->model = zap->prodid; 133 134 switch (sc->model) { 135 case ZORRO_PRODID_MED1K2: 136 aprint_normal(": ELBOX Mediator PCI 1200\n"); 137 break; 138 case ZORRO_PRODID_MED1K2SX: 139 aprint_normal(": ELBOX Mediator PCI 1200 SX\n"); 140 break; 141 case ZORRO_PRODID_MED1K2LT2: 142 aprint_normal(": ELBOX Mediator PCI 1200 LT2\n"); 143 break; 144 case ZORRO_PRODID_MED1K2LT4: 145 aprint_normal(": ELBOX Mediator PCI 1200 LT4\n"); 146 break; 147 case ZORRO_PRODID_MED1K2TX: 148 aprint_normal(": ELBOX Mediator PCI 1200 TX\n"); 149 break; 150 default: 151 aprint_normal(": ELBOX Mediator PCI (unknown)\n"); 152 break; 153 } 154 155 /* Setup bus space mappings. */ 156 sc->pci_confio_area.base = (bus_addr_t) ba + EMPB_BRIDGE_OFF; 157 sc->pci_confio_area.absm = &amiga_bus_stride_1swap; 158 159 sc->setup_area.base = (bus_addr_t) ba + EMPB_SETUP_OFF; 160 sc->setup_area.absm = &amiga_bus_stride_1; 161 162 /* 163 * Defer everything until later, we need to wait for possible 164 * emmem attachments. 165 */ 166 167 config_defer(self, empb_callback); 168 } 169 170 static void 171 empb_callback(device_t self) { 172 173 struct empb_softc *sc; 174 pci_chipset_tag_t pc; 175 struct pcibus_attach_args pba; 176 #ifdef PCI_NETBSD_CONFIGURE 177 struct extent *ioext, *memext; 178 #endif /* PCI_NETBSD_CONFIGURE */ 179 180 sc = device_private(self); 181 pc = &sc->apc; 182 183 #ifdef EMPB_DEBUG 184 aprint_normal("empb: mapped setup %x->%x, conf/io %x->%x\n", 185 kvtop((void*) sc->setup_area.base), sc->setup_area.base, 186 kvtop((void*) sc->pci_confio_area.base), sc->pci_confio_area.base); 187 #endif 188 189 sc->pci_confio_t = &(sc->pci_confio_area); 190 191 /* 192 * We should not map I/O space here, however we have no choice 193 * since these addresses are shared between configuration space and 194 * I/O space. Not really a problem on m68k, however on PPC... 195 */ 196 if (bus_space_map(sc->pci_confio_t, 0, EMPB_BRIDGE_SIZE, 0, 197 &sc->pci_confio_h)) 198 aprint_error_dev(self, 199 "couldn't map PCI configuration & I/O space\n"); 200 201 sc->apc.pci_conf_datat = sc->pci_confio_t; 202 sc->apc.pci_conf_datah = sc->pci_confio_h; 203 204 sc->setup_area_t = &(sc->setup_area); 205 206 if (bus_space_map(sc->setup_area_t, 0, EMPB_SETUP_SIZE, 0, 207 &sc->setup_area_h)) 208 aprint_error_dev(self, 209 "couldn't map Mediator setup space\n"); 210 211 empb_find_mem(sc); 212 if (sc->pci_mem_win_size == 0) 213 aprint_error_dev(self, 214 "couldn't find memory space, check your WINDOW jumper\n"); 215 216 /* Initialize the PCI chipset tag. */ 217 sc->apc.pc_conf_v = (void*) pc; 218 sc->apc.pc_bus_maxdevs = empb_pci_bus_maxdevs; 219 sc->apc.pc_make_tag = amiga_pci_make_tag; 220 sc->apc.pc_decompose_tag = amiga_pci_decompose_tag; 221 sc->apc.pc_conf_read = empb_pci_conf_read; 222 sc->apc.pc_conf_write = empb_pci_conf_write; 223 sc->apc.pc_attach_hook = empb_pci_attach_hook; 224 225 sc->apc.pc_intr_map = empb_pci_intr_map; 226 sc->apc.pc_intr_string = amiga_pci_intr_string; 227 sc->apc.pc_intr_establish = amiga_pci_intr_establish; 228 sc->apc.pc_intr_disestablish = amiga_pci_intr_disestablish; 229 230 sc->apc.pc_conf_hook = empb_pci_conf_hook; 231 sc->apc.pc_conf_interrupt = amiga_pci_conf_interrupt; 232 233 sc->apc.cookie = sc; 234 235 #ifdef PCI_NETBSD_CONFIGURE 236 ioext = extent_create("empbio", 0, EMPB_BRIDGE_SIZE, 237 NULL, 0, EX_NOWAIT); 238 239 memext = extent_create("empbmem", EMPB_MEM_BASE, EMPB_MEM_END, 240 NULL, 0, EX_NOWAIT); 241 242 pci_configure_bus(pc, ioext, memext, NULL, 0, CACHELINE_SIZE); 243 244 extent_destroy(ioext); 245 extent_destroy(memext); 246 247 #endif /* PCI_NETBSD_CONFIGURE */ 248 249 pba.pba_iot = &(sc->pci_confio_area); 250 pba.pba_dmat = NULL; 251 pba.pba_dmat64 = NULL; 252 pba.pba_pc = pc; 253 pba.pba_flags = PCI_FLAGS_IO_OKAY; 254 255 if(sc->pci_mem_win_size > 0) { 256 pba.pba_memt = &(sc->pci_mem_win); 257 pba.pba_flags |= PCI_FLAGS_MEM_OKAY; 258 } else 259 pba.pba_memt = NULL; 260 261 pba.pba_bus = 0; 262 pba.pba_bridgetag = NULL; 263 264 /* Attach power management on SX and TX models. */ 265 switch (sc->model) { 266 case ZORRO_PRODID_MED1K2SX: 267 case ZORRO_PRODID_MED1K2TX: 268 empb_empm_attach(sc); 269 default: 270 break; 271 } 272 273 empb_intr_enable(sc); 274 275 config_found_ia(self, "pcibus", &pba, pcibusprint); 276 } 277 278 static void 279 empb_empm_attach(struct empb_softc *sc) 280 { 281 struct empm_attach_args aa; 282 aa.setup_area_t = sc->setup_area_t; 283 284 config_found_ia(sc->sc_dev, "empmdev", &aa, empb_empm_print); 285 } 286 287 static int 288 empb_empm_print(void *aux, const char *pnp) 289 { 290 if (pnp) 291 aprint_normal("empm at %s", pnp); 292 293 return UNCONF; 294 } 295 296 static void 297 empb_intr_enable(struct empb_softc *sc) 298 { 299 bus_space_write_1(sc->setup_area_t, sc->setup_area_h, 300 EMPB_SETUP_INTR_OFF, EMPB_INTR_ENABLE); 301 } 302 303 /* 304 * Switch between configuration space and I/O space. 305 */ 306 static void 307 empb_switch_bridge(struct empb_softc *sc, uint8_t mode) 308 { 309 bus_space_write_1(sc->setup_area_t, sc->setup_area_h, 310 EMPB_SETUP_BRIDGE_OFF, mode); 311 } 312 313 314 /* 315 * Try to find a (optional) memory window board. 316 */ 317 static void 318 empb_find_mem(struct empb_softc *sc) 319 { 320 device_t memdev; 321 struct emmem_softc *mem_sc; 322 323 memdev = device_find_by_xname("emmem0"); 324 sc->pci_mem_win_size = 0; 325 326 if(memdev == NULL) { 327 return; 328 } 329 330 mem_sc = device_private(memdev); 331 332 sc->pci_mem_win.base = (bus_addr_t) mem_sc->sc_base; 333 sc->pci_mem_win.absm = &empb_bus_swap; 334 sc->pci_mem_win_size = mem_sc->sc_size; 335 sc->pci_mem_win_t = &sc->pci_mem_win; 336 337 if(sc->pci_mem_win_size == 8*1024*1024) 338 sc->pci_mem_win_mask = EMPB_WINDOW_MASK_8M; 339 else if(sc->pci_mem_win_size == 4*1024*1024) 340 sc->pci_mem_win_mask = EMPB_WINDOW_MASK_4M; 341 else /* disable anyway */ 342 sc->pci_mem_win_size = 0; 343 344 #ifdef EMPB_DEBUG 345 aprint_normal("empb: found %x b window at %p, switch mask %x\n", 346 sc->pci_mem_win_size, (void*) sc->pci_mem_win.base, 347 sc->pci_mem_win_mask); 348 #endif /* EMPB_DEBUG */ 349 350 } 351 352 /* 353 * Switch memory window position. Return PCI mem address seen at the beginning 354 * of window. 355 */ 356 bus_addr_t 357 empb_switch_window(struct empb_softc *sc, bus_addr_t address) 358 { 359 int s; 360 uint16_t win_reg; 361 #ifdef EMPB_DEBUG 362 uint16_t rwin_reg; 363 #endif /* EMPB_DEBUG */ 364 365 WINDOW_LOCK(s); 366 367 win_reg = bswap16((address >> EMPB_WINDOW_SHIFT) 368 & sc->pci_mem_win_mask); 369 370 bus_space_write_2(sc->setup_area_t, sc->setup_area_h, 371 EMPB_SETUP_WINDOW_OFF, win_reg); 372 373 /* store window pos, like: sc->pci_mem_win_pos = win_reg ? */ 374 375 #ifdef EMPB_DEBUG 376 rwin_reg = bus_space_read_2(sc->setup_area_t, sc->setup_area_h, 377 EMPB_SETUP_WINDOW_OFF); 378 379 aprint_normal("empb: access to %p window switch to %x => reg now %x\n", 380 (void*) address, win_reg, rwin_reg); 381 #endif /* EMPB_DEBUG */ 382 383 WINDOW_UNLOCK(s); 384 385 return (bus_addr_t)((bswap16(win_reg)) << EMPB_WINDOW_SHIFT); 386 } 387 388 389 pcireg_t 390 empb_pci_conf_read(pci_chipset_tag_t pc, pcitag_t tag, int reg) 391 { 392 uint32_t data; 393 uint32_t bus, dev, func; 394 struct empb_softc *sc; 395 int s; 396 397 if ((unsigned int)reg >= PCI_CONF_SIZE) 398 return (pcireg_t) -1; 399 400 sc = pc->cookie; 401 402 pci_decompose_tag(pc, tag, &bus, &dev, &func); 403 404 PCI_CONF_LOCK(s); 405 406 empb_switch_bridge(sc, BRIDGE_CONF); 407 408 data = bus_space_read_4(pc->pci_conf_datat, pc->pci_conf_datah, 409 EMPB_CONF_DEV_STRIDE*dev + EMPB_CONF_FUNC_STRIDE*func + reg); 410 #ifdef EMPB_DEBUG_CONF 411 aprint_normal("empb conf read va: %lx, bus: %d, dev: %d, " 412 "func: %d, reg: %d -r-> data %x\n", 413 pc->pci_conf_datah, bus, dev, func, reg, data); 414 #endif /* EMPB_DEBUG_CONF */ 415 416 empb_switch_bridge(sc, BRIDGE_IO); 417 418 PCI_CONF_UNLOCK(s); 419 420 return data; 421 } 422 423 void 424 empb_pci_conf_write(pci_chipset_tag_t pc, pcitag_t tag, int reg, pcireg_t val) 425 { 426 uint32_t bus, dev, func; 427 struct empb_softc *sc; 428 int s; 429 430 if ((unsigned int)reg >= PCI_CONF_SIZE) 431 return; 432 433 sc = pc->cookie; 434 435 pci_decompose_tag(pc, tag, &bus, &dev, &func); 436 437 PCI_CONF_LOCK(s); 438 439 empb_switch_bridge(sc, BRIDGE_CONF); 440 441 bus_space_write_4(pc->pci_conf_datat, pc->pci_conf_datah, 442 EMPB_CONF_DEV_STRIDE*dev + EMPB_CONF_FUNC_STRIDE*func + reg, val); 443 #ifdef EMPB_DEBUG_CONF 444 aprint_normal("empb conf write va: %lx, bus: %d, dev: %d, " 445 "func: %d, reg: %d -w-> data %x\n", 446 pc->pci_conf_datah, bus, dev, func, reg, val); 447 #endif /* EMPB_DEBUG_CONF */ 448 empb_switch_bridge(sc, BRIDGE_IO); 449 450 PCI_CONF_UNLOCK(s); 451 } 452 453 int 454 empb_pci_bus_maxdevs(pci_chipset_tag_t pc, int busno) 455 { 456 return 6; /* no Mediator with more than 6 slots? */ 457 } 458 459 void 460 empb_pci_attach_hook(device_t parent, device_t self, 461 struct pcibus_attach_args *pba) 462 { 463 } 464 465 int 466 empb_pci_intr_map(const struct pci_attach_args *pa, pci_intr_handle_t *ihp) 467 { 468 /* TODO: add sanity checking */ 469 470 *ihp = EMPB_INT; 471 return 0; 472 } 473 474 const struct evcnt * 475 empb_pci_intr_evcnt(pci_chipset_tag_t pc, pci_intr_handle_t ih) 476 { 477 /* TODO: implement */ 478 return NULL; 479 } 480 481 int 482 empb_pci_conf_hook(pci_chipset_tag_t pct, int bus, int dev, int func, 483 pcireg_t id) 484 { 485 486 /* 487 * Register information about some known PCI devices with 488 * DMA-able memory. 489 */ 490 /*if ((PCI_VENDOR(id) == PCI_VENDOR_3DFX) && 491 (PCI_PRODUCT(id) >= PCI_PRODUCT_3DFX_VOODOO3))*/ 492 493 494 return PCI_CONF_DEFAULT; 495 } 496