1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2012 Oleksandr Tymoshenko <gonzo@freebsd.org> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 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_firmware.h> 45 #include <arm/broadcom/bcm2835/bcm2835_mbox.h> 46 #include <arm/broadcom/bcm2835/bcm2835_mbox_prop.h> 47 #include <arm/broadcom/bcm2835/bcm2835_vcbus.h> 48 49 #include "mbox_if.h" 50 51 #define REG_READ 0x00 52 #define REG_POL 0x10 53 #define REG_SENDER 0x14 54 #define REG_STATUS 0x18 55 #define STATUS_FULL 0x80000000 56 #define STATUS_EMPTY 0x40000000 57 #define REG_CONFIG 0x1C 58 #define CONFIG_DATA_IRQ 0x00000001 59 #define REG_WRITE 0x20 /* This is Mailbox 1 address */ 60 61 #define MBOX_MSG(chan, data) (((data) & ~0xf) | ((chan) & 0xf)) 62 #define MBOX_CHAN(msg) ((msg) & 0xf) 63 #define MBOX_DATA(msg) ((msg) & ~0xf) 64 65 #define MBOX_LOCK(sc) do { \ 66 mtx_lock(&(sc)->lock); \ 67 } while(0) 68 69 #define MBOX_UNLOCK(sc) do { \ 70 mtx_unlock(&(sc)->lock); \ 71 } while(0) 72 73 #ifdef DEBUG 74 #define dprintf(fmt, args...) printf(fmt, ##args) 75 #else 76 #define dprintf(fmt, args...) 77 #endif 78 79 struct bcm_mbox_softc { 80 struct mtx lock; 81 struct resource * mem_res; 82 struct resource * irq_res; 83 void* intr_hl; 84 bus_space_tag_t bst; 85 bus_space_handle_t bsh; 86 int msg[BCM2835_MBOX_CHANS]; 87 int have_message[BCM2835_MBOX_CHANS]; 88 struct sx property_chan_lock; 89 }; 90 91 #define mbox_read_4(sc, reg) \ 92 bus_space_read_4((sc)->bst, (sc)->bsh, reg) 93 #define mbox_write_4(sc, reg, val) \ 94 bus_space_write_4((sc)->bst, (sc)->bsh, reg, val) 95 96 static struct ofw_compat_data compat_data[] = { 97 {"broadcom,bcm2835-mbox", 1}, 98 {"brcm,bcm2835-mbox", 1}, 99 {NULL, 0} 100 }; 101 102 static int 103 bcm_mbox_read_msg(struct bcm_mbox_softc *sc, int *ochan) 104 { 105 #ifdef DEBUG 106 uint32_t data; 107 #endif 108 uint32_t msg; 109 int chan; 110 111 msg = mbox_read_4(sc, REG_READ); 112 dprintf("bcm_mbox_intr: raw data %08x\n", msg); 113 chan = MBOX_CHAN(msg); 114 #ifdef DEBUG 115 data = MBOX_DATA(msg); 116 #endif 117 if (sc->msg[chan]) { 118 printf("bcm_mbox_intr: channel %d oveflow\n", chan); 119 return (1); 120 } 121 dprintf("bcm_mbox_intr: chan %d, data %08x\n", chan, data); 122 sc->msg[chan] = msg; 123 124 if (ochan != NULL) 125 *ochan = chan; 126 127 return (0); 128 } 129 130 static void 131 bcm_mbox_intr(void *arg) 132 { 133 struct bcm_mbox_softc *sc = arg; 134 int chan; 135 136 MBOX_LOCK(sc); 137 while (!(mbox_read_4(sc, REG_STATUS) & STATUS_EMPTY)) 138 if (bcm_mbox_read_msg(sc, &chan) == 0) { 139 sc->have_message[chan] = 1; 140 wakeup(&sc->have_message[chan]); 141 } 142 MBOX_UNLOCK(sc); 143 } 144 145 static int 146 bcm_mbox_probe(device_t dev) 147 { 148 149 if (!ofw_bus_status_okay(dev)) 150 return (ENXIO); 151 152 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0) 153 return (ENXIO); 154 155 device_set_desc(dev, "BCM2835 VideoCore Mailbox"); 156 157 return (BUS_PROBE_DEFAULT); 158 } 159 160 static int 161 bcm_mbox_attach(device_t dev) 162 { 163 struct bcm_mbox_softc *sc = device_get_softc(dev); 164 int i; 165 int rid = 0; 166 167 sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE); 168 if (sc->mem_res == NULL) { 169 device_printf(dev, "could not allocate memory resource\n"); 170 return (ENXIO); 171 } 172 173 sc->bst = rman_get_bustag(sc->mem_res); 174 sc->bsh = rman_get_bushandle(sc->mem_res); 175 176 rid = 0; 177 sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE); 178 if (sc->irq_res == NULL) { 179 device_printf(dev, "could not allocate interrupt resource\n"); 180 return (ENXIO); 181 } 182 183 /* Setup and enable the timer */ 184 if (bus_setup_intr(dev, sc->irq_res, INTR_MPSAFE | INTR_TYPE_MISC, 185 NULL, bcm_mbox_intr, sc, &sc->intr_hl) != 0) { 186 bus_release_resource(dev, SYS_RES_IRQ, rid, sc->irq_res); 187 device_printf(dev, "Unable to setup the clock irq handler.\n"); 188 return (ENXIO); 189 } 190 191 mtx_init(&sc->lock, "vcio mbox", NULL, MTX_DEF); 192 for (i = 0; i < BCM2835_MBOX_CHANS; i++) { 193 sc->msg[i] = 0; 194 sc->have_message[i] = 0; 195 } 196 197 sx_init(&sc->property_chan_lock, "mboxprop"); 198 199 /* Read all pending messages */ 200 while ((mbox_read_4(sc, REG_STATUS) & STATUS_EMPTY) == 0) 201 (void)mbox_read_4(sc, REG_READ); 202 203 mbox_write_4(sc, REG_CONFIG, CONFIG_DATA_IRQ); 204 205 return (0); 206 } 207 208 /* 209 * Mailbox API 210 */ 211 static int 212 bcm_mbox_write(device_t dev, int chan, uint32_t data) 213 { 214 int limit = 1000; 215 struct bcm_mbox_softc *sc = device_get_softc(dev); 216 217 dprintf("bcm_mbox_write: chan %d, data %08x\n", chan, data); 218 MBOX_LOCK(sc); 219 sc->have_message[chan] = 0; 220 while ((mbox_read_4(sc, REG_STATUS) & STATUS_FULL) && --limit) 221 DELAY(5); 222 if (limit == 0) { 223 printf("bcm_mbox_write: STATUS_FULL stuck"); 224 MBOX_UNLOCK(sc); 225 return (EAGAIN); 226 } 227 mbox_write_4(sc, REG_WRITE, MBOX_MSG(chan, data)); 228 MBOX_UNLOCK(sc); 229 230 return (0); 231 } 232 233 static int 234 bcm_mbox_read(device_t dev, int chan, uint32_t *data) 235 { 236 struct bcm_mbox_softc *sc = device_get_softc(dev); 237 int err, read_chan; 238 239 dprintf("bcm_mbox_read: chan %d\n", chan); 240 241 err = 0; 242 MBOX_LOCK(sc); 243 if (!cold) { 244 if (sc->have_message[chan] == 0) { 245 if (mtx_sleep(&sc->have_message[chan], &sc->lock, 0, 246 "mbox", 10*hz) != 0) { 247 device_printf(dev, "timeout waiting for message on chan %d\n", chan); 248 err = ETIMEDOUT; 249 } 250 } 251 } else { 252 do { 253 /* Wait for a message */ 254 while ((mbox_read_4(sc, REG_STATUS) & STATUS_EMPTY)) 255 ; 256 /* Read the message */ 257 if (bcm_mbox_read_msg(sc, &read_chan) != 0) { 258 err = EINVAL; 259 goto out; 260 } 261 } while (read_chan != chan); 262 } 263 /* 264 * get data from intr handler, the same channel is never coming 265 * because of holding sc lock. 266 */ 267 *data = MBOX_DATA(sc->msg[chan]); 268 sc->msg[chan] = 0; 269 sc->have_message[chan] = 0; 270 out: 271 MBOX_UNLOCK(sc); 272 dprintf("bcm_mbox_read: chan %d, data %08x\n", chan, *data); 273 274 return (err); 275 } 276 277 static device_method_t bcm_mbox_methods[] = { 278 DEVMETHOD(device_probe, bcm_mbox_probe), 279 DEVMETHOD(device_attach, bcm_mbox_attach), 280 281 DEVMETHOD(mbox_read, bcm_mbox_read), 282 DEVMETHOD(mbox_write, bcm_mbox_write), 283 284 DEVMETHOD_END 285 }; 286 287 static driver_t bcm_mbox_driver = { 288 "mbox", 289 bcm_mbox_methods, 290 sizeof(struct bcm_mbox_softc), 291 }; 292 293 EARLY_DRIVER_MODULE(mbox, simplebus, bcm_mbox_driver, 0, 0, 294 BUS_PASS_INTERRUPT + BUS_PASS_ORDER_LAST); 295 296 static void 297 bcm2835_mbox_dma_cb(void *arg, bus_dma_segment_t *segs, int nseg, int err) 298 { 299 bus_addr_t *addr; 300 301 if (err) 302 return; 303 addr = (bus_addr_t *)arg; 304 *addr = ARMC_TO_VCBUS(segs[0].ds_addr); 305 } 306 307 static void * 308 bcm2835_mbox_init_dma(device_t dev, size_t len, bus_dma_tag_t *tag, 309 bus_dmamap_t *map, bus_addr_t *phys) 310 { 311 void *buf; 312 int err; 313 314 err = bus_dma_tag_create(bus_get_dma_tag(dev), 16, 0, 315 bcm283x_dmabus_peripheral_lowaddr(), BUS_SPACE_MAXADDR, NULL, NULL, 316 len, 1, len, 0, NULL, NULL, tag); 317 if (err != 0) { 318 device_printf(dev, "can't create DMA tag\n"); 319 return (NULL); 320 } 321 322 err = bus_dmamem_alloc(*tag, &buf, 0, map); 323 if (err != 0) { 324 bus_dma_tag_destroy(*tag); 325 device_printf(dev, "can't allocate dmamem\n"); 326 return (NULL); 327 } 328 329 err = bus_dmamap_load(*tag, *map, buf, len, bcm2835_mbox_dma_cb, 330 phys, 0); 331 if (err != 0) { 332 bus_dmamem_free(*tag, buf, *map); 333 bus_dma_tag_destroy(*tag); 334 device_printf(dev, "can't load DMA map\n"); 335 return (NULL); 336 } 337 338 return (buf); 339 } 340 341 static int 342 bcm2835_mbox_err(device_t dev, bus_addr_t msg_phys, uint32_t resp_phys, 343 struct bcm2835_mbox_hdr *msg, size_t len) 344 { 345 int idx; 346 struct bcm2835_mbox_tag_hdr *tag; 347 uint8_t *last; 348 349 if ((uint32_t)msg_phys != resp_phys) { 350 device_printf(dev, "response channel mismatch\n"); 351 return (EIO); 352 } 353 if (msg->code != BCM2835_MBOX_CODE_RESP_SUCCESS) { 354 device_printf(dev, "mbox response error\n"); 355 return (EIO); 356 } 357 358 /* Loop until the end tag. */ 359 tag = (struct bcm2835_mbox_tag_hdr *)(msg + 1); 360 last = (uint8_t *)msg + len; 361 for (idx = 0; tag->tag != 0; idx++) { 362 /* 363 * When setting the GPIO config or state the firmware doesn't 364 * set tag->val_len correctly. 365 */ 366 if ((tag->tag == BCM2835_FIRMWARE_TAG_SET_GPIO_CONFIG || 367 tag->tag == BCM2835_FIRMWARE_TAG_SET_GPIO_STATE) && 368 tag->val_len == 0) { 369 tag->val_len = BCM2835_MBOX_TAG_VAL_LEN_RESPONSE | 370 tag->val_buf_size; 371 } 372 if ((tag->val_len & BCM2835_MBOX_TAG_VAL_LEN_RESPONSE) == 0) { 373 device_printf(dev, "tag %d response error\n", idx); 374 return (EIO); 375 } 376 /* Clear the response bit. */ 377 tag->val_len &= ~BCM2835_MBOX_TAG_VAL_LEN_RESPONSE; 378 379 /* Next tag. */ 380 tag = (struct bcm2835_mbox_tag_hdr *)((uint8_t *)tag + 381 sizeof(*tag) + tag->val_buf_size); 382 383 if ((uint8_t *)tag > last) { 384 device_printf(dev, "mbox buffer size error\n"); 385 return (EIO); 386 } 387 } 388 389 return (0); 390 } 391 392 int 393 bcm2835_mbox_property(void *msg, size_t msg_size) 394 { 395 struct bcm_mbox_softc *sc; 396 bus_dma_tag_t msg_tag; 397 bus_dmamap_t msg_map; 398 bus_addr_t msg_phys; 399 char *buf; 400 uint32_t reg; 401 device_t mbox; 402 int err; 403 404 /* get mbox device */ 405 mbox = devclass_get_device(devclass_find("mbox"), 0); 406 if (mbox == NULL) 407 return (ENXIO); 408 409 sc = device_get_softc(mbox); 410 sx_xlock(&sc->property_chan_lock); 411 412 /* Allocate memory for the message */ 413 buf = bcm2835_mbox_init_dma(mbox, msg_size, &msg_tag, &msg_map, 414 &msg_phys); 415 if (buf == NULL) { 416 err = ENOMEM; 417 goto out; 418 } 419 420 memcpy(buf, msg, msg_size); 421 422 bus_dmamap_sync(msg_tag, msg_map, 423 BUS_DMASYNC_PREWRITE); 424 425 MBOX_WRITE(mbox, BCM2835_MBOX_CHAN_PROP, (uint32_t)msg_phys); 426 MBOX_READ(mbox, BCM2835_MBOX_CHAN_PROP, ®); 427 428 bus_dmamap_sync(msg_tag, msg_map, 429 BUS_DMASYNC_PREREAD); 430 431 memcpy(msg, buf, msg_size); 432 433 err = bcm2835_mbox_err(mbox, msg_phys, reg, 434 (struct bcm2835_mbox_hdr *)msg, msg_size); 435 436 bus_dmamap_unload(msg_tag, msg_map); 437 bus_dmamem_free(msg_tag, buf, msg_map); 438 bus_dma_tag_destroy(msg_tag); 439 out: 440 sx_xunlock(&sc->property_chan_lock); 441 return (err); 442 } 443 444 int 445 bcm2835_mbox_set_power_state(uint32_t device_id, boolean_t on) 446 { 447 struct msg_set_power_state msg; 448 int err; 449 450 memset(&msg, 0, sizeof(msg)); 451 msg.hdr.buf_size = sizeof(msg); 452 msg.hdr.code = BCM2835_MBOX_CODE_REQ; 453 msg.tag_hdr.tag = BCM2835_MBOX_TAG_SET_POWER_STATE; 454 msg.tag_hdr.val_buf_size = sizeof(msg.body); 455 msg.tag_hdr.val_len = sizeof(msg.body.req); 456 msg.body.req.device_id = device_id; 457 msg.body.req.state = (on ? BCM2835_MBOX_POWER_ON : 0) | 458 BCM2835_MBOX_POWER_WAIT; 459 msg.end_tag = 0; 460 461 err = bcm2835_mbox_property(&msg, sizeof(msg)); 462 463 return (err); 464 } 465 466 int 467 bcm2835_mbox_notify_xhci_reset(uint32_t pci_dev_addr) 468 { 469 struct msg_notify_xhci_reset msg; 470 int err; 471 472 memset(&msg, 0, sizeof(msg)); 473 msg.hdr.buf_size = sizeof(msg); 474 msg.hdr.code = BCM2835_MBOX_CODE_REQ; 475 msg.tag_hdr.tag = BCM2835_MBOX_TAG_NOTIFY_XHCI_RESET; 476 msg.tag_hdr.val_buf_size = sizeof(msg.body); 477 msg.tag_hdr.val_len = sizeof(msg.body.req); 478 msg.body.req.pci_device_addr = pci_dev_addr; 479 msg.end_tag = 0; 480 481 err = bcm2835_mbox_property(&msg, sizeof(msg)); 482 483 return (err); 484 } 485 486 int 487 bcm2835_mbox_get_clock_rate(uint32_t clock_id, uint32_t *hz) 488 { 489 struct msg_get_clock_rate msg; 490 int err; 491 492 memset(&msg, 0, sizeof(msg)); 493 msg.hdr.buf_size = sizeof(msg); 494 msg.hdr.code = BCM2835_MBOX_CODE_REQ; 495 msg.tag_hdr.tag = BCM2835_MBOX_TAG_GET_CLOCK_RATE; 496 msg.tag_hdr.val_buf_size = sizeof(msg.body); 497 msg.tag_hdr.val_len = sizeof(msg.body.req); 498 msg.body.req.clock_id = clock_id; 499 msg.end_tag = 0; 500 501 err = bcm2835_mbox_property(&msg, sizeof(msg)); 502 *hz = msg.body.resp.rate_hz; 503 504 return (err); 505 } 506 507 int 508 bcm2835_mbox_fb_get_w_h(struct bcm2835_fb_config *fb) 509 { 510 int err; 511 struct msg_fb_get_w_h msg; 512 513 memset(&msg, 0, sizeof(msg)); 514 msg.hdr.buf_size = sizeof(msg); 515 msg.hdr.code = BCM2835_MBOX_CODE_REQ; 516 BCM2835_MBOX_INIT_TAG(&msg.physical_w_h, GET_PHYSICAL_W_H); 517 msg.physical_w_h.tag_hdr.val_len = 0; 518 msg.end_tag = 0; 519 520 err = bcm2835_mbox_property(&msg, sizeof(msg)); 521 if (err == 0) { 522 fb->xres = msg.physical_w_h.body.resp.width; 523 fb->yres = msg.physical_w_h.body.resp.height; 524 } 525 526 return (err); 527 } 528 529 int 530 bcm2835_mbox_fb_get_bpp(struct bcm2835_fb_config *fb) 531 { 532 int err; 533 struct msg_fb_get_bpp msg; 534 535 memset(&msg, 0, sizeof(msg)); 536 msg.hdr.buf_size = sizeof(msg); 537 msg.hdr.code = BCM2835_MBOX_CODE_REQ; 538 BCM2835_MBOX_INIT_TAG(&msg.bpp, GET_DEPTH); 539 msg.bpp.tag_hdr.val_len = 0; 540 msg.end_tag = 0; 541 542 err = bcm2835_mbox_property(&msg, sizeof(msg)); 543 if (err == 0) 544 fb->bpp = msg.bpp.body.resp.bpp; 545 546 return (err); 547 } 548 549 int 550 bcm2835_mbox_fb_init(struct bcm2835_fb_config *fb) 551 { 552 int err; 553 struct msg_fb_setup msg; 554 555 memset(&msg, 0, sizeof(msg)); 556 msg.hdr.buf_size = sizeof(msg); 557 msg.hdr.code = BCM2835_MBOX_CODE_REQ; 558 BCM2835_MBOX_INIT_TAG(&msg.physical_w_h, SET_PHYSICAL_W_H); 559 msg.physical_w_h.body.req.width = fb->xres; 560 msg.physical_w_h.body.req.height = fb->yres; 561 BCM2835_MBOX_INIT_TAG(&msg.virtual_w_h, SET_VIRTUAL_W_H); 562 msg.virtual_w_h.body.req.width = fb->vxres; 563 msg.virtual_w_h.body.req.height = fb->vyres; 564 BCM2835_MBOX_INIT_TAG(&msg.offset, SET_VIRTUAL_OFFSET); 565 msg.offset.body.req.x = fb->xoffset; 566 msg.offset.body.req.y = fb->yoffset; 567 BCM2835_MBOX_INIT_TAG(&msg.depth, SET_DEPTH); 568 msg.depth.body.req.bpp = fb->bpp; 569 BCM2835_MBOX_INIT_TAG(&msg.alpha, SET_ALPHA_MODE); 570 msg.alpha.body.req.alpha = BCM2835_MBOX_ALPHA_MODE_IGNORED; 571 BCM2835_MBOX_INIT_TAG(&msg.buffer, ALLOCATE_BUFFER); 572 msg.buffer.body.req.alignment = PAGE_SIZE; 573 BCM2835_MBOX_INIT_TAG(&msg.pitch, GET_PITCH); 574 msg.end_tag = 0; 575 576 err = bcm2835_mbox_property(&msg, sizeof(msg)); 577 if (err == 0) { 578 fb->xres = msg.physical_w_h.body.resp.width; 579 fb->yres = msg.physical_w_h.body.resp.height; 580 fb->vxres = msg.virtual_w_h.body.resp.width; 581 fb->vyres = msg.virtual_w_h.body.resp.height; 582 fb->xoffset = msg.offset.body.resp.x; 583 fb->yoffset = msg.offset.body.resp.y; 584 fb->pitch = msg.pitch.body.resp.pitch; 585 fb->base = VCBUS_TO_ARMC(msg.buffer.body.resp.fb_address); 586 fb->size = msg.buffer.body.resp.fb_size; 587 } 588 589 return (err); 590 } 591