1248557Sray/*-
2250357Sray * Copyright (c) 2012, 2013 The FreeBSD Foundation
3248557Sray * All rights reserved.
4248557Sray *
5248557Sray * This software was developed by Oleksandr Rybalko under sponsorship
6248557Sray * from the FreeBSD Foundation.
7248557Sray *
8248557Sray * Redistribution and use in source and binary forms, with or without
9248557Sray * modification, are permitted provided that the following conditions
10248557Sray * are met:
11248557Sray * 1.	Redistributions of source code must retain the above copyright
12248557Sray *	notice, this list of conditions and the following disclaimer.
13248557Sray * 2.	Redistributions in binary form must reproduce the above copyright
14248557Sray *	notice, this list of conditions and the following disclaimer in the
15248557Sray *	documentation and/or other materials provided with the distribution.
16248557Sray *
17248557Sray * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18248557Sray * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19248557Sray * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20248557Sray * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21248557Sray * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22248557Sray * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23248557Sray * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24248557Sray * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25248557Sray * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26248557Sray * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27248557Sray * SUCH DAMAGE.
28248557Sray */
29248557Sray
30248557Sray#include <sys/cdefs.h>
31248557Sray__FBSDID("$FreeBSD: releng/10.3/sys/arm/freescale/imx/tzic.c 266160 2014-05-15 17:30:16Z ian $");
32248557Sray
33248557Sray#include <sys/param.h>
34248557Sray#include <sys/systm.h>
35248557Sray#include <sys/bus.h>
36248557Sray#include <sys/kernel.h>
37248557Sray#include <sys/ktr.h>
38248557Sray#include <sys/module.h>
39248557Sray#include <sys/rman.h>
40248557Sray#include <sys/pcpu.h>
41248557Sray#include <sys/proc.h>
42248557Sray#include <sys/cpuset.h>
43248557Sray#include <sys/lock.h>
44248557Sray#include <sys/mutex.h>
45248557Sray#include <machine/bus.h>
46248557Sray#include <machine/intr.h>
47248557Sray
48248557Sray#include <dev/fdt/fdt_common.h>
49248557Sray#include <dev/ofw/openfirm.h>
50248557Sray#include <dev/ofw/ofw_bus.h>
51248557Sray#include <dev/ofw/ofw_bus_subr.h>
52248557Sray
53248557Sray#include <arm/freescale/imx/imx51_tzicreg.h>
54248557Sray
55248557Sraystruct tzic_softc {
56248557Sray	struct resource *	tzic_res[3];
57248557Sray	bus_space_tag_t		tzic_bst;
58248557Sray	bus_space_handle_t	tzic_bsh;
59248557Sray	uint8_t			ver;
60248557Sray};
61248557Sray
62248557Sraystatic struct resource_spec tzic_spec[] = {
63248557Sray	{ SYS_RES_MEMORY,	0,	RF_ACTIVE },
64248557Sray	{ -1, 0 }
65248557Sray};
66248557Sray
67248557Sraystatic struct tzic_softc *tzic_sc = NULL;
68248557Sray
69248557Sray#define	tzic_read_4(reg)		\
70248557Sray    bus_space_read_4(tzic_sc->tzic_bst, tzic_sc->tzic_bsh, reg)
71248557Sray#define	tzic_write_4(reg, val)		\
72248557Sray    bus_space_write_4(tzic_sc->tzic_bst, tzic_sc->tzic_bsh, reg, val)
73248557Sray
74248557Sraystatic void tzic_post_filter(void *);
75248557Sray
76248557Sraystatic int
77248557Sraytzic_probe(device_t dev)
78248557Sray{
79266152Sian
80266152Sian	if (!ofw_bus_status_okay(dev))
81266152Sian		return (ENXIO);
82266152Sian
83248557Sray	if (ofw_bus_is_compatible(dev, "fsl,tzic")) {
84248557Sray		device_set_desc(dev, "TrustZone Interrupt Controller");
85248557Sray		return (BUS_PROBE_DEFAULT);
86248557Sray	}
87248557Sray	return (ENXIO);
88248557Sray}
89248557Sray
90248557Sraystatic int
91248557Sraytzic_attach(device_t dev)
92248557Sray{
93248557Sray	struct		tzic_softc *sc = device_get_softc(dev);
94248557Sray	int		i;
95248557Sray	uint32_t	reg;
96248557Sray
97248557Sray	if (tzic_sc)
98248557Sray		return (ENXIO);
99248557Sray
100248557Sray	if (bus_alloc_resources(dev, tzic_spec, sc->tzic_res)) {
101248557Sray		device_printf(dev, "could not allocate resources\n");
102248557Sray		return (ENXIO);
103248557Sray	}
104248557Sray
105248557Sray	arm_post_filter = tzic_post_filter;
106248557Sray
107248557Sray	/* Distributor Interface */
108248557Sray	sc->tzic_bst = rman_get_bustag(sc->tzic_res[0]);
109248557Sray	sc->tzic_bsh = rman_get_bushandle(sc->tzic_res[0]);
110248557Sray
111248557Sray	tzic_sc = sc;
112248557Sray
113248557Sray	reg = tzic_read_4(TZIC_INTCNTL);
114248557Sray	tzic_write_4(TZIC_INTCNTL, INTCNTL_NSEN_MASK|INTCNTL_NSEN|INTCNTL_EN);
115248557Sray	reg = tzic_read_4(TZIC_INTCNTL);
116248557Sray	tzic_write_4(TZIC_PRIOMASK, 0x1f);
117248557Sray	reg = tzic_read_4(TZIC_PRIOMASK);
118248557Sray
119248557Sray	tzic_write_4(TZIC_SYNCCTRL, 0x02);
120248557Sray	reg = tzic_read_4(TZIC_SYNCCTRL);
121248557Sray
122248557Sray	/* route all interrupts to IRQ.  secure interrupts are for FIQ */
123248557Sray	for (i = 0; i < 4; i++)
124248557Sray		tzic_write_4(TZIC_INTSEC(i), 0xffffffff);
125248557Sray
126248557Sray	/* disable all interrupts */
127248557Sray	for (i = 0; i < 4; i++)
128248557Sray		tzic_write_4(TZIC_ENCLEAR(i), 0xffffffff);
129248557Sray
130248557Sray	return (0);
131248557Sray}
132248557Sray
133248557Sraystatic device_method_t tzic_methods[] = {
134248557Sray	DEVMETHOD(device_probe,		tzic_probe),
135248557Sray	DEVMETHOD(device_attach,	tzic_attach),
136248557Sray	{ 0, 0 }
137248557Sray};
138248557Sray
139248557Sraystatic driver_t tzic_driver = {
140248557Sray	"tzic",
141248557Sray	tzic_methods,
142248557Sray	sizeof(struct tzic_softc),
143248557Sray};
144248557Sray
145248557Sraystatic devclass_t tzic_devclass;
146248557Sray
147248557Sray/*
148248557Sray * Memory space of controller located outside of device range, so let him to
149266160Sian * attach not only to simplebus, but ofwbus also.
150248557Sray */
151266160SianEARLY_DRIVER_MODULE(tzic, ofwbus, tzic_driver, tzic_devclass, 0, 0,
152248557Sray    BUS_PASS_INTERRUPT);
153248557SrayEARLY_DRIVER_MODULE(tzic, simplebus, tzic_driver, tzic_devclass, 0, 0,
154248557Sray    BUS_PASS_INTERRUPT);
155248557Sray
156248557Sraystatic void
157248557Sraytzic_post_filter(void *arg)
158248557Sray{
159248557Sray
160248557Sray}
161248557Sray
162248557Srayint
163248557Srayarm_get_next_irq(int last_irq)
164248557Sray{
165248557Sray	uint32_t pending;
166248557Sray	int i, b;
167248557Sray
168248557Sray	for (i = 0; i < 4; i++) {
169248557Sray		pending = tzic_read_4(TZIC_PND(i));
170259348Sian		for (b = 0; pending != 0 && b < 32; b++)
171248557Sray			if (pending & (1 << b)) {
172248557Sray				return (i * 32 + b);
173248557Sray			}
174248557Sray	}
175248557Sray
176248557Sray	return (-1);
177248557Sray}
178248557Sray
179248557Srayvoid
180248557Srayarm_mask_irq(uintptr_t nb)
181248557Sray{
182248557Sray
183248557Sray	tzic_write_4(TZIC_ENCLEAR(nb / 32), (1UL <<(nb % 32)));
184248557Sray}
185248557Sray
186248557Srayvoid
187248557Srayarm_unmask_irq(uintptr_t nb)
188248557Sray{
189248557Sray
190248557Sray	tzic_write_4(TZIC_ENSET(nb / 32), (1UL <<(nb % 32)));
191248557Sray}
192