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