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 bus_space_tag_t sc_st;
97 bus_space_tag_t sc_pciio;
98 bus_space_tag_t sc_pcimem;
99 bus_space_handle_t sc_ioh_icu1;
100 bus_space_handle_t sc_ioh_icu2;
101 bus_space_handle_t sc_ioh_elcr;
104 struct rman sc_mem_rman;
105 struct rman sc_io_rman;
106 struct rman sc_irq_rman;
110 struct resource *sc_irq;
111 struct intr_event *sc_eventstab[ICU_LEN];
115 uint16_t sc_reserved;
121 gt_pci_set_icus(struct gt_pci_softc *sc)
123 /* Enable the cascade IRQ (2) if 8-15 is enabled. */
124 if ((sc->sc_imask & 0xff00) != 0xff00)
125 sc->sc_imask &= ~(1U << 2);
127 sc->sc_imask |= (1U << 2);
129 bus_space_write_1(sc->sc_pciio, sc->sc_ioh_icu1, PIC_OCW1,
130 sc->sc_imask & 0xff);
131 bus_space_write_1(sc->sc_pciio, sc->sc_ioh_icu2, PIC_OCW1,
132 (sc->sc_imask >> 8) & 0xff);
134 bus_space_write_1(sc->sc_pciio, sc->sc_ioh_elcr, 0,
136 bus_space_write_1(sc->sc_pciio, sc->sc_ioh_elcr, 1,
137 (sc->sc_elcr >> 8) & 0xff);
143 struct gt_pci_softc *sc = v;
144 struct intr_event *event;
148 bus_space_write_1(sc->sc_pciio, sc->sc_ioh_icu1, PIC_OCW3,
150 irq = bus_space_read_1(sc->sc_pciio, sc->sc_ioh_icu1, PIC_OCW3);
151 if ((irq & OCW3_POLL_PENDING) == 0)
153 return FILTER_HANDLED;
156 irq = OCW3_POLL_IRQ(irq);
159 bus_space_write_1(sc->sc_pciio, sc->sc_ioh_icu2,
160 PIC_OCW3, OCW3_SEL | OCW3_P);
161 irq = bus_space_read_1(sc->sc_pciio, sc->sc_ioh_icu2,
163 if (irq & OCW3_POLL_PENDING)
164 irq = OCW3_POLL_IRQ(irq) + 8;
169 event = sc->sc_eventstab[irq];
171 if (!event || TAILQ_EMPTY(&event->ie_handlers))
174 /* TODO: frame instead of NULL? */
175 intr_event_handle(event, NULL);
176 /* XXX: Log stray IRQs */
178 /* Send a specific EOI to the 8259. */
180 bus_space_write_1(sc->sc_pciio, sc->sc_ioh_icu2,
181 PIC_OCW2, OCW2_SELECT | OCW2_EOI | OCW2_SL |
186 bus_space_write_1(sc->sc_pciio, sc->sc_ioh_icu1, PIC_OCW2,
187 OCW2_SELECT | OCW2_EOI | OCW2_SL | OCW2_ILS(irq));
190 return FILTER_HANDLED;
194 gt_pci_probe(device_t dev)
196 device_set_desc(dev, "GT64120 PCI bridge");
201 gt_pci_attach(device_t dev)
205 struct gt_pci_softc *sc = device_get_softc(dev);
210 sc->sc_busno = busno;
211 sc->sc_pciio = MIPS_BUS_SPACE_IO;
212 sc->sc_pcimem = MIPS_BUS_SPACE_MEM;
214 /* Use KSEG1 to access IO ports for it is uncached */
215 sc->sc_io = MIPS_PHYS_TO_KSEG1(MALTA_PCI0_IO_BASE);
216 sc->sc_io_rman.rm_type = RMAN_ARRAY;
217 sc->sc_io_rman.rm_descr = "GT64120 PCI I/O Ports";
218 if (rman_init(&sc->sc_io_rman) != 0 ||
219 rman_manage_region(&sc->sc_io_rman, 0, 0xffff) != 0) {
220 panic("gt_pci_attach: failed to set up I/O rman");
223 /* Use KSEG1 to access PCI memory for it is uncached */
224 sc->sc_mem = MIPS_PHYS_TO_KSEG1(MALTA_PCIMEM1_BASE);
225 sc->sc_mem_rman.rm_type = RMAN_ARRAY;
226 sc->sc_mem_rman.rm_descr = "GT64120 PCI Memory";
227 if (rman_init(&sc->sc_mem_rman) != 0 ||
228 rman_manage_region(&sc->sc_mem_rman,
229 sc->sc_mem, sc->sc_mem + MALTA_PCIMEM1_SIZE) != 0) {
230 panic("gt_pci_attach: failed to set up memory rman");
232 sc->sc_irq_rman.rm_type = RMAN_ARRAY;
233 sc->sc_irq_rman.rm_descr = "GT64120 PCI IRQs";
234 if (rman_init(&sc->sc_irq_rman) != 0 ||
235 rman_manage_region(&sc->sc_irq_rman, 1, 31) != 0)
236 panic("gt_pci_attach: failed to set up IRQ rman");
239 * Map the PIC/ELCR registers.
242 if (bus_space_map(sc->sc_pciio, 0x4d0, 2, 0, &sc->sc_ioh_elcr) != 0)
243 device_printf(dev, "unable to map ELCR registers\n");
244 if (bus_space_map(sc->sc_pciio, IO_ICU1, 2, 0, &sc->sc_ioh_icu1) != 0)
245 device_printf(dev, "unable to map ICU1 registers\n");
246 if (bus_space_map(sc->sc_pciio, IO_ICU2, 2, 0, &sc->sc_ioh_icu2) != 0)
247 device_printf(dev, "unable to map ICU2 registers\n");
249 sc->sc_ioh_elcr = sc->sc_io + 0x4d0;
250 sc->sc_ioh_icu1 = sc->sc_io + IO_ICU1;
251 sc->sc_ioh_icu2 = sc->sc_io + IO_ICU2;
255 /* All interrupts default to "masked off". */
256 sc->sc_imask = 0xffff;
258 /* All interrupts default to edge-triggered. */
262 * Initialize the 8259s.
264 /* reset, program device, 4 bytes */
265 bus_space_write_1(sc->sc_pciio, sc->sc_ioh_icu1, 0,
266 ICW1_RESET | ICW1_IC4);
268 * XXX: values from NetBSD's <dev/ic/i8259reg.h>
270 bus_space_write_1(sc->sc_pciio, sc->sc_ioh_icu1, 1,
272 bus_space_write_1(sc->sc_pciio, sc->sc_ioh_icu1, 1,
274 bus_space_write_1(sc->sc_pciio, sc->sc_ioh_icu1, 1,
277 /* mask all interrupts */
278 bus_space_write_1(sc->sc_pciio, sc->sc_ioh_icu1, 0,
279 sc->sc_imask & 0xff);
281 /* enable special mask mode */
282 bus_space_write_1(sc->sc_pciio, sc->sc_ioh_icu1, 1,
283 OCW3_SEL | OCW3_ESMM | OCW3_SMM);
285 /* read IRR by default */
286 bus_space_write_1(sc->sc_pciio, sc->sc_ioh_icu1, 1,
289 /* reset, program device, 4 bytes */
290 bus_space_write_1(sc->sc_pciio, sc->sc_ioh_icu2, 0,
291 ICW1_RESET | ICW1_IC4);
292 bus_space_write_1(sc->sc_pciio, sc->sc_ioh_icu2, 1,
294 bus_space_write_1(sc->sc_pciio, sc->sc_ioh_icu2, 1,
296 bus_space_write_1(sc->sc_pciio, sc->sc_ioh_icu2, 1,
299 /* mask all interrupts */
300 bus_space_write_1(sc->sc_pciio, sc->sc_ioh_icu2, 0,
301 sc->sc_imask & 0xff);
303 /* enable special mask mode */
304 bus_space_write_1(sc->sc_pciio, sc->sc_ioh_icu2, 1,
305 OCW3_SEL | OCW3_ESMM | OCW3_SMM);
307 /* read IRR by default */
308 bus_space_write_1(sc->sc_pciio, sc->sc_ioh_icu2, 1,
312 * Default all interrupts to edge-triggered.
314 bus_space_write_1(sc->sc_pciio, sc->sc_ioh_elcr, 0,
316 bus_space_write_1(sc->sc_pciio, sc->sc_ioh_elcr, 1,
317 (sc->sc_elcr >> 8) & 0xff);
320 * Some ISA interrupts are reserved for devices that
321 * we know are hard-wired to certain IRQs.
324 (1U << 0) | /* timer */
325 (1U << 1) | /* keyboard controller (keyboard) */
326 (1U << 2) | /* PIC cascade */
327 (1U << 3) | /* COM 2 */
328 (1U << 4) | /* COM 1 */
329 (1U << 6) | /* floppy */
330 (1U << 7) | /* centronics */
331 (1U << 8) | /* RTC */
332 (1U << 9) | /* I2C */
333 (1U << 12) | /* keyboard controller (mouse) */
334 (1U << 14) | /* IDE primary */
335 (1U << 15); /* IDE secondary */
337 /* Hook up our interrupt handler. */
338 if ((sc->sc_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid,
339 MALTA_SOUTHBRIDGE_INTR, MALTA_SOUTHBRIDGE_INTR, 1,
340 RF_SHAREABLE | RF_ACTIVE)) == NULL) {
341 device_printf(dev, "unable to allocate IRQ resource\n");
345 if ((bus_setup_intr(dev, sc->sc_irq, INTR_TYPE_MISC,
346 gt_pci_intr, NULL, sc, &sc->sc_ih))) {
348 "WARNING: unable to register interrupt handler\n");
352 /* Initialize memory and i/o rmans. */
353 device_add_child(dev, "pci", busno);
354 return (bus_generic_attach(dev));
358 gt_pci_maxslots(device_t dev)
360 return (PCI_SLOTMAX);
364 gt_pci_conf_setup(struct gt_pci_softc *sc, int bus, int slot, int func,
365 int reg, uint32_t *addr)
367 *addr = (bus << 16) | (slot << 11) | (func << 8) | reg;
373 gt_pci_read_config(device_t dev, u_int bus, u_int slot, u_int func, u_int reg,
376 struct gt_pci_softc *sc = device_get_softc(dev);
379 uint32_t shift, mask;
381 if (gt_pci_conf_setup(sc, bus, slot, func, reg & ~3, &addr))
382 return (uint32_t)(-1);
384 /* Clear cause register bits. */
385 GT_REGVAL(GT_INTR_CAUSE) = 0;
387 GT_REGVAL(GT_PCI0_CFG_ADDR) = (1 << 31) | addr;
388 data = GT_REGVAL(GT_PCI0_CFG_DATA);
390 /* Check for master abort. */
391 if (GT_REGVAL(GT_INTR_CAUSE) & (GTIC_MASABORT0 | GTIC_TARABORT0))
392 data = (uint32_t) -1;
395 * XXX: We assume that words readed from GT chip are BE.
396 * Should we set the mode explicitly during chip
419 data = (data >> shift) & mask;
426 data = (data >> 16) & mask;
431 panic("gt_pci_readconfig: wrong bytes count");
435 printf("PCICONF_READ(%02x:%02x.%02x[%04x] -> %02x(%d)\n",
436 bus, slot, func, reg, data, bytes);
443 gt_pci_write_config(device_t dev, u_int bus, u_int slot, u_int func, u_int reg,
444 uint32_t data, int bytes)
446 struct gt_pci_softc *sc = device_get_softc(dev);
449 uint32_t shift, mask;
453 reg_data = gt_pci_read_config(dev, bus, slot, func, reg, 4);
456 * XXX: We assume that words readed from GT chip are BE.
457 * Should we set the mode explicitly during chip
460 shift = 8 * (reg & 3);
466 data = (reg_data & ~ (mask << shift)) | (data << shift);
471 data = (reg_data & ~mask) | data;
473 data = (reg_data & ~ (mask << shift)) |
479 panic("gt_pci_readconfig: wrong bytes count");
484 if (gt_pci_conf_setup(sc, bus, slot, func, reg & ~3, &addr))
487 /* The galileo has problems accessing device 31. */
488 if (bus == 0 && slot == 31)
491 /* XXX: no support for bus > 0 yet */
495 /* Clear cause register bits. */
496 GT_REGVAL(GT_INTR_CAUSE) = 0;
498 GT_REGVAL(GT_PCI0_CFG_ADDR) = (1 << 31) | addr;
499 GT_REGVAL(GT_PCI0_CFG_DATA) = data;
503 gt_pci_route_interrupt(device_t pcib, device_t dev, int pin)
508 /* struct gt_pci_softc *sc = device_get_softc(pcib); */
509 bus = pci_get_bus(dev);
510 device = pci_get_slot(dev);
511 func = pci_get_function(dev);
513 * XXXMIPS: We need routing logic. This is just a stub .
517 * PIIX4 IDE adapter. HW IRQ0
521 printf("No mapping for %d/%d/%d/%d\n", bus, device, func, pin);
529 gt_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
531 struct gt_pci_softc *sc = device_get_softc(dev);
533 case PCIB_IVAR_DOMAIN:
537 *result = sc->sc_busno;
545 gt_write_ivar(device_t dev, device_t child, int which, uintptr_t result)
547 struct gt_pci_softc * sc = device_get_softc(dev);
551 sc->sc_busno = result;
557 static struct resource *
558 gt_pci_alloc_resource(device_t bus, device_t child, int type, int *rid,
559 u_long start, u_long end, u_long count, u_int flags)
561 struct gt_pci_softc *sc = device_get_softc(bus);
562 struct resource *rv = NULL;
564 bus_space_tag_t bt = 0;
565 bus_space_handle_t bh = 0;
569 rm = &sc->sc_irq_rman;
572 rm = &sc->sc_mem_rman;
577 rm = &sc->sc_io_rman;
585 rv = rman_reserve_resource(rm, start, end, count, flags, child);
588 rman_set_rid(rv, *rid);
589 if (type != SYS_RES_IRQ) {
590 bh += (rman_get_start(rv));
592 rman_set_bustag(rv, bt);
593 rman_set_bushandle(rv, bh);
594 if (flags & RF_ACTIVE) {
595 if (bus_activate_resource(child, type, *rid, rv)) {
596 rman_release_resource(rv);
605 gt_pci_activate_resource(device_t bus, device_t child, int type, int rid,
608 bus_space_handle_t p;
611 if ((type == SYS_RES_MEMORY) || (type == SYS_RES_IOPORT)) {
612 error = bus_space_map(rman_get_bustag(r),
613 rman_get_bushandle(r), rman_get_size(r), 0, &p);
616 rman_set_bushandle(r, p);
618 return (rman_activate_resource(r));
622 gt_pci_setup_intr(device_t dev, device_t child, struct resource *ires,
623 int flags, driver_filter_t *filt, driver_intr_t *handler,
624 void *arg, void **cookiep)
626 struct gt_pci_softc *sc = device_get_softc(dev);
627 struct intr_event *event;
630 irq = rman_get_start(ires);
631 if (irq >= ICU_LEN || irq == 2)
632 panic("%s: bad irq or type", __func__);
634 event = sc->sc_eventstab[irq];
636 error = intr_event_create(&event, (void *)irq, 0, irq,
637 (mask_fn)mips_mask_irq, (mask_fn)mips_unmask_irq,
638 (mask_fn)mips_unmask_irq, NULL, "gt_pci intr%d:", irq);
641 sc->sc_eventstab[irq] = event;
644 intr_event_add_handler(event, device_get_nameunit(child), filt,
645 handler, arg, intr_priority(flags), flags, cookiep);
647 /* Enable it, set trigger mode. */
648 sc->sc_imask &= ~(1 << irq);
649 sc->sc_elcr &= ~(1 << irq);
657 gt_pci_teardown_intr(device_t dev, device_t child, struct resource *res,
660 return (intr_event_remove_handler(cookie));
663 static device_method_t gt_pci_methods[] = {
664 /* Device interface */
665 DEVMETHOD(device_probe, gt_pci_probe),
666 DEVMETHOD(device_attach, gt_pci_attach),
667 DEVMETHOD(device_shutdown, bus_generic_shutdown),
668 DEVMETHOD(device_suspend, bus_generic_suspend),
669 DEVMETHOD(device_resume, bus_generic_resume),
672 DEVMETHOD(bus_print_child, bus_generic_print_child),
673 DEVMETHOD(bus_read_ivar, gt_read_ivar),
674 DEVMETHOD(bus_write_ivar, gt_write_ivar),
675 DEVMETHOD(bus_alloc_resource, gt_pci_alloc_resource),
676 DEVMETHOD(bus_release_resource, bus_generic_release_resource),
677 DEVMETHOD(bus_activate_resource, gt_pci_activate_resource),
678 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
679 DEVMETHOD(bus_setup_intr, gt_pci_setup_intr),
680 DEVMETHOD(bus_teardown_intr, gt_pci_teardown_intr),
683 DEVMETHOD(pcib_maxslots, gt_pci_maxslots),
684 DEVMETHOD(pcib_read_config, gt_pci_read_config),
685 DEVMETHOD(pcib_write_config, gt_pci_write_config),
686 DEVMETHOD(pcib_route_interrupt, gt_pci_route_interrupt),
691 static driver_t gt_pci_driver = {
694 sizeof(struct gt_pci_softc),
697 static devclass_t gt_pci_devclass;
699 DRIVER_MODULE(gt_pci, gt, gt_pci_driver, gt_pci_devclass, 0, 0);