s3c2800_pci.c revision 1.1 1 /* $NetBSD: s3c2800_pci.c,v 1.1 2002/11/20 17:52:50 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 printf("%s at %s", pci_pba->pba_busname, pnp);
228 if (strcmp(pci_pba->pba_busname, "pci") == 0)
229 printf(" 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,
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", 0, S3C2800_PCI_IOSPACE_SIZE,
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
319 printf("pci_configure_bus done\n");
320 #endif /* PCI_NETBSD_CONFIGURE */
321
322 /* initialize bus space tag */
323 sspci_io_tag = *iot;
324 sspci_io_tag.bs_cookie = (void *) S3C2800_PCI_IOSPACE_BASE;
325 sspci_io_tag.bs_map = sspci_bs_map;
326 sspci_mem_tag = *iot;
327 sspci_mem_tag.bs_cookie = (void *) S3C2800_PCI_MEMSPACE_BASE;
328 sspci_mem_tag.bs_map = sspci_bs_map;
329
330
331 /* Platform provides PCI DMA tag */
332 pci_dma_tag = s3c2800_pci_dma_init();
333
334 pci_pba.pba_busname = "pci";
335 pci_pba.pba_pc = &sspci_chipset;
336 pci_pba.pba_iot = &sspci_io_tag;
337 pci_pba.pba_memt = &sspci_mem_tag;
338 pci_pba.pba_dmat = pci_dma_tag;
339 pci_pba.pba_flags = PCI_FLAGS_IO_ENABLED | PCI_FLAGS_MEM_ENABLED;
340 pci_pba.pba_bus = 0;
341 pci_pba.pba_bridgetag = NULL;
342
343 config_found(self, &pci_pba, sspci_print);
344
345 return;
346
347 #undef FAIL
348 abort:
349 panic("%s: map failed (%s)",
350 self->dv_xname, error_on);
351 }
352
353
354 static int
355 sspci_bs_map(void *t, bus_addr_t bpa, bus_size_t size, int flag,
356 bus_space_handle_t * bshp)
357 {
358 bus_addr_t startpa, endpa;
359 vaddr_t va;
360
361 #ifdef PCI_DEBUG
362 printf("sspci_bs_map: t=%p, addr=%lx, size=%lx, flag=%d\n",
363 t, bpa, size, flag);
364 #endif
365
366 /* Round the allocation to page boundries */
367 startpa = trunc_page(bpa);
368 endpa = round_page(bpa + size);
369
370 /* Get some VM. */
371 va = uvm_km_valloc(kernel_map, endpa - startpa);
372 if (va == 0)
373 return ENOMEM;
374
375 /* Store the bus space handle */
376 *bshp = va + (bpa & PGOFSET);
377
378 /* Now map the pages */
379 /* The cookie is the physical base address for PCI I/O or memory area */
380 while (startpa < endpa) {
381 /* XXX pmap_kenter_pa maps pages cacheable -- not what we
382 * want. */
383 pmap_enter(pmap_kernel(), va, (bus_addr_t) t + startpa,
384 VM_PROT_READ | VM_PROT_WRITE, 0);
385 va += NBPG;
386 startpa += NBPG;
387 }
388 pmap_update(pmap_kernel());
389
390 return 0;
391 }
392
393
394
395 void
396 pci_conf_interrupt(pci_chipset_tag_t pc, int bus, int dev, int func,
397 int swiz, int *iline)
398 {
399 #ifdef PCI_DEBUG
400 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);
401 #endif
402 if (bus == 0) {
403 *iline = dev;
404 } else {
405 panic("pci_conf_interrupt: bus=%d: not yet implemented", bus);
406 }
407 }
408
409 void
410 s3c2800_pci_attach_hook(struct device * parent, struct device * self,
411 struct pcibus_attach_args * pba)
412 {
413
414 /* Nothing to do. */
415 #ifdef PCI_DEBUG
416 printf("s3c2800_pci_attach_hook()\n");
417 #endif
418 }
419
420 int
421 s3c2800_pci_bus_maxdevs(void *v, int busno)
422 {
423
424 #ifdef PCI_DEBUG
425 printf("s3c2800_pci_bus_maxdevs(v=%p, busno=%d)\n", v, busno);
426 #endif
427 return (32);
428 }
429 pcitag_t
430 s3c2800_pci_make_tag(void *v, int bus, int device, int function)
431 {
432
433 return ((bus << BUSNO_SHIFT) | (device << DEVNO_SHIFT) |
434 (function << FUNNO_SHIFT));
435 }
436
437 void
438 s3c2800_pci_decompose_tag(void *v, pcitag_t tag, int *bp, int *dp, int *fp)
439 {
440 if (bp != NULL)
441 *bp = (tag >> BUSNO_SHIFT) & 0xff;
442 if (dp != NULL)
443 *dp = (tag >> DEVNO_SHIFT) & 0x1f;
444 if (fp != NULL)
445 *fp = (tag >> FUNNO_SHIFT) & 0x7;
446 }
447
448 static vaddr_t
449 make_pci_conf_va(struct sspci_softc * sc, pcitag_t tag, int offset)
450 {
451 if ((tag & BUSNO_MASK) == 0) {
452 /* configuration type 0 */
453 int devno = tag_to_devno(tag);
454
455 if (devno < BUS0_DEV_MIN || BUS0_DEV_MAX < devno)
456 return 0;
457
458 return (vaddr_t) bus_space_vaddr(sc->sc_iot, sc->sc_conf0_ioh) +
459 (tag & (DEVNO_MASK | FUNNO_MASK)) + offset;
460 } else {
461 /* XXX */
462 return (vaddr_t) - 1; /* cause fault */
463 }
464 }
465
466
467 pcireg_t
468 s3c2800_pci_conf_read(void *v, pcitag_t tag, int offset)
469 {
470 struct sspci_softc *sc = v;
471 vaddr_t va = make_pci_conf_va(sc, tag, offset);
472 int s;
473 pcireg_t rv;
474
475 #ifdef PCI_DEBUG
476 printf("s3c2800_pci_conf_read: base=%lx tag=%lx offset=%x\n",
477 sc->sc_conf0_ioh, tag, offset);
478 #endif
479 if (va == 0)
480 return -1;
481
482 PCI_CONF_LOCK(s);
483
484 if (badaddr_read((void *) va, sizeof(rv), &rv)) {
485 #if PCI_DEBUG
486 printf("conf_read: %lx bad address\n", va);
487 #endif
488 rv = (pcireg_t) - 1;
489 }
490 PCI_CONF_UNLOCK(s);
491
492 return rv;
493 }
494
495 void
496 s3c2800_pci_conf_write(void *v, pcitag_t tag, int offset, pcireg_t val)
497 {
498 struct sspci_softc *sc = v;
499 vaddr_t va = make_pci_conf_va(sc, tag, offset);
500 u_int s;
501
502 #ifdef PCI_DEBUG
503 printf("s3c2800_pci_conf_write: tag=%lx offset=%x -> va=%lx\n", tag, offset, va);
504 #endif
505
506 PCI_CONF_LOCK(s);
507
508 *(pcireg_t *) va = val;
509
510 PCI_CONF_UNLOCK(s);
511 }
512
513 void *
514 s3c2800_pci_intr_establish(void *pcv, pci_intr_handle_t ih, int level,
515 int (*func) (void *), void *arg)
516 {
517 struct sspci_softc *sc = pcv;
518 struct sspci_irq_handler *handler;
519 int s;
520
521 #ifdef PCI_DEBUG
522 printf("s3c2800_pci_intr_establish(pcv=%p, ih=0x%lx, level=%d, "
523 "func=%p, arg=%p)\n", pcv, ih, level, func, arg);
524 #endif
525
526 handler = malloc(sizeof *handler, M_DEVBUF, cold ? M_NOWAIT : M_WAITOK);
527 if (handler == NULL)
528 panic("sspci_intr_establish: can't malloc handler info");
529
530 handler->func = func;
531 handler->arg = arg;
532 handler->level = level;
533
534 s = splhigh();
535 SLIST_INSERT_HEAD(&sc->sc_irq_handlers, handler, link);
536 splx(s);
537
538 return (handler);
539 }
540
541 void
542 s3c2800_pci_intr_disestablish(void *pcv, void *cookie)
543 {
544 struct sspci_softc *sc = pcv;
545 struct sspci_irq_handler *ih = cookie;
546 int s;
547
548 #ifdef PCI_DEBUG
549 printf("s3c2800_pci_intr_disestablish(pcv=%p, cookie=%p)\n",
550 pcv, cookie);
551 #endif
552
553 s = splhigh();
554 SLIST_REMOVE(&sc->sc_irq_handlers, ih, sspci_irq_handler, link);
555 splx(s);
556 }
557
558 int
559 s3c2800_pci_intr_map(struct pci_attach_args * pa, pci_intr_handle_t * ihp)
560 {
561 #ifdef PCI_DEBUG
562 int pin = pa->pa_intrpin;
563 void *pcv = pa->pa_pc;
564 pcitag_t intrtag = pa->pa_intrtag;
565 int bus, device, function;
566
567 s3c2800_pci_decompose_tag(pcv, intrtag, &bus, &device, &function);
568 printf("s3c2800_pci_intr_map: pcv=%p, tag=%08lx pin=%d dev=%d\n",
569 pcv, intrtag, pin, device);
570 #endif
571
572
573 /* S3C2800 has only one interrupt line for PCI */
574 *ihp = 0;
575 return 0;
576 }
577
578 const char *
579 s3c2800_pci_intr_string(void *pcv, pci_intr_handle_t ih)
580 {
581 /* We have only one interrupt source from PCI */
582 return "pciint";
583 }
584
585 const struct evcnt *
586 s3c2800_pci_intr_evcnt(void *pcv, pci_intr_handle_t ih)
587 {
588
589 /* XXX for now, no evcnt parent reported */
590 return NULL;
591 }
592 /*
593 * Initialize PCI controller
594 */
595 int
596 sspci_init_controller(struct sspci_softc * sc)
597 {
598 bus_space_tag_t iot = sc->sc_iot;
599 bus_space_handle_t ioh = sc->sc_reg_ioh;
600
601 /* disable PCI command */
602 bus_space_write_4(iot, ioh, PCI_COMMAND_STATUS_REG,
603 0xffff0000);
604
605 /* latency=0x10, cacheline=8 */
606 bus_space_write_4(iot, ioh, PCI_BHLC_REG,
607 PCI_BHLC_CODE(0, 0, 0, 0x10, 8));
608
609 bus_space_write_4(iot, ioh, PCI_INTERRUPT_REG,
610 PCI_INTERRUPT_CODE(0, 0, 0, 0));
611
612
613
614 #if 1
615 bus_space_write_4(iot, ioh, PCI_MAPREG_START,
616 PCI_MAPREG_MEM_TYPE_32BIT | 0x80000000);
617 /* Cover all DBANKs with BAR0 */
618 bus_space_write_4(iot, ioh, PCICTL_PCIBAM0, 0xf8000000);
619 bus_space_write_4(iot, ioh, PCICTL_PCIBATPA0, S3C2800_DBANK0_START);
620 #else
621 bus_space_write_4(iot, ioh, PCI_MAPREG_START,
622 PCI_MAPREG_MEM_TYPE_32BIT | 0xf0000000);
623 bus_space_write_4(iot, ioh, PCI_MAPREG_START + 4,
624 PCI_MAPREG_MEM_TYPE_32BIT | 0x80000000);
625
626 bus_space_write_4(iot, ioh, PCICTL_PCIBAM0, 0xffff0000);
627 bus_space_write_4(iot, ioh, PCICTL_PCIBATPA0, 0xffff0000);
628 bus_space_write_4(iot, ioh, PCICTL_PCIBAM1, 0xf1000000);
629 bus_space_write_4(iot, ioh, PCICTL_PCIBATPA1, S3C2800_DBANK0_START);
630 #endif
631
632 bus_space_write_4(iot, ioh, PCI_COMMAND_STATUS_REG,
633 PCI_STATUS_PARITY_DETECT |
634 PCI_STATUS_SPECIAL_ERROR |
635 PCI_STATUS_MASTER_ABORT |
636 PCI_STATUS_MASTER_TARGET_ABORT |
637 PCI_STATUS_TARGET_TARGET_ABORT |
638 PCI_STATUS_DEVSEL_MEDIUM |
639 PCI_STATUS_PARITY_ERROR |
640 PCI_STATUS_BACKTOBACK_SUPPORT |
641 PCI_STATUS_CAPLIST_SUPPORT |
642 PCI_COMMAND_MASTER_ENABLE |
643 PCI_COMMAND_MEM_ENABLE |
644 PCI_COMMAND_IO_ENABLE);
645
646 bus_space_write_4(iot, ioh, PCICTL_PCICON,
647 PCICON_ARB | PCICON_HST);
648
649 bus_space_write_4(iot, ioh, PCICTL_PCISET, 0);
650 /* clear all interrupts */
651 bus_space_write_4(iot, ioh, PCICTL_PCIINTST, 0xffffffff);
652 bus_space_write_4(iot, ioh, PCICTL_PCIINTEN, 0);
653
654 bus_space_write_4(iot, ioh, PCICTL_PCICON,
655 PCICON_RDY | PCICON_CFD | PCICON_ATS |
656 PCICON_ARB | PCICON_HST);
657
658
659 #ifdef PCI_DEBUG
660 {
661 pcireg_t reg;
662 int i;
663
664 for (i = 0; i <= 0x40; i += sizeof(pcireg_t)) {
665 reg = bus_space_read_4(iot, ioh, i);
666 printf("%03x: %08x\n", i, reg);
667 }
668 for (i = 0x100; i <= 0x154; i += sizeof(pcireg_t)) {
669 reg = bus_space_read_4(iot, ioh, i);
670 printf("%03x: %08x\n", i, reg);
671 }
672 }
673 #endif
674 return 0;
675 }
676
677
678 static const char *pci_abnormal_error_name[] = {
679 "PCI reset deasserted",
680 "PCI reset asserted",
681 "PCI master detected fatal error",
682 "PCI master detected parity error",
683 "PCI target detected parity error",
684 "PCI SERR# asserted",
685 };
686
687 static int
688 sspci_intr(void *arg)
689 {
690 struct sspci_softc *sc = arg;
691 int s;
692 bus_space_tag_t iot = sc->sc_iot;
693 bus_space_handle_t ioh = sc->sc_reg_ioh;
694 uint32_t interrupts, errors;
695
696 interrupts = bus_space_read_4(iot, ioh, PCICTL_PCIINTST);
697
698 if (interrupts & PCIINT_INA) {
699 s = splhigh();
700 softintr_schedule(sc->sc_softinterrupt);
701
702 /* mask INTA itnerrupt until softinterrupt is handled */
703 sc->sc_pciinten &= ~PCIINT_INA;
704 bus_space_write_4(iot, ioh, PCICTL_PCIINTEN,
705 sc->sc_pciinten);
706
707 /* acknowledge INTA interrupt */
708 bus_space_write_4(iot, ioh, PCICTL_PCIINTST, PCIINT_INA);
709
710 splx(s);
711
712 interrupts &= ~PCIINT_INA;
713
714 }
715 errors = interrupts & (PCIINT_SER | PCIINT_TPE | PCIINT_MPE |
716 PCIINT_MFE | PCIINT_PRA | PCIINT_PRD);
717 if (errors) {
718 int i;
719
720 for (i = 0; errors; ++i) {
721 if ((errors & (1 << i)) == 0)
722 continue;
723
724 printf("%s: %s\n", sc->sc_dev.dv_xname,
725 pci_abnormal_error_name[i > 4 ? 5 : i]);
726
727 errors &= ~(1 << i);
728 }
729 /* acknowledge interrupts */
730 bus_space_write_4(iot, ioh, PCICTL_PCIINTST, interrupts);
731 }
732 return 0;
733 }
734
735 static void
736 sspci_softintr(void *arg)
737 {
738 struct sspci_softc *sc = arg;
739 struct sspci_irq_handler *ih;
740 int s;
741
742
743 SLIST_FOREACH(ih, &(sc->sc_irq_handlers), link) {
744 s = _splraise(ih->level);
745 ih->func(ih->arg);
746 splx(s);
747 }
748
749 /* unmask INTA interrupt */
750 s = splhigh();
751 sc->sc_pciinten |= PCIINT_INA;
752 bus_space_write_4(sc->sc_iot, sc->sc_reg_ioh, PCICTL_PCIINTEN,
753 sc->sc_pciinten);
754 splx(s);
755 }
756