1 /*- 2 * Copyright (c) 2012 Oleksandr Tymoshenko <gonzo@freebsd.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 __FBSDID("$FreeBSD$"); 29 30 #include <sys/param.h> 31 #include <sys/systm.h> 32 #include <sys/bus.h> 33 #include <sys/kernel.h> 34 #include <sys/module.h> 35 #include <sys/malloc.h> 36 #include <sys/rman.h> 37 #include <sys/timeet.h> 38 #include <sys/timetc.h> 39 #include <sys/watchdog.h> 40 #include <machine/bus.h> 41 #include <machine/cpu.h> 42 #include <machine/intr.h> 43 44 #include <dev/fdt/fdt_common.h> 45 #include <dev/ofw/openfirm.h> 46 #include <dev/ofw/ofw_bus.h> 47 #include <dev/ofw/ofw_bus_subr.h> 48 49 #include <machine/bus.h> 50 #include <machine/fdt.h> 51 52 #include <arm/broadcom/bcm2835/bcm2835_mbox.h> 53 54 #include "mbox_if.h" 55 56 #define REG_READ 0x00 57 #define REG_POL 0x10 58 #define REG_SENDER 0x14 59 #define REG_STATUS 0x18 60 #define STATUS_FULL 0x80000000 61 #define STATUS_EMPTY 0x40000000 62 #define REG_CONFIG 0x1C 63 #define CONFIG_DATA_IRQ 0x00000001 64 #define REG_WRITE 0x20 /* This is Mailbox 1 address */ 65 66 #define MBOX_MSG(chan, data) (((data) & ~0xf) | ((chan) & 0xf)) 67 #define MBOX_CHAN(msg) ((msg) & 0xf) 68 #define MBOX_DATA(msg) ((msg) & ~0xf) 69 70 #define MBOX_LOCK(sc) do { \ 71 mtx_lock(&(sc)->lock); \ 72 } while(0) 73 74 #define MBOX_UNLOCK(sc) do { \ 75 mtx_unlock(&(sc)->lock); \ 76 } while(0) 77 78 #ifdef DEBUG 79 #define dprintf(fmt, args...) printf(fmt, ##args) 80 #else 81 #define dprintf(fmt, args...) 82 #endif 83 84 struct bcm_mbox_softc { 85 struct mtx lock; 86 struct resource * mem_res; 87 struct resource * irq_res; 88 void* intr_hl; 89 bus_space_tag_t bst; 90 bus_space_handle_t bsh; 91 int valid[BCM2835_MBOX_CHANS]; 92 int msg[BCM2835_MBOX_CHANS]; 93 }; 94 95 #define mbox_read_4(sc, reg) \ 96 bus_space_read_4((sc)->bst, (sc)->bsh, reg) 97 #define mbox_write_4(sc, reg, val) \ 98 bus_space_write_4((sc)->bst, (sc)->bsh, reg, val) 99 100 static void 101 bcm_mbox_intr(void *arg) 102 { 103 struct bcm_mbox_softc *sc = arg; 104 int chan; 105 uint32_t data; 106 uint32_t msg; 107 108 MBOX_LOCK(sc); 109 while (!(mbox_read_4(sc, REG_STATUS) & STATUS_EMPTY)) { 110 msg = mbox_read_4(sc, REG_READ); 111 dprintf("bcm_mbox_intr: raw data %08x\n", msg); 112 chan = MBOX_CHAN(msg); 113 data = MBOX_DATA(msg); 114 if (sc->valid[chan]) { 115 printf("bcm_mbox_intr: channel %d oveflow\n", chan); 116 continue; 117 } 118 dprintf("bcm_mbox_intr: chan %d, data %08x\n", chan, data); 119 sc->msg[chan] = data; 120 sc->valid[chan] = 1; 121 wakeup(&sc->msg[chan]); 122 123 } 124 MBOX_UNLOCK(sc); 125 } 126 127 static int 128 bcm_mbox_probe(device_t dev) 129 { 130 131 if (ofw_bus_is_compatible(dev, "broadcom,bcm2835-mbox")) { 132 device_set_desc(dev, "BCM2835 VideoCore Mailbox"); 133 return(BUS_PROBE_DEFAULT); 134 } 135 136 return (ENXIO); 137 } 138 139 static int 140 bcm_mbox_attach(device_t dev) 141 { 142 struct bcm_mbox_softc *sc = device_get_softc(dev); 143 int i; 144 int rid = 0; 145 146 sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE); 147 if (sc->mem_res == NULL) { 148 device_printf(dev, "could not allocate memory resource\n"); 149 return (ENXIO); 150 } 151 152 sc->bst = rman_get_bustag(sc->mem_res); 153 sc->bsh = rman_get_bushandle(sc->mem_res); 154 155 rid = 0; 156 sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE); 157 if (sc->irq_res == NULL) { 158 device_printf(dev, "could not allocate interrupt resource\n"); 159 return (ENXIO); 160 } 161 162 /* Setup and enable the timer */ 163 if (bus_setup_intr(dev, sc->irq_res, INTR_MPSAFE | INTR_TYPE_MISC, 164 NULL, bcm_mbox_intr, sc, &sc->intr_hl) != 0) { 165 bus_release_resource(dev, SYS_RES_IRQ, rid, sc->irq_res); 166 device_printf(dev, "Unable to setup the clock irq handler.\n"); 167 return (ENXIO); 168 } 169 170 mtx_init(&sc->lock, "vcio mbox", MTX_DEF, 0); 171 for (i = 0; i < BCM2835_MBOX_CHANS; i++) { 172 sc->valid[0] = 0; 173 sc->msg[0] = 0; 174 } 175 176 /* Read all pending messages */ 177 bcm_mbox_intr(sc); 178 179 mbox_write_4(sc, REG_CONFIG, CONFIG_DATA_IRQ); 180 181 return (0); 182 } 183 184 /* 185 * Mailbox API 186 */ 187 static int 188 bcm_mbox_write(device_t dev, int chan, uint32_t data) 189 { 190 int limit = 20000; 191 struct bcm_mbox_softc *sc = device_get_softc(dev); 192 193 dprintf("bcm_mbox_write: chan %d, data %08x\n", chan, data); 194 MBOX_LOCK(sc); 195 196 while ((mbox_read_4(sc, REG_STATUS) & STATUS_FULL) && limit--) { 197 DELAY(2); 198 } 199 200 if (limit == 0) { 201 printf("bcm_mbox_write: STATUS_FULL stuck"); 202 MBOX_UNLOCK(sc); 203 return (EAGAIN); 204 } 205 206 mbox_write_4(sc, REG_WRITE, MBOX_MSG(chan, data)); 207 208 MBOX_UNLOCK(sc); 209 return (0); 210 } 211 212 static int 213 bcm_mbox_read(device_t dev, int chan, uint32_t *data) 214 { 215 struct bcm_mbox_softc *sc = device_get_softc(dev); 216 217 dprintf("bcm_mbox_read: chan %d\n", chan); 218 MBOX_LOCK(sc); 219 while (!sc->valid[chan]) 220 msleep(&sc->msg[chan], &sc->lock, PZERO, "vcio mbox read", 0); 221 *data = sc->msg[chan]; 222 sc->valid[chan] = 0; 223 MBOX_UNLOCK(sc); 224 dprintf("bcm_mbox_read: chan %d, data %08x\n", chan, *data); 225 226 return (0); 227 } 228 229 static device_method_t bcm_mbox_methods[] = { 230 DEVMETHOD(device_probe, bcm_mbox_probe), 231 DEVMETHOD(device_attach, bcm_mbox_attach), 232 233 DEVMETHOD(mbox_read, bcm_mbox_read), 234 DEVMETHOD(mbox_write, bcm_mbox_write), 235 236 DEVMETHOD_END 237 }; 238 239 static driver_t bcm_mbox_driver = { 240 "mbox", 241 bcm_mbox_methods, 242 sizeof(struct bcm_mbox_softc), 243 }; 244 245 static devclass_t bcm_mbox_devclass; 246 247 DRIVER_MODULE(mbox, simplebus, bcm_mbox_driver, bcm_mbox_devclass, 0, 0); 248 249