2 * Copyright 2015 Andrew Turner.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
22 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/module.h>
38 #include <machine/bus.h>
39 #include <machine/resource.h>
41 #include <dev/ofw/ofw_bus_subr.h>
42 #include <dev/ofw/ofw_bus.h>
44 #include <arm/broadcom/bcm2835/bcm2836.h>
46 #define ARM_LOCAL_BASE 0x40000000
47 #define ARM_LOCAL_SIZE 0x00001000
49 #define ARM_LOCAL_CONTROL 0x00
50 #define ARM_LOCAL_PRESCALER 0x08
51 #define PRESCALER_19_2 0x80000000 /* 19.2 MHz */
52 #define ARM_LOCAL_INT_TIMER(n) (0x40 + (n) * 4)
53 #define ARM_LOCAL_INT_MAILBOX(n) (0x50 + (n) * 4)
54 #define ARM_LOCAL_INT_PENDING(n) (0x60 + (n) * 4)
55 #define INT_PENDING_MASK 0x0f
58 * A driver for features of the bcm2836.
61 struct bcm2836_softc {
63 struct resource *sc_mem;
66 static device_identify_t bcm2836_identify;
67 static device_probe_t bcm2836_probe;
68 static device_attach_t bcm2836_attach;
70 struct bcm2836_softc *softc;
73 bcm2836_identify(driver_t *driver, device_t parent)
76 if (BUS_ADD_CHILD(parent, 0, "bcm2836", -1) == NULL)
77 device_printf(parent, "add child failed\n");
81 bcm2836_probe(device_t dev)
87 device_set_desc(dev, "Broadcom bcm2836");
89 return (BUS_PROBE_DEFAULT);
93 bcm2836_attach(device_t dev)
97 softc = device_get_softc(dev);
101 softc->sc_mem = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid,
102 ARM_LOCAL_BASE, ARM_LOCAL_BASE + ARM_LOCAL_SIZE, ARM_LOCAL_SIZE,
104 if (softc->sc_mem == NULL) {
105 device_printf(dev, "could not allocate memory resource\n");
109 bus_write_4(softc->sc_mem, ARM_LOCAL_CONTROL, 0);
110 bus_write_4(softc->sc_mem, ARM_LOCAL_PRESCALER, PRESCALER_19_2);
112 for (i = 0; i < 4; i++)
113 bus_write_4(softc->sc_mem, ARM_LOCAL_INT_TIMER(i), 0);
115 for (i = 0; i < 4; i++)
116 bus_write_4(softc->sc_mem, ARM_LOCAL_INT_MAILBOX(i), 1);
122 bcm2836_get_next_irq(int last_irq)
128 cpu = PCPU_GET(cpuid);
130 reg = bus_read_4(softc->sc_mem, ARM_LOCAL_INT_PENDING(cpu));
131 reg &= INT_PENDING_MASK;
141 bcm2836_mask_irq(uintptr_t irq)
146 for (i = 0; i < 4; i++) {
147 reg = bus_read_4(softc->sc_mem, ARM_LOCAL_INT_TIMER(i));
149 bus_write_4(softc->sc_mem, ARM_LOCAL_INT_TIMER(i), reg);
154 bcm2836_unmask_irq(uintptr_t irq)
159 for (i = 0; i < 4; i++) {
160 reg = bus_read_4(softc->sc_mem, ARM_LOCAL_INT_TIMER(i));
162 bus_write_4(softc->sc_mem, ARM_LOCAL_INT_TIMER(i), reg);
166 static device_method_t bcm2836_methods[] = {
167 /* Device interface */
168 DEVMETHOD(device_identify, bcm2836_identify),
169 DEVMETHOD(device_probe, bcm2836_probe),
170 DEVMETHOD(device_attach, bcm2836_attach),
175 static devclass_t bcm2836_devclass;
177 static driver_t bcm2836_driver = {
180 sizeof(struct bcm2836_softc),
183 EARLY_DRIVER_MODULE(bcm2836, nexus, bcm2836_driver, bcm2836_devclass, 0, 0,
184 BUS_PASS_INTERRUPT + BUS_PASS_ORDER_MIDDLE);