Home | History | Annotate | Line # | Download | only in s3c2xx0
s3c2800_pci.c revision 1.7
      1 /*	$NetBSD: s3c2800_pci.c,v 1.7 2003/07/15 00:24:49 lukem Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2002 Fujitsu Component Limited
      5  * Copyright (c) 2002 Genetec Corporation
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. Neither the name of The Fujitsu Component Limited nor the name of
     17  *    Genetec corporation may not be used to endorse or promote products
     18  *    derived from this software without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY FUJITSU COMPONENT LIMITED AND GENETEC
     21  * CORPORATION ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
     22  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     23  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     24  * DISCLAIMED.  IN NO EVENT SHALL FUJITSU COMPONENT LIMITED OR GENETEC
     25  * CORPORATION BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     26  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     27  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
     28  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     29  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
     31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  * SUCH DAMAGE.
     33  *
     34  * derived from evbarm/ifpga/ifpga_pci.c
     35  */
     36 
     37 /*
     38  * Copyright (c) 2001 ARM Ltd
     39  * All rights reserved.
     40  *
     41  * Redistribution and use in source and binary forms, with or without
     42  * modification, are permitted provided that the following conditions
     43  * are met:
     44  * 1. Redistributions of source code must retain the above copyright
     45  *    notice, this list of conditions and the following disclaimer.
     46  * 2. Redistributions in binary form must reproduce the above copyright
     47  *    notice, this list of conditions and the following disclaimer in the
     48  *    documentation and/or other materials provided with the distribution.
     49  * 3. The name of the company may not be used to endorse or promote
     50  *    products derived from this software without specific prior written
     51  *    permission.
     52  *
     53  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
     54  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     55  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     56  * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
     57  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     58  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     59  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     60  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     61  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     62  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     63  * SUCH DAMAGE.
     64  *
     65  * Copyright (c) 1997,1998 Mark Brinicombe.
     66  * Copyright (c) 1997,1998 Causality Limited
     67  * All rights reserved.
     68  *
     69  * Redistribution and use in source and binary forms, with or without
     70  * modification, are permitted provided that the following conditions
     71  * are met:
     72  * 1. Redistributions of source code must retain the above copyright
     73  *    notice, this list of conditions and the following disclaimer.
     74  * 2. Redistributions in binary form must reproduce the above copyright
     75  *    notice, this list of conditions and the following disclaimer in the
     76  *    documentation and/or other materials provided with the distribution.
     77  * 3. All advertising materials mentioning features or use of this software
     78  *    must display the following acknowledgement:
     79  *	This product includes software developed by Mark Brinicombe
     80  *	for the NetBSD Project.
     81  * 4. The name of the company nor the name of the author may be used to
     82  *    endorse or promote products derived from this software without specific
     83  *    prior written permission.
     84  *
     85  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
     86  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     87  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     88  * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
     89  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     90  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     91  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     92  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     93  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     94  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     95  * SUCH DAMAGE.
     96  */
     97 
     98 /*
     99  * PCI configuration support for Samsung s3c2800.
    100  */
    101 
    102 #include <sys/cdefs.h>
    103 __KERNEL_RCSID(0, "$NetBSD: s3c2800_pci.c,v 1.7 2003/07/15 00:24:49 lukem Exp $");
    104 
    105 #include <sys/param.h>
    106 #include <sys/systm.h>
    107 #include <sys/kernel.h>
    108 #include <sys/device.h>
    109 #include <sys/extent.h>
    110 #include <sys/malloc.h>
    111 
    112 #include <uvm/uvm_extern.h>
    113 
    114 #include <machine/bus.h>
    115 
    116 #include <arm/s3c2xx0/s3c2800reg.h>
    117 #include <arm/s3c2xx0/s3c2800var.h>
    118 
    119 #include <dev/pci/pcireg.h>
    120 #include <dev/pci/pciconf.h>
    121 
    122 #include "opt_pci.h"
    123 #include "pci.h"
    124 
    125 /*
    126  * pci tag encoding.
    127  * also useful for configuration type 0 address
    128  */
    129 #define BUSNO_SHIFT	16
    130 #define BUSNO_MASK	(0xff<<BUSNO_SHIFT)
    131 #define DEVNO_SHIFT	11
    132 #define DEVNO_MASK	(0x1f<<DEVNO_SHIFT)
    133 #define tag_to_devno(tag)	(((tag)&DEVNO_MASK)>>DEVNO_SHIFT)
    134 #define FUNNO_SHIFT	8
    135 #define FUNNO_MASK	(0x07<<FUNNO_SHIFT)
    136 
    137 #define BUS0_DEV_MIN	1
    138 #define BUS0_DEV_MAX	21
    139 
    140 void	s3c2800_pci_attach_hook(struct device *, struct device *,
    141 			        struct pcibus_attach_args *);
    142 int	s3c2800_pci_bus_maxdevs(void *, int);
    143 pcitag_t s3c2800_pci_make_tag(void *, int, int, int);
    144 void	s3c2800_pci_decompose_tag(void *, pcitag_t, int *, int *, int *);
    145 pcireg_t s3c2800_pci_conf_read(void *, pcitag_t, int);
    146 void	s3c2800_pci_conf_write(void *, pcitag_t, int, pcireg_t);
    147 int	s3c2800_pci_intr_map(struct pci_attach_args *, pci_intr_handle_t *);
    148 const char *s3c2800_pci_intr_string(void *, pci_intr_handle_t);
    149 const struct evcnt *s3c2800_pci_intr_evcnt(void *, pci_intr_handle_t);
    150 void * s3c2800_pci_intr_establish(void *, pci_intr_handle_t, int,
    151 				  int (*) (void *), void *);
    152 void	s3c2800_pci_intr_disestablish(void *, void *);
    153 
    154 #define	PCI_CONF_LOCK(s)	(s) = disable_interrupts(I32_bit)
    155 #define	PCI_CONF_UNLOCK(s)	restore_interrupts((s))
    156 
    157 struct sspci_irq_handler {
    158 	int (*func) (void *);
    159 	void *arg;
    160 	int level;
    161 	SLIST_ENTRY(sspci_irq_handler) link;
    162 };
    163 
    164 struct sspci_softc {
    165 	struct device sc_dev;
    166 
    167 	bus_space_tag_t sc_iot;
    168 	bus_space_handle_t sc_reg_ioh;
    169 	bus_space_handle_t sc_conf0_ioh;	/* config type0 space */
    170 	bus_space_handle_t sc_conf1_ioh;	/* config type1 space */
    171 
    172 	uint32_t sc_pciinten;	/* copy of PCIINTEN register */
    173 
    174 	/* list of interrupt handlers. SLIST is not good for removing
    175 	 * element from it, but intr_disestablish is rarely called */
    176 	SLIST_HEAD(, sspci_irq_handler) sc_irq_handlers;
    177 
    178 	void *sc_softinterrupt;
    179 };
    180 
    181 static int sspci_match(struct device *, struct cfdata *, void *aux);
    182 static void sspci_attach(struct device *, struct device *, void *);
    183 
    184 static int sspci_bs_map(void *, bus_addr_t, bus_size_t, int,
    185 			     bus_space_handle_t *);
    186 static int sspci_init_controller(struct sspci_softc *);
    187 static int sspci_intr(void *);
    188 static void sspci_softintr(void *);
    189 
    190 /* attach structures */
    191 CFATTACH_DECL(sspci, sizeof(struct sspci_softc), sspci_match, sspci_attach,
    192     NULL, NULL);
    193 
    194 
    195 struct arm32_pci_chipset sspci_chipset = {
    196 	NULL,		/* conf_v */
    197 	s3c2800_pci_attach_hook,
    198 	s3c2800_pci_bus_maxdevs,
    199 	s3c2800_pci_make_tag,
    200 	s3c2800_pci_decompose_tag,
    201 	s3c2800_pci_conf_read,
    202 	s3c2800_pci_conf_write,
    203 	NULL,		/* intr_v */
    204 	s3c2800_pci_intr_map,
    205 	s3c2800_pci_intr_string,
    206 	s3c2800_pci_intr_evcnt,
    207 	s3c2800_pci_intr_establish,
    208 	s3c2800_pci_intr_disestablish
    209 };
    210 
    211 
    212 /*
    213  * bus space tag for PCI IO/Memory access space.
    214  * filled in by sspci_attach()
    215  */
    216 struct bus_space sspci_io_tag, sspci_mem_tag;
    217 
    218 static int
    219 sspci_match(struct device *parent, struct cfdata *match, void *aux)
    220 {
    221 	return 1;
    222 }
    223 
    224 static int
    225 sspci_print(void *aux, const char *pnp)
    226 {
    227 	struct pcibus_attach_args *pci_pba = (struct pcibus_attach_args *) aux;
    228 
    229 	if (pnp)
    230 		aprint_normal("%s at %s", pci_pba->pba_busname, pnp);
    231 	if (strcmp(pci_pba->pba_busname, "pci") == 0)
    232 		aprint_normal(" bus %d", pci_pba->pba_bus);
    233 
    234 	return UNCONF;
    235 }
    236 
    237 static void
    238 sspci_attach(struct device *parent, struct device *self, void *aux)
    239 {
    240 	struct sspci_softc *sc = (struct sspci_softc *) self;
    241 	struct s3c2xx0_attach_args *aa = aux;
    242 	bus_space_tag_t iot;
    243 	bus_dma_tag_t pci_dma_tag;
    244 	const char *error_on;	/* for panic message */
    245 #if defined(PCI_NETBSD_CONFIGURE)
    246 	struct extent *ioext, *memext;
    247 	struct pcibus_attach_args pci_pba;
    248 #endif
    249 
    250 #define FAIL(which)  do { \
    251 	error_on=(which); goto abort; }while(/*CONSTCOND*/0)
    252 
    253 	iot = sc->sc_iot = aa->sa_iot;
    254 	if (bus_space_map(iot, S3C2800_PCICTL_BASE,
    255 		S3C2800_PCICTL_SIZE, 0, &sc->sc_reg_ioh))
    256 		FAIL("control regs");
    257 
    258 	if (bus_space_map(iot, S3C2800_PCI_CONF0_BASE,
    259 		S3C2800_PCI_CONF0_SIZE, 0, &sc->sc_conf0_ioh))
    260 		FAIL("config type 0 area");
    261 
    262 #if 0
    263 	if (bus_space_map(iot, S3C2800_PCI_CONF1_BASE,
    264 		S3C2800_PCI_CONF1_SIZE, 0, &sc->sc_conf1_ioh))
    265 		FAIL("config type 1 area");
    266 #endif
    267 	printf("\n");
    268 
    269 	SLIST_INIT(&sc->sc_irq_handlers);
    270 	if (!s3c2800_intr_establish(S3C2800_INT_PCI, IPL_AUDIO, IST_LEVEL,
    271 		sspci_intr, sc))
    272 		FAIL("intr_establish");
    273 
    274 	sc->sc_softinterrupt = softintr_establish(IPL_SOFT,
    275 	    sspci_softintr, sc);
    276 	if (sc->sc_softinterrupt == NULL)
    277 		FAIL("softintr_establish");
    278 
    279 #if defined(PCI_NETBSD_CONFIGURE)
    280 	if (sspci_init_controller(sc)) {
    281 		printf("%s: failed to initialize controller\n", self->dv_xname);
    282 		return;
    283 	}
    284 #endif
    285 
    286 	sc->sc_pciinten =
    287 	    PCIINT_INA | PCIINT_SER | PCIINT_TPE | PCIINT_MPE |
    288 	    PCIINT_MFE | PCIINT_PRA | PCIINT_PRD;
    289 
    290 	bus_space_write_4(iot, sc->sc_reg_ioh, PCICTL_PCIINTEN,
    291 	    sc->sc_pciinten);
    292 
    293 	{
    294 		pcireg_t id_reg, class_reg;
    295 		char buf[1000];
    296 
    297 		id_reg = bus_space_read_4(iot, sc->sc_reg_ioh,
    298 		    PCI_ID_REG);
    299 		class_reg = bus_space_read_4(iot,
    300 		    sc->sc_reg_ioh, PCI_CLASS_REG);
    301 
    302 		pci_devinfo(id_reg, class_reg, 1, buf);
    303 		printf("%s: %s\n", self->dv_xname, buf);
    304 	}
    305 
    306 #if defined(PCI_NETBSD_CONFIGURE)
    307 	ioext = extent_create("pciio", 0x100, S3C2800_PCI_IOSPACE_SIZE - 0x100,
    308 	    M_DEVBUF, NULL, 0, EX_NOWAIT);
    309 
    310 	memext = extent_create("pcimem", 0, S3C2800_PCI_MEMSPACE_SIZE,
    311 	    M_DEVBUF, NULL, 0, EX_NOWAIT);
    312 
    313 	sspci_chipset.pc_conf_v = (void *) sc;
    314 	sspci_chipset.pc_intr_v = (void *) sc;
    315 
    316 	pci_configure_bus(&sspci_chipset, ioext, memext, NULL, 0,
    317 	    arm_dcache_align);
    318 
    319 	extent_destroy(memext);
    320 	extent_destroy(ioext);
    321 #endif				/* PCI_NETBSD_CONFIGURE */
    322 
    323 	/* initialize bus space tag */
    324 	sspci_io_tag = *iot;
    325 	sspci_io_tag.bs_cookie = (void *) S3C2800_PCI_IOSPACE_BASE;
    326 	sspci_io_tag.bs_map = sspci_bs_map;
    327 	sspci_mem_tag = *iot;
    328 	sspci_mem_tag.bs_cookie = (void *) S3C2800_PCI_MEMSPACE_BASE;
    329 	sspci_mem_tag.bs_map = sspci_bs_map;
    330 
    331 
    332 	/* Platform provides PCI DMA tag */
    333 	pci_dma_tag = s3c2800_pci_dma_init();
    334 
    335 	pci_pba.pba_busname = "pci";
    336 	pci_pba.pba_pc = &sspci_chipset;
    337 	pci_pba.pba_iot = &sspci_io_tag;
    338 	pci_pba.pba_memt = &sspci_mem_tag;
    339 	pci_pba.pba_dmat = pci_dma_tag;
    340 	pci_pba.pba_dmat64 = NULL;
    341 	pci_pba.pba_flags = PCI_FLAGS_IO_ENABLED | PCI_FLAGS_MEM_ENABLED;
    342 	pci_pba.pba_bus = 0;
    343 	pci_pba.pba_bridgetag = NULL;
    344 
    345 	config_found(self, &pci_pba, sspci_print);
    346 
    347 	return;
    348 
    349 #undef FAIL
    350 abort:
    351 	panic("%s: map failed (%s)",
    352 	    self->dv_xname, error_on);
    353 }
    354 
    355 
    356 static int
    357 sspci_bs_map(void *t, bus_addr_t bpa, bus_size_t size, int flag,
    358 	     bus_space_handle_t * bshp)
    359 {
    360 	bus_addr_t startpa, endpa;
    361 	vaddr_t va;
    362 
    363 #ifdef PCI_DEBUG
    364 	printf("sspci_bs_map: t=%p, addr=%lx, size=%lx, flag=%d\n",
    365 	    t, bpa, size, flag);
    366 #endif
    367 
    368 	/* Round the allocation to page boundries */
    369 	startpa = trunc_page(bpa);
    370 	endpa = round_page(bpa + size);
    371 
    372 	/* Get some VM.  */
    373 	va = uvm_km_valloc(kernel_map, endpa - startpa);
    374 	if (va == 0)
    375 		return ENOMEM;
    376 
    377 	/* Store the bus space handle */
    378 	*bshp = va + (bpa & PGOFSET);
    379 
    380 	/* Now map the pages */
    381 	/* The cookie is the physical base address for PCI I/O or memory area */
    382 	while (startpa < endpa) {
    383 		/* XXX pmap_kenter_pa maps pages cacheable -- not what we
    384 		 * want.  */
    385 		pmap_enter(pmap_kernel(), va, (bus_addr_t) t + startpa,
    386 		    VM_PROT_READ | VM_PROT_WRITE, 0);
    387 		va += PAGE_SIZE;
    388 		startpa += PAGE_SIZE;
    389 	}
    390 	pmap_update(pmap_kernel());
    391 
    392 	return 0;
    393 }
    394 
    395 
    396 
    397 void
    398 pci_conf_interrupt(pci_chipset_tag_t pc, int bus, int dev, int func,
    399 		   int swiz, int *iline)
    400 {
    401 #ifdef PCI_DEBUG
    402 	printf("pci_conf_interrupt(pc(%lx), bus(%d), dev(%d), func(%d), swiz(%d), *iline(%p)\n", (unsigned long) pc, bus, dev, func, swiz, iline);
    403 #endif
    404 	if (bus == 0) {
    405 		*iline = dev;
    406 	} else {
    407 		panic("pci_conf_interrupt: bus=%d: not yet implemented", bus);
    408 	}
    409 }
    410 
    411 void
    412 s3c2800_pci_attach_hook(struct device * parent, struct device * self,
    413 			struct pcibus_attach_args * pba)
    414 {
    415 
    416 	/* Nothing to do. */
    417 #ifdef PCI_DEBUG
    418 	printf("s3c2800_pci_attach_hook()\n");
    419 #endif
    420 }
    421 
    422 int
    423 s3c2800_pci_bus_maxdevs(void *v, int busno)
    424 {
    425 
    426 #ifdef PCI_DEBUG
    427 	printf("s3c2800_pci_bus_maxdevs(v=%p, busno=%d)\n", v, busno);
    428 #endif
    429 	return (32);
    430 }
    431 pcitag_t
    432 s3c2800_pci_make_tag(void *v, int bus, int device, int function)
    433 {
    434 
    435 	return ((bus << BUSNO_SHIFT) | (device << DEVNO_SHIFT) |
    436 	    (function << FUNNO_SHIFT));
    437 }
    438 
    439 void
    440 s3c2800_pci_decompose_tag(void *v, pcitag_t tag, int *bp, int *dp, int *fp)
    441 {
    442 	if (bp != NULL)
    443 		*bp = (tag >> BUSNO_SHIFT) & 0xff;
    444 	if (dp != NULL)
    445 		*dp = (tag >> DEVNO_SHIFT) & 0x1f;
    446 	if (fp != NULL)
    447 		*fp = (tag >> FUNNO_SHIFT) & 0x7;
    448 }
    449 
    450 static vaddr_t
    451 make_pci_conf_va(struct sspci_softc * sc, pcitag_t tag, int offset)
    452 {
    453 	if ((tag & BUSNO_MASK) == 0) {
    454 		/* configuration type 0 */
    455 		int devno = tag_to_devno(tag);
    456 
    457 		if (devno < BUS0_DEV_MIN || BUS0_DEV_MAX < devno)
    458 			return 0;
    459 
    460 		return (vaddr_t) bus_space_vaddr(sc->sc_iot, sc->sc_conf0_ioh) +
    461 		    (tag & (DEVNO_MASK | FUNNO_MASK)) + offset;
    462 	} else {
    463 		/* XXX */
    464 		return (vaddr_t) - 1;	/* cause fault */
    465 	}
    466 }
    467 
    468 
    469 pcireg_t
    470 s3c2800_pci_conf_read(void *v, pcitag_t tag, int offset)
    471 {
    472 	struct sspci_softc *sc = v;
    473 	vaddr_t va = make_pci_conf_va(sc, tag, offset);
    474 	int s;
    475 	pcireg_t rv;
    476 
    477 #ifdef PCI_DEBUG
    478 	printf("s3c2800_pci_conf_read: base=%lx tag=%lx offset=%x\n",
    479 	    sc->sc_conf0_ioh, tag, offset);
    480 #endif
    481 	if (va == 0)
    482 		return -1;
    483 
    484 	PCI_CONF_LOCK(s);
    485 
    486 	if (badaddr_read((void *) va, sizeof(rv), &rv)) {
    487 #if PCI_DEBUG
    488 		printf("conf_read: %lx bad address\n", va);
    489 #endif
    490 		rv = (pcireg_t) - 1;
    491 	}
    492 	PCI_CONF_UNLOCK(s);
    493 
    494 	return rv;
    495 }
    496 
    497 void
    498 s3c2800_pci_conf_write(void *v, pcitag_t tag, int offset, pcireg_t val)
    499 {
    500 	struct sspci_softc *sc = v;
    501 	vaddr_t va = make_pci_conf_va(sc, tag, offset);
    502 	u_int s;
    503 
    504 #ifdef PCI_DEBUG
    505 	printf("s3c2800_pci_conf_write: tag=%lx offset=%x -> va=%lx\n", tag, offset, va);
    506 #endif
    507 
    508 	PCI_CONF_LOCK(s);
    509 
    510 	*(pcireg_t *) va = val;
    511 
    512 	PCI_CONF_UNLOCK(s);
    513 }
    514 
    515 void *
    516 s3c2800_pci_intr_establish(void *pcv, pci_intr_handle_t ih, int level,
    517 			   int (*func) (void *), void *arg)
    518 {
    519 	struct sspci_softc *sc = pcv;
    520 	struct sspci_irq_handler *handler;
    521 	int s;
    522 
    523 #ifdef PCI_DEBUG
    524 	printf("s3c2800_pci_intr_establish(pcv=%p, ih=0x%lx, level=%d, "
    525 	    "func=%p, arg=%p)\n", pcv, ih, level, func, arg);
    526 #endif
    527 
    528 	handler = malloc(sizeof *handler, M_DEVBUF, cold ? M_NOWAIT : M_WAITOK);
    529 	if (handler == NULL)
    530 		panic("sspci_intr_establish: can't malloc handler info");
    531 
    532 	handler->func = func;
    533 	handler->arg = arg;
    534 	handler->level = level;
    535 
    536 	s = splhigh();
    537 	SLIST_INSERT_HEAD(&sc->sc_irq_handlers, handler, link);
    538 	splx(s);
    539 
    540 	return (handler);
    541 }
    542 
    543 void
    544 s3c2800_pci_intr_disestablish(void *pcv, void *cookie)
    545 {
    546 	struct sspci_softc *sc = pcv;
    547 	struct sspci_irq_handler *ih = cookie;
    548 	int s;
    549 
    550 #ifdef PCI_DEBUG
    551 	printf("s3c2800_pci_intr_disestablish(pcv=%p, cookie=%p)\n",
    552 	    pcv, cookie);
    553 #endif
    554 
    555 	s = splhigh();
    556 	SLIST_REMOVE(&sc->sc_irq_handlers, ih, sspci_irq_handler, link);
    557 	splx(s);
    558 }
    559 
    560 int
    561 s3c2800_pci_intr_map(struct pci_attach_args * pa, pci_intr_handle_t * ihp)
    562 {
    563 #ifdef PCI_DEBUG
    564 	int pin = pa->pa_intrpin;
    565 	void *pcv = pa->pa_pc;
    566 	pcitag_t intrtag = pa->pa_intrtag;
    567 	int bus, device, function;
    568 
    569 	s3c2800_pci_decompose_tag(pcv, intrtag, &bus, &device, &function);
    570 	printf("s3c2800_pci_intr_map: pcv=%p, tag=%08lx pin=%d dev=%d\n",
    571 	    pcv, intrtag, pin, device);
    572 #endif
    573 
    574 
    575 	/* S3C2800 has only one interrupt line for PCI */
    576 	*ihp = 0;
    577 	return 0;
    578 }
    579 
    580 const char *
    581 s3c2800_pci_intr_string(void *pcv, pci_intr_handle_t ih)
    582 {
    583 	/* We have only one interrupt source from PCI */
    584 	return "pciint";
    585 }
    586 
    587 const struct evcnt *
    588 s3c2800_pci_intr_evcnt(void *pcv, pci_intr_handle_t ih)
    589 {
    590 
    591 	/* XXX for now, no evcnt parent reported */
    592 	return NULL;
    593 }
    594 /*
    595  * Initialize PCI controller
    596  */
    597 int
    598 sspci_init_controller(struct sspci_softc * sc)
    599 {
    600 	bus_space_tag_t iot = sc->sc_iot;
    601 	bus_space_handle_t ioh = sc->sc_reg_ioh;
    602 
    603 	/* disable PCI command */
    604 	bus_space_write_4(iot, ioh, PCI_COMMAND_STATUS_REG,
    605 	    0xffff0000);
    606 
    607 	/* latency=0x10, cacheline=8 */
    608 	bus_space_write_4(iot, ioh, PCI_BHLC_REG,
    609 	    PCI_BHLC_CODE(0, 0, 0, 0x10, 8));
    610 
    611 	bus_space_write_4(iot, ioh, PCI_INTERRUPT_REG,
    612 	    PCI_INTERRUPT_CODE(0, 0, 0, 0));
    613 
    614 
    615 
    616 #if 1
    617 	bus_space_write_4(iot, ioh, PCI_MAPREG_START,
    618 	    PCI_MAPREG_MEM_TYPE_32BIT | 0x80000000);
    619 	/* Cover all DBANKs with BAR0 */
    620 	bus_space_write_4(iot, ioh, PCICTL_PCIBAM0, 0xf8000000);
    621 	bus_space_write_4(iot, ioh, PCICTL_PCIBATPA0, S3C2800_DBANK0_START);
    622 #else
    623 	bus_space_write_4(iot, ioh, PCI_MAPREG_START,
    624 	    PCI_MAPREG_MEM_TYPE_32BIT | 0xf0000000);
    625 	bus_space_write_4(iot, ioh, PCI_MAPREG_START + 4,
    626 	    PCI_MAPREG_MEM_TYPE_32BIT | 0x80000000);
    627 
    628 	bus_space_write_4(iot, ioh, PCICTL_PCIBAM0, 0xffff0000);
    629 	bus_space_write_4(iot, ioh, PCICTL_PCIBATPA0, 0xffff0000);
    630 	bus_space_write_4(iot, ioh, PCICTL_PCIBAM1, 0xf1000000);
    631 	bus_space_write_4(iot, ioh, PCICTL_PCIBATPA1, S3C2800_DBANK0_START);
    632 #endif
    633 
    634 	bus_space_write_4(iot, ioh, PCI_COMMAND_STATUS_REG,
    635 	    PCI_STATUS_PARITY_DETECT |
    636 	    PCI_STATUS_SPECIAL_ERROR |
    637 	    PCI_STATUS_MASTER_ABORT |
    638 	    PCI_STATUS_MASTER_TARGET_ABORT |
    639 	    PCI_STATUS_TARGET_TARGET_ABORT |
    640 	    PCI_STATUS_DEVSEL_MEDIUM |
    641 	    PCI_STATUS_PARITY_ERROR |
    642 	    PCI_STATUS_BACKTOBACK_SUPPORT |
    643 	    PCI_STATUS_CAPLIST_SUPPORT |
    644 	    PCI_COMMAND_MASTER_ENABLE |
    645 	    PCI_COMMAND_MEM_ENABLE |
    646 	    PCI_COMMAND_IO_ENABLE);
    647 
    648 	bus_space_write_4(iot, ioh, PCICTL_PCICON,
    649 	    PCICON_ARB | PCICON_HST);
    650 
    651 	bus_space_write_4(iot, ioh, PCICTL_PCISET, 0);
    652 	/* clear all interrupts */
    653 	bus_space_write_4(iot, ioh, PCICTL_PCIINTST, 0xffffffff);
    654 	bus_space_write_4(iot, ioh, PCICTL_PCIINTEN, 0);
    655 
    656 	bus_space_write_4(iot, ioh, PCICTL_PCICON,
    657 	    PCICON_RDY | PCICON_CFD | PCICON_ATS |
    658 	    PCICON_ARB | PCICON_HST);
    659 
    660 
    661 #ifdef PCI_DEBUG
    662 	{
    663 		pcireg_t reg;
    664 		int i;
    665 
    666 		for (i = 0; i <= 0x40; i += sizeof(pcireg_t)) {
    667 			reg = bus_space_read_4(iot, ioh, i);
    668 			printf("%03x: %08x\n", i, reg);
    669 		}
    670 		for (i = 0x100; i <= 0x154; i += sizeof(pcireg_t)) {
    671 			reg = bus_space_read_4(iot, ioh, i);
    672 			printf("%03x: %08x\n", i, reg);
    673 		}
    674 	}
    675 #endif
    676 	return 0;
    677 }
    678 
    679 
    680 static const char *pci_abnormal_error_name[] = {
    681 	"PCI reset deasserted",
    682 	"PCI reset asserted",
    683 	"PCI master detected fatal error",
    684 	"PCI master detected parity error",
    685 	"PCI target detected parity error",
    686 	"PCI SERR# asserted",
    687 };
    688 
    689 static int
    690 sspci_intr(void *arg)
    691 {
    692 	struct sspci_softc *sc = arg;
    693 	int s;
    694 	bus_space_tag_t iot = sc->sc_iot;
    695 	bus_space_handle_t ioh = sc->sc_reg_ioh;
    696 	uint32_t interrupts, errors;
    697 
    698 	interrupts = bus_space_read_4(iot, ioh, PCICTL_PCIINTST);
    699 
    700 	if (interrupts & PCIINT_INA) {
    701 		s = splhigh();
    702 		softintr_schedule(sc->sc_softinterrupt);
    703 
    704 		/* mask INTA itnerrupt until softinterrupt is handled */
    705 		sc->sc_pciinten &= ~PCIINT_INA;
    706 		bus_space_write_4(iot, ioh, PCICTL_PCIINTEN,
    707 		    sc->sc_pciinten);
    708 
    709 		/* acknowledge INTA interrupt */
    710 		bus_space_write_4(iot, ioh, PCICTL_PCIINTST, PCIINT_INA);
    711 
    712 		splx(s);
    713 
    714 		interrupts &= ~PCIINT_INA;
    715 
    716 	}
    717 	errors = interrupts & (PCIINT_SER | PCIINT_TPE | PCIINT_MPE |
    718 	    PCIINT_MFE | PCIINT_PRA | PCIINT_PRD);
    719 	if (errors) {
    720 		int i;
    721 
    722 		for (i = 0; errors; ++i) {
    723 			if ((errors & (1 << i)) == 0)
    724 				continue;
    725 
    726 			printf("%s: %s\n", sc->sc_dev.dv_xname,
    727 			    pci_abnormal_error_name[i > 4 ? 5 : i]);
    728 
    729 			errors &= ~(1 << i);
    730 		}
    731 		/* acknowledge interrupts */
    732 		bus_space_write_4(iot, ioh, PCICTL_PCIINTST, interrupts);
    733 	}
    734 	return 0;
    735 }
    736 
    737 static void
    738 sspci_softintr(void *arg)
    739 {
    740 	struct sspci_softc *sc = arg;
    741 	struct sspci_irq_handler *ih;
    742 	int s;
    743 
    744 
    745 	SLIST_FOREACH(ih, &(sc->sc_irq_handlers), link) {
    746 		s = _splraise(ih->level);
    747 		ih->func(ih->arg);
    748 		splx(s);
    749 	}
    750 
    751 	/* unmask INTA interrupt */
    752 	s = splhigh();
    753 	sc->sc_pciinten |= PCIINT_INA;
    754 	bus_space_write_4(sc->sc_iot, sc->sc_reg_ioh, PCICTL_PCIINTEN,
    755 	    sc->sc_pciinten);
    756 	splx(s);
    757 }
    758