1 /*
2  * Copyright (c) 2018-2019, ARM Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include <platform_def.h>
8 
9 #include <arch_helpers.h>
10 #include <common/debug.h>
11 #include <lib/mmio.h>
12 
13 #include <rpi_hw.h>
14 
15 #include <drivers/rpi3/mailbox/rpi3_mbox.h>
16 
17 #define RPI3_MAILBOX_MAX_RETRIES	U(1000000)
18 
19 /*******************************************************************************
20  * Routine to send requests to the VideoCore using the mailboxes.
21  ******************************************************************************/
rpi3_vc_mailbox_request_send(rpi3_mbox_request_t * req,int req_size)22 void rpi3_vc_mailbox_request_send(rpi3_mbox_request_t *req, int req_size)
23 {
24 	uint32_t st, data;
25 	uintptr_t resp_addr, addr;
26 	unsigned int retries;
27 
28 	/* This is the location of the request buffer */
29 	addr = (uintptr_t)req;
30 
31 	/* Make sure that the changes are seen by the VideoCore */
32 	flush_dcache_range(addr, req_size);
33 
34 	/* Wait until the outbound mailbox is empty */
35 	retries = 0U;
36 
37 	do {
38 		st = mmio_read_32(RPI3_MBOX_BASE + RPI3_MBOX1_STATUS_OFFSET);
39 
40 		retries++;
41 		if (retries == RPI3_MAILBOX_MAX_RETRIES) {
42 			ERROR("rpi3: mbox: Send request timeout\n");
43 			return;
44 		}
45 
46 	} while ((st & RPI3_MBOX_STATUS_EMPTY_MASK) == 0U);
47 
48 	/* Send base address of this message to start request */
49 	mmio_write_32(RPI3_MBOX_BASE + RPI3_MBOX1_WRITE_OFFSET,
50 		      RPI3_CHANNEL_ARM_TO_VC | (uint32_t) addr);
51 
52 	/* Wait until the inbound mailbox isn't empty */
53 	retries = 0U;
54 
55 	do {
56 		st = mmio_read_32(RPI3_MBOX_BASE + RPI3_MBOX0_STATUS_OFFSET);
57 
58 		retries++;
59 		if (retries == RPI3_MAILBOX_MAX_RETRIES) {
60 			ERROR("rpi3: mbox: Receive response timeout\n");
61 			return;
62 		}
63 
64 	} while ((st & RPI3_MBOX_STATUS_EMPTY_MASK) != 0U);
65 
66 	/* Get location and channel */
67 	data = mmio_read_32(RPI3_MBOX_BASE + RPI3_MBOX0_READ_OFFSET);
68 
69 	if ((data & RPI3_CHANNEL_MASK) != RPI3_CHANNEL_ARM_TO_VC) {
70 		ERROR("rpi3: mbox: Wrong channel: 0x%08x\n", data);
71 		panic();
72 	}
73 
74 	resp_addr = (uintptr_t)(data & ~RPI3_CHANNEL_MASK);
75 	if (addr != resp_addr) {
76 		ERROR("rpi3: mbox: Unexpected address: 0x%08x\n", data);
77 		panic();
78 	}
79 
80 	/* Make sure that the data seen by the CPU is up to date */
81 	inv_dcache_range(addr, req_size);
82 }
83