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