2 * (C) Copyright 2012 Stephen Warren
4 * SPDX-License-Identifier: GPL-2.0+
9 #include <asm/arch/mbox.h>
12 #define TIMEOUT 1000 /* ms */
14 int bcm2835_mbox_call_raw(u32 chan, u32 send, u32 *recv)
16 struct bcm2835_mbox_regs *regs =
17 (struct bcm2835_mbox_regs *)BCM2835_MBOX_PHYSADDR;
18 ulong endtime = get_timer(0) + TIMEOUT;
21 debug("time: %lu timeout: %lu\n", get_timer(0), endtime);
23 if (send & BCM2835_CHAN_MASK) {
24 printf("mbox: Illegal mbox data 0x%08x\n", send);
28 /* Drain any stale responses */
31 val = readl(®s->status);
32 if (val & BCM2835_MBOX_STATUS_RD_EMPTY)
34 if (get_timer(0) >= endtime) {
35 printf("mbox: Timeout draining stale responses\n");
38 val = readl(®s->read);
41 /* Wait for space to send */
44 val = readl(®s->status);
45 if (!(val & BCM2835_MBOX_STATUS_WR_FULL))
47 if (get_timer(0) >= endtime) {
48 printf("mbox: Timeout waiting for send space\n");
53 /* Send the request */
55 val = BCM2835_MBOX_PACK(chan, send);
56 debug("mbox: TX raw: 0x%08x\n", val);
57 writel(val, ®s->write);
59 /* Wait for the response */
62 val = readl(®s->status);
63 if (!(val & BCM2835_MBOX_STATUS_RD_EMPTY))
65 if (get_timer(0) >= endtime) {
66 printf("mbox: Timeout waiting for response\n");
71 /* Read the response */
73 val = readl(®s->read);
74 debug("mbox: RX raw: 0x%08x\n", val);
76 /* Validate the response */
78 if (BCM2835_MBOX_UNPACK_CHAN(val) != chan) {
79 printf("mbox: Response channel mismatch\n");
83 *recv = BCM2835_MBOX_UNPACK_DATA(val);
89 void dump_buf(struct bcm2835_mbox_hdr *buffer)
96 words = buffer->buf_size / 4;
97 for (i = 0; i < words; i++)
98 printf(" 0x%04x: 0x%08x\n", i * 4, p[i]);
102 int bcm2835_mbox_call_prop(u32 chan, struct bcm2835_mbox_hdr *buffer)
106 struct bcm2835_mbox_tag_hdr *tag;
110 printf("mbox: TX buffer\n");
114 ret = bcm2835_mbox_call_raw(chan, phys_to_bus((u32)buffer), &rbuffer);
117 if (rbuffer != phys_to_bus((u32)buffer)) {
118 printf("mbox: Response buffer mismatch\n");
123 printf("mbox: RX buffer\n");
127 /* Validate overall response status */
129 if (buffer->code != BCM2835_MBOX_RESP_CODE_SUCCESS) {
130 printf("mbox: Header response code invalid\n");
134 /* Validate each tag's response status */
136 tag = (void *)(buffer + 1);
139 if (!(tag->val_len & BCM2835_MBOX_TAG_VAL_LEN_RESPONSE)) {
140 printf("mbox: Tag %d missing val_len response bit\n",
145 * Clear the reponse bit so clients can just look right at the
146 * length field without extra processing
148 tag->val_len &= ~BCM2835_MBOX_TAG_VAL_LEN_RESPONSE;
149 tag = (void *)(((u8 *)tag) + sizeof(*tag) + tag->val_buf_size);