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/lock.h> 35 #include <sys/module.h> 36 #include <sys/mutex.h> 37 #include <sys/rman.h> 38 #include <sys/sema.h> 39 #include <machine/bus.h> 40 41 #include <dev/ofw/ofw_bus.h> 42 #include <dev/ofw/ofw_bus_subr.h> 43 44 #include <arm/broadcom/bcm2835/bcm2835_mbox.h> 45 46 #include "mbox_if.h" 47 48 #define REG_READ 0x00 49 #define REG_POL 0x10 50 #define REG_SENDER 0x14 51 #define REG_STATUS 0x18 52 #define STATUS_FULL 0x80000000 53 #define STATUS_EMPTY 0x40000000 54 #define REG_CONFIG 0x1C 55 #define CONFIG_DATA_IRQ 0x00000001 56 #define REG_WRITE 0x20 /* This is Mailbox 1 address */ 57 58 #define MBOX_MSG(chan, data) (((data) & ~0xf) | ((chan) & 0xf)) 59 #define MBOX_CHAN(msg) ((msg) & 0xf) 60 #define MBOX_DATA(msg) ((msg) & ~0xf) 61 62 #define MBOX_LOCK(sc) do { \ 63 mtx_lock(&(sc)->lock); \ 64 } while(0) 65 66 #define MBOX_UNLOCK(sc) do { \ 67 mtx_unlock(&(sc)->lock); \ 68 } while(0) 69 70 #ifdef DEBUG 71 #define dprintf(fmt, args...) printf(fmt, ##args) 72 #else 73 #define dprintf(fmt, args...) 74 #endif 75 76 struct bcm_mbox_softc { 77 struct mtx lock; 78 struct resource * mem_res; 79 struct resource * irq_res; 80 void* intr_hl; 81 bus_space_tag_t bst; 82 bus_space_handle_t bsh; 83 int msg[BCM2835_MBOX_CHANS]; 84 struct sema sema[BCM2835_MBOX_CHANS]; 85 }; 86 87 #define mbox_read_4(sc, reg) \ 88 bus_space_read_4((sc)->bst, (sc)->bsh, reg) 89 #define mbox_write_4(sc, reg, val) \ 90 bus_space_write_4((sc)->bst, (sc)->bsh, reg, val) 91 92 static int 93 bcm_mbox_read_msg(struct bcm_mbox_softc *sc, int *ochan) 94 { 95 uint32_t data; 96 uint32_t msg; 97 int chan; 98 99 msg = mbox_read_4(sc, REG_READ); 100 dprintf("bcm_mbox_intr: raw data %08x\n", msg); 101 chan = MBOX_CHAN(msg); 102 data = MBOX_DATA(msg); 103 if (sc->msg[chan]) { 104 printf("bcm_mbox_intr: channel %d oveflow\n", chan); 105 return (1); 106 } 107 dprintf("bcm_mbox_intr: chan %d, data %08x\n", chan, data); 108 sc->msg[chan] = msg; 109 110 if (ochan != NULL) 111 *ochan = chan; 112 113 return (0); 114 } 115 116 static void 117 bcm_mbox_intr(void *arg) 118 { 119 struct bcm_mbox_softc *sc = arg; 120 int chan; 121 122 while (!(mbox_read_4(sc, REG_STATUS) & STATUS_EMPTY)) 123 if (bcm_mbox_read_msg(sc, &chan) == 0) 124 sema_post(&sc->sema[chan]); 125 } 126 127 static int 128 bcm_mbox_probe(device_t dev) 129 { 130 131 if (!ofw_bus_status_okay(dev)) 132 return (ENXIO); 133 134 if (ofw_bus_is_compatible(dev, "broadcom,bcm2835-mbox")) { 135 device_set_desc(dev, "BCM2835 VideoCore Mailbox"); 136 return(BUS_PROBE_DEFAULT); 137 } 138 139 return (ENXIO); 140 } 141 142 static int 143 bcm_mbox_attach(device_t dev) 144 { 145 struct bcm_mbox_softc *sc = device_get_softc(dev); 146 int i; 147 int rid = 0; 148 149 sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE); 150 if (sc->mem_res == NULL) { 151 device_printf(dev, "could not allocate memory resource\n"); 152 return (ENXIO); 153 } 154 155 sc->bst = rman_get_bustag(sc->mem_res); 156 sc->bsh = rman_get_bushandle(sc->mem_res); 157 158 rid = 0; 159 sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE); 160 if (sc->irq_res == NULL) { 161 device_printf(dev, "could not allocate interrupt resource\n"); 162 return (ENXIO); 163 } 164 165 /* Setup and enable the timer */ 166 if (bus_setup_intr(dev, sc->irq_res, INTR_MPSAFE | INTR_TYPE_MISC, 167 NULL, bcm_mbox_intr, sc, &sc->intr_hl) != 0) { 168 bus_release_resource(dev, SYS_RES_IRQ, rid, sc->irq_res); 169 device_printf(dev, "Unable to setup the clock irq handler.\n"); 170 return (ENXIO); 171 } 172 173 mtx_init(&sc->lock, "vcio mbox", NULL, MTX_DEF); 174 for (i = 0; i < BCM2835_MBOX_CHANS; i++) { 175 sc->msg[i] = 0; 176 sema_init(&sc->sema[i], 0, "mbox"); 177 } 178 179 /* Read all pending messages */ 180 while ((mbox_read_4(sc, REG_STATUS) & STATUS_EMPTY) == 0) 181 (void)mbox_read_4(sc, REG_READ); 182 183 mbox_write_4(sc, REG_CONFIG, CONFIG_DATA_IRQ); 184 185 return (0); 186 } 187 188 /* 189 * Mailbox API 190 */ 191 static int 192 bcm_mbox_write(device_t dev, int chan, uint32_t data) 193 { 194 int limit = 1000; 195 struct bcm_mbox_softc *sc = device_get_softc(dev); 196 197 dprintf("bcm_mbox_write: chan %d, data %08x\n", chan, data); 198 MBOX_LOCK(sc); 199 while ((mbox_read_4(sc, REG_STATUS) & STATUS_FULL) && --limit) 200 DELAY(5); 201 if (limit == 0) { 202 printf("bcm_mbox_write: STATUS_FULL stuck"); 203 MBOX_UNLOCK(sc); 204 return (EAGAIN); 205 } 206 mbox_write_4(sc, REG_WRITE, MBOX_MSG(chan, data)); 207 MBOX_UNLOCK(sc); 208 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 int err, read_chan; 217 218 dprintf("bcm_mbox_read: chan %d\n", chan); 219 220 err = 0; 221 MBOX_LOCK(sc); 222 if (!cold) { 223 while (sema_trywait(&sc->sema[chan]) == 0) { 224 /* do not unlock sc while waiting for the mbox */ 225 if (sema_timedwait(&sc->sema[chan], 10*hz) == 0) 226 break; 227 printf("timeout sema for chan %d\n", chan); 228 } 229 } else { 230 do { 231 /* Wait for a message */ 232 while ((mbox_read_4(sc, REG_STATUS) & STATUS_EMPTY)) 233 ; 234 /* Read the message */ 235 if (bcm_mbox_read_msg(sc, &read_chan) != 0) { 236 err = EINVAL; 237 goto out; 238 } 239 } while (read_chan != chan); 240 } 241 /* 242 * get data from intr handler, the same channel is never coming 243 * because of holding sc lock. 244 */ 245 *data = MBOX_DATA(sc->msg[chan]); 246 sc->msg[chan] = 0; 247 out: 248 MBOX_UNLOCK(sc); 249 dprintf("bcm_mbox_read: chan %d, data %08x\n", chan, *data); 250 251 return (err); 252 } 253 254 static device_method_t bcm_mbox_methods[] = { 255 DEVMETHOD(device_probe, bcm_mbox_probe), 256 DEVMETHOD(device_attach, bcm_mbox_attach), 257 258 DEVMETHOD(mbox_read, bcm_mbox_read), 259 DEVMETHOD(mbox_write, bcm_mbox_write), 260 261 DEVMETHOD_END 262 }; 263 264 static driver_t bcm_mbox_driver = { 265 "mbox", 266 bcm_mbox_methods, 267 sizeof(struct bcm_mbox_softc), 268 }; 269 270 static devclass_t bcm_mbox_devclass; 271 272 DRIVER_MODULE(mbox, simplebus, bcm_mbox_driver, bcm_mbox_devclass, 0, 0); 273