xref: /freebsd/sys/powerpc/powermac/hrowpic.c (revision 71e3c3083b47ad0f04322c5a1173377433c05a6e)
160727d8bSWarner Losh /*-
2*71e3c308SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
3*71e3c308SPedro 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  * $FreeBSD$
30bd687ebfSPeter Grehan  */
31bd687ebfSPeter Grehan 
32bd687ebfSPeter Grehan /*
33bd687ebfSPeter Grehan  * A driver for the PIC found in the Heathrow/Paddington MacIO chips.
34bd687ebfSPeter Grehan  * This was superseded by an OpenPIC in the Keylargo and beyond
35bd687ebfSPeter Grehan  * MacIO versions.
36bd687ebfSPeter Grehan  */
37bd687ebfSPeter Grehan 
38bd687ebfSPeter Grehan #include <sys/param.h>
39bd687ebfSPeter Grehan #include <sys/systm.h>
4040cdee9dSPeter Grehan #include <sys/module.h>
41bd687ebfSPeter Grehan #include <sys/bus.h>
42bd687ebfSPeter Grehan #include <sys/conf.h>
43bd687ebfSPeter Grehan #include <sys/kernel.h>
4477d40ffdSMarcel Moolenaar #include <sys/rman.h>
45bd687ebfSPeter Grehan 
4626280d88SMarius Strobl #include <dev/ofw/ofw_bus.h>
47bd687ebfSPeter Grehan #include <dev/ofw/openfirm.h>
48bd687ebfSPeter Grehan 
49bd687ebfSPeter Grehan #include <machine/bus.h>
50bd687ebfSPeter Grehan #include <machine/intr_machdep.h>
51bd687ebfSPeter Grehan #include <machine/md_var.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 
1726d2d7b8cSMarcel Moolenaar 	powerpc_register_pic(dev, ofw_bus_get_node(dev), 64, 0, FALSE);
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 
18637a7f596SNathan Whitehorn 	KASSERT((irq > 0) && (irq <= HROWPIC_IRQMAX), ("en irq out of range"));
18737a7f596SNathan Whitehorn 
18837a7f596SNathan Whitehorn 	/*
18937a7f596SNathan Whitehorn 	 * Humor the SMP layer if it wants to set up an IPI handler.
19037a7f596SNathan Whitehorn 	 */
19137a7f596SNathan Whitehorn 	if (irq == HROWPIC_IRQMAX)
19237a7f596SNathan Whitehorn 		return;
193bd687ebfSPeter Grehan 
194bd687ebfSPeter Grehan 	/*
195bd687ebfSPeter Grehan 	 * Calculate prim/sec register bank for the IRQ, update soft copy,
196bd687ebfSPeter Grehan 	 * and enable the IRQ as an interrupt source
197bd687ebfSPeter Grehan 	 */
198bd687ebfSPeter Grehan 	roffset = HPIC_INT_TO_BANK(irq);
199bd687ebfSPeter Grehan 	rbit = HPIC_INT_TO_REGBIT(irq);
200bd687ebfSPeter Grehan 
201bd687ebfSPeter Grehan 	if (enable)
202bd687ebfSPeter Grehan 		sc->sc_softreg[roffset] |= (1 << rbit);
203bd687ebfSPeter Grehan 	else
204bd687ebfSPeter Grehan 		sc->sc_softreg[roffset] &= ~(1 << rbit);
205bd687ebfSPeter Grehan 
206bd687ebfSPeter Grehan 	hrowpic_write_reg(sc, HPIC_ENABLE, roffset, sc->sc_softreg[roffset]);
207bd687ebfSPeter Grehan }
208bd687ebfSPeter Grehan 
20977d40ffdSMarcel Moolenaar /*
21077d40ffdSMarcel Moolenaar  * PIC I/F methods.
21177d40ffdSMarcel Moolenaar  */
21277d40ffdSMarcel Moolenaar 
213bd687ebfSPeter Grehan static void
21477d40ffdSMarcel Moolenaar hrowpic_dispatch(device_t dev, struct trapframe *tf)
215bd687ebfSPeter Grehan {
21677d40ffdSMarcel Moolenaar 	struct hrowpic_softc *sc;
21777d40ffdSMarcel Moolenaar 	uint64_t mask;
21877d40ffdSMarcel Moolenaar 	uint32_t reg;
21977d40ffdSMarcel Moolenaar 	u_int irq;
22077d40ffdSMarcel Moolenaar 
22177d40ffdSMarcel Moolenaar 	sc = device_get_softc(dev);
22277d40ffdSMarcel Moolenaar 	while (1) {
22377d40ffdSMarcel Moolenaar 		mask = hrowpic_read_reg(sc, HPIC_STATUS, HPIC_SECONDARY);
22477d40ffdSMarcel Moolenaar 		reg = hrowpic_read_reg(sc, HPIC_STATUS, HPIC_PRIMARY);
22577d40ffdSMarcel Moolenaar 		mask = (mask << 32) | reg;
22677d40ffdSMarcel Moolenaar 		if (mask == 0)
22777d40ffdSMarcel Moolenaar 			break;
22877d40ffdSMarcel Moolenaar 
22977d40ffdSMarcel Moolenaar 		irq = 0;
23077d40ffdSMarcel Moolenaar 		while (irq < HROWPIC_IRQMAX) {
23177d40ffdSMarcel Moolenaar 			if (mask & 1)
23277d40ffdSMarcel Moolenaar 				powerpc_dispatch_intr(sc->sc_vector[irq], tf);
23377d40ffdSMarcel Moolenaar 			mask >>= 1;
23477d40ffdSMarcel Moolenaar 			irq++;
23577d40ffdSMarcel Moolenaar 		}
23677d40ffdSMarcel Moolenaar 	}
23777d40ffdSMarcel Moolenaar }
23877d40ffdSMarcel Moolenaar 
23977d40ffdSMarcel Moolenaar static void
24077d40ffdSMarcel Moolenaar hrowpic_enable(device_t dev, u_int irq, u_int vector)
24177d40ffdSMarcel Moolenaar {
242bd687ebfSPeter Grehan 	struct hrowpic_softc *sc;
243bd687ebfSPeter Grehan 
24477d40ffdSMarcel Moolenaar 	sc = device_get_softc(dev);
24577d40ffdSMarcel Moolenaar 	sc->sc_vector[irq] = vector;
24677d40ffdSMarcel Moolenaar 	hrowpic_toggle_irq(sc, irq, 1);
247bd687ebfSPeter Grehan }
248bd687ebfSPeter Grehan 
249bd687ebfSPeter Grehan static void
2509e731915SRui Paulo hrowpic_eoi(device_t dev, u_int irq)
251bd687ebfSPeter Grehan {
25277d40ffdSMarcel Moolenaar 	struct hrowpic_softc *sc;
25377d40ffdSMarcel Moolenaar 	int bank;
25477d40ffdSMarcel Moolenaar 
25577d40ffdSMarcel Moolenaar 	sc = device_get_softc(dev);
25677d40ffdSMarcel Moolenaar 	bank = (irq >= 32) ? HPIC_SECONDARY : HPIC_PRIMARY ;
25777d40ffdSMarcel Moolenaar 	hrowpic_write_reg(sc, HPIC_CLEAR, bank, 1U << (irq & 0x1f));
258bd687ebfSPeter Grehan }
259bd687ebfSPeter Grehan 
260bd687ebfSPeter Grehan static void
26105c62b81SMarcel Moolenaar hrowpic_ipi(device_t dev, u_int irq)
26205c62b81SMarcel Moolenaar {
26305c62b81SMarcel Moolenaar 	/* No SMP support. */
26405c62b81SMarcel Moolenaar }
26505c62b81SMarcel Moolenaar 
26605c62b81SMarcel Moolenaar static void
26777d40ffdSMarcel Moolenaar hrowpic_mask(device_t dev, u_int irq)
268bd687ebfSPeter Grehan {
26977d40ffdSMarcel Moolenaar 	struct hrowpic_softc *sc;
27077d40ffdSMarcel Moolenaar 
27177d40ffdSMarcel Moolenaar 	sc = device_get_softc(dev);
27277d40ffdSMarcel Moolenaar 	hrowpic_toggle_irq(sc, irq, 0);
273bd687ebfSPeter Grehan }
274bd687ebfSPeter Grehan 
27577d40ffdSMarcel Moolenaar static void
27677d40ffdSMarcel Moolenaar hrowpic_unmask(device_t dev, u_int irq)
277bd687ebfSPeter Grehan {
27877d40ffdSMarcel Moolenaar 	struct hrowpic_softc *sc;
279bd687ebfSPeter Grehan 
28077d40ffdSMarcel Moolenaar 	sc = device_get_softc(dev);
28177d40ffdSMarcel Moolenaar 	hrowpic_toggle_irq(sc, irq, 1);
282bd687ebfSPeter Grehan }
283