1 /* $NetBSD: gt_pci.c,v 1.4 2003/07/15 00:24:54 lukem Exp $ */
4 * Copyright (c) 2001, 2002 Wasabi Systems, Inc.
7 * Written by Jason R. Thorpe for Wasabi Systems, Inc.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed for the NetBSD Project by
20 * Wasabi Systems, Inc.
21 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 * or promote products derived from this software without specific prior
25 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
39 * PCI configuration support for gt I/O Processor chip.
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD$");
45 #include <sys/param.h>
46 #include <sys/systm.h>
49 #include <sys/interrupt.h>
50 #include <sys/malloc.h>
51 #include <sys/kernel.h>
52 #include <sys/module.h>
57 #include <vm/vm_extern.h>
59 #include <machine/bus.h>
60 #include <machine/cpu.h>
61 #include <machine/pmap.h>
63 #include <mips/malta/maltareg.h>
65 #include <mips/malta/gtreg.h>
66 #include <mips/malta/gtvar.h>
68 #include <isa/isareg.h>
69 #include <dev/ic/i8259.h>
71 #include <dev/pci/pcireg.h>
72 #include <dev/pci/pcivar.h>
74 #include <dev/pci/pcib_private.h>
78 #define ICU_LEN 16 /* number of ISA IRQs */
81 * XXX: These defines are from NetBSD's <dev/ic/i8259reg.h>. Respective file
82 * from FreeBSD src tree <dev/ic/i8259.h> lacks some definitions.
89 #define OCW2_ILS(x) ((x) << 0) /* interrupt level select */
91 #define OCW3_POLL_IRQ(x) ((x) & 0x7f)
92 #define OCW3_POLL_PENDING (1U << 7)
96 struct gt_pci_intr_cookie {
98 struct gt_pci_softc *sc;
101 struct gt_pci_softc {
103 bus_space_tag_t sc_st;
104 bus_space_handle_t sc_ioh_icu1;
105 bus_space_handle_t sc_ioh_icu2;
106 bus_space_handle_t sc_ioh_elcr;
109 struct rman sc_mem_rman;
110 struct rman sc_io_rman;
111 struct rman sc_irq_rman;
112 unsigned long sc_mem;
113 bus_space_handle_t sc_io;
115 struct resource *sc_irq;
116 struct intr_event *sc_eventstab[ICU_LEN];
117 struct gt_pci_intr_cookie sc_intr_cookies[ICU_LEN];
121 uint16_t sc_reserved;
126 static void gt_pci_set_icus(struct gt_pci_softc *);
127 static int gt_pci_intr(void *v);
128 static int gt_pci_probe(device_t);
129 static int gt_pci_attach(device_t);
130 static int gt_pci_activate_resource(device_t, device_t, int, int,
132 static int gt_pci_setup_intr(device_t, device_t, struct resource *,
133 int, driver_filter_t *, driver_intr_t *, void *, void **);
134 static int gt_pci_teardown_intr(device_t, device_t, struct resource *, void*);
135 static int gt_pci_maxslots(device_t );
136 static int gt_pci_conf_setup(struct gt_pci_softc *, int, int, int, int,
138 static uint32_t gt_pci_read_config(device_t, u_int, u_int, u_int, u_int, int);
139 static void gt_pci_write_config(device_t, u_int, u_int, u_int, u_int,
141 static int gt_pci_route_interrupt(device_t pcib, device_t dev, int pin);
142 static struct resource * gt_pci_alloc_resource(device_t, device_t, int,
143 int *, u_long, u_long, u_long, u_int);
146 gt_pci_mask_irq(void *source)
148 struct gt_pci_intr_cookie *cookie = source;
149 struct gt_pci_softc *sc = cookie->sc;
150 int irq = cookie->irq;
152 sc->sc_imask |= (1 << irq);
153 sc->sc_elcr |= (1 << irq);
159 gt_pci_unmask_irq(void *source)
161 struct gt_pci_intr_cookie *cookie = source;
162 struct gt_pci_softc *sc = cookie->sc;
163 int irq = cookie->irq;
165 /* Enable it, set trigger mode. */
166 sc->sc_imask &= ~(1 << irq);
167 sc->sc_elcr &= ~(1 << irq);
173 gt_pci_set_icus(struct gt_pci_softc *sc)
175 /* Enable the cascade IRQ (2) if 8-15 is enabled. */
176 if ((sc->sc_imask & 0xff00) != 0xff00)
177 sc->sc_imask &= ~(1U << 2);
179 sc->sc_imask |= (1U << 2);
181 bus_space_write_1(sc->sc_st, sc->sc_ioh_icu1, PIC_OCW1,
182 sc->sc_imask & 0xff);
183 bus_space_write_1(sc->sc_st, sc->sc_ioh_icu2, PIC_OCW1,
184 (sc->sc_imask >> 8) & 0xff);
186 bus_space_write_1(sc->sc_st, sc->sc_ioh_elcr, 0,
188 bus_space_write_1(sc->sc_st, sc->sc_ioh_elcr, 1,
189 (sc->sc_elcr >> 8) & 0xff);
195 struct gt_pci_softc *sc = v;
196 struct intr_event *event;
200 bus_space_write_1(sc->sc_st, sc->sc_ioh_icu1, PIC_OCW3,
202 irq = bus_space_read_1(sc->sc_st, sc->sc_ioh_icu1, PIC_OCW3);
203 if ((irq & OCW3_POLL_PENDING) == 0)
205 return FILTER_HANDLED;
208 irq = OCW3_POLL_IRQ(irq);
211 bus_space_write_1(sc->sc_st, sc->sc_ioh_icu2,
212 PIC_OCW3, OCW3_SEL | OCW3_P);
213 irq = bus_space_read_1(sc->sc_st, sc->sc_ioh_icu2,
215 if (irq & OCW3_POLL_PENDING)
216 irq = OCW3_POLL_IRQ(irq) + 8;
221 event = sc->sc_eventstab[irq];
223 if (!event || TAILQ_EMPTY(&event->ie_handlers))
226 /* TODO: frame instead of NULL? */
227 intr_event_handle(event, NULL);
228 /* XXX: Log stray IRQs */
230 /* Send a specific EOI to the 8259. */
232 bus_space_write_1(sc->sc_st, sc->sc_ioh_icu2,
233 PIC_OCW2, OCW2_SELECT | OCW2_EOI | OCW2_SL |
238 bus_space_write_1(sc->sc_st, sc->sc_ioh_icu1, PIC_OCW2,
239 OCW2_SELECT | OCW2_EOI | OCW2_SL | OCW2_ILS(irq));
242 return FILTER_HANDLED;
246 gt_pci_probe(device_t dev)
248 device_set_desc(dev, "GT64120 PCI bridge");
253 gt_pci_attach(device_t dev)
257 struct gt_pci_softc *sc = device_get_softc(dev);
262 sc->sc_busno = busno;
263 sc->sc_st = mips_bus_space_generic;
265 /* Use KSEG1 to access IO ports for it is uncached */
266 sc->sc_io = MIPS_PHYS_TO_KSEG1(MALTA_PCI0_IO_BASE);
267 sc->sc_io_rman.rm_type = RMAN_ARRAY;
268 sc->sc_io_rman.rm_descr = "GT64120 PCI I/O Ports";
269 if (rman_init(&sc->sc_io_rman) != 0 ||
270 rman_manage_region(&sc->sc_io_rman, 0, 0xffff) != 0) {
271 panic("gt_pci_attach: failed to set up I/O rman");
274 /* Use KSEG1 to access PCI memory for it is uncached */
275 sc->sc_mem = MIPS_PHYS_TO_KSEG1(MALTA_PCIMEM1_BASE);
276 sc->sc_mem_rman.rm_type = RMAN_ARRAY;
277 sc->sc_mem_rman.rm_descr = "GT64120 PCI Memory";
278 if (rman_init(&sc->sc_mem_rman) != 0 ||
279 rman_manage_region(&sc->sc_mem_rman,
280 sc->sc_mem, sc->sc_mem + MALTA_PCIMEM1_SIZE) != 0) {
281 panic("gt_pci_attach: failed to set up memory rman");
283 sc->sc_irq_rman.rm_type = RMAN_ARRAY;
284 sc->sc_irq_rman.rm_descr = "GT64120 PCI IRQs";
285 if (rman_init(&sc->sc_irq_rman) != 0 ||
286 rman_manage_region(&sc->sc_irq_rman, 1, 31) != 0)
287 panic("gt_pci_attach: failed to set up IRQ rman");
290 * Map the PIC/ELCR registers.
293 if (bus_space_map(sc->sc_st, 0x4d0, 2, 0, &sc->sc_ioh_elcr) != 0)
294 device_printf(dev, "unable to map ELCR registers\n");
295 if (bus_space_map(sc->sc_st, IO_ICU1, 2, 0, &sc->sc_ioh_icu1) != 0)
296 device_printf(dev, "unable to map ICU1 registers\n");
297 if (bus_space_map(sc->sc_st, IO_ICU2, 2, 0, &sc->sc_ioh_icu2) != 0)
298 device_printf(dev, "unable to map ICU2 registers\n");
300 sc->sc_ioh_elcr = sc->sc_io + 0x4d0;
301 sc->sc_ioh_icu1 = sc->sc_io + IO_ICU1;
302 sc->sc_ioh_icu2 = sc->sc_io + IO_ICU2;
306 /* All interrupts default to "masked off". */
307 sc->sc_imask = 0xffff;
309 /* All interrupts default to edge-triggered. */
313 * Initialize the 8259s.
315 /* reset, program device, 4 bytes */
316 bus_space_write_1(sc->sc_st, sc->sc_ioh_icu1, 0,
317 ICW1_RESET | ICW1_IC4);
319 * XXX: values from NetBSD's <dev/ic/i8259reg.h>
321 bus_space_write_1(sc->sc_st, sc->sc_ioh_icu1, 1,
323 bus_space_write_1(sc->sc_st, sc->sc_ioh_icu1, 1,
325 bus_space_write_1(sc->sc_st, sc->sc_ioh_icu1, 1,
328 /* mask all interrupts */
329 bus_space_write_1(sc->sc_st, sc->sc_ioh_icu1, 1,
330 sc->sc_imask & 0xff);
332 /* enable special mask mode */
333 bus_space_write_1(sc->sc_st, sc->sc_ioh_icu1, 0,
334 OCW3_SEL | OCW3_ESMM | OCW3_SMM);
336 /* read IRR by default */
337 bus_space_write_1(sc->sc_st, sc->sc_ioh_icu1, 0,
340 /* reset, program device, 4 bytes */
341 bus_space_write_1(sc->sc_st, sc->sc_ioh_icu2, 0,
342 ICW1_RESET | ICW1_IC4);
343 bus_space_write_1(sc->sc_st, sc->sc_ioh_icu2, 1,
345 bus_space_write_1(sc->sc_st, sc->sc_ioh_icu2, 1,
347 bus_space_write_1(sc->sc_st, sc->sc_ioh_icu2, 1,
350 /* mask all interrupts */
351 bus_space_write_1(sc->sc_st, sc->sc_ioh_icu2, 1,
352 sc->sc_imask & 0xff);
354 /* enable special mask mode */
355 bus_space_write_1(sc->sc_st, sc->sc_ioh_icu2, 0,
356 OCW3_SEL | OCW3_ESMM | OCW3_SMM);
358 /* read IRR by default */
359 bus_space_write_1(sc->sc_st, sc->sc_ioh_icu2, 0,
363 * Default all interrupts to edge-triggered.
365 bus_space_write_1(sc->sc_st, sc->sc_ioh_elcr, 0,
367 bus_space_write_1(sc->sc_st, sc->sc_ioh_elcr, 1,
368 (sc->sc_elcr >> 8) & 0xff);
371 * Some ISA interrupts are reserved for devices that
372 * we know are hard-wired to certain IRQs.
375 (1U << 0) | /* timer */
376 (1U << 1) | /* keyboard controller (keyboard) */
377 (1U << 2) | /* PIC cascade */
378 (1U << 3) | /* COM 2 */
379 (1U << 4) | /* COM 1 */
380 (1U << 6) | /* floppy */
381 (1U << 7) | /* centronics */
382 (1U << 8) | /* RTC */
383 (1U << 9) | /* I2C */
384 (1U << 12) | /* keyboard controller (mouse) */
385 (1U << 14) | /* IDE primary */
386 (1U << 15); /* IDE secondary */
388 /* Hook up our interrupt handler. */
389 if ((sc->sc_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid,
390 MALTA_SOUTHBRIDGE_INTR, MALTA_SOUTHBRIDGE_INTR, 1,
391 RF_SHAREABLE | RF_ACTIVE)) == NULL) {
392 device_printf(dev, "unable to allocate IRQ resource\n");
396 if ((bus_setup_intr(dev, sc->sc_irq, INTR_TYPE_MISC,
397 gt_pci_intr, NULL, sc, &sc->sc_ih))) {
399 "WARNING: unable to register interrupt handler\n");
403 /* Initialize memory and i/o rmans. */
404 device_add_child(dev, "pci", busno);
405 return (bus_generic_attach(dev));
409 gt_pci_maxslots(device_t dev)
411 return (PCI_SLOTMAX);
415 gt_pci_conf_setup(struct gt_pci_softc *sc, int bus, int slot, int func,
416 int reg, uint32_t *addr)
418 *addr = (bus << 16) | (slot << 11) | (func << 8) | reg;
424 gt_pci_read_config(device_t dev, u_int bus, u_int slot, u_int func, u_int reg,
427 struct gt_pci_softc *sc = device_get_softc(dev);
430 uint32_t shift, mask;
432 if (gt_pci_conf_setup(sc, bus, slot, func, reg & ~3, &addr))
433 return (uint32_t)(-1);
435 /* Clear cause register bits. */
436 GT_REGVAL(GT_INTR_CAUSE) = 0;
438 GT_REGVAL(GT_PCI0_CFG_ADDR) = (1 << 31) | addr;
439 data = GT_REGVAL(GT_PCI0_CFG_DATA);
441 /* Check for master abort. */
442 if (GT_REGVAL(GT_INTR_CAUSE) & (GTIC_MASABORT0 | GTIC_TARABORT0))
443 data = (uint32_t) -1;
446 * XXX: We assume that words readed from GT chip are BE.
447 * Should we set the mode explicitly during chip
470 data = (data >> shift) & mask;
477 data = (data >> 16) & mask;
482 panic("gt_pci_readconfig: wrong bytes count");
486 printf("PCICONF_READ(%02x:%02x.%02x[%04x] -> %02x(%d)\n",
487 bus, slot, func, reg, data, bytes);
494 gt_pci_write_config(device_t dev, u_int bus, u_int slot, u_int func, u_int reg,
495 uint32_t data, int bytes)
497 struct gt_pci_softc *sc = device_get_softc(dev);
500 uint32_t shift, mask;
504 reg_data = gt_pci_read_config(dev, bus, slot, func, reg, 4);
507 * XXX: We assume that words readed from GT chip are BE.
508 * Should we set the mode explicitly during chip
511 shift = 8 * (reg & 3);
517 data = (reg_data & ~ (mask << shift)) | (data << shift);
522 data = (reg_data & ~mask) | data;
524 data = (reg_data & ~ (mask << shift)) |
530 panic("gt_pci_readconfig: wrong bytes count");
535 if (gt_pci_conf_setup(sc, bus, slot, func, reg & ~3, &addr))
538 /* The galileo has problems accessing device 31. */
539 if (bus == 0 && slot == 31)
542 /* XXX: no support for bus > 0 yet */
546 /* Clear cause register bits. */
547 GT_REGVAL(GT_INTR_CAUSE) = 0;
549 GT_REGVAL(GT_PCI0_CFG_ADDR) = (1 << 31) | addr;
550 GT_REGVAL(GT_PCI0_CFG_DATA) = data;
554 gt_pci_route_interrupt(device_t pcib, device_t dev, int pin)
559 /* struct gt_pci_softc *sc = device_get_softc(pcib); */
560 bus = pci_get_bus(dev);
561 device = pci_get_slot(dev);
562 func = pci_get_function(dev);
564 * XXXMIPS: We need routing logic. This is just a stub .
568 * PIIX4 IDE adapter. HW IRQ0
572 printf("No mapping for %d/%d/%d/%d\n", bus, device, func, pin);
580 gt_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
582 struct gt_pci_softc *sc = device_get_softc(dev);
584 case PCIB_IVAR_DOMAIN:
588 *result = sc->sc_busno;
596 gt_write_ivar(device_t dev, device_t child, int which, uintptr_t result)
598 struct gt_pci_softc * sc = device_get_softc(dev);
602 sc->sc_busno = result;
608 static struct resource *
609 gt_pci_alloc_resource(device_t bus, device_t child, int type, int *rid,
610 u_long start, u_long end, u_long count, u_int flags)
612 struct gt_pci_softc *sc = device_get_softc(bus);
613 struct resource *rv = NULL;
615 bus_space_tag_t bt = 0;
616 bus_space_handle_t bh = 0;
620 rm = &sc->sc_irq_rman;
623 rm = &sc->sc_mem_rman;
628 rm = &sc->sc_io_rman;
636 rv = rman_reserve_resource(rm, start, end, count, flags, child);
639 rman_set_rid(rv, *rid);
640 if (type != SYS_RES_IRQ) {
641 bh += (rman_get_start(rv));
643 rman_set_bustag(rv, bt);
644 rman_set_bushandle(rv, bh);
645 if (flags & RF_ACTIVE) {
646 if (bus_activate_resource(child, type, *rid, rv)) {
647 rman_release_resource(rv);
656 gt_pci_activate_resource(device_t bus, device_t child, int type, int rid,
659 bus_space_handle_t p;
662 if ((type == SYS_RES_MEMORY) || (type == SYS_RES_IOPORT)) {
663 error = bus_space_map(rman_get_bustag(r),
664 rman_get_bushandle(r), rman_get_size(r), 0, &p);
667 rman_set_bushandle(r, p);
669 return (rman_activate_resource(r));
673 gt_pci_setup_intr(device_t dev, device_t child, struct resource *ires,
674 int flags, driver_filter_t *filt, driver_intr_t *handler,
675 void *arg, void **cookiep)
677 struct gt_pci_softc *sc = device_get_softc(dev);
678 struct intr_event *event;
681 irq = rman_get_start(ires);
682 if (irq >= ICU_LEN || irq == 2)
683 panic("%s: bad irq or type", __func__);
685 event = sc->sc_eventstab[irq];
686 sc->sc_intr_cookies[irq].irq = irq;
687 sc->sc_intr_cookies[irq].sc = sc;
689 error = intr_event_create(&event,
690 (void *)&sc->sc_intr_cookies[irq], 0, irq,
691 gt_pci_mask_irq, gt_pci_unmask_irq,
692 NULL, NULL, "gt_pci intr%d:", irq);
695 sc->sc_eventstab[irq] = event;
698 intr_event_add_handler(event, device_get_nameunit(child), filt,
699 handler, arg, intr_priority(flags), flags, cookiep);
701 gt_pci_unmask_irq((void *)&sc->sc_intr_cookies[irq]);
706 gt_pci_teardown_intr(device_t dev, device_t child, struct resource *res,
709 struct gt_pci_softc *sc = device_get_softc(dev);
712 irq = rman_get_start(res);
713 gt_pci_mask_irq((void *)&sc->sc_intr_cookies[irq]);
715 return (intr_event_remove_handler(cookie));
718 static device_method_t gt_pci_methods[] = {
719 /* Device interface */
720 DEVMETHOD(device_probe, gt_pci_probe),
721 DEVMETHOD(device_attach, gt_pci_attach),
722 DEVMETHOD(device_shutdown, bus_generic_shutdown),
723 DEVMETHOD(device_suspend, bus_generic_suspend),
724 DEVMETHOD(device_resume, bus_generic_resume),
727 DEVMETHOD(bus_read_ivar, gt_read_ivar),
728 DEVMETHOD(bus_write_ivar, gt_write_ivar),
729 DEVMETHOD(bus_alloc_resource, gt_pci_alloc_resource),
730 DEVMETHOD(bus_release_resource, bus_generic_release_resource),
731 DEVMETHOD(bus_activate_resource, gt_pci_activate_resource),
732 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
733 DEVMETHOD(bus_setup_intr, gt_pci_setup_intr),
734 DEVMETHOD(bus_teardown_intr, gt_pci_teardown_intr),
737 DEVMETHOD(pcib_maxslots, gt_pci_maxslots),
738 DEVMETHOD(pcib_read_config, gt_pci_read_config),
739 DEVMETHOD(pcib_write_config, gt_pci_write_config),
740 DEVMETHOD(pcib_route_interrupt, gt_pci_route_interrupt),
745 static driver_t gt_pci_driver = {
748 sizeof(struct gt_pci_softc),
751 static devclass_t gt_pci_devclass;
753 DRIVER_MODULE(gt_pci, gt, gt_pci_driver, gt_pci_devclass, 0, 0);