blob: be77dc82e2e36a8aedd507d8b954adb54e16aab7 [file] [log] [blame]
Andrew F. Davisa513b2a2018-05-04 19:06:09 +00001/*
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 */
27struct 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 */
37struct ti_sci_info {
38 const struct ti_sci_desc desc;
39 uint8_t seq;
40};
41
42static 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 */
55struct 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 */
75static 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 */
116static 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 */
155static 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 */
183int 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/**
Andrew F. Davis4f2a0552018-05-04 19:06:10 +0000208 * ti_sci_is_response_ack() - Generic ACK/NACK message check
209 *
210 * @r: pointer to response buffer
211 *
212 * Return: true if the response was an ACK, else returns false
213 */
214static inline bool ti_sci_is_response_ack(void *r)
215{
216 struct ti_sci_msg_hdr *hdr = r;
217
218 return hdr->flags & TI_SCI_FLAG_RESP_GENERIC_ACK ? true : false;
219}
220
221/**
222 * ti_sci_device_set_state() - Set device state
223 *
224 * @id: Device identifier
225 * @flags: flags to setup for the device
226 * @state: State to move the device to
227 *
228 * Return: 0 if all goes well, else appropriate error message
229 */
230int ti_sci_device_set_state(uint32_t id, uint32_t flags, uint8_t state)
231{
232 struct ti_sci_msg_req_set_device_state req;
233 struct ti_sci_msg_hdr resp;
234
235 struct ti_sci_xfer xfer;
236 int ret;
237
238 ret = ti_sci_setup_one_xfer(TI_SCI_MSG_SET_DEVICE_STATE,
239 flags | TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
240 &req, sizeof(req),
241 &resp, sizeof(resp),
242 &xfer);
243 if (ret) {
244 ERROR("Message alloc failed (%d)\n", ret);
245 return ret;
246 }
247
248 req.id = id;
249 req.state = state;
250
251 ret = ti_sci_do_xfer(&xfer);
252 if (ret) {
253 ERROR("Transfer send failed (%d)\n", ret);
254 return ret;
255 }
256
257 if (!ti_sci_is_response_ack(&resp))
258 return -ENODEV;
259
260 return 0;
261}
262
263/**
264 * ti_sci_device_get_state() - Get device state
265 *
266 * @id: Device Identifier
267 * @clcnt: Pointer to Context Loss Count
268 * @resets: pointer to resets
269 * @p_state: pointer to p_state
270 * @c_state: pointer to c_state
271 *
272 * Return: 0 if all goes well, else appropriate error message
273 */
274int ti_sci_device_get_state(uint32_t id, uint32_t *clcnt, uint32_t *resets,
275 uint8_t *p_state, uint8_t *c_state)
276{
277 struct ti_sci_msg_req_get_device_state req;
278 struct ti_sci_msg_resp_get_device_state resp;
279
280 struct ti_sci_xfer xfer;
281 int ret;
282
283 if (!clcnt && !resets && !p_state && !c_state)
284 return -EINVAL;
285
286 ret = ti_sci_setup_one_xfer(TI_SCI_MSG_GET_DEVICE_STATE, 0,
287 &req, sizeof(req),
288 &resp, sizeof(resp),
289 &xfer);
290 if (ret) {
291 ERROR("Message alloc failed (%d)\n", ret);
292 return ret;
293 }
294
295 req.id = id;
296
297 ret = ti_sci_do_xfer(&xfer);
298 if (ret) {
299 ERROR("Transfer send failed (%d)\n", ret);
300 return ret;
301 }
302
303 if (!ti_sci_is_response_ack(&resp))
304 return -ENODEV;
305
306 if (clcnt)
307 *clcnt = resp.context_loss_count;
308 if (resets)
309 *resets = resp.resets;
310 if (p_state)
311 *p_state = resp.programmed_state;
312 if (c_state)
313 *c_state = resp.current_state;
314
315 return 0;
316}
317
318/**
319 * ti_sci_device_get() - Request for device managed by TISCI
320 *
321 * @id: Device Identifier
322 *
323 * Request for the device - NOTE: the client MUST maintain integrity of
324 * usage count by balancing get_device with put_device. No refcounting is
325 * managed by driver for that purpose.
326 *
327 * NOTE: The request is for exclusive access for the processor.
328 *
329 * Return: 0 if all goes well, else appropriate error message
330 */
331int ti_sci_device_get(uint32_t id)
332{
333 return ti_sci_device_set_state(id,
334 MSG_FLAG_DEVICE_EXCLUSIVE,
335 MSG_DEVICE_SW_STATE_ON);
336}
337
338/**
339 * ti_sci_device_idle() - Idle a device managed by TISCI
340 *
341 * @id: Device Identifier
342 *
343 * Request for the device - NOTE: the client MUST maintain integrity of
344 * usage count by balancing get_device with put_device. No refcounting is
345 * managed by driver for that purpose.
346 *
347 * Return: 0 if all goes well, else appropriate error message
348 */
349int ti_sci_device_idle(uint32_t id)
350{
351 return ti_sci_device_set_state(id,
352 MSG_FLAG_DEVICE_EXCLUSIVE,
353 MSG_DEVICE_SW_STATE_RETENTION);
354}
355
356/**
357 * ti_sci_device_put() - Release a device managed by TISCI
358 *
359 * @id: Device Identifier
360 *
361 * Request for the device - NOTE: the client MUST maintain integrity of
362 * usage count by balancing get_device with put_device. No refcounting is
363 * managed by driver for that purpose.
364 *
365 * Return: 0 if all goes well, else appropriate error message
366 */
367int ti_sci_device_put(uint32_t id)
368{
369 return ti_sci_device_set_state(id, 0, MSG_DEVICE_SW_STATE_AUTO_OFF);
370}
371
372/**
373 * ti_sci_device_is_valid() - Is the device valid
374 *
375 * @id: Device Identifier
376 *
377 * Return: 0 if all goes well and the device ID is valid, else return
378 * appropriate error
379 */
380int ti_sci_device_is_valid(uint32_t id)
381{
382 uint8_t unused;
383
384 /* check the device state which will also tell us if the ID is valid */
385 return ti_sci_device_get_state(id, NULL, NULL, NULL, &unused);
386}
387
388/**
389 * ti_sci_device_get_clcnt() - Get context loss counter
390 *
391 * @id: Device Identifier
392 * @count: Pointer to Context Loss counter to populate
393 *
394 * Return: 0 if all goes well, else appropriate error message
395 */
396int ti_sci_device_get_clcnt(uint32_t id, uint32_t *count)
397{
398 return ti_sci_device_get_state(id, count, NULL, NULL, NULL);
399}
400
401/**
402 * ti_sci_device_is_idle() - Check if the device is requested to be idle
403 *
404 * @id: Device Identifier
405 * @r_state: true if requested to be idle
406 *
407 * Return: 0 if all goes well, else appropriate error message
408 */
409int ti_sci_device_is_idle(uint32_t id, bool *r_state)
410{
411 int ret;
412 uint8_t state;
413
414 if (!r_state)
415 return -EINVAL;
416
417 ret = ti_sci_device_get_state(id, NULL, NULL, &state, NULL);
418 if (ret)
419 return ret;
420
421 *r_state = (state == MSG_DEVICE_SW_STATE_RETENTION);
422
423 return 0;
424}
425
426/**
427 * ti_sci_device_is_stop() - Check if the device is requested to be stopped
428 *
429 * @id: Device Identifier
430 * @r_state: true if requested to be stopped
431 * @curr_state: true if currently stopped
432 *
433 * Return: 0 if all goes well, else appropriate error message
434 */
435int ti_sci_device_is_stop(uint32_t id, bool *r_state, bool *curr_state)
436{
437 int ret;
438 uint8_t p_state, c_state;
439
440 if (!r_state && !curr_state)
441 return -EINVAL;
442
443 ret = ti_sci_device_get_state(id, NULL, NULL, &p_state, &c_state);
444 if (ret)
445 return ret;
446
447 if (r_state)
448 *r_state = (p_state == MSG_DEVICE_SW_STATE_AUTO_OFF);
449 if (curr_state)
450 *curr_state = (c_state == MSG_DEVICE_HW_STATE_OFF);
451
452 return 0;
453}
454
455/**
456 * ti_sci_device_is_on() - Check if the device is requested to be ON
457 *
458 * @id: Device Identifier
459 * @r_state: true if requested to be ON
460 * @curr_state: true if currently ON and active
461 *
462 * Return: 0 if all goes well, else appropriate error message
463 */
464int ti_sci_device_is_on(uint32_t id, bool *r_state, bool *curr_state)
465{
466 int ret;
467 uint8_t p_state, c_state;
468
469 if (!r_state && !curr_state)
470 return -EINVAL;
471
472 ret =
473 ti_sci_device_get_state(id, NULL, NULL, &p_state, &c_state);
474 if (ret)
475 return ret;
476
477 if (r_state)
478 *r_state = (p_state == MSG_DEVICE_SW_STATE_ON);
479 if (curr_state)
480 *curr_state = (c_state == MSG_DEVICE_HW_STATE_ON);
481
482 return 0;
483}
484
485/**
486 * ti_sci_device_is_trans() - Check if the device is currently transitioning
487 *
488 * @id: Device Identifier
489 * @curr_state: true if currently transitioning
490 *
491 * Return: 0 if all goes well, else appropriate error message
492 */
493int ti_sci_device_is_trans(uint32_t id, bool *curr_state)
494{
495 int ret;
496 uint8_t state;
497
498 if (!curr_state)
499 return -EINVAL;
500
501 ret = ti_sci_device_get_state(id, NULL, NULL, NULL, &state);
502 if (ret)
503 return ret;
504
505 *curr_state = (state == MSG_DEVICE_HW_STATE_TRANS);
506
507 return 0;
508}
509
510/**
511 * ti_sci_device_set_resets() - Set resets for device managed by TISCI
512 *
513 * @id: Device Identifier
514 * @reset_state: Device specific reset bit field
515 *
516 * Return: 0 if all goes well, else appropriate error message
517 */
518int ti_sci_device_set_resets(uint32_t id, uint32_t reset_state)
519{
520 struct ti_sci_msg_req_set_device_resets req;
521 struct ti_sci_msg_hdr resp;
522
523 struct ti_sci_xfer xfer;
524 int ret;
525
526 ret = ti_sci_setup_one_xfer(TI_SCI_MSG_SET_DEVICE_RESETS,
527 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
528 &req, sizeof(req),
529 &resp, sizeof(resp),
530 &xfer);
531 if (ret) {
532 ERROR("Message alloc failed (%d)\n", ret);
533 return ret;
534 }
535
536 req.id = id;
537 req.resets = reset_state;
538
539 ret = ti_sci_do_xfer(&xfer);
540 if (ret) {
541 ERROR("Transfer send failed (%d)\n", ret);
542 return ret;
543 }
544
545 if (!ti_sci_is_response_ack(&resp))
546 return -ENODEV;
547
548 return 0;
549}
550
551/**
552 * ti_sci_device_get_resets() - Get reset state for device managed by TISCI
553 *
554 * @id: Device Identifier
555 * @reset_state: Pointer to reset state to populate
556 *
557 * Return: 0 if all goes well, else appropriate error message
558 */
559int ti_sci_device_get_resets(uint32_t id, uint32_t *reset_state)
560{
561 return ti_sci_device_get_state(id, NULL, reset_state, NULL, NULL);
562}
563
564/**
Andrew F. Davisa513b2a2018-05-04 19:06:09 +0000565 * ti_sci_init() - Basic initialization
566 *
567 * Return: 0 if all goes well, else appropriate error message
568 */
569int ti_sci_init(void)
570{
571 struct ti_sci_msg_resp_version rev_info;
572 int ret;
573
574 ret = ti_sci_get_revision(&rev_info);
575 if (ret) {
576 ERROR("Unable to communicate with control firmware (%d)\n", ret);
577 return ret;
578 }
579
580 INFO("SYSFW ABI: %d.%d (firmware rev 0x%04x '%s')\n",
581 rev_info.abi_major, rev_info.abi_minor,
582 rev_info.firmware_revision,
583 rev_info.firmware_description);
584
585 return 0;
586}