summaryrefslogtreecommitdiffstats
path: root/arch/arm/mach-bcm283x/mbox.c
blob: 4959a1a652ec0cc5bd55fb14eb854cb3c1beb567 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
 * based on U-Boot code
 *
 * (C) Copyright 2012 Stephen Warren
 *
 * SPDX-License-Identifier:	GPL-2.0+
 */

#define pr_fmt(fmt) "rpi-mbox: " fmt

#include <clock.h>
#include <common.h>
#include <dma.h>
#include <init.h>
#include <io.h>
#include <of_address.h>
#include <pbl.h>

#include <mach/mbox.h>
#include <mach/core.h>

#define TIMEOUT (MSECOND * 1000)

static void __iomem *mbox_base;

#ifdef __PBL__
#define is_timeout_non_interruptible(start, timeout) ((void)start, 0)
#define get_time_ns() 0
#endif

static int bcm2835_mbox_call_raw(u32 chan, struct bcm2835_mbox_hdr *buffer,
					u32 *recv)
{
	uint64_t starttime = get_time_ns();
	u32 send = virt_to_phys(buffer);
	u32 val;

	if (send & BCM2835_CHAN_MASK) {
		pr_err("mbox: Illegal mbox data 0x%08x\n", send);
		return -EINVAL;
	}

	/* Drain any stale responses */
	for (;;) {
		val = readl(mbox_base + MAIL0_STA);
		if (val & BCM2835_MBOX_STATUS_RD_EMPTY)
			break;
		if (is_timeout_non_interruptible(starttime, TIMEOUT)) {
			pr_err("mbox: Timeout draining stale responses\n");
			return -ETIMEDOUT;
		}
		val = readl(mbox_base + MAIL0_RD);
	}

	/* Wait for space to send */
	for (;;) {
		val = readl(mbox_base + MAIL0_STA);
		if (!(val & BCM2835_MBOX_STATUS_WR_FULL))
			break;
		if (is_timeout_non_interruptible(starttime, TIMEOUT)) {
			pr_err("mbox: Timeout waiting for send space\n");
			return -ETIMEDOUT;
		}
	}

	/* Send the request */
	val = BCM2835_MBOX_PACK(chan, send);
	pr_debug("mbox: TX raw: 0x%08x\n", val);
	dma_sync_single_for_device((unsigned long)send, buffer->buf_size,
				   DMA_BIDIRECTIONAL);
	writel(val, mbox_base + MAIL1_WRT);

	/* Wait for the response */
	for (;;) {
		val = readl(mbox_base + MAIL0_STA);
		if (!(val & BCM2835_MBOX_STATUS_RD_EMPTY))
			break;
		if (is_timeout_non_interruptible(starttime, TIMEOUT)) {
			pr_err("mbox: Timeout waiting for response\n");
			return -ETIMEDOUT;
		}
	}

	/* Read the response */
	val = readl(mbox_base + MAIL0_RD);
	pr_debug("mbox: RX raw: 0x%08x\n", val);
	dma_sync_single_for_cpu((unsigned long)send, buffer->buf_size,
				DMA_BIDIRECTIONAL);

	/* Validate the response */
	if (BCM2835_MBOX_UNPACK_CHAN(val) != chan) {
		pr_err("mbox: Response channel mismatch\n");
		return -EIO;
	}

	*recv = BCM2835_MBOX_UNPACK_DATA(val);

	return 0;
}

#ifdef DEBUG
static void dump_buf(struct bcm2835_mbox_hdr *buffer)
{
	u32 *p;
	u32 words;
	int i;

	p = (u32 *)buffer;
	words = buffer->buf_size / 4;
	for (i = 0; i < words; i++)
		pr_debug("    0x%04x: 0x%08x\n", i * 4, p[i]);
}
#else
static void dump_buf(struct bcm2835_mbox_hdr *buffer)
{
}
#endif

static void __iomem *bcm2835_mbox_probe(void)
{
	struct device_node *mbox_node;

	if (IN_PBL)
		return bcm2835_get_mmio_base_by_cpuid() + 0xb880;

	mbox_node = of_find_compatible_node(NULL, NULL, "brcm,bcm2835-mbox");
	if (!mbox_node) {
		pr_err("Missing mbox node\n");
		return NULL;
	}

	return of_iomap(mbox_node, 0);
}

int bcm2835_mbox_call_prop(u32 chan, struct bcm2835_mbox_hdr *buffer)
{
	int ret;
	u32 rbuffer;
	struct bcm2835_mbox_tag_hdr *tag;
	int tag_index;

	if (!mbox_base) {
		mbox_base = bcm2835_mbox_probe();
		if (!mbox_base)
			return -ENOENT;
	}

	pr_debug("mbox: TX buffer\n");
	dump_buf(buffer);

	ret = bcm2835_mbox_call_raw(chan, buffer, &rbuffer);
	if (ret)
		return ret;
	if (rbuffer != (uintptr_t)buffer) {
		pr_err("mbox: Response buffer mismatch\n");
		return -EIO;
	}

	pr_debug("mbox: RX buffer\n");
	dump_buf(buffer);

	/* Validate overall response status */
	if (buffer->code != BCM2835_MBOX_RESP_CODE_SUCCESS) {
		pr_err("mbox: Header response code invalid\n");
		return -EIO;
	}

	/* Validate each tag's response status */
	tag = (void *)(buffer + 1);
	tag_index = 0;
	while (tag->tag) {
		/*
		 * Clear the reponse bit so clients can just look right at the
		 * length field without extra processing
		 */
		tag->val_len &= ~BCM2835_MBOX_TAG_VAL_LEN_RESPONSE;
		tag = (void *)(((u8 *)tag) + sizeof(*tag) + tag->val_buf_size);
		tag_index++;
	}

	return 0;
}