xref: /freebsd/sys/arm/broadcom/bcm2835/bcm2836.c (revision e2e050c8ef733138fc6a9e514e4b856fefbc3ff1)
14e46a66eSAndrew Turner /*
24e46a66eSAndrew Turner  * Copyright 2015 Andrew Turner.
3120b6fc9SSvatopluk Kraus  * Copyright 2016 Svatopluk Kraus
44e46a66eSAndrew Turner  * All rights reserved.
54e46a66eSAndrew Turner  *
64e46a66eSAndrew Turner  * Redistribution and use in source and binary forms, with or without
74e46a66eSAndrew Turner  * modification, are permitted provided that the following conditions are
84e46a66eSAndrew Turner  * met:
94e46a66eSAndrew Turner  *
104e46a66eSAndrew Turner  *  1. Redistributions of source code must retain the above copyright
114e46a66eSAndrew Turner  *     notice, this list of conditions and the following disclaimer.
124e46a66eSAndrew Turner  *  2. Redistributions in binary form must reproduce the above copyright
134e46a66eSAndrew Turner  *     notice, this list of conditions and the following disclaimer in the
144e46a66eSAndrew Turner  *     documentation and/or other materials provided with the distribution.
154e46a66eSAndrew Turner  *
164e46a66eSAndrew Turner  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
174e46a66eSAndrew Turner  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
184e46a66eSAndrew Turner  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
194e46a66eSAndrew Turner  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE
204e46a66eSAndrew Turner  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
214e46a66eSAndrew Turner  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
224e46a66eSAndrew Turner  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
234e46a66eSAndrew Turner  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
244e46a66eSAndrew Turner  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
254e46a66eSAndrew Turner  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
264e46a66eSAndrew Turner  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
274e46a66eSAndrew Turner  */
284e46a66eSAndrew Turner 
294e46a66eSAndrew Turner #include <sys/cdefs.h>
304e46a66eSAndrew Turner __FBSDID("$FreeBSD$");
314e46a66eSAndrew Turner 
32120b6fc9SSvatopluk Kraus #include "opt_platform.h"
33120b6fc9SSvatopluk Kraus 
344e46a66eSAndrew Turner #include <sys/param.h>
354e46a66eSAndrew Turner #include <sys/systm.h>
364e46a66eSAndrew Turner #include <sys/bus.h>
37120b6fc9SSvatopluk Kraus #include <sys/cpuset.h>
384e46a66eSAndrew Turner #include <sys/kernel.h>
39*e2e050c8SConrad Meyer #include <sys/lock.h>
404e46a66eSAndrew Turner #include <sys/module.h>
41*e2e050c8SConrad Meyer #include <sys/mutex.h>
42120b6fc9SSvatopluk Kraus #include <sys/proc.h>
434e46a66eSAndrew Turner #include <sys/rman.h>
44120b6fc9SSvatopluk Kraus #ifdef SMP
45120b6fc9SSvatopluk Kraus #include <sys/smp.h>
46120b6fc9SSvatopluk Kraus #endif
474e46a66eSAndrew Turner 
484e46a66eSAndrew Turner #include <machine/bus.h>
49120b6fc9SSvatopluk Kraus #include <machine/intr.h>
504e46a66eSAndrew Turner #include <machine/resource.h>
51120b6fc9SSvatopluk Kraus #ifdef SMP
52120b6fc9SSvatopluk Kraus #include <machine/smp.h>
53120b6fc9SSvatopluk Kraus #endif
544e46a66eSAndrew Turner 
554e46a66eSAndrew Turner #include <dev/ofw/ofw_bus_subr.h>
564e46a66eSAndrew Turner #include <dev/ofw/ofw_bus.h>
574e46a66eSAndrew Turner 
58120b6fc9SSvatopluk Kraus #include "pic_if.h"
594e46a66eSAndrew Turner 
60120b6fc9SSvatopluk Kraus #define	BCM_LINTC_CONTROL_REG		0x00
61120b6fc9SSvatopluk Kraus #define	BCM_LINTC_PRESCALER_REG		0x08
62120b6fc9SSvatopluk Kraus #define	BCM_LINTC_GPU_ROUTING_REG	0x0c
63120b6fc9SSvatopluk Kraus #define	BCM_LINTC_PMU_ROUTING_SET_REG	0x10
64120b6fc9SSvatopluk Kraus #define	BCM_LINTC_PMU_ROUTING_CLR_REG	0x14
65120b6fc9SSvatopluk Kraus #define	BCM_LINTC_TIMER_CFG_REG(n)	(0x40 + (n) * 4)
66120b6fc9SSvatopluk Kraus #define	BCM_LINTC_MBOX_CFG_REG(n)	(0x50 + (n) * 4)
67120b6fc9SSvatopluk Kraus #define	BCM_LINTC_PENDING_REG(n)	(0x60 + (n) * 4)
68120b6fc9SSvatopluk Kraus #define	BCM_LINTC_MBOX0_SET_REG(n)	(0x80 + (n) * 16)
69120b6fc9SSvatopluk Kraus #define	BCM_LINTC_MBOX1_SET_REG(n)	(0x84 + (n) * 16)
70120b6fc9SSvatopluk Kraus #define	BCM_LINTC_MBOX2_SET_REG(n)	(0x88 + (n) * 16)
71120b6fc9SSvatopluk Kraus #define	BCM_LINTC_MBOX3_SET_REG(n)	(0x8C + (n) * 16)
72120b6fc9SSvatopluk Kraus #define	BCM_LINTC_MBOX0_CLR_REG(n)	(0xC0 + (n) * 16)
73120b6fc9SSvatopluk Kraus #define	BCM_LINTC_MBOX1_CLR_REG(n)	(0xC4 + (n) * 16)
74120b6fc9SSvatopluk Kraus #define	BCM_LINTC_MBOX2_CLR_REG(n)	(0xC8 + (n) * 16)
75120b6fc9SSvatopluk Kraus #define	BCM_LINTC_MBOX3_CLR_REG(n)	(0xCC + (n) * 16)
76120b6fc9SSvatopluk Kraus 
77120b6fc9SSvatopluk Kraus /* Prescaler Register */
78120b6fc9SSvatopluk Kraus #define	BCM_LINTC_PSR_19_2		0x80000000	/* 19.2 MHz */
79120b6fc9SSvatopluk Kraus 
80120b6fc9SSvatopluk Kraus /* GPU Interrupt Routing Register */
81120b6fc9SSvatopluk Kraus #define	BCM_LINTC_GIRR_IRQ_CORE(n)	(n)
82120b6fc9SSvatopluk Kraus #define	BCM_LINTC_GIRR_FIQ_CORE(n)	((n) << 2)
83120b6fc9SSvatopluk Kraus 
84120b6fc9SSvatopluk Kraus /* PMU Interrupt Routing Register */
85120b6fc9SSvatopluk Kraus #define	BCM_LINTC_PIRR_IRQ_EN_CORE(n)	(1 << (n))
86120b6fc9SSvatopluk Kraus #define	BCM_LINTC_PIRR_FIQ_EN_CORE(n)	(1 << ((n) + 4))
87120b6fc9SSvatopluk Kraus 
88120b6fc9SSvatopluk Kraus /* Timer Config Register */
89120b6fc9SSvatopluk Kraus #define	BCM_LINTC_TCR_IRQ_EN_TIMER(n)	(1 << (n))
90120b6fc9SSvatopluk Kraus #define	BCM_LINTC_TCR_FIQ_EN_TIMER(n)	(1 << ((n) + 4))
91120b6fc9SSvatopluk Kraus 
92120b6fc9SSvatopluk Kraus /* MBOX Config Register */
93120b6fc9SSvatopluk Kraus #define	BCM_LINTC_MCR_IRQ_EN_MBOX(n)	(1 << (n))
94120b6fc9SSvatopluk Kraus #define	BCM_LINTC_MCR_FIQ_EN_MBOX(n)	(1 << ((n) + 4))
95120b6fc9SSvatopluk Kraus 
96120b6fc9SSvatopluk Kraus #define	BCM_LINTC_CNTPSIRQ_IRQ		0
97120b6fc9SSvatopluk Kraus #define	BCM_LINTC_CNTPNSIRQ_IRQ		1
98120b6fc9SSvatopluk Kraus #define	BCM_LINTC_CNTHPIRQ_IRQ		2
99120b6fc9SSvatopluk Kraus #define	BCM_LINTC_CNTVIRQ_IRQ		3
100120b6fc9SSvatopluk Kraus #define	BCM_LINTC_MBOX0_IRQ		4
101120b6fc9SSvatopluk Kraus #define	BCM_LINTC_MBOX1_IRQ		5
102120b6fc9SSvatopluk Kraus #define	BCM_LINTC_MBOX2_IRQ		6
103120b6fc9SSvatopluk Kraus #define	BCM_LINTC_MBOX3_IRQ		7
104120b6fc9SSvatopluk Kraus #define	BCM_LINTC_GPU_IRQ		8
105120b6fc9SSvatopluk Kraus #define	BCM_LINTC_PMU_IRQ		9
106120b6fc9SSvatopluk Kraus #define	BCM_LINTC_AXI_IRQ		10
107120b6fc9SSvatopluk Kraus #define	BCM_LINTC_LTIMER_IRQ		11
108120b6fc9SSvatopluk Kraus 
109120b6fc9SSvatopluk Kraus #define	BCM_LINTC_NIRQS			12
110120b6fc9SSvatopluk Kraus 
111120b6fc9SSvatopluk Kraus #define	BCM_LINTC_TIMER0_IRQ		BCM_LINTC_CNTPSIRQ_IRQ
112120b6fc9SSvatopluk Kraus #define	BCM_LINTC_TIMER1_IRQ		BCM_LINTC_CNTPNSIRQ_IRQ
113120b6fc9SSvatopluk Kraus #define	BCM_LINTC_TIMER2_IRQ		BCM_LINTC_CNTHPIRQ_IRQ
114120b6fc9SSvatopluk Kraus #define	BCM_LINTC_TIMER3_IRQ		BCM_LINTC_CNTVIRQ_IRQ
115120b6fc9SSvatopluk Kraus 
116120b6fc9SSvatopluk Kraus #define	BCM_LINTC_TIMER0_IRQ_MASK	(1 << BCM_LINTC_TIMER0_IRQ)
117120b6fc9SSvatopluk Kraus #define	BCM_LINTC_TIMER1_IRQ_MASK	(1 << BCM_LINTC_TIMER1_IRQ)
118120b6fc9SSvatopluk Kraus #define	BCM_LINTC_TIMER2_IRQ_MASK	(1 << BCM_LINTC_TIMER2_IRQ)
119120b6fc9SSvatopluk Kraus #define	BCM_LINTC_TIMER3_IRQ_MASK	(1 << BCM_LINTC_TIMER3_IRQ)
120120b6fc9SSvatopluk Kraus #define	BCM_LINTC_MBOX0_IRQ_MASK	(1 << BCM_LINTC_MBOX0_IRQ)
121120b6fc9SSvatopluk Kraus #define	BCM_LINTC_GPU_IRQ_MASK		(1 << BCM_LINTC_GPU_IRQ)
122120b6fc9SSvatopluk Kraus #define	BCM_LINTC_PMU_IRQ_MASK		(1 << BCM_LINTC_PMU_IRQ)
123120b6fc9SSvatopluk Kraus 
124120b6fc9SSvatopluk Kraus #define	BCM_LINTC_UP_PENDING_MASK	\
125120b6fc9SSvatopluk Kraus     (BCM_LINTC_TIMER0_IRQ_MASK |	\
126120b6fc9SSvatopluk Kraus      BCM_LINTC_TIMER1_IRQ_MASK |	\
127120b6fc9SSvatopluk Kraus      BCM_LINTC_TIMER2_IRQ_MASK |	\
128120b6fc9SSvatopluk Kraus      BCM_LINTC_TIMER3_IRQ_MASK |	\
129120b6fc9SSvatopluk Kraus      BCM_LINTC_GPU_IRQ_MASK |		\
130120b6fc9SSvatopluk Kraus      BCM_LINTC_PMU_IRQ_MASK)
131120b6fc9SSvatopluk Kraus 
132120b6fc9SSvatopluk Kraus #define	BCM_LINTC_SMP_PENDING_MASK	\
133120b6fc9SSvatopluk Kraus     (BCM_LINTC_UP_PENDING_MASK |	\
134120b6fc9SSvatopluk Kraus      BCM_LINTC_MBOX0_IRQ_MASK)
135120b6fc9SSvatopluk Kraus 
136120b6fc9SSvatopluk Kraus #ifdef SMP
137120b6fc9SSvatopluk Kraus #define BCM_LINTC_PENDING_MASK		BCM_LINTC_SMP_PENDING_MASK
138120b6fc9SSvatopluk Kraus #else
139120b6fc9SSvatopluk Kraus #define BCM_LINTC_PENDING_MASK		BCM_LINTC_UP_PENDING_MASK
140120b6fc9SSvatopluk Kraus #endif
141120b6fc9SSvatopluk Kraus 
142120b6fc9SSvatopluk Kraus struct bcm_lintc_irqsrc {
143120b6fc9SSvatopluk Kraus 	struct intr_irqsrc	bli_isrc;
144120b6fc9SSvatopluk Kraus 	u_int			bli_irq;
145120b6fc9SSvatopluk Kraus 	union {
146120b6fc9SSvatopluk Kraus 		u_int		bli_mask;	/* for timers */
147120b6fc9SSvatopluk Kraus 		u_int		bli_value;	/* for GPU */
148120b6fc9SSvatopluk Kraus 	};
149120b6fc9SSvatopluk Kraus };
150120b6fc9SSvatopluk Kraus 
151120b6fc9SSvatopluk Kraus struct bcm_lintc_softc {
152120b6fc9SSvatopluk Kraus 	device_t		bls_dev;
153120b6fc9SSvatopluk Kraus 	struct mtx		bls_mtx;
154120b6fc9SSvatopluk Kraus 	struct resource *	bls_mem;
155120b6fc9SSvatopluk Kraus 	bus_space_tag_t		bls_bst;
156120b6fc9SSvatopluk Kraus 	bus_space_handle_t	bls_bsh;
157120b6fc9SSvatopluk Kraus 	struct bcm_lintc_irqsrc	bls_isrcs[BCM_LINTC_NIRQS];
158120b6fc9SSvatopluk Kraus };
159120b6fc9SSvatopluk Kraus 
160120b6fc9SSvatopluk Kraus static struct bcm_lintc_softc *bcm_lintc_sc;
161120b6fc9SSvatopluk Kraus 
162120b6fc9SSvatopluk Kraus #ifdef SMP
163120b6fc9SSvatopluk Kraus #define BCM_LINTC_NIPIS		32	/* only mailbox 0 is used for IPI */
164120b6fc9SSvatopluk Kraus CTASSERT(INTR_IPI_COUNT <= BCM_LINTC_NIPIS);
165120b6fc9SSvatopluk Kraus #endif
166120b6fc9SSvatopluk Kraus 
167120b6fc9SSvatopluk Kraus #define	BCM_LINTC_LOCK(sc)		mtx_lock_spin(&(sc)->bls_mtx)
168120b6fc9SSvatopluk Kraus #define	BCM_LINTC_UNLOCK(sc)		mtx_unlock_spin(&(sc)->bls_mtx)
169120b6fc9SSvatopluk Kraus #define	BCM_LINTC_LOCK_INIT(sc)		mtx_init(&(sc)->bls_mtx,	\
170120b6fc9SSvatopluk Kraus     device_get_nameunit((sc)->bls_dev), "bmc_local_intc", MTX_SPIN)
171120b6fc9SSvatopluk Kraus #define	BCM_LINTC_LOCK_DESTROY(sc)	mtx_destroy(&(sc)->bls_mtx)
172120b6fc9SSvatopluk Kraus 
173120b6fc9SSvatopluk Kraus #define	bcm_lintc_read_4(sc, reg)		\
174120b6fc9SSvatopluk Kraus     bus_space_read_4((sc)->bls_bst, (sc)->bls_bsh, (reg))
175120b6fc9SSvatopluk Kraus #define	bcm_lintc_write_4(sc, reg, val)		\
176120b6fc9SSvatopluk Kraus     bus_space_write_4((sc)->bls_bst, (sc)->bls_bsh, (reg), (val))
177120b6fc9SSvatopluk Kraus 
178120b6fc9SSvatopluk Kraus static inline void
179120b6fc9SSvatopluk Kraus bcm_lintc_rwreg_clr(struct bcm_lintc_softc *sc, uint32_t reg,
180120b6fc9SSvatopluk Kraus     uint32_t mask)
181120b6fc9SSvatopluk Kraus {
182120b6fc9SSvatopluk Kraus 
183120b6fc9SSvatopluk Kraus 	bcm_lintc_write_4(sc, reg, bcm_lintc_read_4(sc, reg) & ~mask);
184120b6fc9SSvatopluk Kraus }
185120b6fc9SSvatopluk Kraus 
186120b6fc9SSvatopluk Kraus static inline void
187120b6fc9SSvatopluk Kraus bcm_lintc_rwreg_set(struct bcm_lintc_softc *sc, uint32_t reg,
188120b6fc9SSvatopluk Kraus     uint32_t mask)
189120b6fc9SSvatopluk Kraus {
190120b6fc9SSvatopluk Kraus 
191120b6fc9SSvatopluk Kraus 	bcm_lintc_write_4(sc, reg, bcm_lintc_read_4(sc, reg) | mask);
192120b6fc9SSvatopluk Kraus }
193120b6fc9SSvatopluk Kraus 
194120b6fc9SSvatopluk Kraus static void
195120b6fc9SSvatopluk Kraus bcm_lintc_timer_mask(struct bcm_lintc_softc *sc, struct bcm_lintc_irqsrc *bli)
196120b6fc9SSvatopluk Kraus {
197120b6fc9SSvatopluk Kraus 	cpuset_t *cpus;
198120b6fc9SSvatopluk Kraus 	uint32_t cpu;
199120b6fc9SSvatopluk Kraus 
200120b6fc9SSvatopluk Kraus 	cpus = &bli->bli_isrc.isrc_cpu;
201120b6fc9SSvatopluk Kraus 
202120b6fc9SSvatopluk Kraus 	BCM_LINTC_LOCK(sc);
203120b6fc9SSvatopluk Kraus 	for (cpu = 0; cpu < 4; cpu++)
204120b6fc9SSvatopluk Kraus 		if (CPU_ISSET(cpu, cpus))
205120b6fc9SSvatopluk Kraus 			bcm_lintc_rwreg_clr(sc, BCM_LINTC_TIMER_CFG_REG(cpu),
206120b6fc9SSvatopluk Kraus 			    bli->bli_mask);
207120b6fc9SSvatopluk Kraus 	BCM_LINTC_UNLOCK(sc);
208120b6fc9SSvatopluk Kraus }
209120b6fc9SSvatopluk Kraus 
210120b6fc9SSvatopluk Kraus static void
211120b6fc9SSvatopluk Kraus bcm_lintc_timer_unmask(struct bcm_lintc_softc *sc, struct bcm_lintc_irqsrc *bli)
212120b6fc9SSvatopluk Kraus {
213120b6fc9SSvatopluk Kraus 	cpuset_t *cpus;
214120b6fc9SSvatopluk Kraus 	uint32_t cpu;
215120b6fc9SSvatopluk Kraus 
216120b6fc9SSvatopluk Kraus 	cpus = &bli->bli_isrc.isrc_cpu;
217120b6fc9SSvatopluk Kraus 
218120b6fc9SSvatopluk Kraus 	BCM_LINTC_LOCK(sc);
219120b6fc9SSvatopluk Kraus 	for (cpu = 0; cpu < 4; cpu++)
220120b6fc9SSvatopluk Kraus 		if (CPU_ISSET(cpu, cpus))
221120b6fc9SSvatopluk Kraus 			bcm_lintc_rwreg_set(sc, BCM_LINTC_TIMER_CFG_REG(cpu),
222120b6fc9SSvatopluk Kraus 			    bli->bli_mask);
223120b6fc9SSvatopluk Kraus 	BCM_LINTC_UNLOCK(sc);
224120b6fc9SSvatopluk Kraus }
225120b6fc9SSvatopluk Kraus 
226120b6fc9SSvatopluk Kraus static inline void
227120b6fc9SSvatopluk Kraus bcm_lintc_gpu_mask(struct bcm_lintc_softc *sc, struct bcm_lintc_irqsrc *bli)
228120b6fc9SSvatopluk Kraus {
229120b6fc9SSvatopluk Kraus 
230120b6fc9SSvatopluk Kraus 	/* It's accessed just and only by one core. */
231120b6fc9SSvatopluk Kraus 	bcm_lintc_write_4(sc, BCM_LINTC_GPU_ROUTING_REG, 0);
232120b6fc9SSvatopluk Kraus }
233120b6fc9SSvatopluk Kraus 
234120b6fc9SSvatopluk Kraus static inline void
235120b6fc9SSvatopluk Kraus bcm_lintc_gpu_unmask(struct bcm_lintc_softc *sc, struct bcm_lintc_irqsrc *bli)
236120b6fc9SSvatopluk Kraus {
237120b6fc9SSvatopluk Kraus 
238120b6fc9SSvatopluk Kraus 	/* It's accessed just and only by one core. */
239120b6fc9SSvatopluk Kraus 	bcm_lintc_write_4(sc, BCM_LINTC_GPU_ROUTING_REG, bli->bli_value);
240120b6fc9SSvatopluk Kraus }
241120b6fc9SSvatopluk Kraus 
242120b6fc9SSvatopluk Kraus static inline void
243120b6fc9SSvatopluk Kraus bcm_lintc_pmu_mask(struct bcm_lintc_softc *sc, struct bcm_lintc_irqsrc *bli)
244120b6fc9SSvatopluk Kraus {
245120b6fc9SSvatopluk Kraus 	cpuset_t *cpus;
246120b6fc9SSvatopluk Kraus 	uint32_t cpu, mask;
247120b6fc9SSvatopluk Kraus 
248120b6fc9SSvatopluk Kraus 	mask = 0;
249120b6fc9SSvatopluk Kraus 	cpus = &bli->bli_isrc.isrc_cpu;
250120b6fc9SSvatopluk Kraus 
251120b6fc9SSvatopluk Kraus 	BCM_LINTC_LOCK(sc);
252120b6fc9SSvatopluk Kraus 	for (cpu = 0; cpu < 4; cpu++)
253120b6fc9SSvatopluk Kraus 		if (CPU_ISSET(cpu, cpus))
254120b6fc9SSvatopluk Kraus 			mask |= BCM_LINTC_PIRR_IRQ_EN_CORE(cpu);
255120b6fc9SSvatopluk Kraus 	/* Write-clear register. */
256120b6fc9SSvatopluk Kraus 	bcm_lintc_write_4(sc, BCM_LINTC_PMU_ROUTING_CLR_REG, mask);
257120b6fc9SSvatopluk Kraus 	BCM_LINTC_UNLOCK(sc);
258120b6fc9SSvatopluk Kraus }
259120b6fc9SSvatopluk Kraus 
260120b6fc9SSvatopluk Kraus static inline void
261120b6fc9SSvatopluk Kraus bcm_lintc_pmu_unmask(struct bcm_lintc_softc *sc, struct bcm_lintc_irqsrc *bli)
262120b6fc9SSvatopluk Kraus {
263120b6fc9SSvatopluk Kraus 	cpuset_t *cpus;
264120b6fc9SSvatopluk Kraus 	uint32_t cpu, mask;
265120b6fc9SSvatopluk Kraus 
266120b6fc9SSvatopluk Kraus 	mask = 0;
267120b6fc9SSvatopluk Kraus 	cpus = &bli->bli_isrc.isrc_cpu;
268120b6fc9SSvatopluk Kraus 
269120b6fc9SSvatopluk Kraus 	BCM_LINTC_LOCK(sc);
270120b6fc9SSvatopluk Kraus 	for (cpu = 0; cpu < 4; cpu++)
271120b6fc9SSvatopluk Kraus 		if (CPU_ISSET(cpu, cpus))
272120b6fc9SSvatopluk Kraus 			mask |= BCM_LINTC_PIRR_IRQ_EN_CORE(cpu);
273120b6fc9SSvatopluk Kraus 	/* Write-set register. */
274120b6fc9SSvatopluk Kraus 	bcm_lintc_write_4(sc, BCM_LINTC_PMU_ROUTING_SET_REG, mask);
275120b6fc9SSvatopluk Kraus 	BCM_LINTC_UNLOCK(sc);
276120b6fc9SSvatopluk Kraus }
277120b6fc9SSvatopluk Kraus 
278120b6fc9SSvatopluk Kraus static void
279120b6fc9SSvatopluk Kraus bcm_lintc_mask(struct bcm_lintc_softc *sc, struct bcm_lintc_irqsrc *bli)
280120b6fc9SSvatopluk Kraus {
281120b6fc9SSvatopluk Kraus 
282120b6fc9SSvatopluk Kraus 	switch (bli->bli_irq) {
283120b6fc9SSvatopluk Kraus 	case BCM_LINTC_TIMER0_IRQ:
284120b6fc9SSvatopluk Kraus 	case BCM_LINTC_TIMER1_IRQ:
285120b6fc9SSvatopluk Kraus 	case BCM_LINTC_TIMER2_IRQ:
286120b6fc9SSvatopluk Kraus 	case BCM_LINTC_TIMER3_IRQ:
287120b6fc9SSvatopluk Kraus 		bcm_lintc_timer_mask(sc, bli);
288120b6fc9SSvatopluk Kraus 		return;
289120b6fc9SSvatopluk Kraus 	case BCM_LINTC_MBOX0_IRQ:
290120b6fc9SSvatopluk Kraus 	case BCM_LINTC_MBOX1_IRQ:
291120b6fc9SSvatopluk Kraus 	case BCM_LINTC_MBOX2_IRQ:
292120b6fc9SSvatopluk Kraus 	case BCM_LINTC_MBOX3_IRQ:
293120b6fc9SSvatopluk Kraus 		return;
294120b6fc9SSvatopluk Kraus 	case BCM_LINTC_GPU_IRQ:
295120b6fc9SSvatopluk Kraus 		bcm_lintc_gpu_mask(sc, bli);
296120b6fc9SSvatopluk Kraus 		return;
297120b6fc9SSvatopluk Kraus 	case BCM_LINTC_PMU_IRQ:
298120b6fc9SSvatopluk Kraus 		bcm_lintc_pmu_mask(sc, bli);
299120b6fc9SSvatopluk Kraus 		return;
300120b6fc9SSvatopluk Kraus 	default:
301120b6fc9SSvatopluk Kraus 		panic("%s: not implemented for irq %u", __func__, bli->bli_irq);
302120b6fc9SSvatopluk Kraus 	}
303120b6fc9SSvatopluk Kraus }
304120b6fc9SSvatopluk Kraus 
305120b6fc9SSvatopluk Kraus static void
306120b6fc9SSvatopluk Kraus bcm_lintc_unmask(struct bcm_lintc_softc *sc, struct bcm_lintc_irqsrc *bli)
307120b6fc9SSvatopluk Kraus {
308120b6fc9SSvatopluk Kraus 
309120b6fc9SSvatopluk Kraus 	switch (bli->bli_irq) {
310120b6fc9SSvatopluk Kraus 	case BCM_LINTC_TIMER0_IRQ:
311120b6fc9SSvatopluk Kraus 	case BCM_LINTC_TIMER1_IRQ:
312120b6fc9SSvatopluk Kraus 	case BCM_LINTC_TIMER2_IRQ:
313120b6fc9SSvatopluk Kraus 	case BCM_LINTC_TIMER3_IRQ:
314120b6fc9SSvatopluk Kraus 		bcm_lintc_timer_unmask(sc, bli);
315120b6fc9SSvatopluk Kraus 		return;
316120b6fc9SSvatopluk Kraus 	case BCM_LINTC_MBOX0_IRQ:
317120b6fc9SSvatopluk Kraus 	case BCM_LINTC_MBOX1_IRQ:
318120b6fc9SSvatopluk Kraus 	case BCM_LINTC_MBOX2_IRQ:
319120b6fc9SSvatopluk Kraus 	case BCM_LINTC_MBOX3_IRQ:
320120b6fc9SSvatopluk Kraus 		return;
321120b6fc9SSvatopluk Kraus 	case BCM_LINTC_GPU_IRQ:
322120b6fc9SSvatopluk Kraus 		bcm_lintc_gpu_unmask(sc, bli);
323120b6fc9SSvatopluk Kraus 		return;
324120b6fc9SSvatopluk Kraus 	case BCM_LINTC_PMU_IRQ:
325120b6fc9SSvatopluk Kraus 		bcm_lintc_pmu_unmask(sc, bli);
326120b6fc9SSvatopluk Kraus 		return;
327120b6fc9SSvatopluk Kraus 	default:
328120b6fc9SSvatopluk Kraus 		panic("%s: not implemented for irq %u", __func__, bli->bli_irq);
329120b6fc9SSvatopluk Kraus 	}
330120b6fc9SSvatopluk Kraus }
331120b6fc9SSvatopluk Kraus 
332120b6fc9SSvatopluk Kraus #ifdef SMP
333120b6fc9SSvatopluk Kraus static inline void
334120b6fc9SSvatopluk Kraus bcm_lintc_ipi_write(struct bcm_lintc_softc *sc, cpuset_t cpus, u_int ipi)
335120b6fc9SSvatopluk Kraus {
336120b6fc9SSvatopluk Kraus 	u_int cpu;
337120b6fc9SSvatopluk Kraus 	uint32_t mask;
338120b6fc9SSvatopluk Kraus 
339120b6fc9SSvatopluk Kraus 	mask = 1 << ipi;
340120b6fc9SSvatopluk Kraus 	for (cpu = 0; cpu < mp_ncpus; cpu++)
341120b6fc9SSvatopluk Kraus 		if (CPU_ISSET(cpu, &cpus))
342120b6fc9SSvatopluk Kraus 			bcm_lintc_write_4(sc, BCM_LINTC_MBOX0_SET_REG(cpu),
343120b6fc9SSvatopluk Kraus 			    mask);
344120b6fc9SSvatopluk Kraus }
345120b6fc9SSvatopluk Kraus 
346120b6fc9SSvatopluk Kraus static inline void
347120b6fc9SSvatopluk Kraus bcm_lintc_ipi_dispatch(struct bcm_lintc_softc *sc, u_int cpu,
348120b6fc9SSvatopluk Kraus     struct trapframe *tf)
349120b6fc9SSvatopluk Kraus {
350120b6fc9SSvatopluk Kraus 	u_int ipi;
351120b6fc9SSvatopluk Kraus 	uint32_t mask;
352120b6fc9SSvatopluk Kraus 
353120b6fc9SSvatopluk Kraus 	mask = bcm_lintc_read_4(sc, BCM_LINTC_MBOX0_CLR_REG(cpu));
354120b6fc9SSvatopluk Kraus 	if (mask == 0) {
355120b6fc9SSvatopluk Kraus 		device_printf(sc->bls_dev, "Spurious ipi detected\n");
356120b6fc9SSvatopluk Kraus 		return;
357120b6fc9SSvatopluk Kraus 	}
358120b6fc9SSvatopluk Kraus 
359120b6fc9SSvatopluk Kraus 	for (ipi = 0; mask != 0; mask >>= 1, ipi++) {
360120b6fc9SSvatopluk Kraus 		if ((mask & 0x01) == 0)
361120b6fc9SSvatopluk Kraus 			continue;
362120b6fc9SSvatopluk Kraus 		/*
363120b6fc9SSvatopluk Kraus 		 * Clear an IPI before dispatching to not miss anyone
364120b6fc9SSvatopluk Kraus 		 * and make sure that it's observed by everybody.
365120b6fc9SSvatopluk Kraus 		 */
366120b6fc9SSvatopluk Kraus 		bcm_lintc_write_4(sc, BCM_LINTC_MBOX0_CLR_REG(cpu), 1 << ipi);
3670f04f5deSOleksandr Tymoshenko #if defined(__aarch64__)
3680f04f5deSOleksandr Tymoshenko 		dsb(sy);
3690f04f5deSOleksandr Tymoshenko #else
370120b6fc9SSvatopluk Kraus 		dsb();
3710f04f5deSOleksandr Tymoshenko #endif
372120b6fc9SSvatopluk Kraus 		intr_ipi_dispatch(ipi, tf);
373120b6fc9SSvatopluk Kraus 	}
374120b6fc9SSvatopluk Kraus }
375120b6fc9SSvatopluk Kraus #endif
376120b6fc9SSvatopluk Kraus 
377120b6fc9SSvatopluk Kraus static inline void
378120b6fc9SSvatopluk Kraus bcm_lintc_irq_dispatch(struct bcm_lintc_softc *sc, u_int irq,
379120b6fc9SSvatopluk Kraus     struct trapframe *tf)
380120b6fc9SSvatopluk Kraus {
381120b6fc9SSvatopluk Kraus 	struct bcm_lintc_irqsrc *bli;
382120b6fc9SSvatopluk Kraus 
383120b6fc9SSvatopluk Kraus 	bli = &sc->bls_isrcs[irq];
384120b6fc9SSvatopluk Kraus 	if (intr_isrc_dispatch(&bli->bli_isrc, tf) != 0)
385120b6fc9SSvatopluk Kraus 		device_printf(sc->bls_dev, "Stray irq %u detected\n", irq);
386120b6fc9SSvatopluk Kraus }
387120b6fc9SSvatopluk Kraus 
388120b6fc9SSvatopluk Kraus static int
389120b6fc9SSvatopluk Kraus bcm_lintc_intr(void *arg)
390120b6fc9SSvatopluk Kraus {
391120b6fc9SSvatopluk Kraus 	struct bcm_lintc_softc *sc;
392120b6fc9SSvatopluk Kraus 	u_int cpu;
393120b6fc9SSvatopluk Kraus 	uint32_t num, reg;
394120b6fc9SSvatopluk Kraus 	struct trapframe *tf;
395120b6fc9SSvatopluk Kraus 
396120b6fc9SSvatopluk Kraus 	sc = arg;
397120b6fc9SSvatopluk Kraus 	cpu = PCPU_GET(cpuid);
398120b6fc9SSvatopluk Kraus 	tf = curthread->td_intr_frame;
399120b6fc9SSvatopluk Kraus 
400120b6fc9SSvatopluk Kraus 	for (num = 0; ; num++) {
401120b6fc9SSvatopluk Kraus 		reg = bcm_lintc_read_4(sc, BCM_LINTC_PENDING_REG(cpu));
402120b6fc9SSvatopluk Kraus 		if ((reg & BCM_LINTC_PENDING_MASK) == 0)
403120b6fc9SSvatopluk Kraus 			break;
404120b6fc9SSvatopluk Kraus #ifdef SMP
405120b6fc9SSvatopluk Kraus 		if (reg & BCM_LINTC_MBOX0_IRQ_MASK)
406120b6fc9SSvatopluk Kraus 			bcm_lintc_ipi_dispatch(sc, cpu, tf);
407120b6fc9SSvatopluk Kraus #endif
408120b6fc9SSvatopluk Kraus 		if (reg & BCM_LINTC_TIMER0_IRQ_MASK)
409120b6fc9SSvatopluk Kraus 			bcm_lintc_irq_dispatch(sc, BCM_LINTC_TIMER0_IRQ, tf);
410120b6fc9SSvatopluk Kraus 		if (reg & BCM_LINTC_TIMER1_IRQ_MASK)
411120b6fc9SSvatopluk Kraus 			bcm_lintc_irq_dispatch(sc, BCM_LINTC_TIMER1_IRQ, tf);
412120b6fc9SSvatopluk Kraus 		if (reg & BCM_LINTC_TIMER2_IRQ_MASK)
413120b6fc9SSvatopluk Kraus 			bcm_lintc_irq_dispatch(sc, BCM_LINTC_TIMER2_IRQ, tf);
414120b6fc9SSvatopluk Kraus 		if (reg & BCM_LINTC_TIMER3_IRQ_MASK)
415120b6fc9SSvatopluk Kraus 			bcm_lintc_irq_dispatch(sc, BCM_LINTC_TIMER3_IRQ, tf);
416120b6fc9SSvatopluk Kraus 		if (reg & BCM_LINTC_GPU_IRQ_MASK)
417120b6fc9SSvatopluk Kraus 			bcm_lintc_irq_dispatch(sc, BCM_LINTC_GPU_IRQ, tf);
418120b6fc9SSvatopluk Kraus 		if (reg & BCM_LINTC_PMU_IRQ_MASK)
419120b6fc9SSvatopluk Kraus 			bcm_lintc_irq_dispatch(sc, BCM_LINTC_PMU_IRQ, tf);
420120b6fc9SSvatopluk Kraus 
421120b6fc9SSvatopluk Kraus 		arm_irq_memory_barrier(0); /* XXX */
422120b6fc9SSvatopluk Kraus 	}
423120b6fc9SSvatopluk Kraus 	reg &= ~BCM_LINTC_PENDING_MASK;
424120b6fc9SSvatopluk Kraus 	if (reg != 0)
425120b6fc9SSvatopluk Kraus 		device_printf(sc->bls_dev, "Unknown interrupt(s) %x\n", reg);
4269e655cd5SIan Lepore 	else if (num == 0 && bootverbose)
427120b6fc9SSvatopluk Kraus 		device_printf(sc->bls_dev, "Spurious interrupt detected\n");
428120b6fc9SSvatopluk Kraus 
429120b6fc9SSvatopluk Kraus 	return (FILTER_HANDLED);
430120b6fc9SSvatopluk Kraus }
431120b6fc9SSvatopluk Kraus 
432120b6fc9SSvatopluk Kraus static void
433120b6fc9SSvatopluk Kraus bcm_lintc_disable_intr(device_t dev, struct intr_irqsrc *isrc)
434120b6fc9SSvatopluk Kraus {
435120b6fc9SSvatopluk Kraus 
436120b6fc9SSvatopluk Kraus 	bcm_lintc_mask(device_get_softc(dev), (struct bcm_lintc_irqsrc *)isrc);
437120b6fc9SSvatopluk Kraus }
438120b6fc9SSvatopluk Kraus 
439120b6fc9SSvatopluk Kraus static void
440120b6fc9SSvatopluk Kraus bcm_lintc_enable_intr(device_t dev, struct intr_irqsrc *isrc)
441120b6fc9SSvatopluk Kraus {
442120b6fc9SSvatopluk Kraus 	struct bcm_lintc_irqsrc *bli = (struct bcm_lintc_irqsrc *)isrc;
443120b6fc9SSvatopluk Kraus 
444120b6fc9SSvatopluk Kraus 	arm_irq_memory_barrier(bli->bli_irq);
445120b6fc9SSvatopluk Kraus 	bcm_lintc_unmask(device_get_softc(dev), bli);
446120b6fc9SSvatopluk Kraus }
447120b6fc9SSvatopluk Kraus 
448120b6fc9SSvatopluk Kraus static int
449120b6fc9SSvatopluk Kraus bcm_lintc_map_intr(device_t dev, struct intr_map_data *data,
450120b6fc9SSvatopluk Kraus     struct intr_irqsrc **isrcp)
451120b6fc9SSvatopluk Kraus {
452cd642c88SSvatopluk Kraus 	struct intr_map_data_fdt *daf;
453120b6fc9SSvatopluk Kraus 	struct bcm_lintc_softc *sc;
454120b6fc9SSvatopluk Kraus 
455120b6fc9SSvatopluk Kraus 	if (data->type != INTR_MAP_DATA_FDT)
456120b6fc9SSvatopluk Kraus 		return (ENOTSUP);
457cd642c88SSvatopluk Kraus 
458cd642c88SSvatopluk Kraus 	daf = (struct intr_map_data_fdt *)data;
4595ebc699aSOleksandr Tymoshenko 	if (daf->ncells > 2 || daf->cells[0] >= BCM_LINTC_NIRQS)
460120b6fc9SSvatopluk Kraus 		return (EINVAL);
461120b6fc9SSvatopluk Kraus 
4625ebc699aSOleksandr Tymoshenko 	/* TODO: handle IRQ type here */
4635ebc699aSOleksandr Tymoshenko 
464120b6fc9SSvatopluk Kraus 	sc = device_get_softc(dev);
465cd642c88SSvatopluk Kraus 	*isrcp = &sc->bls_isrcs[daf->cells[0]].bli_isrc;
466120b6fc9SSvatopluk Kraus 	return (0);
467120b6fc9SSvatopluk Kraus }
468120b6fc9SSvatopluk Kraus 
469120b6fc9SSvatopluk Kraus static void
470120b6fc9SSvatopluk Kraus bcm_lintc_pre_ithread(device_t dev, struct intr_irqsrc *isrc)
471120b6fc9SSvatopluk Kraus {
472120b6fc9SSvatopluk Kraus 	struct bcm_lintc_irqsrc *bli = (struct bcm_lintc_irqsrc *)isrc;
473120b6fc9SSvatopluk Kraus 
474120b6fc9SSvatopluk Kraus 	if (bli->bli_irq == BCM_LINTC_GPU_IRQ)
475120b6fc9SSvatopluk Kraus 		bcm_lintc_gpu_mask(device_get_softc(dev), bli);
476120b6fc9SSvatopluk Kraus 	else {
477120b6fc9SSvatopluk Kraus 		/*
478120b6fc9SSvatopluk Kraus 		 * Handler for PPI interrupt does not make sense much unless
479120b6fc9SSvatopluk Kraus 		 * there is one bound ithread for each core for it. Thus the
480120b6fc9SSvatopluk Kraus 		 * interrupt can be masked on current core only while ithread
481120b6fc9SSvatopluk Kraus 		 * bounded to this core ensures unmasking on the same core.
482120b6fc9SSvatopluk Kraus 		 */
483120b6fc9SSvatopluk Kraus 		panic ("%s: handlers are not supported", __func__);
484120b6fc9SSvatopluk Kraus 	}
485120b6fc9SSvatopluk Kraus }
486120b6fc9SSvatopluk Kraus 
487120b6fc9SSvatopluk Kraus static void
488120b6fc9SSvatopluk Kraus bcm_lintc_post_ithread(device_t dev, struct intr_irqsrc *isrc)
489120b6fc9SSvatopluk Kraus {
490120b6fc9SSvatopluk Kraus 	struct bcm_lintc_irqsrc *bli = (struct bcm_lintc_irqsrc *)isrc;
491120b6fc9SSvatopluk Kraus 
492120b6fc9SSvatopluk Kraus 	if (bli->bli_irq == BCM_LINTC_GPU_IRQ)
493120b6fc9SSvatopluk Kraus 		bcm_lintc_gpu_unmask(device_get_softc(dev), bli);
494120b6fc9SSvatopluk Kraus 	else {
495120b6fc9SSvatopluk Kraus 		/* See comment in bcm_lintc_pre_ithread(). */
496120b6fc9SSvatopluk Kraus 		panic ("%s: handlers are not supported", __func__);
497120b6fc9SSvatopluk Kraus 	}
498120b6fc9SSvatopluk Kraus }
499120b6fc9SSvatopluk Kraus 
500120b6fc9SSvatopluk Kraus static void
501120b6fc9SSvatopluk Kraus bcm_lintc_post_filter(device_t dev, struct intr_irqsrc *isrc)
502120b6fc9SSvatopluk Kraus {
503120b6fc9SSvatopluk Kraus }
504120b6fc9SSvatopluk Kraus 
505120b6fc9SSvatopluk Kraus static int
506120b6fc9SSvatopluk Kraus bcm_lintc_setup_intr(device_t dev, struct intr_irqsrc *isrc,
507120b6fc9SSvatopluk Kraus     struct resource *res, struct intr_map_data *data)
508120b6fc9SSvatopluk Kraus {
509120b6fc9SSvatopluk Kraus 	struct bcm_lintc_softc *sc;
510120b6fc9SSvatopluk Kraus 
511120b6fc9SSvatopluk Kraus 	if (isrc->isrc_handlers == 0 && isrc->isrc_flags & INTR_ISRCF_PPI) {
512120b6fc9SSvatopluk Kraus 		sc = device_get_softc(dev);
513120b6fc9SSvatopluk Kraus 		BCM_LINTC_LOCK(sc);
514120b6fc9SSvatopluk Kraus 		CPU_SET(PCPU_GET(cpuid), &isrc->isrc_cpu);
515120b6fc9SSvatopluk Kraus 		BCM_LINTC_UNLOCK(sc);
516120b6fc9SSvatopluk Kraus 	}
517120b6fc9SSvatopluk Kraus 	return (0);
518120b6fc9SSvatopluk Kraus }
519120b6fc9SSvatopluk Kraus 
520120b6fc9SSvatopluk Kraus #ifdef SMP
521120b6fc9SSvatopluk Kraus static void
522120b6fc9SSvatopluk Kraus bcm_lintc_init_rwreg_on_ap(struct bcm_lintc_softc *sc, u_int cpu, u_int irq,
523120b6fc9SSvatopluk Kraus     uint32_t reg, uint32_t mask)
524120b6fc9SSvatopluk Kraus {
525120b6fc9SSvatopluk Kraus 
5265b613c19SSvatopluk Kraus 	if (intr_isrc_init_on_cpu(&sc->bls_isrcs[irq].bli_isrc, cpu))
527120b6fc9SSvatopluk Kraus 		bcm_lintc_rwreg_set(sc, reg, mask);
528120b6fc9SSvatopluk Kraus }
529120b6fc9SSvatopluk Kraus 
530120b6fc9SSvatopluk Kraus static void
531120b6fc9SSvatopluk Kraus bcm_lintc_init_pmu_on_ap(struct bcm_lintc_softc *sc, u_int cpu)
532120b6fc9SSvatopluk Kraus {
5335b613c19SSvatopluk Kraus 	struct intr_irqsrc *isrc = &sc->bls_isrcs[BCM_LINTC_PMU_IRQ].bli_isrc;
534120b6fc9SSvatopluk Kraus 
5355b613c19SSvatopluk Kraus 	if (intr_isrc_init_on_cpu(isrc, cpu)) {
536120b6fc9SSvatopluk Kraus 		/* Write-set register. */
537120b6fc9SSvatopluk Kraus 		bcm_lintc_write_4(sc, BCM_LINTC_PMU_ROUTING_SET_REG,
538120b6fc9SSvatopluk Kraus 		    BCM_LINTC_PIRR_IRQ_EN_CORE(cpu));
539120b6fc9SSvatopluk Kraus 	}
540120b6fc9SSvatopluk Kraus }
541120b6fc9SSvatopluk Kraus 
542120b6fc9SSvatopluk Kraus static void
543120b6fc9SSvatopluk Kraus bcm_lintc_init_secondary(device_t dev)
544120b6fc9SSvatopluk Kraus {
545120b6fc9SSvatopluk Kraus 	u_int cpu;
546120b6fc9SSvatopluk Kraus 	struct bcm_lintc_softc *sc;
547120b6fc9SSvatopluk Kraus 
548120b6fc9SSvatopluk Kraus 	cpu = PCPU_GET(cpuid);
549120b6fc9SSvatopluk Kraus 	sc = device_get_softc(dev);
550120b6fc9SSvatopluk Kraus 
551120b6fc9SSvatopluk Kraus 	BCM_LINTC_LOCK(sc);
552120b6fc9SSvatopluk Kraus 	bcm_lintc_init_rwreg_on_ap(sc, cpu, BCM_LINTC_TIMER0_IRQ,
553120b6fc9SSvatopluk Kraus 	    BCM_LINTC_TIMER_CFG_REG(cpu), BCM_LINTC_TCR_IRQ_EN_TIMER(0));
554120b6fc9SSvatopluk Kraus 	bcm_lintc_init_rwreg_on_ap(sc, cpu, BCM_LINTC_TIMER1_IRQ,
555120b6fc9SSvatopluk Kraus 	    BCM_LINTC_TIMER_CFG_REG(cpu), BCM_LINTC_TCR_IRQ_EN_TIMER(1));
556120b6fc9SSvatopluk Kraus 	bcm_lintc_init_rwreg_on_ap(sc, cpu, BCM_LINTC_TIMER2_IRQ,
557120b6fc9SSvatopluk Kraus 	    BCM_LINTC_TIMER_CFG_REG(cpu), BCM_LINTC_TCR_IRQ_EN_TIMER(2));
558120b6fc9SSvatopluk Kraus 	bcm_lintc_init_rwreg_on_ap(sc, cpu, BCM_LINTC_TIMER3_IRQ,
559120b6fc9SSvatopluk Kraus 	    BCM_LINTC_TIMER_CFG_REG(cpu), BCM_LINTC_TCR_IRQ_EN_TIMER(3));
560120b6fc9SSvatopluk Kraus 	bcm_lintc_init_pmu_on_ap(sc, cpu);
561120b6fc9SSvatopluk Kraus 	BCM_LINTC_UNLOCK(sc);
562120b6fc9SSvatopluk Kraus }
563120b6fc9SSvatopluk Kraus 
564120b6fc9SSvatopluk Kraus static void
565120b6fc9SSvatopluk Kraus bcm_lintc_ipi_send(device_t dev, struct intr_irqsrc *isrc, cpuset_t cpus,
566120b6fc9SSvatopluk Kraus     u_int ipi)
567120b6fc9SSvatopluk Kraus {
568120b6fc9SSvatopluk Kraus 	struct bcm_lintc_softc *sc = device_get_softc(dev);
569120b6fc9SSvatopluk Kraus 
570120b6fc9SSvatopluk Kraus 	KASSERT(isrc == &sc->bls_isrcs[BCM_LINTC_MBOX0_IRQ].bli_isrc,
571120b6fc9SSvatopluk Kraus 	    ("%s: bad ISRC %p argument", __func__, isrc));
572120b6fc9SSvatopluk Kraus 	bcm_lintc_ipi_write(sc, cpus, ipi);
573120b6fc9SSvatopluk Kraus }
574120b6fc9SSvatopluk Kraus 
575120b6fc9SSvatopluk Kraus static int
576120b6fc9SSvatopluk Kraus bcm_lintc_ipi_setup(device_t dev, u_int ipi, struct intr_irqsrc **isrcp)
577120b6fc9SSvatopluk Kraus {
578120b6fc9SSvatopluk Kraus 	struct bcm_lintc_softc *sc = device_get_softc(dev);
579120b6fc9SSvatopluk Kraus 
580120b6fc9SSvatopluk Kraus 	KASSERT(ipi < BCM_LINTC_NIPIS, ("%s: too high ipi %u", __func__, ipi));
581120b6fc9SSvatopluk Kraus 
582120b6fc9SSvatopluk Kraus 	*isrcp = &sc->bls_isrcs[BCM_LINTC_MBOX0_IRQ].bli_isrc;
583120b6fc9SSvatopluk Kraus 	return (0);
584120b6fc9SSvatopluk Kraus }
585120b6fc9SSvatopluk Kraus #endif
586120b6fc9SSvatopluk Kraus 
587120b6fc9SSvatopluk Kraus static int
588120b6fc9SSvatopluk Kraus bcm_lintc_pic_attach(struct bcm_lintc_softc *sc)
589120b6fc9SSvatopluk Kraus {
590120b6fc9SSvatopluk Kraus 	struct bcm_lintc_irqsrc *bisrcs;
5919346e913SAndrew Turner 	struct intr_pic *pic;
592120b6fc9SSvatopluk Kraus 	int error;
593120b6fc9SSvatopluk Kraus 	u_int flags;
594120b6fc9SSvatopluk Kraus 	uint32_t irq;
595120b6fc9SSvatopluk Kraus 	const char *name;
596120b6fc9SSvatopluk Kraus 	intptr_t xref;
597120b6fc9SSvatopluk Kraus 
598120b6fc9SSvatopluk Kraus 	bisrcs = sc->bls_isrcs;
599120b6fc9SSvatopluk Kraus 	name = device_get_nameunit(sc->bls_dev);
600120b6fc9SSvatopluk Kraus 	for (irq = 0; irq < BCM_LINTC_NIRQS; irq++) {
601120b6fc9SSvatopluk Kraus 		bisrcs[irq].bli_irq = irq;
602120b6fc9SSvatopluk Kraus 		switch (irq) {
603120b6fc9SSvatopluk Kraus 		case BCM_LINTC_TIMER0_IRQ:
604120b6fc9SSvatopluk Kraus 			bisrcs[irq].bli_mask = BCM_LINTC_TCR_IRQ_EN_TIMER(0);
605120b6fc9SSvatopluk Kraus 			flags = INTR_ISRCF_PPI;
606120b6fc9SSvatopluk Kraus 			break;
607120b6fc9SSvatopluk Kraus 		case BCM_LINTC_TIMER1_IRQ:
608120b6fc9SSvatopluk Kraus 			bisrcs[irq].bli_mask = BCM_LINTC_TCR_IRQ_EN_TIMER(1);
609120b6fc9SSvatopluk Kraus 			flags = INTR_ISRCF_PPI;
610120b6fc9SSvatopluk Kraus 			break;
611120b6fc9SSvatopluk Kraus 		case BCM_LINTC_TIMER2_IRQ:
612120b6fc9SSvatopluk Kraus 			bisrcs[irq].bli_mask = BCM_LINTC_TCR_IRQ_EN_TIMER(2);
613120b6fc9SSvatopluk Kraus 			flags = INTR_ISRCF_PPI;
614120b6fc9SSvatopluk Kraus 			break;
615120b6fc9SSvatopluk Kraus 		case BCM_LINTC_TIMER3_IRQ:
616120b6fc9SSvatopluk Kraus 			bisrcs[irq].bli_mask = BCM_LINTC_TCR_IRQ_EN_TIMER(3);
617120b6fc9SSvatopluk Kraus 			flags = INTR_ISRCF_PPI;
618120b6fc9SSvatopluk Kraus 			break;
619120b6fc9SSvatopluk Kraus 		case BCM_LINTC_MBOX0_IRQ:
620120b6fc9SSvatopluk Kraus 		case BCM_LINTC_MBOX1_IRQ:
621120b6fc9SSvatopluk Kraus 		case BCM_LINTC_MBOX2_IRQ:
622120b6fc9SSvatopluk Kraus 		case BCM_LINTC_MBOX3_IRQ:
623120b6fc9SSvatopluk Kraus 			bisrcs[irq].bli_value = 0;	/* not used */
624120b6fc9SSvatopluk Kraus 			flags = INTR_ISRCF_IPI;
625120b6fc9SSvatopluk Kraus 			break;
626120b6fc9SSvatopluk Kraus 		case BCM_LINTC_GPU_IRQ:
627120b6fc9SSvatopluk Kraus 			bisrcs[irq].bli_value = BCM_LINTC_GIRR_IRQ_CORE(0);
628120b6fc9SSvatopluk Kraus 			flags = 0;
629120b6fc9SSvatopluk Kraus 			break;
630120b6fc9SSvatopluk Kraus 		case BCM_LINTC_PMU_IRQ:
631120b6fc9SSvatopluk Kraus 			bisrcs[irq].bli_value = 0;	/* not used */
632120b6fc9SSvatopluk Kraus 			flags = INTR_ISRCF_PPI;
633120b6fc9SSvatopluk Kraus 			break;
634120b6fc9SSvatopluk Kraus 		default:
635120b6fc9SSvatopluk Kraus 			bisrcs[irq].bli_value = 0;	/* not used */
636120b6fc9SSvatopluk Kraus 			flags = 0;
637120b6fc9SSvatopluk Kraus 			break;
638120b6fc9SSvatopluk Kraus 		}
639120b6fc9SSvatopluk Kraus 
640120b6fc9SSvatopluk Kraus 		error = intr_isrc_register(&bisrcs[irq].bli_isrc, sc->bls_dev,
641120b6fc9SSvatopluk Kraus 		    flags, "%s,%u", name, irq);
642120b6fc9SSvatopluk Kraus 		if (error != 0)
643120b6fc9SSvatopluk Kraus 			return (error);
644120b6fc9SSvatopluk Kraus 	}
645120b6fc9SSvatopluk Kraus 
646120b6fc9SSvatopluk Kraus 	xref = OF_xref_from_node(ofw_bus_get_node(sc->bls_dev));
6479346e913SAndrew Turner 	pic = intr_pic_register(sc->bls_dev, xref);
6489346e913SAndrew Turner 	if (pic == NULL)
6499346e913SAndrew Turner 		return (ENXIO);
650120b6fc9SSvatopluk Kraus 
651120b6fc9SSvatopluk Kraus 	return (intr_pic_claim_root(sc->bls_dev, xref, bcm_lintc_intr, sc, 0));
652120b6fc9SSvatopluk Kraus }
653120b6fc9SSvatopluk Kraus 
654120b6fc9SSvatopluk Kraus static int
655120b6fc9SSvatopluk Kraus bcm_lintc_probe(device_t dev)
656120b6fc9SSvatopluk Kraus {
657120b6fc9SSvatopluk Kraus 
658120b6fc9SSvatopluk Kraus 	if (!ofw_bus_status_okay(dev))
659120b6fc9SSvatopluk Kraus 		return (ENXIO);
660120b6fc9SSvatopluk Kraus 
661120b6fc9SSvatopluk Kraus 	if (!ofw_bus_is_compatible(dev, "brcm,bcm2836-l1-intc"))
662120b6fc9SSvatopluk Kraus 		return (ENXIO);
663120b6fc9SSvatopluk Kraus 	device_set_desc(dev, "BCM2836 Interrupt Controller");
664120b6fc9SSvatopluk Kraus 	return (BUS_PROBE_DEFAULT);
665120b6fc9SSvatopluk Kraus }
666120b6fc9SSvatopluk Kraus 
667120b6fc9SSvatopluk Kraus static int
668120b6fc9SSvatopluk Kraus bcm_lintc_attach(device_t dev)
669120b6fc9SSvatopluk Kraus {
670120b6fc9SSvatopluk Kraus 	struct bcm_lintc_softc *sc;
671120b6fc9SSvatopluk Kraus 	int cpu, rid;
672120b6fc9SSvatopluk Kraus 
673120b6fc9SSvatopluk Kraus 	sc = device_get_softc(dev);
674120b6fc9SSvatopluk Kraus 
675120b6fc9SSvatopluk Kraus 	sc->bls_dev = dev;
676120b6fc9SSvatopluk Kraus 	if (bcm_lintc_sc != NULL)
677120b6fc9SSvatopluk Kraus 		return (ENXIO);
678120b6fc9SSvatopluk Kraus 
679120b6fc9SSvatopluk Kraus 	rid = 0;
680120b6fc9SSvatopluk Kraus 	sc->bls_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
681120b6fc9SSvatopluk Kraus 	    RF_ACTIVE);
682120b6fc9SSvatopluk Kraus 	if (sc->bls_mem == NULL) {
683120b6fc9SSvatopluk Kraus 		device_printf(dev, "could not allocate memory resource\n");
684120b6fc9SSvatopluk Kraus 		return (ENXIO);
685120b6fc9SSvatopluk Kraus 	}
686120b6fc9SSvatopluk Kraus 
687120b6fc9SSvatopluk Kraus 	sc->bls_bst = rman_get_bustag(sc->bls_mem);
688120b6fc9SSvatopluk Kraus 	sc->bls_bsh = rman_get_bushandle(sc->bls_mem);
689120b6fc9SSvatopluk Kraus 
690120b6fc9SSvatopluk Kraus 	bcm_lintc_write_4(sc, BCM_LINTC_CONTROL_REG, 0);
691120b6fc9SSvatopluk Kraus 	bcm_lintc_write_4(sc, BCM_LINTC_PRESCALER_REG, BCM_LINTC_PSR_19_2);
692120b6fc9SSvatopluk Kraus 
693120b6fc9SSvatopluk Kraus 	/* Disable all timers on all cores. */
694120b6fc9SSvatopluk Kraus 	for (cpu = 0; cpu < 4; cpu++)
695120b6fc9SSvatopluk Kraus 		bcm_lintc_write_4(sc, BCM_LINTC_TIMER_CFG_REG(cpu), 0);
696120b6fc9SSvatopluk Kraus 
697120b6fc9SSvatopluk Kraus #ifdef SMP
698120b6fc9SSvatopluk Kraus 	/* Enable mailbox 0 on all cores used for IPI. */
699120b6fc9SSvatopluk Kraus 	for (cpu = 0; cpu < 4; cpu++)
700120b6fc9SSvatopluk Kraus 		bcm_lintc_write_4(sc, BCM_LINTC_MBOX_CFG_REG(cpu),
701120b6fc9SSvatopluk Kraus 		    BCM_LINTC_MCR_IRQ_EN_MBOX(0));
702120b6fc9SSvatopluk Kraus #endif
703120b6fc9SSvatopluk Kraus 
704120b6fc9SSvatopluk Kraus 	if (bcm_lintc_pic_attach(sc) != 0) {
705120b6fc9SSvatopluk Kraus 		device_printf(dev, "could not attach PIC\n");
706120b6fc9SSvatopluk Kraus 		return (ENXIO);
707120b6fc9SSvatopluk Kraus 	}
708120b6fc9SSvatopluk Kraus 
709120b6fc9SSvatopluk Kraus 	BCM_LINTC_LOCK_INIT(sc);
710120b6fc9SSvatopluk Kraus 	bcm_lintc_sc = sc;
711120b6fc9SSvatopluk Kraus 	return (0);
712120b6fc9SSvatopluk Kraus }
713120b6fc9SSvatopluk Kraus 
714120b6fc9SSvatopluk Kraus static device_method_t bcm_lintc_methods[] = {
715120b6fc9SSvatopluk Kraus 	DEVMETHOD(device_probe,		bcm_lintc_probe),
716120b6fc9SSvatopluk Kraus 	DEVMETHOD(device_attach,	bcm_lintc_attach),
717120b6fc9SSvatopluk Kraus 
718120b6fc9SSvatopluk Kraus 	DEVMETHOD(pic_disable_intr,	bcm_lintc_disable_intr),
719120b6fc9SSvatopluk Kraus 	DEVMETHOD(pic_enable_intr,	bcm_lintc_enable_intr),
720120b6fc9SSvatopluk Kraus 	DEVMETHOD(pic_map_intr,		bcm_lintc_map_intr),
721120b6fc9SSvatopluk Kraus 	DEVMETHOD(pic_post_filter,	bcm_lintc_post_filter),
722120b6fc9SSvatopluk Kraus 	DEVMETHOD(pic_post_ithread,	bcm_lintc_post_ithread),
723120b6fc9SSvatopluk Kraus 	DEVMETHOD(pic_pre_ithread,	bcm_lintc_pre_ithread),
724120b6fc9SSvatopluk Kraus 	DEVMETHOD(pic_setup_intr,	bcm_lintc_setup_intr),
725120b6fc9SSvatopluk Kraus #ifdef SMP
726120b6fc9SSvatopluk Kraus 	DEVMETHOD(pic_init_secondary,	bcm_lintc_init_secondary),
727120b6fc9SSvatopluk Kraus 	DEVMETHOD(pic_ipi_send,		bcm_lintc_ipi_send),
728120b6fc9SSvatopluk Kraus 	DEVMETHOD(pic_ipi_setup,	bcm_lintc_ipi_setup),
729120b6fc9SSvatopluk Kraus #endif
730120b6fc9SSvatopluk Kraus 
731120b6fc9SSvatopluk Kraus 	DEVMETHOD_END
732120b6fc9SSvatopluk Kraus };
733120b6fc9SSvatopluk Kraus 
734120b6fc9SSvatopluk Kraus static driver_t bcm_lintc_driver = {
7359e655cd5SIan Lepore 	"lintc",
736120b6fc9SSvatopluk Kraus 	bcm_lintc_methods,
737120b6fc9SSvatopluk Kraus 	sizeof(struct bcm_lintc_softc),
738120b6fc9SSvatopluk Kraus };
739120b6fc9SSvatopluk Kraus 
740120b6fc9SSvatopluk Kraus static devclass_t bcm_lintc_devclass;
741120b6fc9SSvatopluk Kraus 
7429e655cd5SIan Lepore EARLY_DRIVER_MODULE(lintc, simplebus, bcm_lintc_driver, bcm_lintc_devclass,
74391cc58afSOleksandr Tymoshenko     0, 0, BUS_PASS_INTERRUPT);
744