at91_aic.c revision 262925
1/*-
2 * Copyright (c) 2014 Warner Losh.  All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23 * SUCH DAMAGE.
24 */
25
26#include "opt_platform.h"
27
28#include <sys/cdefs.h>
29__FBSDID("$FreeBSD: head/sys/arm/at91/at91_aic.c 262925 2014-03-08 06:06:50Z imp $");
30
31#include <sys/param.h>
32#include <sys/bus.h>
33#include <sys/kernel.h>
34#include <sys/module.h>
35#include <sys/resource.h>
36#include <sys/systm.h>
37#include <sys/rman.h>
38
39#include <machine/bus.h>
40#include <machine/cpu.h>
41#include <machine/cpufunc.h>
42#include <machine/frame.h>
43#include <machine/intr.h>
44#include <machine/resource.h>
45
46#include <arm/at91/at91var.h>
47#include <arm/at91/at91_aicreg.h>
48
49#ifdef FDT
50#include <dev/fdt/fdt_common.h>
51#include <dev/ofw/ofw_bus.h>
52#include <dev/ofw/ofw_bus_subr.h>
53#endif
54
55static struct aic_softc {
56	struct resource	*mem_res;	/* Memory resource */
57	void		*intrhand;	/* Interrupt handle */
58	device_t	sc_dev;
59} *sc;
60
61static inline uint32_t
62RD4(struct aic_softc *sc, bus_size_t off)
63{
64
65	return (bus_read_4(sc->mem_res, off));
66}
67
68static inline void
69WR4(struct aic_softc *sc, bus_size_t off, uint32_t val)
70{
71
72	bus_write_4(sc->mem_res, off, val);
73}
74
75void
76arm_mask_irq(uintptr_t nb)
77{
78
79	WR4(sc, IC_IDCR, 1 << nb);
80}
81
82int
83arm_get_next_irq(int last __unused)
84{
85	int status;
86	int irq;
87
88	irq = RD4(sc, IC_IVR);
89	status = RD4(sc, IC_ISR);
90	if (status == 0) {
91		WR4(sc, IC_EOICR, 1);
92		return (-1);
93	}
94	return (irq);
95}
96
97void
98arm_unmask_irq(uintptr_t nb)
99{
100
101	WR4(sc, IC_IECR, 1 << nb);
102	WR4(sc, IC_EOICR, 0);
103}
104
105static int
106at91_aic_probe(device_t dev)
107{
108#ifdef FDT
109	if (!ofw_bus_is_compatible(dev, "atmel,at91rm9200-aic"))
110		return (ENXIO);
111#endif
112	device_set_desc(dev, "AIC");
113        return (0);
114}
115
116static int
117at91_aic_attach(device_t dev)
118{
119	int i, rid, err = 0;
120
121	device_printf(dev, "Attach %d\n", bus_current_pass);
122
123	sc = device_get_softc(dev);
124	sc->sc_dev = dev;
125
126	rid = 0;
127	sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
128	    RF_ACTIVE);
129
130	if (sc->mem_res == NULL)
131		panic("couldn't allocate register resources");
132
133	/*
134	 * Setup the interrupt table.
135	 */
136	if (soc_info.soc_data == NULL || soc_info.soc_data->soc_irq_prio == NULL)
137		panic("Interrupt priority table missing\n");
138	for (i = 0; i < 32; i++) {
139		WR4(sc, IC_SVR + i * 4, i);
140		/* Priority. */
141		WR4(sc, IC_SMR + i * 4, soc_info.soc_data->soc_irq_prio[i]);
142		if (i < 8)
143			WR4(sc, IC_EOICR, 1);
144	}
145
146	WR4(sc, IC_SPU, 32);
147	/* No debug. */
148	WR4(sc, IC_DCR, 0);
149	/* Disable and clear all interrupts. */
150	WR4(sc, IC_IDCR, 0xffffffff);
151	WR4(sc, IC_ICCR, 0xffffffff);
152	enable_interrupts(I32_bit | F32_bit);
153
154	return (err);
155}
156
157static void
158at91_aic_new_pass(device_t dev)
159{
160	device_printf(dev, "Pass %d\n", bus_current_pass);
161}
162
163static device_method_t at91_aic_methods[] = {
164	DEVMETHOD(device_probe, at91_aic_probe),
165	DEVMETHOD(device_attach, at91_aic_attach),
166	DEVMETHOD(bus_new_pass, at91_aic_new_pass),
167	DEVMETHOD_END
168};
169
170static driver_t at91_aic_driver = {
171	"at91_aic",
172	at91_aic_methods,
173	sizeof(struct aic_softc),
174};
175
176static devclass_t at91_aic_devclass;
177
178#ifdef FDT
179DRIVER_MODULE(at91_aic, simplebus, at91_aic_driver, at91_aic_devclass, NULL,
180    NULL);
181#else
182DRIVER_MODULE(at91_aic, atmelarm, at91_aic_driver, at91_aic_devclass, NULL,
183    NULL);
184#endif
185/* not yet
186EARLY_DRIVER_MODULE(at91_aic, simplebus, at91_aic_driver, at91_aic_devclass,
187    NULL, NULL, BUS_PASS_INTERRUPT);
188*/
189