160727d8bSWarner Losh /*- 2bd687ebfSPeter Grehan * Copyright 2003 by Peter Grehan. All rights reserved. 3bd687ebfSPeter Grehan * 4bd687ebfSPeter Grehan * Redistribution and use in source and binary forms, with or without 5bd687ebfSPeter Grehan * modification, are permitted provided that the following conditions 6bd687ebfSPeter Grehan * are met: 7bd687ebfSPeter Grehan * 1. Redistributions of source code must retain the above copyright 8bd687ebfSPeter Grehan * notice, this list of conditions and the following disclaimer. 9bd687ebfSPeter Grehan * 2. Redistributions in binary form must reproduce the above copyright 10bd687ebfSPeter Grehan * notice, this list of conditions and the following disclaimer in the 11bd687ebfSPeter Grehan * documentation and/or other materials provided with the distribution. 12bd687ebfSPeter Grehan * 3. The name of the author may not be used to endorse or promote products 13bd687ebfSPeter Grehan * derived from this software without specific prior written permission. 14bd687ebfSPeter Grehan * 15bd687ebfSPeter Grehan * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16bd687ebfSPeter Grehan * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17bd687ebfSPeter Grehan * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18bd687ebfSPeter Grehan * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19bd687ebfSPeter Grehan * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 20bd687ebfSPeter Grehan * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21bd687ebfSPeter Grehan * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 22bd687ebfSPeter Grehan * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23bd687ebfSPeter Grehan * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24bd687ebfSPeter Grehan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25bd687ebfSPeter Grehan * SUCH DAMAGE. 26bd687ebfSPeter Grehan * 27bd687ebfSPeter Grehan * $FreeBSD$ 28bd687ebfSPeter Grehan */ 29bd687ebfSPeter Grehan 30bd687ebfSPeter Grehan /* 31bd687ebfSPeter Grehan * A driver for the PIC found in the Heathrow/Paddington MacIO chips. 32bd687ebfSPeter Grehan * This was superseded by an OpenPIC in the Keylargo and beyond 33bd687ebfSPeter Grehan * MacIO versions. 34bd687ebfSPeter Grehan */ 35bd687ebfSPeter Grehan 36bd687ebfSPeter Grehan #include <sys/param.h> 37bd687ebfSPeter Grehan #include <sys/systm.h> 3840cdee9dSPeter Grehan #include <sys/module.h> 39bd687ebfSPeter Grehan #include <sys/bus.h> 40bd687ebfSPeter Grehan #include <sys/conf.h> 41bd687ebfSPeter Grehan #include <sys/kernel.h> 4277d40ffdSMarcel Moolenaar #include <sys/rman.h> 43bd687ebfSPeter Grehan 4426280d88SMarius Strobl #include <dev/ofw/ofw_bus.h> 45bd687ebfSPeter Grehan #include <dev/ofw/openfirm.h> 46bd687ebfSPeter Grehan 47bd687ebfSPeter Grehan #include <machine/bus.h> 48bd687ebfSPeter Grehan #include <machine/intr.h> 49bd687ebfSPeter Grehan #include <machine/intr_machdep.h> 50bd687ebfSPeter Grehan #include <machine/md_var.h> 51bd687ebfSPeter Grehan #include <machine/nexusvar.h> 52bd687ebfSPeter Grehan #include <machine/pio.h> 53bd687ebfSPeter Grehan #include <machine/resource.h> 54bd687ebfSPeter Grehan 55bd687ebfSPeter Grehan #include <vm/vm.h> 56bd687ebfSPeter Grehan #include <vm/pmap.h> 57bd687ebfSPeter Grehan 58bd687ebfSPeter Grehan #include <powerpc/powermac/hrowpicvar.h> 59bd687ebfSPeter Grehan 60bd687ebfSPeter Grehan #include "pic_if.h" 61bd687ebfSPeter Grehan 62bd687ebfSPeter Grehan /* 6377d40ffdSMarcel Moolenaar * MacIO interface 64bd687ebfSPeter Grehan */ 65bd687ebfSPeter Grehan static int hrowpic_probe(device_t); 66bd687ebfSPeter Grehan static int hrowpic_attach(device_t); 67bd687ebfSPeter Grehan 6877d40ffdSMarcel Moolenaar static void hrowpic_dispatch(device_t, struct trapframe *); 6977d40ffdSMarcel Moolenaar static void hrowpic_enable(device_t, u_int, u_int); 7077d40ffdSMarcel Moolenaar static void hrowpic_eoi(device_t, u_int); 7105c62b81SMarcel Moolenaar static void hrowpic_ipi(device_t, u_int); 7277d40ffdSMarcel Moolenaar static void hrowpic_mask(device_t, u_int); 7377d40ffdSMarcel Moolenaar static void hrowpic_unmask(device_t, u_int); 74bd687ebfSPeter Grehan 75bd687ebfSPeter Grehan static device_method_t hrowpic_methods[] = { 76bd687ebfSPeter Grehan /* Device interface */ 77bd687ebfSPeter Grehan DEVMETHOD(device_probe, hrowpic_probe), 78bd687ebfSPeter Grehan DEVMETHOD(device_attach, hrowpic_attach), 79bd687ebfSPeter Grehan 80bd687ebfSPeter Grehan /* PIC interface */ 8177d40ffdSMarcel Moolenaar DEVMETHOD(pic_dispatch, hrowpic_dispatch), 8277d40ffdSMarcel Moolenaar DEVMETHOD(pic_enable, hrowpic_enable), 8377d40ffdSMarcel Moolenaar DEVMETHOD(pic_eoi, hrowpic_eoi), 8405c62b81SMarcel Moolenaar DEVMETHOD(pic_ipi, hrowpic_ipi), 8577d40ffdSMarcel Moolenaar DEVMETHOD(pic_mask, hrowpic_mask), 8677d40ffdSMarcel Moolenaar DEVMETHOD(pic_unmask, hrowpic_unmask), 87bd687ebfSPeter Grehan 8877d40ffdSMarcel Moolenaar { 0, 0 }, 89bd687ebfSPeter Grehan }; 90bd687ebfSPeter Grehan 91bd687ebfSPeter Grehan static driver_t hrowpic_driver = { 92bd687ebfSPeter Grehan "hrowpic", 93bd687ebfSPeter Grehan hrowpic_methods, 94bd687ebfSPeter Grehan sizeof(struct hrowpic_softc) 95bd687ebfSPeter Grehan }; 96bd687ebfSPeter Grehan 97bd687ebfSPeter Grehan static devclass_t hrowpic_devclass; 98bd687ebfSPeter Grehan 9977d40ffdSMarcel Moolenaar DRIVER_MODULE(hrowpic, macio, hrowpic_driver, hrowpic_devclass, 0, 0); 100bd687ebfSPeter Grehan 10177d40ffdSMarcel Moolenaar static uint32_t 102bd687ebfSPeter Grehan hrowpic_read_reg(struct hrowpic_softc *sc, u_int reg, u_int bank) 103bd687ebfSPeter Grehan { 104bd687ebfSPeter Grehan if (bank == HPIC_PRIMARY) 105bd687ebfSPeter Grehan reg += HPIC_1ST_OFFSET; 106bd687ebfSPeter Grehan 107bd687ebfSPeter Grehan return (bus_space_read_4(sc->sc_bt, sc->sc_bh, reg)); 108bd687ebfSPeter Grehan } 109bd687ebfSPeter Grehan 110bd687ebfSPeter Grehan static void 11177d40ffdSMarcel Moolenaar hrowpic_write_reg(struct hrowpic_softc *sc, u_int reg, u_int bank, 11277d40ffdSMarcel Moolenaar uint32_t val) 113bd687ebfSPeter Grehan { 11477d40ffdSMarcel Moolenaar 11577d40ffdSMarcel Moolenaar if (bank == HPIC_PRIMARY) 11677d40ffdSMarcel Moolenaar reg += HPIC_1ST_OFFSET; 11777d40ffdSMarcel Moolenaar 11877d40ffdSMarcel Moolenaar bus_space_write_4(sc->sc_bt, sc->sc_bh, reg, val); 11977d40ffdSMarcel Moolenaar 12077d40ffdSMarcel Moolenaar /* XXX Issue a read to force the write to complete. */ 12177d40ffdSMarcel Moolenaar bus_space_read_4(sc->sc_bt, sc->sc_bh, reg); 12277d40ffdSMarcel Moolenaar } 12377d40ffdSMarcel Moolenaar 12477d40ffdSMarcel Moolenaar static int 12577d40ffdSMarcel Moolenaar hrowpic_probe(device_t dev) 12677d40ffdSMarcel Moolenaar { 12777d40ffdSMarcel Moolenaar const char *type = ofw_bus_get_type(dev); 12877d40ffdSMarcel Moolenaar 12977d40ffdSMarcel Moolenaar /* 13077d40ffdSMarcel Moolenaar * OpenPIC cells have a type of "open-pic", so this 13177d40ffdSMarcel Moolenaar * is sufficient to identify a Heathrow cell 13277d40ffdSMarcel Moolenaar */ 13377d40ffdSMarcel Moolenaar if (strcmp(type, "interrupt-controller") != 0) 13477d40ffdSMarcel Moolenaar return (ENXIO); 13577d40ffdSMarcel Moolenaar 13677d40ffdSMarcel Moolenaar /* 13777d40ffdSMarcel Moolenaar * The description was already printed out in the nexus 13877d40ffdSMarcel Moolenaar * probe, so don't do it again here 13977d40ffdSMarcel Moolenaar */ 14077d40ffdSMarcel Moolenaar device_set_desc(dev, "Heathrow MacIO interrupt controller"); 14177d40ffdSMarcel Moolenaar return (0); 14277d40ffdSMarcel Moolenaar } 14377d40ffdSMarcel Moolenaar 14477d40ffdSMarcel Moolenaar static int 14577d40ffdSMarcel Moolenaar hrowpic_attach(device_t dev) 14677d40ffdSMarcel Moolenaar { 14777d40ffdSMarcel Moolenaar struct hrowpic_softc *sc; 14877d40ffdSMarcel Moolenaar 14977d40ffdSMarcel Moolenaar sc = device_get_softc(dev); 15077d40ffdSMarcel Moolenaar sc->sc_dev = dev; 15177d40ffdSMarcel Moolenaar 15277d40ffdSMarcel Moolenaar sc->sc_rrid = 0; 15377d40ffdSMarcel Moolenaar sc->sc_rres = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->sc_rrid, 15477d40ffdSMarcel Moolenaar RF_ACTIVE); 15577d40ffdSMarcel Moolenaar 15677d40ffdSMarcel Moolenaar if (sc->sc_rres == NULL) { 15777d40ffdSMarcel Moolenaar device_printf(dev, "Could not alloc mem resource!\n"); 15877d40ffdSMarcel Moolenaar return (ENXIO); 15977d40ffdSMarcel Moolenaar } 16077d40ffdSMarcel Moolenaar 16177d40ffdSMarcel Moolenaar sc->sc_bt = rman_get_bustag(sc->sc_rres); 16277d40ffdSMarcel Moolenaar sc->sc_bh = rman_get_bushandle(sc->sc_rres); 16377d40ffdSMarcel Moolenaar 164bd687ebfSPeter Grehan /* 165bd687ebfSPeter Grehan * Disable all interrupt sources and clear outstanding interrupts 166bd687ebfSPeter Grehan */ 167bd687ebfSPeter Grehan hrowpic_write_reg(sc, HPIC_ENABLE, HPIC_PRIMARY, 0); 168bd687ebfSPeter Grehan hrowpic_write_reg(sc, HPIC_CLEAR, HPIC_PRIMARY, 0xffffffff); 169bd687ebfSPeter Grehan hrowpic_write_reg(sc, HPIC_ENABLE, HPIC_SECONDARY, 0); 170bd687ebfSPeter Grehan hrowpic_write_reg(sc, HPIC_CLEAR, HPIC_SECONDARY, 0xffffffff); 17177d40ffdSMarcel Moolenaar 17205c62b81SMarcel Moolenaar powerpc_register_pic(dev, 64); 17377d40ffdSMarcel Moolenaar return (0); 174bd687ebfSPeter Grehan } 175bd687ebfSPeter Grehan 17677d40ffdSMarcel Moolenaar /* 17777d40ffdSMarcel Moolenaar * Local routines 17877d40ffdSMarcel Moolenaar */ 17977d40ffdSMarcel Moolenaar 180bd687ebfSPeter Grehan static void 181bd687ebfSPeter Grehan hrowpic_toggle_irq(struct hrowpic_softc *sc, int irq, int enable) 182bd687ebfSPeter Grehan { 183bd687ebfSPeter Grehan u_int roffset; 184bd687ebfSPeter Grehan u_int rbit; 185bd687ebfSPeter Grehan 186bd687ebfSPeter Grehan KASSERT((irq > 0) && (irq < HROWPIC_IRQMAX), ("en irq out of range")); 187bd687ebfSPeter Grehan 188bd687ebfSPeter Grehan /* 189bd687ebfSPeter Grehan * Calculate prim/sec register bank for the IRQ, update soft copy, 190bd687ebfSPeter Grehan * and enable the IRQ as an interrupt source 191bd687ebfSPeter Grehan */ 192bd687ebfSPeter Grehan roffset = HPIC_INT_TO_BANK(irq); 193bd687ebfSPeter Grehan rbit = HPIC_INT_TO_REGBIT(irq); 194bd687ebfSPeter Grehan 195bd687ebfSPeter Grehan if (enable) 196bd687ebfSPeter Grehan sc->sc_softreg[roffset] |= (1 << rbit); 197bd687ebfSPeter Grehan else 198bd687ebfSPeter Grehan sc->sc_softreg[roffset] &= ~(1 << rbit); 199bd687ebfSPeter Grehan 200bd687ebfSPeter Grehan hrowpic_write_reg(sc, HPIC_ENABLE, roffset, sc->sc_softreg[roffset]); 201bd687ebfSPeter Grehan } 202bd687ebfSPeter Grehan 20377d40ffdSMarcel Moolenaar /* 20477d40ffdSMarcel Moolenaar * PIC I/F methods. 20577d40ffdSMarcel Moolenaar */ 20677d40ffdSMarcel Moolenaar 207bd687ebfSPeter Grehan static void 20877d40ffdSMarcel Moolenaar hrowpic_dispatch(device_t dev, struct trapframe *tf) 209bd687ebfSPeter Grehan { 21077d40ffdSMarcel Moolenaar struct hrowpic_softc *sc; 21177d40ffdSMarcel Moolenaar uint64_t mask; 21277d40ffdSMarcel Moolenaar uint32_t reg; 21377d40ffdSMarcel Moolenaar u_int irq; 21477d40ffdSMarcel Moolenaar 21577d40ffdSMarcel Moolenaar sc = device_get_softc(dev); 21677d40ffdSMarcel Moolenaar while (1) { 21777d40ffdSMarcel Moolenaar mask = hrowpic_read_reg(sc, HPIC_STATUS, HPIC_SECONDARY); 21877d40ffdSMarcel Moolenaar reg = hrowpic_read_reg(sc, HPIC_STATUS, HPIC_PRIMARY); 21977d40ffdSMarcel Moolenaar mask = (mask << 32) | reg; 22077d40ffdSMarcel Moolenaar if (mask == 0) 22177d40ffdSMarcel Moolenaar break; 22277d40ffdSMarcel Moolenaar 22377d40ffdSMarcel Moolenaar irq = 0; 22477d40ffdSMarcel Moolenaar while (irq < HROWPIC_IRQMAX) { 22577d40ffdSMarcel Moolenaar if (mask & 1) 22677d40ffdSMarcel Moolenaar powerpc_dispatch_intr(sc->sc_vector[irq], tf); 22777d40ffdSMarcel Moolenaar mask >>= 1; 22877d40ffdSMarcel Moolenaar irq++; 22977d40ffdSMarcel Moolenaar } 23077d40ffdSMarcel Moolenaar } 23177d40ffdSMarcel Moolenaar } 23277d40ffdSMarcel Moolenaar 23377d40ffdSMarcel Moolenaar static void 23477d40ffdSMarcel Moolenaar hrowpic_enable(device_t dev, u_int irq, u_int vector) 23577d40ffdSMarcel Moolenaar { 236bd687ebfSPeter Grehan struct hrowpic_softc *sc; 237bd687ebfSPeter Grehan 23877d40ffdSMarcel Moolenaar sc = device_get_softc(dev); 23977d40ffdSMarcel Moolenaar sc->sc_vector[irq] = vector; 24077d40ffdSMarcel Moolenaar hrowpic_toggle_irq(sc, irq, 1); 241bd687ebfSPeter Grehan } 242bd687ebfSPeter Grehan 243bd687ebfSPeter Grehan static void 24477d40ffdSMarcel Moolenaar hrowpic_eoi(device_t dev __unused, u_int irq __unused) 245bd687ebfSPeter Grehan { 24677d40ffdSMarcel Moolenaar struct hrowpic_softc *sc; 24777d40ffdSMarcel Moolenaar int bank; 24877d40ffdSMarcel Moolenaar 24977d40ffdSMarcel Moolenaar sc = device_get_softc(dev); 25077d40ffdSMarcel Moolenaar bank = (irq >= 32) ? HPIC_SECONDARY : HPIC_PRIMARY ; 25177d40ffdSMarcel Moolenaar hrowpic_write_reg(sc, HPIC_CLEAR, bank, 1U << (irq & 0x1f)); 252bd687ebfSPeter Grehan } 253bd687ebfSPeter Grehan 254bd687ebfSPeter Grehan static void 25505c62b81SMarcel Moolenaar hrowpic_ipi(device_t dev, u_int irq) 25605c62b81SMarcel Moolenaar { 25705c62b81SMarcel Moolenaar /* No SMP support. */ 25805c62b81SMarcel Moolenaar } 25905c62b81SMarcel Moolenaar 26005c62b81SMarcel Moolenaar static void 26177d40ffdSMarcel Moolenaar hrowpic_mask(device_t dev, u_int irq) 262bd687ebfSPeter Grehan { 26377d40ffdSMarcel Moolenaar struct hrowpic_softc *sc; 26477d40ffdSMarcel Moolenaar int bank; 26577d40ffdSMarcel Moolenaar 26677d40ffdSMarcel Moolenaar sc = device_get_softc(dev); 26777d40ffdSMarcel Moolenaar hrowpic_toggle_irq(sc, irq, 0); 26877d40ffdSMarcel Moolenaar bank = (irq >= 32) ? HPIC_SECONDARY : HPIC_PRIMARY ; 26977d40ffdSMarcel Moolenaar hrowpic_write_reg(sc, HPIC_CLEAR, bank, 1U << (irq & 0x1f)); 270bd687ebfSPeter Grehan } 271bd687ebfSPeter Grehan 27277d40ffdSMarcel Moolenaar static void 27377d40ffdSMarcel Moolenaar hrowpic_unmask(device_t dev, u_int irq) 274bd687ebfSPeter Grehan { 27577d40ffdSMarcel Moolenaar struct hrowpic_softc *sc; 276bd687ebfSPeter Grehan 27777d40ffdSMarcel Moolenaar sc = device_get_softc(dev); 27877d40ffdSMarcel Moolenaar hrowpic_toggle_irq(sc, irq, 1); 279bd687ebfSPeter Grehan } 280