tzic.c revision 250357
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: head/sys/arm/freescale/imx/tzic.c 250357 2013-05-08 09:42:50Z ray $");
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{
79248557Sray	if (ofw_bus_is_compatible(dev, "fsl,tzic")) {
80248557Sray		device_set_desc(dev, "TrustZone Interrupt Controller");
81248557Sray		return (BUS_PROBE_DEFAULT);
82248557Sray	}
83248557Sray	return (ENXIO);
84248557Sray}
85248557Sray
86248557Sraystatic int
87248557Sraytzic_attach(device_t dev)
88248557Sray{
89248557Sray	struct		tzic_softc *sc = device_get_softc(dev);
90248557Sray	int		i;
91248557Sray	uint32_t	reg;
92248557Sray
93248557Sray	if (tzic_sc)
94248557Sray		return (ENXIO);
95248557Sray
96248557Sray	if (bus_alloc_resources(dev, tzic_spec, sc->tzic_res)) {
97248557Sray		device_printf(dev, "could not allocate resources\n");
98248557Sray		return (ENXIO);
99248557Sray	}
100248557Sray
101248557Sray	arm_post_filter = tzic_post_filter;
102248557Sray
103248557Sray	/* Distributor Interface */
104248557Sray	sc->tzic_bst = rman_get_bustag(sc->tzic_res[0]);
105248557Sray	sc->tzic_bsh = rman_get_bushandle(sc->tzic_res[0]);
106248557Sray
107248557Sray	tzic_sc = sc;
108248557Sray
109248557Sray	reg = tzic_read_4(TZIC_INTCNTL);
110248557Sray	tzic_write_4(TZIC_INTCNTL, INTCNTL_NSEN_MASK|INTCNTL_NSEN|INTCNTL_EN);
111248557Sray	reg = tzic_read_4(TZIC_INTCNTL);
112248557Sray	tzic_write_4(TZIC_PRIOMASK, 0x1f);
113248557Sray	reg = tzic_read_4(TZIC_PRIOMASK);
114248557Sray
115248557Sray	tzic_write_4(TZIC_SYNCCTRL, 0x02);
116248557Sray	reg = tzic_read_4(TZIC_SYNCCTRL);
117248557Sray
118248557Sray	/* route all interrupts to IRQ.  secure interrupts are for FIQ */
119248557Sray	for (i = 0; i < 4; i++)
120248557Sray		tzic_write_4(TZIC_INTSEC(i), 0xffffffff);
121248557Sray
122248557Sray	/* disable all interrupts */
123248557Sray	for (i = 0; i < 4; i++)
124248557Sray		tzic_write_4(TZIC_ENCLEAR(i), 0xffffffff);
125248557Sray
126248557Sray	return (0);
127248557Sray}
128248557Sray
129248557Sraystatic device_method_t tzic_methods[] = {
130248557Sray	DEVMETHOD(device_probe,		tzic_probe),
131248557Sray	DEVMETHOD(device_attach,	tzic_attach),
132248557Sray	{ 0, 0 }
133248557Sray};
134248557Sray
135248557Sraystatic driver_t tzic_driver = {
136248557Sray	"tzic",
137248557Sray	tzic_methods,
138248557Sray	sizeof(struct tzic_softc),
139248557Sray};
140248557Sray
141248557Sraystatic devclass_t tzic_devclass;
142248557Sray
143248557Sray/*
144248557Sray * Memory space of controller located outside of device range, so let him to
145248557Sray * attach not only to simplebus, but fdtbus also.
146248557Sray */
147248557SrayEARLY_DRIVER_MODULE(tzic, fdtbus, tzic_driver, tzic_devclass, 0, 0,
148248557Sray    BUS_PASS_INTERRUPT);
149248557SrayEARLY_DRIVER_MODULE(tzic, simplebus, tzic_driver, tzic_devclass, 0, 0,
150248557Sray    BUS_PASS_INTERRUPT);
151248557Sray
152248557Sraystatic void
153248557Sraytzic_post_filter(void *arg)
154248557Sray{
155248557Sray
156248557Sray}
157248557Sray
158248557Srayint
159248557Srayarm_get_next_irq(int last_irq)
160248557Sray{
161248557Sray	uint32_t pending;
162248557Sray	int i, b;
163248557Sray
164248557Sray	for (i = 0; i < 4; i++) {
165248557Sray		pending = tzic_read_4(TZIC_PND(i));
166248557Sray		for (b = 0; b < 32; b++)
167248557Sray			if (pending & (1 << b)) {
168248557Sray				return (i * 32 + b);
169248557Sray			}
170248557Sray	}
171248557Sray
172248557Sray	return (-1);
173248557Sray}
174248557Sray
175248557Srayvoid
176248557Srayarm_mask_irq(uintptr_t nb)
177248557Sray{
178248557Sray
179248557Sray	tzic_write_4(TZIC_ENCLEAR(nb / 32), (1UL <<(nb % 32)));
180248557Sray}
181248557Sray
182248557Srayvoid
183248557Srayarm_unmask_irq(uintptr_t nb)
184248557Sray{
185248557Sray
186248557Sray	tzic_write_4(TZIC_ENSET(nb / 32), (1UL <<(nb % 32)));
187248557Sray}
188