2 * (C) Copyright 2012 Stephen Warren
4 * See file CREDITS for list of people who contributed to this
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of
10 * the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
20 #include <asm/arch/mbox.h>
22 #define TIMEOUT (100 * 1000) /* 100mS in uS */
24 int bcm2835_mbox_call_raw(u32 chan, u32 send, u32 *recv)
26 struct bcm2835_mbox_regs *regs =
27 (struct bcm2835_mbox_regs *)BCM2835_MBOX_PHYSADDR;
28 ulong endtime = get_timer(0) + TIMEOUT;
31 debug("time: %lu timeout: %lu\n", get_timer(0), endtime);
33 if (send & BCM2835_CHAN_MASK) {
34 printf("mbox: Illegal mbox data 0x%08x\n", send);
38 /* Drain any stale responses */
41 val = readl(®s->status);
42 if (val & BCM2835_MBOX_STATUS_RD_EMPTY)
44 if (get_timer(0) >= endtime) {
45 printf("mbox: Timeout draining stale responses\n");
48 val = readl(®s->read);
51 /* Wait for space to send */
54 val = readl(®s->status);
55 if (!(val & BCM2835_MBOX_STATUS_WR_FULL))
57 if (get_timer(0) >= endtime) {
58 printf("mbox: Timeout waiting for send space\n");
63 /* Send the request */
65 val = BCM2835_MBOX_PACK(chan, send);
66 debug("mbox: TX raw: 0x%08x\n", val);
67 writel(val, ®s->write);
69 /* Wait for the response */
72 val = readl(®s->status);
73 if (!(val & BCM2835_MBOX_STATUS_RD_EMPTY))
75 if (get_timer(0) >= endtime) {
76 printf("mbox: Timeout waiting for response\n");
81 /* Read the response */
83 val = readl(®s->read);
84 debug("mbox: RX raw: 0x%08x\n", val);
86 /* Validate the response */
88 if (BCM2835_MBOX_UNPACK_CHAN(val) != chan) {
89 printf("mbox: Response channel mismatch\n");
93 *recv = BCM2835_MBOX_UNPACK_DATA(val);
99 void dump_buf(struct bcm2835_mbox_hdr *buffer)
106 words = buffer->buf_size / 4;
107 for (i = 0; i < words; i++)
108 printf(" 0x%04x: 0x%08x\n", i * 4, p[i]);
112 int bcm2835_mbox_call_prop(u32 chan, struct bcm2835_mbox_hdr *buffer)
116 struct bcm2835_mbox_tag_hdr *tag;
120 printf("mbox: TX buffer\n");
124 ret = bcm2835_mbox_call_raw(chan, (u32)buffer, &rbuffer);
127 if (rbuffer != (u32)buffer) {
128 printf("mbox: Response buffer mismatch\n");
133 printf("mbox: RX buffer\n");
137 /* Validate overall response status */
139 if (buffer->code != BCM2835_MBOX_RESP_CODE_SUCCESS) {
140 printf("mbox: Header response code invalid\n");
144 /* Validate each tag's response status */
146 tag = (void *)(buffer + 1);
149 if (!(tag->val_len & BCM2835_MBOX_TAG_VAL_LEN_RESPONSE)) {
150 printf("mbox: Tag %d missing val_len response bit\n",
155 * Clear the reponse bit so clients can just look right at the
156 * length field without extra processing
158 tag->val_len &= ~BCM2835_MBOX_TAG_VAL_LEN_RESPONSE;
159 tag = (void *)(((u8 *)tag) + sizeof(*tag) + tag->val_buf_size);