160727d8bSWarner Losh /*-
271e3c308SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
371e3c308SPedro F. Giffuni *
4bd687ebfSPeter Grehan * Copyright 2003 by Peter Grehan. All rights reserved.
5bd687ebfSPeter Grehan *
6bd687ebfSPeter Grehan * Redistribution and use in source and binary forms, with or without
7bd687ebfSPeter Grehan * modification, are permitted provided that the following conditions
8bd687ebfSPeter Grehan * are met:
9bd687ebfSPeter Grehan * 1. Redistributions of source code must retain the above copyright
10bd687ebfSPeter Grehan * notice, this list of conditions and the following disclaimer.
11bd687ebfSPeter Grehan * 2. Redistributions in binary form must reproduce the above copyright
12bd687ebfSPeter Grehan * notice, this list of conditions and the following disclaimer in the
13bd687ebfSPeter Grehan * documentation and/or other materials provided with the distribution.
14bd687ebfSPeter Grehan * 3. The name of the author may not be used to endorse or promote products
15bd687ebfSPeter Grehan * derived from this software without specific prior written permission.
16bd687ebfSPeter Grehan *
17bd687ebfSPeter Grehan * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18bd687ebfSPeter Grehan * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19bd687ebfSPeter Grehan * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20bd687ebfSPeter Grehan * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21bd687ebfSPeter Grehan * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
22bd687ebfSPeter Grehan * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23bd687ebfSPeter Grehan * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24bd687ebfSPeter Grehan * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25bd687ebfSPeter Grehan * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26bd687ebfSPeter Grehan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27bd687ebfSPeter Grehan * SUCH DAMAGE.
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_machdep.h>
49bd687ebfSPeter Grehan #include <machine/md_var.h>
50bd687ebfSPeter Grehan #include <machine/pio.h>
51bd687ebfSPeter Grehan #include <machine/resource.h>
52bd687ebfSPeter Grehan
53bd687ebfSPeter Grehan #include <vm/vm.h>
54bd687ebfSPeter Grehan #include <vm/pmap.h>
55bd687ebfSPeter Grehan
56bd687ebfSPeter Grehan #include <powerpc/powermac/hrowpicvar.h>
57bd687ebfSPeter Grehan
58bd687ebfSPeter Grehan #include "pic_if.h"
59bd687ebfSPeter Grehan
60bd687ebfSPeter Grehan /*
6177d40ffdSMarcel Moolenaar * MacIO interface
62bd687ebfSPeter Grehan */
63bd687ebfSPeter Grehan static int hrowpic_probe(device_t);
64bd687ebfSPeter Grehan static int hrowpic_attach(device_t);
65bd687ebfSPeter Grehan
6677d40ffdSMarcel Moolenaar static void hrowpic_dispatch(device_t, struct trapframe *);
6756505ec0SJustin Hibbits static void hrowpic_enable(device_t, u_int, u_int, void **);
6856505ec0SJustin Hibbits static void hrowpic_eoi(device_t, u_int, void *);
6905c62b81SMarcel Moolenaar static void hrowpic_ipi(device_t, u_int);
7056505ec0SJustin Hibbits static void hrowpic_mask(device_t, u_int, void *);
7156505ec0SJustin Hibbits static void hrowpic_unmask(device_t, u_int, void *);
72bd687ebfSPeter Grehan
73bd687ebfSPeter Grehan static device_method_t hrowpic_methods[] = {
74bd687ebfSPeter Grehan /* Device interface */
75bd687ebfSPeter Grehan DEVMETHOD(device_probe, hrowpic_probe),
76bd687ebfSPeter Grehan DEVMETHOD(device_attach, hrowpic_attach),
77bd687ebfSPeter Grehan
78bd687ebfSPeter Grehan /* PIC interface */
7977d40ffdSMarcel Moolenaar DEVMETHOD(pic_dispatch, hrowpic_dispatch),
8077d40ffdSMarcel Moolenaar DEVMETHOD(pic_enable, hrowpic_enable),
8177d40ffdSMarcel Moolenaar DEVMETHOD(pic_eoi, hrowpic_eoi),
8205c62b81SMarcel Moolenaar DEVMETHOD(pic_ipi, hrowpic_ipi),
8377d40ffdSMarcel Moolenaar DEVMETHOD(pic_mask, hrowpic_mask),
8477d40ffdSMarcel Moolenaar DEVMETHOD(pic_unmask, hrowpic_unmask),
85bd687ebfSPeter Grehan
8677d40ffdSMarcel Moolenaar { 0, 0 },
87bd687ebfSPeter Grehan };
88bd687ebfSPeter Grehan
89bd687ebfSPeter Grehan static driver_t hrowpic_driver = {
90bd687ebfSPeter Grehan "hrowpic",
91bd687ebfSPeter Grehan hrowpic_methods,
92bd687ebfSPeter Grehan sizeof(struct hrowpic_softc)
93bd687ebfSPeter Grehan };
94bd687ebfSPeter Grehan
95*992ae60bSJohn Baldwin DRIVER_MODULE(hrowpic, macio, hrowpic_driver, 0, 0);
96bd687ebfSPeter Grehan
9777d40ffdSMarcel Moolenaar static uint32_t
hrowpic_read_reg(struct hrowpic_softc * sc,u_int reg,u_int bank)98bd687ebfSPeter Grehan hrowpic_read_reg(struct hrowpic_softc *sc, u_int reg, u_int bank)
99bd687ebfSPeter Grehan {
100bd687ebfSPeter Grehan if (bank == HPIC_PRIMARY)
101bd687ebfSPeter Grehan reg += HPIC_1ST_OFFSET;
102bd687ebfSPeter Grehan
103bd687ebfSPeter Grehan return (bus_space_read_4(sc->sc_bt, sc->sc_bh, reg));
104bd687ebfSPeter Grehan }
105bd687ebfSPeter Grehan
106bd687ebfSPeter Grehan static void
hrowpic_write_reg(struct hrowpic_softc * sc,u_int reg,u_int bank,uint32_t val)10777d40ffdSMarcel Moolenaar hrowpic_write_reg(struct hrowpic_softc *sc, u_int reg, u_int bank,
10877d40ffdSMarcel Moolenaar uint32_t val)
109bd687ebfSPeter Grehan {
11077d40ffdSMarcel Moolenaar
11177d40ffdSMarcel Moolenaar if (bank == HPIC_PRIMARY)
11277d40ffdSMarcel Moolenaar reg += HPIC_1ST_OFFSET;
11377d40ffdSMarcel Moolenaar
11477d40ffdSMarcel Moolenaar bus_space_write_4(sc->sc_bt, sc->sc_bh, reg, val);
11577d40ffdSMarcel Moolenaar
11677d40ffdSMarcel Moolenaar /* XXX Issue a read to force the write to complete. */
11777d40ffdSMarcel Moolenaar bus_space_read_4(sc->sc_bt, sc->sc_bh, reg);
11877d40ffdSMarcel Moolenaar }
11977d40ffdSMarcel Moolenaar
12077d40ffdSMarcel Moolenaar static int
hrowpic_probe(device_t dev)12177d40ffdSMarcel Moolenaar hrowpic_probe(device_t dev)
12277d40ffdSMarcel Moolenaar {
12377d40ffdSMarcel Moolenaar const char *type = ofw_bus_get_type(dev);
12477d40ffdSMarcel Moolenaar
12577d40ffdSMarcel Moolenaar /*
12677d40ffdSMarcel Moolenaar * OpenPIC cells have a type of "open-pic", so this
12777d40ffdSMarcel Moolenaar * is sufficient to identify a Heathrow cell
12877d40ffdSMarcel Moolenaar */
12977d40ffdSMarcel Moolenaar if (strcmp(type, "interrupt-controller") != 0)
13077d40ffdSMarcel Moolenaar return (ENXIO);
13177d40ffdSMarcel Moolenaar
13277d40ffdSMarcel Moolenaar /*
13377d40ffdSMarcel Moolenaar * The description was already printed out in the nexus
13477d40ffdSMarcel Moolenaar * probe, so don't do it again here
13577d40ffdSMarcel Moolenaar */
13677d40ffdSMarcel Moolenaar device_set_desc(dev, "Heathrow MacIO interrupt controller");
13777d40ffdSMarcel Moolenaar return (0);
13877d40ffdSMarcel Moolenaar }
13977d40ffdSMarcel Moolenaar
14077d40ffdSMarcel Moolenaar static int
hrowpic_attach(device_t dev)14177d40ffdSMarcel Moolenaar hrowpic_attach(device_t dev)
14277d40ffdSMarcel Moolenaar {
14377d40ffdSMarcel Moolenaar struct hrowpic_softc *sc;
14477d40ffdSMarcel Moolenaar
14577d40ffdSMarcel Moolenaar sc = device_get_softc(dev);
14677d40ffdSMarcel Moolenaar sc->sc_dev = dev;
14777d40ffdSMarcel Moolenaar
14877d40ffdSMarcel Moolenaar sc->sc_rrid = 0;
14977d40ffdSMarcel Moolenaar sc->sc_rres = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->sc_rrid,
15077d40ffdSMarcel Moolenaar RF_ACTIVE);
15177d40ffdSMarcel Moolenaar
15277d40ffdSMarcel Moolenaar if (sc->sc_rres == NULL) {
15377d40ffdSMarcel Moolenaar device_printf(dev, "Could not alloc mem resource!\n");
15477d40ffdSMarcel Moolenaar return (ENXIO);
15577d40ffdSMarcel Moolenaar }
15677d40ffdSMarcel Moolenaar
15777d40ffdSMarcel Moolenaar sc->sc_bt = rman_get_bustag(sc->sc_rres);
15877d40ffdSMarcel Moolenaar sc->sc_bh = rman_get_bushandle(sc->sc_rres);
15977d40ffdSMarcel Moolenaar
160bd687ebfSPeter Grehan /*
161bd687ebfSPeter Grehan * Disable all interrupt sources and clear outstanding interrupts
162bd687ebfSPeter Grehan */
163bd687ebfSPeter Grehan hrowpic_write_reg(sc, HPIC_ENABLE, HPIC_PRIMARY, 0);
164bd687ebfSPeter Grehan hrowpic_write_reg(sc, HPIC_CLEAR, HPIC_PRIMARY, 0xffffffff);
165bd687ebfSPeter Grehan hrowpic_write_reg(sc, HPIC_ENABLE, HPIC_SECONDARY, 0);
166bd687ebfSPeter Grehan hrowpic_write_reg(sc, HPIC_CLEAR, HPIC_SECONDARY, 0xffffffff);
16777d40ffdSMarcel Moolenaar
1686d2d7b8cSMarcel Moolenaar powerpc_register_pic(dev, ofw_bus_get_node(dev), 64, 0, FALSE);
16977d40ffdSMarcel Moolenaar return (0);
170bd687ebfSPeter Grehan }
171bd687ebfSPeter Grehan
17277d40ffdSMarcel Moolenaar /*
17377d40ffdSMarcel Moolenaar * Local routines
17477d40ffdSMarcel Moolenaar */
17577d40ffdSMarcel Moolenaar
176bd687ebfSPeter Grehan static void
hrowpic_toggle_irq(struct hrowpic_softc * sc,int irq,int enable)177bd687ebfSPeter Grehan hrowpic_toggle_irq(struct hrowpic_softc *sc, int irq, int enable)
178bd687ebfSPeter Grehan {
179bd687ebfSPeter Grehan u_int roffset;
180bd687ebfSPeter Grehan u_int rbit;
181bd687ebfSPeter Grehan
18237a7f596SNathan Whitehorn KASSERT((irq > 0) && (irq <= HROWPIC_IRQMAX), ("en irq out of range"));
18337a7f596SNathan Whitehorn
18437a7f596SNathan Whitehorn /*
18537a7f596SNathan Whitehorn * Humor the SMP layer if it wants to set up an IPI handler.
18637a7f596SNathan Whitehorn */
18737a7f596SNathan Whitehorn if (irq == HROWPIC_IRQMAX)
18837a7f596SNathan Whitehorn return;
189bd687ebfSPeter Grehan
190bd687ebfSPeter Grehan /*
191bd687ebfSPeter Grehan * Calculate prim/sec register bank for the IRQ, update soft copy,
192bd687ebfSPeter Grehan * and enable the IRQ as an interrupt source
193bd687ebfSPeter Grehan */
194bd687ebfSPeter Grehan roffset = HPIC_INT_TO_BANK(irq);
195bd687ebfSPeter Grehan rbit = HPIC_INT_TO_REGBIT(irq);
196bd687ebfSPeter Grehan
197bd687ebfSPeter Grehan if (enable)
198bd687ebfSPeter Grehan sc->sc_softreg[roffset] |= (1 << rbit);
199bd687ebfSPeter Grehan else
200bd687ebfSPeter Grehan sc->sc_softreg[roffset] &= ~(1 << rbit);
201bd687ebfSPeter Grehan
202bd687ebfSPeter Grehan hrowpic_write_reg(sc, HPIC_ENABLE, roffset, sc->sc_softreg[roffset]);
203bd687ebfSPeter Grehan }
204bd687ebfSPeter Grehan
20577d40ffdSMarcel Moolenaar /*
20677d40ffdSMarcel Moolenaar * PIC I/F methods.
20777d40ffdSMarcel Moolenaar */
20877d40ffdSMarcel Moolenaar
209bd687ebfSPeter Grehan static void
hrowpic_dispatch(device_t dev,struct trapframe * tf)21077d40ffdSMarcel Moolenaar hrowpic_dispatch(device_t dev, struct trapframe *tf)
211bd687ebfSPeter Grehan {
21277d40ffdSMarcel Moolenaar struct hrowpic_softc *sc;
21377d40ffdSMarcel Moolenaar uint64_t mask;
21477d40ffdSMarcel Moolenaar uint32_t reg;
21577d40ffdSMarcel Moolenaar u_int irq;
21677d40ffdSMarcel Moolenaar
21777d40ffdSMarcel Moolenaar sc = device_get_softc(dev);
21877d40ffdSMarcel Moolenaar while (1) {
21977d40ffdSMarcel Moolenaar mask = hrowpic_read_reg(sc, HPIC_STATUS, HPIC_SECONDARY);
22077d40ffdSMarcel Moolenaar reg = hrowpic_read_reg(sc, HPIC_STATUS, HPIC_PRIMARY);
22177d40ffdSMarcel Moolenaar mask = (mask << 32) | reg;
22277d40ffdSMarcel Moolenaar if (mask == 0)
22377d40ffdSMarcel Moolenaar break;
22477d40ffdSMarcel Moolenaar
22577d40ffdSMarcel Moolenaar irq = 0;
22677d40ffdSMarcel Moolenaar while (irq < HROWPIC_IRQMAX) {
22777d40ffdSMarcel Moolenaar if (mask & 1)
22877d40ffdSMarcel Moolenaar powerpc_dispatch_intr(sc->sc_vector[irq], tf);
22977d40ffdSMarcel Moolenaar mask >>= 1;
23077d40ffdSMarcel Moolenaar irq++;
23177d40ffdSMarcel Moolenaar }
23277d40ffdSMarcel Moolenaar }
23377d40ffdSMarcel Moolenaar }
23477d40ffdSMarcel Moolenaar
23577d40ffdSMarcel Moolenaar static void
hrowpic_enable(device_t dev,u_int irq,u_int vector,void ** priv __unused)23656505ec0SJustin Hibbits hrowpic_enable(device_t dev, u_int irq, u_int vector, void **priv __unused)
23777d40ffdSMarcel Moolenaar {
238bd687ebfSPeter Grehan struct hrowpic_softc *sc;
239bd687ebfSPeter Grehan
24077d40ffdSMarcel Moolenaar sc = device_get_softc(dev);
24177d40ffdSMarcel Moolenaar sc->sc_vector[irq] = vector;
24277d40ffdSMarcel Moolenaar hrowpic_toggle_irq(sc, irq, 1);
243bd687ebfSPeter Grehan }
244bd687ebfSPeter Grehan
245bd687ebfSPeter Grehan static void
hrowpic_eoi(device_t dev,u_int irq,void * priv __unused)24656505ec0SJustin Hibbits hrowpic_eoi(device_t dev, u_int irq, void *priv __unused)
247bd687ebfSPeter Grehan {
24877d40ffdSMarcel Moolenaar struct hrowpic_softc *sc;
24977d40ffdSMarcel Moolenaar int bank;
25077d40ffdSMarcel Moolenaar
25177d40ffdSMarcel Moolenaar sc = device_get_softc(dev);
25277d40ffdSMarcel Moolenaar bank = (irq >= 32) ? HPIC_SECONDARY : HPIC_PRIMARY ;
25377d40ffdSMarcel Moolenaar hrowpic_write_reg(sc, HPIC_CLEAR, bank, 1U << (irq & 0x1f));
254bd687ebfSPeter Grehan }
255bd687ebfSPeter Grehan
256bd687ebfSPeter Grehan static void
hrowpic_ipi(device_t dev,u_int irq)25705c62b81SMarcel Moolenaar hrowpic_ipi(device_t dev, u_int irq)
25805c62b81SMarcel Moolenaar {
25905c62b81SMarcel Moolenaar /* No SMP support. */
26005c62b81SMarcel Moolenaar }
26105c62b81SMarcel Moolenaar
26205c62b81SMarcel Moolenaar static void
hrowpic_mask(device_t dev,u_int irq,void * priv __unused)26356505ec0SJustin Hibbits hrowpic_mask(device_t dev, u_int irq, void *priv __unused)
264bd687ebfSPeter Grehan {
26577d40ffdSMarcel Moolenaar struct hrowpic_softc *sc;
26677d40ffdSMarcel Moolenaar
26777d40ffdSMarcel Moolenaar sc = device_get_softc(dev);
26877d40ffdSMarcel Moolenaar hrowpic_toggle_irq(sc, irq, 0);
269bd687ebfSPeter Grehan }
270bd687ebfSPeter Grehan
27177d40ffdSMarcel Moolenaar static void
hrowpic_unmask(device_t dev,u_int irq,void * priv __unused)27256505ec0SJustin Hibbits hrowpic_unmask(device_t dev, u_int irq, void *priv __unused)
273bd687ebfSPeter Grehan {
27477d40ffdSMarcel Moolenaar struct hrowpic_softc *sc;
275bd687ebfSPeter Grehan
27677d40ffdSMarcel Moolenaar sc = device_get_softc(dev);
27777d40ffdSMarcel Moolenaar hrowpic_toggle_irq(sc, irq, 1);
278bd687ebfSPeter Grehan }
279