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/sx.h> 38 #include <sys/rman.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 #include <arm/broadcom/bcm2835/bcm2835_mbox_prop.h> 46 #include <arm/broadcom/bcm2835/bcm2835_vcbus.h> 47 48 #include "mbox_if.h" 49 50 #define REG_READ 0x00 51 #define REG_POL 0x10 52 #define REG_SENDER 0x14 53 #define REG_STATUS 0x18 54 #define STATUS_FULL 0x80000000 55 #define STATUS_EMPTY 0x40000000 56 #define REG_CONFIG 0x1C 57 #define CONFIG_DATA_IRQ 0x00000001 58 #define REG_WRITE 0x20 /* This is Mailbox 1 address */ 59 60 #define MBOX_MSG(chan, data) (((data) & ~0xf) | ((chan) & 0xf)) 61 #define MBOX_CHAN(msg) ((msg) & 0xf) 62 #define MBOX_DATA(msg) ((msg) & ~0xf) 63 64 #define MBOX_LOCK(sc) do { \ 65 mtx_lock(&(sc)->lock); \ 66 } while(0) 67 68 #define MBOX_UNLOCK(sc) do { \ 69 mtx_unlock(&(sc)->lock); \ 70 } while(0) 71 72 #ifdef DEBUG 73 #define dprintf(fmt, args...) printf(fmt, ##args) 74 #else 75 #define dprintf(fmt, args...) 76 #endif 77 78 struct bcm_mbox_softc { 79 struct mtx lock; 80 struct resource * mem_res; 81 struct resource * irq_res; 82 void* intr_hl; 83 bus_space_tag_t bst; 84 bus_space_handle_t bsh; 85 int msg[BCM2835_MBOX_CHANS]; 86 int have_message[BCM2835_MBOX_CHANS]; 87 struct sx property_chan_lock; 88 }; 89 90 #define mbox_read_4(sc, reg) \ 91 bus_space_read_4((sc)->bst, (sc)->bsh, reg) 92 #define mbox_write_4(sc, reg, val) \ 93 bus_space_write_4((sc)->bst, (sc)->bsh, reg, val) 94 95 static int 96 bcm_mbox_read_msg(struct bcm_mbox_softc *sc, int *ochan) 97 { 98 uint32_t data; 99 uint32_t msg; 100 int chan; 101 102 msg = mbox_read_4(sc, REG_READ); 103 dprintf("bcm_mbox_intr: raw data %08x\n", msg); 104 chan = MBOX_CHAN(msg); 105 data = MBOX_DATA(msg); 106 if (sc->msg[chan]) { 107 printf("bcm_mbox_intr: channel %d oveflow\n", chan); 108 return (1); 109 } 110 dprintf("bcm_mbox_intr: chan %d, data %08x\n", chan, data); 111 sc->msg[chan] = msg; 112 113 if (ochan != NULL) 114 *ochan = chan; 115 116 return (0); 117 } 118 119 static void 120 bcm_mbox_intr(void *arg) 121 { 122 struct bcm_mbox_softc *sc = arg; 123 int chan; 124 125 MBOX_LOCK(sc); 126 while (!(mbox_read_4(sc, REG_STATUS) & STATUS_EMPTY)) 127 if (bcm_mbox_read_msg(sc, &chan) == 0) { 128 sc->have_message[chan] = 1; 129 wakeup(&sc->have_message[chan]); 130 } 131 MBOX_UNLOCK(sc); 132 } 133 134 static int 135 bcm_mbox_probe(device_t dev) 136 { 137 138 if (!ofw_bus_status_okay(dev)) 139 return (ENXIO); 140 141 if (ofw_bus_is_compatible(dev, "broadcom,bcm2835-mbox")) { 142 device_set_desc(dev, "BCM2835 VideoCore Mailbox"); 143 return(BUS_PROBE_DEFAULT); 144 } 145 146 return (ENXIO); 147 } 148 149 static int 150 bcm_mbox_attach(device_t dev) 151 { 152 struct bcm_mbox_softc *sc = device_get_softc(dev); 153 int i; 154 int rid = 0; 155 156 sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE); 157 if (sc->mem_res == NULL) { 158 device_printf(dev, "could not allocate memory resource\n"); 159 return (ENXIO); 160 } 161 162 sc->bst = rman_get_bustag(sc->mem_res); 163 sc->bsh = rman_get_bushandle(sc->mem_res); 164 165 rid = 0; 166 sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE); 167 if (sc->irq_res == NULL) { 168 device_printf(dev, "could not allocate interrupt resource\n"); 169 return (ENXIO); 170 } 171 172 /* Setup and enable the timer */ 173 if (bus_setup_intr(dev, sc->irq_res, INTR_MPSAFE | INTR_TYPE_MISC, 174 NULL, bcm_mbox_intr, sc, &sc->intr_hl) != 0) { 175 bus_release_resource(dev, SYS_RES_IRQ, rid, sc->irq_res); 176 device_printf(dev, "Unable to setup the clock irq handler.\n"); 177 return (ENXIO); 178 } 179 180 mtx_init(&sc->lock, "vcio mbox", NULL, MTX_DEF); 181 for (i = 0; i < BCM2835_MBOX_CHANS; i++) { 182 sc->msg[i] = 0; 183 sc->have_message[i] = 0; 184 } 185 186 sx_init(&sc->property_chan_lock, "mboxprop"); 187 188 /* Read all pending messages */ 189 while ((mbox_read_4(sc, REG_STATUS) & STATUS_EMPTY) == 0) 190 (void)mbox_read_4(sc, REG_READ); 191 192 mbox_write_4(sc, REG_CONFIG, CONFIG_DATA_IRQ); 193 194 return (0); 195 } 196 197 /* 198 * Mailbox API 199 */ 200 static int 201 bcm_mbox_write(device_t dev, int chan, uint32_t data) 202 { 203 int limit = 1000; 204 struct bcm_mbox_softc *sc = device_get_softc(dev); 205 206 dprintf("bcm_mbox_write: chan %d, data %08x\n", chan, data); 207 MBOX_LOCK(sc); 208 sc->have_message[chan] = 0; 209 while ((mbox_read_4(sc, REG_STATUS) & STATUS_FULL) && --limit) 210 DELAY(5); 211 if (limit == 0) { 212 printf("bcm_mbox_write: STATUS_FULL stuck"); 213 MBOX_UNLOCK(sc); 214 return (EAGAIN); 215 } 216 mbox_write_4(sc, REG_WRITE, MBOX_MSG(chan, data)); 217 MBOX_UNLOCK(sc); 218 219 return (0); 220 } 221 222 static int 223 bcm_mbox_read(device_t dev, int chan, uint32_t *data) 224 { 225 struct bcm_mbox_softc *sc = device_get_softc(dev); 226 int err, read_chan; 227 228 dprintf("bcm_mbox_read: chan %d\n", chan); 229 230 err = 0; 231 MBOX_LOCK(sc); 232 if (!cold) { 233 if (sc->have_message[chan] == 0) { 234 if (mtx_sleep(&sc->have_message[chan], &sc->lock, 0, 235 "mbox", 10*hz) != 0) { 236 device_printf(dev, "timeout waiting for message on chan %d\n", chan); 237 err = ETIMEDOUT; 238 } 239 } 240 } else { 241 do { 242 /* Wait for a message */ 243 while ((mbox_read_4(sc, REG_STATUS) & STATUS_EMPTY)) 244 ; 245 /* Read the message */ 246 if (bcm_mbox_read_msg(sc, &read_chan) != 0) { 247 err = EINVAL; 248 goto out; 249 } 250 } while (read_chan != chan); 251 } 252 /* 253 * get data from intr handler, the same channel is never coming 254 * because of holding sc lock. 255 */ 256 *data = MBOX_DATA(sc->msg[chan]); 257 sc->msg[chan] = 0; 258 sc->have_message[chan] = 0; 259 out: 260 MBOX_UNLOCK(sc); 261 dprintf("bcm_mbox_read: chan %d, data %08x\n", chan, *data); 262 263 return (err); 264 } 265 266 static device_method_t bcm_mbox_methods[] = { 267 DEVMETHOD(device_probe, bcm_mbox_probe), 268 DEVMETHOD(device_attach, bcm_mbox_attach), 269 270 DEVMETHOD(mbox_read, bcm_mbox_read), 271 DEVMETHOD(mbox_write, bcm_mbox_write), 272 273 DEVMETHOD_END 274 }; 275 276 static driver_t bcm_mbox_driver = { 277 "mbox", 278 bcm_mbox_methods, 279 sizeof(struct bcm_mbox_softc), 280 }; 281 282 static devclass_t bcm_mbox_devclass; 283 284 DRIVER_MODULE(mbox, simplebus, bcm_mbox_driver, bcm_mbox_devclass, 0, 0); 285 286 static void 287 bcm2835_mbox_dma_cb(void *arg, bus_dma_segment_t *segs, int nseg, int err) 288 { 289 bus_addr_t *addr; 290 291 if (err) 292 return; 293 addr = (bus_addr_t *)arg; 294 *addr = PHYS_TO_VCBUS(segs[0].ds_addr); 295 } 296 297 static void * 298 bcm2835_mbox_init_dma(device_t dev, size_t len, bus_dma_tag_t *tag, 299 bus_dmamap_t *map, bus_addr_t *phys) 300 { 301 void *buf; 302 int err; 303 304 err = bus_dma_tag_create(bus_get_dma_tag(dev), 16, 0, 305 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, 306 len, 1, len, 0, NULL, NULL, tag); 307 if (err != 0) { 308 device_printf(dev, "can't create DMA tag\n"); 309 return (NULL); 310 } 311 312 err = bus_dmamem_alloc(*tag, &buf, 0, map); 313 if (err != 0) { 314 bus_dma_tag_destroy(*tag); 315 device_printf(dev, "can't allocate dmamem\n"); 316 return (NULL); 317 } 318 319 err = bus_dmamap_load(*tag, *map, buf, len, bcm2835_mbox_dma_cb, 320 phys, 0); 321 if (err != 0) { 322 bus_dmamem_free(*tag, buf, *map); 323 bus_dma_tag_destroy(*tag); 324 device_printf(dev, "can't load DMA map\n"); 325 return (NULL); 326 } 327 328 return (buf); 329 } 330 331 static int 332 bcm2835_mbox_err(device_t dev, bus_addr_t msg_phys, uint32_t resp_phys, 333 struct bcm2835_mbox_hdr *msg, size_t len) 334 { 335 int idx; 336 struct bcm2835_mbox_tag_hdr *tag; 337 uint8_t *last; 338 339 if ((uint32_t)msg_phys != resp_phys) { 340 device_printf(dev, "response channel mismatch\n"); 341 return (EIO); 342 } 343 if (msg->code != BCM2835_MBOX_CODE_RESP_SUCCESS) { 344 device_printf(dev, "mbox response error\n"); 345 return (EIO); 346 } 347 348 /* Loop until the end tag. */ 349 tag = (struct bcm2835_mbox_tag_hdr *)(msg + 1); 350 last = (uint8_t *)msg + len; 351 for (idx = 0; tag->tag != 0; idx++) { 352 if ((tag->val_len & BCM2835_MBOX_TAG_VAL_LEN_RESPONSE) == 0) { 353 device_printf(dev, "tag %d response error\n", idx); 354 return (EIO); 355 } 356 /* Clear the response bit. */ 357 tag->val_len &= ~BCM2835_MBOX_TAG_VAL_LEN_RESPONSE; 358 359 /* Next tag. */ 360 tag = (struct bcm2835_mbox_tag_hdr *)((uint8_t *)tag + 361 sizeof(*tag) + tag->val_buf_size); 362 363 if ((uint8_t *)tag > last) { 364 device_printf(dev, "mbox buffer size error\n"); 365 return (EIO); 366 } 367 } 368 369 return (0); 370 } 371 372 int 373 bcm2835_mbox_property(void *msg, size_t msg_size) 374 { 375 struct bcm_mbox_softc *sc; 376 struct msg_set_power_state *buf; 377 bus_dma_tag_t msg_tag; 378 bus_dmamap_t msg_map; 379 bus_addr_t msg_phys; 380 uint32_t reg; 381 device_t mbox; 382 int err; 383 384 /* get mbox device */ 385 mbox = devclass_get_device(devclass_find("mbox"), 0); 386 if (mbox == NULL) 387 return (ENXIO); 388 389 sc = device_get_softc(mbox); 390 sx_xlock(&sc->property_chan_lock); 391 392 /* Allocate memory for the message */ 393 buf = bcm2835_mbox_init_dma(mbox, msg_size, &msg_tag, &msg_map, 394 &msg_phys); 395 if (buf == NULL) { 396 err = ENOMEM; 397 goto out; 398 } 399 400 memcpy(buf, msg, msg_size); 401 402 bus_dmamap_sync(msg_tag, msg_map, 403 BUS_DMASYNC_PREWRITE); 404 405 MBOX_WRITE(mbox, BCM2835_MBOX_CHAN_PROP, (uint32_t)msg_phys); 406 MBOX_READ(mbox, BCM2835_MBOX_CHAN_PROP, ®); 407 408 bus_dmamap_sync(msg_tag, msg_map, 409 BUS_DMASYNC_PREREAD); 410 411 memcpy(msg, buf, msg_size); 412 413 err = bcm2835_mbox_err(mbox, msg_phys, reg, 414 (struct bcm2835_mbox_hdr *)msg, msg_size); 415 416 bus_dmamap_unload(msg_tag, msg_map); 417 bus_dmamem_free(msg_tag, buf, msg_map); 418 bus_dma_tag_destroy(msg_tag); 419 out: 420 sx_xunlock(&sc->property_chan_lock); 421 return (err); 422 } 423 424 int 425 bcm2835_mbox_set_power_state(uint32_t device_id, boolean_t on) 426 { 427 struct msg_set_power_state msg; 428 int err; 429 430 memset(&msg, 0, sizeof(msg)); 431 msg.hdr.buf_size = sizeof(msg); 432 msg.hdr.code = BCM2835_MBOX_CODE_REQ; 433 msg.tag_hdr.tag = BCM2835_MBOX_TAG_SET_POWER_STATE; 434 msg.tag_hdr.val_buf_size = sizeof(msg.body); 435 msg.tag_hdr.val_len = sizeof(msg.body.req); 436 msg.body.req.device_id = device_id; 437 msg.body.req.state = (on ? BCM2835_MBOX_POWER_ON : 0) | 438 BCM2835_MBOX_POWER_WAIT; 439 msg.end_tag = 0; 440 441 err = bcm2835_mbox_property(&msg, sizeof(msg)); 442 443 return (err); 444 } 445 446 int 447 bcm2835_mbox_get_clock_rate(uint32_t clock_id, uint32_t *hz) 448 { 449 struct msg_get_clock_rate msg; 450 int err; 451 452 memset(&msg, 0, sizeof(msg)); 453 msg.hdr.buf_size = sizeof(msg); 454 msg.hdr.code = BCM2835_MBOX_CODE_REQ; 455 msg.tag_hdr.tag = BCM2835_MBOX_TAG_GET_CLOCK_RATE; 456 msg.tag_hdr.val_buf_size = sizeof(msg.body); 457 msg.tag_hdr.val_len = sizeof(msg.body.req); 458 msg.body.req.clock_id = clock_id; 459 msg.end_tag = 0; 460 461 err = bcm2835_mbox_property(&msg, sizeof(msg)); 462 *hz = msg.body.resp.rate_hz; 463 464 return (err); 465 } 466 467 int 468 bcm2835_mbox_fb_get_w_h(struct bcm2835_fb_config *fb) 469 { 470 int err; 471 struct msg_fb_get_w_h msg; 472 473 memset(&msg, 0, sizeof(msg)); 474 msg.hdr.buf_size = sizeof(msg); 475 msg.hdr.code = BCM2835_MBOX_CODE_REQ; 476 BCM2835_MBOX_INIT_TAG(&msg.physical_w_h, GET_PHYSICAL_W_H); 477 msg.physical_w_h.tag_hdr.val_len = 0; 478 msg.end_tag = 0; 479 480 err = bcm2835_mbox_property(&msg, sizeof(msg)); 481 if (err == 0) { 482 fb->xres = msg.physical_w_h.body.resp.width; 483 fb->yres = msg.physical_w_h.body.resp.height; 484 } 485 486 return (err); 487 } 488 489 int 490 bcm2835_mbox_fb_init(struct bcm2835_fb_config *fb) 491 { 492 int err; 493 struct msg_fb_setup msg; 494 495 memset(&msg, 0, sizeof(msg)); 496 msg.hdr.buf_size = sizeof(msg); 497 msg.hdr.code = BCM2835_MBOX_CODE_REQ; 498 BCM2835_MBOX_INIT_TAG(&msg.physical_w_h, SET_PHYSICAL_W_H); 499 msg.physical_w_h.body.req.width = fb->xres; 500 msg.physical_w_h.body.req.height = fb->yres; 501 BCM2835_MBOX_INIT_TAG(&msg.virtual_w_h, SET_VIRTUAL_W_H); 502 msg.virtual_w_h.body.req.width = fb->vxres; 503 msg.virtual_w_h.body.req.height = fb->vyres; 504 BCM2835_MBOX_INIT_TAG(&msg.offset, SET_VIRTUAL_OFFSET); 505 msg.offset.body.req.x = fb->xoffset; 506 msg.offset.body.req.y = fb->yoffset; 507 BCM2835_MBOX_INIT_TAG(&msg.depth, SET_DEPTH); 508 msg.depth.body.req.bpp = fb->bpp; 509 BCM2835_MBOX_INIT_TAG(&msg.alpha, SET_ALPHA_MODE); 510 msg.alpha.body.req.alpha = BCM2835_MBOX_ALPHA_MODE_IGNORED; 511 BCM2835_MBOX_INIT_TAG(&msg.buffer, ALLOCATE_BUFFER); 512 msg.buffer.body.req.alignment = PAGE_SIZE; 513 BCM2835_MBOX_INIT_TAG(&msg.pitch, GET_PITCH); 514 msg.end_tag = 0; 515 516 err = bcm2835_mbox_property(&msg, sizeof(msg)); 517 if (err == 0) { 518 fb->xres = msg.physical_w_h.body.resp.width; 519 fb->yres = msg.physical_w_h.body.resp.height; 520 fb->vxres = msg.virtual_w_h.body.resp.width; 521 fb->vyres = msg.virtual_w_h.body.resp.height; 522 fb->xoffset = msg.offset.body.resp.x; 523 fb->yoffset = msg.offset.body.resp.y; 524 fb->pitch = msg.pitch.body.resp.pitch; 525 fb->base = VCBUS_TO_PHYS(msg.buffer.body.resp.fb_address); 526 fb->size = msg.buffer.body.resp.fb_size; 527 } 528 529 return (err); 530 } 531