1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2012 Stephen Warren
11 #include <asm/arch/base.h>
12 #include <asm/arch/mbox.h>
15 #define TIMEOUT 1000 /* ms */
17 int bcm2835_mbox_call_raw(u32 chan, u32 send, u32 *recv)
19 struct bcm2835_mbox_regs *regs =
20 (struct bcm2835_mbox_regs *)BCM2835_MBOX_PHYSADDR;
21 ulong endtime = get_timer(0) + TIMEOUT;
24 debug("time: %lu timeout: %lu\n", get_timer(0), endtime);
26 if (send & BCM2835_CHAN_MASK) {
27 printf("mbox: Illegal mbox data 0x%08x\n", send);
31 /* Drain any stale responses */
34 val = readl(®s->mail0_status);
35 if (val & BCM2835_MBOX_STATUS_RD_EMPTY)
37 if (get_timer(0) >= endtime) {
38 printf("mbox: Timeout draining stale responses\n");
41 val = readl(®s->read);
44 /* Wait for space to send */
47 val = readl(®s->mail1_status);
48 if (!(val & BCM2835_MBOX_STATUS_WR_FULL))
50 if (get_timer(0) >= endtime) {
51 printf("mbox: Timeout waiting for send space\n");
56 /* Send the request */
58 val = BCM2835_MBOX_PACK(chan, send);
59 debug("mbox: TX raw: 0x%08x\n", val);
60 writel(val, ®s->write);
62 /* Wait for the response */
65 val = readl(®s->mail0_status);
66 if (!(val & BCM2835_MBOX_STATUS_RD_EMPTY))
68 if (get_timer(0) >= endtime) {
69 printf("mbox: Timeout waiting for response\n");
74 /* Read the response */
76 val = readl(®s->read);
77 debug("mbox: RX raw: 0x%08x\n", val);
79 /* Validate the response */
81 if (BCM2835_MBOX_UNPACK_CHAN(val) != chan) {
82 printf("mbox: Response channel mismatch\n");
86 *recv = BCM2835_MBOX_UNPACK_DATA(val);
92 void dump_buf(struct bcm2835_mbox_hdr *buffer)
99 words = buffer->buf_size / 4;
100 for (i = 0; i < words; i++)
101 printf(" 0x%04x: 0x%08x\n", i * 4, p[i]);
105 int bcm2835_mbox_call_prop(u32 chan, struct bcm2835_mbox_hdr *buffer)
109 struct bcm2835_mbox_tag_hdr *tag;
113 printf("mbox: TX buffer\n");
117 flush_dcache_range((unsigned long)buffer,
118 (unsigned long)((void *)buffer +
119 roundup(buffer->buf_size, ARCH_DMA_MINALIGN)));
121 ret = bcm2835_mbox_call_raw(chan,
122 phys_to_bus((unsigned long)buffer),
127 invalidate_dcache_range((unsigned long)buffer,
128 (unsigned long)((void *)buffer +
129 roundup(buffer->buf_size, ARCH_DMA_MINALIGN)));
131 if (rbuffer != phys_to_bus((unsigned long)buffer)) {
132 printf("mbox: Response buffer mismatch\n");
137 printf("mbox: RX buffer\n");
141 /* Validate overall response status */
143 if (buffer->code != BCM2835_MBOX_RESP_CODE_SUCCESS) {
144 printf("mbox: Header response code invalid\n");
148 /* Validate each tag's response status */
150 tag = (void *)(buffer + 1);
153 if (!(tag->val_len & BCM2835_MBOX_TAG_VAL_LEN_RESPONSE)) {
154 printf("mbox: Tag %d missing val_len response bit\n",
159 * Clear the reponse bit so clients can just look right at the
160 * length field without extra processing
162 tag->val_len &= ~BCM2835_MBOX_TAG_VAL_LEN_RESPONSE;
163 tag = (void *)(((u8 *)tag) + sizeof(*tag) + tag->val_buf_size);