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