Andrew F. Davis | a513b2a | 2018-05-04 19:06:09 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Texas Instruments System Control Interface Driver |
| 3 | * Based on Linux and U-Boot implementation |
| 4 | * |
| 5 | * Copyright (C) 2018 Texas Instruments Incorporated - http://www.ti.com/ |
| 6 | * |
| 7 | * SPDX-License-Identifier: BSD-3-Clause |
| 8 | */ |
| 9 | |
| 10 | #include <debug.h> |
| 11 | #include <errno.h> |
| 12 | #include <platform_def.h> |
| 13 | #include <stdbool.h> |
| 14 | #include <stddef.h> |
| 15 | #include <string.h> |
| 16 | |
| 17 | #include <sec_proxy.h> |
| 18 | |
| 19 | #include "ti_sci_protocol.h" |
| 20 | #include "ti_sci.h" |
| 21 | |
| 22 | /** |
| 23 | * struct ti_sci_desc - Description of SoC integration |
| 24 | * @host_id: Host identifier representing the compute entity |
| 25 | * @max_msg_size: Maximum size of data per message that can be handled |
| 26 | */ |
| 27 | struct ti_sci_desc { |
| 28 | uint8_t host_id; |
| 29 | int max_msg_size; |
| 30 | }; |
| 31 | |
| 32 | /** |
| 33 | * struct ti_sci_info - Structure representing a TI SCI instance |
| 34 | * @desc: SoC description for this instance |
| 35 | * @seq: Seq id used for verification for tx and rx message |
| 36 | */ |
| 37 | struct ti_sci_info { |
| 38 | const struct ti_sci_desc desc; |
| 39 | uint8_t seq; |
| 40 | }; |
| 41 | |
| 42 | static struct ti_sci_info info = { |
| 43 | .desc = { |
| 44 | .host_id = TI_SCI_HOST_ID, |
| 45 | .max_msg_size = TI_SCI_MAX_MESSAGE_SIZE, |
| 46 | }, |
| 47 | .seq = 0x0a, |
| 48 | }; |
| 49 | |
| 50 | /** |
| 51 | * struct ti_sci_xfer - Structure representing a message flow |
| 52 | * @tx_message: Transmit message |
| 53 | * @rx_message: Receive message |
| 54 | */ |
| 55 | struct ti_sci_xfer { |
| 56 | struct k3_sec_proxy_msg tx_message; |
| 57 | struct k3_sec_proxy_msg rx_message; |
| 58 | }; |
| 59 | |
| 60 | /** |
| 61 | * ti_sci_setup_one_xfer() - Setup one message type |
| 62 | * |
| 63 | * @msg_type: Message type |
| 64 | * @msg_flags: Flag to set for the message |
| 65 | * @tx_buf: Buffer to be sent to mailbox channel |
| 66 | * @tx_message_size: transmit message size |
| 67 | * @rx_buf: Buffer to be received from mailbox channel |
| 68 | * @rx_message_size: receive message size |
| 69 | * |
| 70 | * Helper function which is used by various command functions that are |
| 71 | * exposed to clients of this driver for allocating a message traffic event. |
| 72 | * |
| 73 | * Return: 0 if all goes well, else appropriate error message |
| 74 | */ |
| 75 | static int ti_sci_setup_one_xfer(uint16_t msg_type, uint32_t msg_flags, |
| 76 | void *tx_buf, |
| 77 | size_t tx_message_size, |
| 78 | void *rx_buf, |
| 79 | size_t rx_message_size, |
| 80 | struct ti_sci_xfer *xfer) |
| 81 | { |
| 82 | struct ti_sci_msg_hdr *hdr; |
| 83 | |
| 84 | /* Ensure we have sane transfer sizes */ |
| 85 | if (rx_message_size > info.desc.max_msg_size || |
| 86 | tx_message_size > info.desc.max_msg_size || |
| 87 | rx_message_size < sizeof(*hdr) || |
| 88 | tx_message_size < sizeof(*hdr)) |
| 89 | return -ERANGE; |
| 90 | |
| 91 | info.seq++; |
| 92 | |
| 93 | hdr = (struct ti_sci_msg_hdr *)tx_buf; |
| 94 | hdr->seq = info.seq; |
| 95 | hdr->type = msg_type; |
| 96 | hdr->host = info.desc.host_id; |
| 97 | hdr->flags = msg_flags; |
| 98 | |
| 99 | xfer->tx_message.buf = tx_buf; |
| 100 | xfer->tx_message.len = tx_message_size; |
| 101 | |
| 102 | xfer->rx_message.buf = rx_buf; |
| 103 | xfer->rx_message.len = rx_message_size; |
| 104 | |
| 105 | return 0; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * ti_sci_get_response() - Receive response from mailbox channel |
| 110 | * |
| 111 | * @xfer: Transfer to initiate and wait for response |
| 112 | * @chan: Channel to receive the response |
| 113 | * |
| 114 | * Return: 0 if all goes well, else appropriate error message |
| 115 | */ |
| 116 | static inline int ti_sci_get_response(struct ti_sci_xfer *xfer, |
| 117 | enum k3_sec_proxy_chan_id chan) |
| 118 | { |
| 119 | struct k3_sec_proxy_msg *msg = &xfer->rx_message; |
| 120 | struct ti_sci_msg_hdr *hdr; |
| 121 | int ret; |
| 122 | |
| 123 | /* Receive the response */ |
| 124 | ret = k3_sec_proxy_recv(chan, msg); |
| 125 | if (ret) { |
| 126 | ERROR("Message receive failed (%d)\n", ret); |
| 127 | return ret; |
| 128 | } |
| 129 | |
| 130 | /* msg is updated by Secure Proxy driver */ |
| 131 | hdr = (struct ti_sci_msg_hdr *)msg->buf; |
| 132 | |
| 133 | /* Sanity check for message response */ |
| 134 | if (hdr->seq != info.seq) { |
| 135 | ERROR("Message for %d is not expected\n", hdr->seq); |
| 136 | return -EINVAL; |
| 137 | } |
| 138 | |
| 139 | if (msg->len > info.desc.max_msg_size) { |
| 140 | ERROR("Unable to handle %lu xfer (max %d)\n", |
| 141 | msg->len, info.desc.max_msg_size); |
| 142 | return -EINVAL; |
| 143 | } |
| 144 | |
| 145 | return 0; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * ti_sci_do_xfer() - Do one transfer |
| 150 | * |
| 151 | * @xfer: Transfer to initiate and wait for response |
| 152 | * |
| 153 | * Return: 0 if all goes well, else appropriate error message |
| 154 | */ |
| 155 | static inline int ti_sci_do_xfer(struct ti_sci_xfer *xfer) |
| 156 | { |
| 157 | struct k3_sec_proxy_msg *msg = &xfer->tx_message; |
| 158 | int ret; |
| 159 | |
| 160 | /* Send the message */ |
| 161 | ret = k3_sec_proxy_send(SP_HIGH_PRIORITY, msg); |
| 162 | if (ret) { |
| 163 | ERROR("Message sending failed (%d)\n", ret); |
| 164 | return ret; |
| 165 | } |
| 166 | |
| 167 | ret = ti_sci_get_response(xfer, SP_RESPONSE); |
| 168 | if (ret) { |
| 169 | ERROR("Failed to get response (%d)\n", ret); |
| 170 | return ret; |
| 171 | } |
| 172 | |
| 173 | return 0; |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * ti_sci_get_revision() - Get the revision of the SCI entity |
| 178 | * |
| 179 | * Updates the SCI information in the internal data structure. |
| 180 | * |
| 181 | * Return: 0 if all goes well, else appropriate error message |
| 182 | */ |
| 183 | int ti_sci_get_revision(struct ti_sci_msg_resp_version *rev_info) |
| 184 | { |
| 185 | struct ti_sci_msg_hdr hdr; |
| 186 | struct ti_sci_xfer xfer; |
| 187 | int ret; |
| 188 | |
| 189 | ret = ti_sci_setup_one_xfer(TI_SCI_MSG_VERSION, 0x0, |
| 190 | &hdr, sizeof(hdr), |
| 191 | rev_info, sizeof(*rev_info), |
| 192 | &xfer); |
| 193 | if (ret) { |
| 194 | ERROR("Message alloc failed (%d)\n", ret); |
| 195 | return ret; |
| 196 | } |
| 197 | |
| 198 | ret = ti_sci_do_xfer(&xfer); |
| 199 | if (ret) { |
| 200 | ERROR("Transfer send failed (%d)\n", ret); |
| 201 | return ret; |
| 202 | } |
| 203 | |
| 204 | return 0; |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * ti_sci_init() - Basic initialization |
| 209 | * |
| 210 | * Return: 0 if all goes well, else appropriate error message |
| 211 | */ |
| 212 | int ti_sci_init(void) |
| 213 | { |
| 214 | struct ti_sci_msg_resp_version rev_info; |
| 215 | int ret; |
| 216 | |
| 217 | ret = ti_sci_get_revision(&rev_info); |
| 218 | if (ret) { |
| 219 | ERROR("Unable to communicate with control firmware (%d)\n", ret); |
| 220 | return ret; |
| 221 | } |
| 222 | |
| 223 | INFO("SYSFW ABI: %d.%d (firmware rev 0x%04x '%s')\n", |
| 224 | rev_info.abi_major, rev_info.abi_minor, |
| 225 | rev_info.firmware_revision, |
| 226 | rev_info.firmware_description); |
| 227 | |
| 228 | return 0; |
| 229 | } |