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