Antonio Nino Diaz | ecf3471 | 2018-07-12 13:38:53 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018, ARM Limited and Contributors. All rights reserved. |
| 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
| 7 | #include <assert.h> |
| 8 | #include <arch_helpers.h> |
| 9 | #include <debug.h> |
| 10 | #include <mmio.h> |
| 11 | #include <platform_def.h> |
| 12 | |
| 13 | #include "rpi3_hw.h" |
| 14 | |
| 15 | /* This struct must be aligned to 16 bytes */ |
| 16 | typedef struct __packed __aligned(16) rpi3_mbox_request { |
| 17 | uint32_t size; /* Buffer size in bytes */ |
| 18 | uint32_t code; /* Request/response code */ |
| 19 | uint32_t tags[0]; |
| 20 | } rpi3_mbox_request_t; |
| 21 | |
| 22 | #define RPI3_MBOX_BUFFER_SIZE U(256) |
| 23 | static uint8_t __aligned(16) rpi3_mbox_buffer[RPI3_MBOX_BUFFER_SIZE]; |
| 24 | |
| 25 | /* Constants to perform a request/check the status of a request. */ |
| 26 | #define RPI3_MBOX_PROCESS_REQUEST U(0x00000000) |
| 27 | #define RPI3_MBOX_REQUEST_SUCCESSFUL U(0x80000000) |
| 28 | #define RPI3_MBOX_REQUEST_ERROR U(0x80000001) |
| 29 | |
| 30 | /* Command constants */ |
| 31 | #define RPI3_TAG_HARDWARE_GET_BOARD_REVISION U(0x00010002) |
| 32 | #define RPI3_TAG_END U(0x00000000) |
| 33 | |
| 34 | #define RPI3_TAG_REQUEST U(0x00000000) |
| 35 | #define RPI3_TAG_IS_RESPONSE U(0x80000000) /* Set if response */ |
| 36 | #define RPI3_TAG_RESPONSE_LENGTH_MASK U(0x7FFFFFFF) |
| 37 | |
| 38 | #define RPI3_CHANNEL_ARM_TO_VC U(0x8) |
| 39 | #define RPI3_CHANNEL_MASK U(0xF) |
| 40 | |
| 41 | #define RPI3_MAILBOX_MAX_RETRIES U(1000000) |
| 42 | |
| 43 | /******************************************************************************* |
| 44 | * Helpers to send requests to the VideoCore using the mailboxes. |
| 45 | ******************************************************************************/ |
| 46 | static void rpi3_vc_mailbox_request_send(void) |
| 47 | { |
| 48 | uint32_t st, data; |
| 49 | uintptr_t resp_addr, addr; |
| 50 | unsigned int retries; |
| 51 | |
| 52 | /* This is the location of the request buffer */ |
| 53 | addr = (uintptr_t) &rpi3_mbox_buffer; |
| 54 | |
| 55 | /* Make sure that the changes are seen by the VideoCore */ |
| 56 | flush_dcache_range(addr, RPI3_MBOX_BUFFER_SIZE); |
| 57 | |
| 58 | /* Wait until the outbound mailbox is empty */ |
| 59 | retries = 0U; |
| 60 | |
| 61 | do { |
| 62 | st = mmio_read_32(RPI3_MBOX_BASE + RPI3_MBOX1_STATUS_OFFSET); |
| 63 | |
| 64 | retries++; |
| 65 | if (retries == RPI3_MAILBOX_MAX_RETRIES) { |
| 66 | ERROR("rpi3: mbox: Send request timeout\n"); |
| 67 | return; |
| 68 | } |
| 69 | |
| 70 | } while ((st & RPI3_MBOX_STATUS_EMPTY_MASK) == 0U); |
| 71 | |
| 72 | /* Send base address of this message to start request */ |
| 73 | mmio_write_32(RPI3_MBOX_BASE + RPI3_MBOX1_WRITE_OFFSET, |
| 74 | RPI3_CHANNEL_ARM_TO_VC | (uint32_t) addr); |
| 75 | |
| 76 | /* Wait until the inbound mailbox isn't empty */ |
| 77 | retries = 0U; |
| 78 | |
| 79 | do { |
| 80 | st = mmio_read_32(RPI3_MBOX_BASE + RPI3_MBOX0_STATUS_OFFSET); |
| 81 | |
| 82 | retries++; |
| 83 | if (retries == RPI3_MAILBOX_MAX_RETRIES) { |
| 84 | ERROR("rpi3: mbox: Receive response timeout\n"); |
| 85 | return; |
| 86 | } |
| 87 | |
| 88 | } while ((st & RPI3_MBOX_STATUS_EMPTY_MASK) != 0U); |
| 89 | |
| 90 | /* Get location and channel */ |
| 91 | data = mmio_read_32(RPI3_MBOX_BASE + RPI3_MBOX0_READ_OFFSET); |
| 92 | |
| 93 | if ((data & RPI3_CHANNEL_MASK) != RPI3_CHANNEL_ARM_TO_VC) { |
| 94 | ERROR("rpi3: mbox: Wrong channel: 0x%08x\n", data); |
| 95 | panic(); |
| 96 | } |
| 97 | |
| 98 | resp_addr = (uintptr_t)(data & ~RPI3_CHANNEL_MASK); |
| 99 | if (addr != resp_addr) { |
| 100 | ERROR("rpi3: mbox: Unexpected address: 0x%08x\n", data); |
| 101 | panic(); |
| 102 | } |
| 103 | |
| 104 | /* Make sure that the data seen by the CPU is up to date */ |
| 105 | inv_dcache_range(addr, RPI3_MBOX_BUFFER_SIZE); |
| 106 | } |
| 107 | |
| 108 | /******************************************************************************* |
| 109 | * Request board revision. Returns the revision and 0 on success, -1 on error. |
| 110 | ******************************************************************************/ |
| 111 | int rpi3_vc_hardware_get_board_revision(uint32_t *revision) |
| 112 | { |
| 113 | uint32_t tag_request_size = sizeof(uint32_t); |
| 114 | rpi3_mbox_request_t *req = (rpi3_mbox_request_t *) rpi3_mbox_buffer; |
| 115 | |
| 116 | assert(revision != NULL); |
| 117 | |
| 118 | VERBOSE("rpi3: mbox: Sending request at %p\n", (void *)req); |
| 119 | |
| 120 | req->size = sizeof(rpi3_mbox_buffer); |
| 121 | req->code = RPI3_MBOX_PROCESS_REQUEST; |
| 122 | |
| 123 | req->tags[0] = RPI3_TAG_HARDWARE_GET_BOARD_REVISION; |
| 124 | req->tags[1] = tag_request_size; /* Space available for the response */ |
| 125 | req->tags[2] = RPI3_TAG_REQUEST; |
| 126 | req->tags[3] = 0; /* Placeholder for the response */ |
| 127 | |
| 128 | req->tags[4] = RPI3_TAG_END; |
| 129 | |
| 130 | rpi3_vc_mailbox_request_send(); |
| 131 | |
| 132 | if (req->code != RPI3_MBOX_REQUEST_SUCCESSFUL) { |
| 133 | ERROR("rpi3: mbox: Code = 0x%08x\n", req->code); |
| 134 | return -1; |
| 135 | } |
| 136 | |
| 137 | if (req->tags[2] != (RPI3_TAG_IS_RESPONSE | tag_request_size)) { |
| 138 | ERROR("rpi3: mbox: get board revision failed (0x%08x)\n", |
| 139 | req->tags[2]); |
| 140 | return -1; |
| 141 | } |
| 142 | |
| 143 | *revision = req->tags[3]; |
| 144 | |
| 145 | return 0; |
| 146 | } |