blob: 4d87cea7aa158bde4c6894420dc57449ad3b1cdd [file] [log] [blame]
Lokesh Vutla5af02db2018-08-27 15:57:32 +05301// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Texas Instruments System Control Interface Protocol Driver
4 * Based on drivers/firmware/ti_sci.c from Linux.
5 *
6 * Copyright (C) 2018 Texas Instruments Incorporated - http://www.ti.com/
7 * Lokesh Vutla <lokeshvutla@ti.com>
8 */
9
10#include <common.h>
11#include <dm.h>
12#include <errno.h>
13#include <mailbox.h>
14#include <dm/device.h>
15#include <linux/err.h>
16#include <linux/soc/ti/k3-sec-proxy.h>
17#include <linux/soc/ti/ti_sci_protocol.h>
18
19#include "ti_sci.h"
20
21/* List of all TI SCI devices active in system */
22static LIST_HEAD(ti_sci_list);
23
24/**
25 * struct ti_sci_xfer - Structure representing a message flow
26 * @tx_message: Transmit message
27 * @rx_len: Receive message length
28 */
29struct ti_sci_xfer {
30 struct k3_sec_proxy_msg tx_message;
31 u8 rx_len;
32};
33
34/**
35 * struct ti_sci_desc - Description of SoC integration
36 * @host_id: Host identifier representing the compute entity
37 * @max_rx_timeout_us: Timeout for communication with SoC (in Microseconds)
38 * @max_msg_size: Maximum size of data per message that can be handled.
39 */
40struct ti_sci_desc {
41 u8 host_id;
42 int max_rx_timeout_us;
43 int max_msg_size;
44};
45
46/**
47 * struct ti_sci_info - Structure representing a TI SCI instance
48 * @dev: Device pointer
49 * @desc: SoC description for this instance
50 * @handle: Instance of TI SCI handle to send to clients.
51 * @chan_tx: Transmit mailbox channel
52 * @chan_rx: Receive mailbox channel
53 * @xfer: xfer info
54 * @list: list head
55 * @is_secure: Determines if the communication is through secure threads.
56 * @host_id: Host identifier representing the compute entity
57 * @seq: Seq id used for verification for tx and rx message.
58 */
59struct ti_sci_info {
60 struct udevice *dev;
61 const struct ti_sci_desc *desc;
62 struct ti_sci_handle handle;
63 struct mbox_chan chan_tx;
64 struct mbox_chan chan_rx;
65 struct mbox_chan chan_notify;
66 struct ti_sci_xfer xfer;
67 struct list_head list;
68 bool is_secure;
69 u8 host_id;
70 u8 seq;
71};
72
73#define handle_to_ti_sci_info(h) container_of(h, struct ti_sci_info, handle)
74
75/**
76 * ti_sci_setup_one_xfer() - Setup one message type
77 * @info: Pointer to SCI entity information
78 * @msg_type: Message type
79 * @msg_flags: Flag to set for the message
80 * @buf: Buffer to be send to mailbox channel
81 * @tx_message_size: transmit message size
82 * @rx_message_size: receive message size
83 *
84 * Helper function which is used by various command functions that are
85 * exposed to clients of this driver for allocating a message traffic event.
86 *
87 * Return: Corresponding ti_sci_xfer pointer if all went fine,
88 * else appropriate error pointer.
89 */
90static struct ti_sci_xfer *ti_sci_setup_one_xfer(struct ti_sci_info *info,
91 u16 msg_type, u32 msg_flags,
92 u32 *buf,
93 size_t tx_message_size,
94 size_t rx_message_size)
95{
96 struct ti_sci_xfer *xfer = &info->xfer;
97 struct ti_sci_msg_hdr *hdr;
98
99 /* Ensure we have sane transfer sizes */
100 if (rx_message_size > info->desc->max_msg_size ||
101 tx_message_size > info->desc->max_msg_size ||
102 rx_message_size < sizeof(*hdr) || tx_message_size < sizeof(*hdr))
103 return ERR_PTR(-ERANGE);
104
105 info->seq = ~info->seq;
106 xfer->tx_message.buf = buf;
107 xfer->tx_message.len = tx_message_size;
108 xfer->rx_len = (u8)rx_message_size;
109
110 hdr = (struct ti_sci_msg_hdr *)buf;
111 hdr->seq = info->seq;
112 hdr->type = msg_type;
113 hdr->host = info->host_id;
114 hdr->flags = msg_flags;
115
116 return xfer;
117}
118
119/**
120 * ti_sci_get_response() - Receive response from mailbox channel
121 * @info: Pointer to SCI entity information
122 * @xfer: Transfer to initiate and wait for response
123 * @chan: Channel to receive the response
124 *
125 * Return: -ETIMEDOUT in case of no response, if transmit error,
126 * return corresponding error, else if all goes well,
127 * return 0.
128 */
129static inline int ti_sci_get_response(struct ti_sci_info *info,
130 struct ti_sci_xfer *xfer,
131 struct mbox_chan *chan)
132{
133 struct k3_sec_proxy_msg *msg = &xfer->tx_message;
134 struct ti_sci_secure_msg_hdr *secure_hdr;
135 struct ti_sci_msg_hdr *hdr;
136 int ret;
137
138 /* Receive the response */
139 ret = mbox_recv(chan, msg, info->desc->max_rx_timeout_us);
140 if (ret) {
141 dev_err(info->dev, "%s: Message receive failed. ret = %d\n",
142 __func__, ret);
143 return ret;
144 }
145
146 /* ToDo: Verify checksum */
147 if (info->is_secure) {
148 secure_hdr = (struct ti_sci_secure_msg_hdr *)msg->buf;
149 msg->buf = (u32 *)((void *)msg->buf + sizeof(*secure_hdr));
150 }
151
152 /* msg is updated by mailbox driver */
153 hdr = (struct ti_sci_msg_hdr *)msg->buf;
154
155 /* Sanity check for message response */
156 if (hdr->seq != info->seq) {
157 dev_dbg(info->dev, "%s: Message for %d is not expected\n",
158 __func__, hdr->seq);
159 return ret;
160 }
161
162 if (msg->len > info->desc->max_msg_size) {
163 dev_err(info->dev, "%s: Unable to handle %zu xfer (max %d)\n",
164 __func__, msg->len, info->desc->max_msg_size);
165 return -EINVAL;
166 }
167
168 if (msg->len < xfer->rx_len) {
169 dev_err(info->dev, "%s: Recv xfer %zu < expected %d length\n",
170 __func__, msg->len, xfer->rx_len);
171 }
172
173 return ret;
174}
175
176/**
177 * ti_sci_do_xfer() - Do one transfer
178 * @info: Pointer to SCI entity information
179 * @xfer: Transfer to initiate and wait for response
180 *
181 * Return: 0 if all went fine, else return appropriate error.
182 */
183static inline int ti_sci_do_xfer(struct ti_sci_info *info,
184 struct ti_sci_xfer *xfer)
185{
186 struct k3_sec_proxy_msg *msg = &xfer->tx_message;
187 u8 secure_buf[info->desc->max_msg_size];
188 struct ti_sci_secure_msg_hdr secure_hdr;
189 int ret;
190
191 if (info->is_secure) {
192 /* ToDo: get checksum of the entire message */
193 secure_hdr.checksum = 0;
194 secure_hdr.reserved = 0;
195 memcpy(&secure_buf[sizeof(secure_hdr)], xfer->tx_message.buf,
196 xfer->tx_message.len);
197
198 xfer->tx_message.buf = (u32 *)secure_buf;
199 xfer->tx_message.len += sizeof(secure_hdr);
200 xfer->rx_len += sizeof(secure_hdr);
201 }
202
203 /* Send the message */
204 ret = mbox_send(&info->chan_tx, msg);
205 if (ret) {
206 dev_err(info->dev, "%s: Message sending failed. ret = %d\n",
207 __func__, ret);
208 return ret;
209 }
210
211 return ti_sci_get_response(info, xfer, &info->chan_rx);
212}
213
214/**
215 * ti_sci_cmd_get_revision() - command to get the revision of the SCI entity
216 * @handle: pointer to TI SCI handle
217 *
218 * Updates the SCI information in the internal data structure.
219 *
220 * Return: 0 if all went fine, else return appropriate error.
221 */
222static int ti_sci_cmd_get_revision(struct ti_sci_handle *handle)
223{
224 struct ti_sci_msg_resp_version *rev_info;
225 struct ti_sci_version_info *ver;
226 struct ti_sci_msg_hdr hdr;
227 struct ti_sci_info *info;
228 struct ti_sci_xfer *xfer;
229 int ret;
230
231 if (IS_ERR(handle))
232 return PTR_ERR(handle);
233 if (!handle)
234 return -EINVAL;
235
236 info = handle_to_ti_sci_info(handle);
237
238 xfer = ti_sci_setup_one_xfer(info, TI_SCI_MSG_VERSION, 0x0,
239 (u32 *)&hdr, sizeof(struct ti_sci_msg_hdr),
240 sizeof(*rev_info));
241 if (IS_ERR(xfer)) {
242 ret = PTR_ERR(xfer);
243 dev_err(info->dev, "Message alloc failed(%d)\n", ret);
244 return ret;
245 }
246
247 ret = ti_sci_do_xfer(info, xfer);
248 if (ret) {
249 dev_err(info->dev, "Mbox communication fail %d\n", ret);
250 return ret;
251 }
252
253 rev_info = (struct ti_sci_msg_resp_version *)xfer->tx_message.buf;
254
255 ver = &handle->version;
256 ver->abi_major = rev_info->abi_major;
257 ver->abi_minor = rev_info->abi_minor;
258 ver->firmware_revision = rev_info->firmware_revision;
259 strncpy(ver->firmware_description, rev_info->firmware_description,
260 sizeof(ver->firmware_description));
261
262 return 0;
263}
264
265/**
266 * ti_sci_is_response_ack() - Generic ACK/NACK message checkup
267 * @r: pointer to response buffer
268 *
269 * Return: true if the response was an ACK, else returns false.
270 */
271static inline bool ti_sci_is_response_ack(void *r)
272{
273 struct ti_sci_msg_hdr *hdr = r;
274
275 return hdr->flags & TI_SCI_FLAG_RESP_GENERIC_ACK ? true : false;
276}
277
278/**
Andreas Dannenberg5299c4c2018-08-27 15:57:33 +0530279 * cmd_set_board_config_using_msg() - Common command to send board configuration
280 * message
281 * @handle: pointer to TI SCI handle
282 * @msg_type: One of the TISCI message types to set board configuration
283 * @addr: Address where the board config structure is located
284 * @size: Size of the board config structure
285 *
286 * Return: 0 if all went well, else returns appropriate error value.
287 */
288static int cmd_set_board_config_using_msg(const struct ti_sci_handle *handle,
289 u16 msg_type, u64 addr, u32 size)
290{
291 struct ti_sci_msg_board_config req;
292 struct ti_sci_msg_hdr *resp;
293 struct ti_sci_info *info;
294 struct ti_sci_xfer *xfer;
295 int ret = 0;
296
297 if (IS_ERR(handle))
298 return PTR_ERR(handle);
299 if (!handle)
300 return -EINVAL;
301
302 info = handle_to_ti_sci_info(handle);
303
304 xfer = ti_sci_setup_one_xfer(info, msg_type,
305 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
306 (u32 *)&req, sizeof(req), sizeof(*resp));
307 if (IS_ERR(xfer)) {
308 ret = PTR_ERR(xfer);
309 dev_err(info->dev, "Message alloc failed(%d)\n", ret);
310 return ret;
311 }
312 req.boardcfgp_high = (addr >> 32) & 0xffffffff;
313 req.boardcfgp_low = addr & 0xffffffff;
314 req.boardcfg_size = size;
315
316 ret = ti_sci_do_xfer(info, xfer);
317 if (ret) {
318 dev_err(info->dev, "Mbox send fail %d\n", ret);
319 return ret;
320 }
321
322 resp = (struct ti_sci_msg_hdr *)xfer->tx_message.buf;
323
324 if (!ti_sci_is_response_ack(resp))
325 return -ENODEV;
326
327 return ret;
328}
329
330/**
331 * ti_sci_cmd_set_board_config() - Command to send board configuration message
332 * @handle: pointer to TI SCI handle
333 * @addr: Address where the board config structure is located
334 * @size: Size of the board config structure
335 *
336 * Return: 0 if all went well, else returns appropriate error value.
337 */
338static int ti_sci_cmd_set_board_config(const struct ti_sci_handle *handle,
339 u64 addr, u32 size)
340{
341 return cmd_set_board_config_using_msg(handle,
342 TI_SCI_MSG_BOARD_CONFIG,
343 addr, size);
344}
345
346/**
347 * ti_sci_cmd_set_board_config_rm() - Command to send board resource
348 * management configuration
349 * @handle: pointer to TI SCI handle
350 * @addr: Address where the board RM config structure is located
351 * @size: Size of the RM config structure
352 *
353 * Return: 0 if all went well, else returns appropriate error value.
354 */
355static
356int ti_sci_cmd_set_board_config_rm(const struct ti_sci_handle *handle,
357 u64 addr, u32 size)
358{
359 return cmd_set_board_config_using_msg(handle,
360 TI_SCI_MSG_BOARD_CONFIG_RM,
361 addr, size);
362}
363
364/**
365 * ti_sci_cmd_set_board_config_security() - Command to send board security
366 * configuration message
367 * @handle: pointer to TI SCI handle
368 * @addr: Address where the board security config structure is located
369 * @size: Size of the security config structure
370 *
371 * Return: 0 if all went well, else returns appropriate error value.
372 */
373static
374int ti_sci_cmd_set_board_config_security(const struct ti_sci_handle *handle,
375 u64 addr, u32 size)
376{
377 return cmd_set_board_config_using_msg(handle,
378 TI_SCI_MSG_BOARD_CONFIG_SECURITY,
379 addr, size);
380}
381
382/**
383 * ti_sci_cmd_set_board_config_pm() - Command to send board power and clock
384 * configuration message
385 * @handle: pointer to TI SCI handle
386 * @addr: Address where the board PM config structure is located
387 * @size: Size of the PM config structure
388 *
389 * Return: 0 if all went well, else returns appropriate error value.
390 */
391static int ti_sci_cmd_set_board_config_pm(const struct ti_sci_handle *handle,
392 u64 addr, u32 size)
393{
394 return cmd_set_board_config_using_msg(handle,
395 TI_SCI_MSG_BOARD_CONFIG_PM,
396 addr, size);
397}
398
Andreas Dannenberg24a4d5e2018-08-27 15:57:34 +0530399/**
400 * ti_sci_set_device_state() - Set device state helper
401 * @handle: pointer to TI SCI handle
402 * @id: Device identifier
403 * @flags: flags to setup for the device
404 * @state: State to move the device to
405 *
406 * Return: 0 if all went well, else returns appropriate error value.
407 */
408static int ti_sci_set_device_state(const struct ti_sci_handle *handle,
409 u32 id, u32 flags, u8 state)
410{
411 struct ti_sci_msg_req_set_device_state req;
412 struct ti_sci_msg_hdr *resp;
413 struct ti_sci_info *info;
414 struct ti_sci_xfer *xfer;
415 int ret = 0;
416
417 if (IS_ERR(handle))
418 return PTR_ERR(handle);
419 if (!handle)
420 return -EINVAL;
421
422 info = handle_to_ti_sci_info(handle);
423
424 xfer = ti_sci_setup_one_xfer(info, TI_SCI_MSG_SET_DEVICE_STATE,
425 flags | TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
426 (u32 *)&req, sizeof(req), sizeof(*resp));
427 if (IS_ERR(xfer)) {
428 ret = PTR_ERR(xfer);
429 dev_err(info->dev, "Message alloc failed(%d)\n", ret);
430 return ret;
431 }
432 req.id = id;
433 req.state = state;
434
435 ret = ti_sci_do_xfer(info, xfer);
436 if (ret) {
437 dev_err(info->dev, "Mbox send fail %d\n", ret);
438 return ret;
439 }
440
441 resp = (struct ti_sci_msg_hdr *)xfer->tx_message.buf;
442
443 if (!ti_sci_is_response_ack(resp))
444 return -ENODEV;
445
446 return ret;
447}
448
449/**
450 * ti_sci_get_device_state() - Get device state helper
451 * @handle: Handle to the device
452 * @id: Device Identifier
453 * @clcnt: Pointer to Context Loss Count
454 * @resets: pointer to resets
455 * @p_state: pointer to p_state
456 * @c_state: pointer to c_state
457 *
458 * Return: 0 if all went fine, else return appropriate error.
459 */
460static int ti_sci_get_device_state(const struct ti_sci_handle *handle,
461 u32 id, u32 *clcnt, u32 *resets,
462 u8 *p_state, u8 *c_state)
463{
464 struct ti_sci_msg_resp_get_device_state *resp;
465 struct ti_sci_msg_req_get_device_state req;
466 struct ti_sci_info *info;
467 struct ti_sci_xfer *xfer;
468 int ret = 0;
469
470 if (IS_ERR(handle))
471 return PTR_ERR(handle);
472 if (!handle)
473 return -EINVAL;
474
475 if (!clcnt && !resets && !p_state && !c_state)
476 return -EINVAL;
477
478 info = handle_to_ti_sci_info(handle);
479
480 /* Response is expected, so need of any flags */
481 xfer = ti_sci_setup_one_xfer(info, TI_SCI_MSG_GET_DEVICE_STATE, 0,
482 (u32 *)&req, sizeof(req), sizeof(*resp));
483 if (IS_ERR(xfer)) {
484 ret = PTR_ERR(xfer);
485 dev_err(info->dev, "Message alloc failed(%d)\n", ret);
486 return ret;
487 }
488 req.id = id;
489
490 ret = ti_sci_do_xfer(info, xfer);
491 if (ret) {
492 dev_err(dev, "Mbox send fail %d\n", ret);
493 return ret;
494 }
495
496 resp = (struct ti_sci_msg_resp_get_device_state *)xfer->tx_message.buf;
497 if (!ti_sci_is_response_ack(resp))
498 return -ENODEV;
499
500 if (clcnt)
501 *clcnt = resp->context_loss_count;
502 if (resets)
503 *resets = resp->resets;
504 if (p_state)
505 *p_state = resp->programmed_state;
506 if (c_state)
507 *c_state = resp->current_state;
508
509 return ret;
510}
511
512/**
513 * ti_sci_cmd_get_device() - command to request for device managed by TISCI
514 * @handle: Pointer to TISCI handle as retrieved by *ti_sci_get_handle
515 * @id: Device Identifier
516 *
517 * Request for the device - NOTE: the client MUST maintain integrity of
518 * usage count by balancing get_device with put_device. No refcounting is
519 * managed by driver for that purpose.
520 *
521 * NOTE: The request is for exclusive access for the processor.
522 *
523 * Return: 0 if all went fine, else return appropriate error.
524 */
525static int ti_sci_cmd_get_device(const struct ti_sci_handle *handle, u32 id)
526{
527 return ti_sci_set_device_state(handle, id,
528 MSG_FLAG_DEVICE_EXCLUSIVE,
529 MSG_DEVICE_SW_STATE_ON);
530}
531
532/**
533 * ti_sci_cmd_idle_device() - Command to idle a device managed by TISCI
534 * @handle: Pointer to TISCI handle as retrieved by *ti_sci_get_handle
535 * @id: Device Identifier
536 *
537 * Request for the device - NOTE: the client MUST maintain integrity of
538 * usage count by balancing get_device with put_device. No refcounting is
539 * managed by driver for that purpose.
540 *
541 * Return: 0 if all went fine, else return appropriate error.
542 */
543static int ti_sci_cmd_idle_device(const struct ti_sci_handle *handle, u32 id)
544{
545 return ti_sci_set_device_state(handle, id,
546 MSG_FLAG_DEVICE_EXCLUSIVE,
547 MSG_DEVICE_SW_STATE_RETENTION);
548}
549
550/**
551 * ti_sci_cmd_put_device() - command to release a device managed by TISCI
552 * @handle: Pointer to TISCI handle as retrieved by *ti_sci_get_handle
553 * @id: Device Identifier
554 *
555 * Request for the device - NOTE: the client MUST maintain integrity of
556 * usage count by balancing get_device with put_device. No refcounting is
557 * managed by driver for that purpose.
558 *
559 * Return: 0 if all went fine, else return appropriate error.
560 */
561static int ti_sci_cmd_put_device(const struct ti_sci_handle *handle, u32 id)
562{
563 return ti_sci_set_device_state(handle, id,
564 0, MSG_DEVICE_SW_STATE_AUTO_OFF);
565}
566
567/**
568 * ti_sci_cmd_dev_is_valid() - Is the device valid
569 * @handle: Pointer to TISCI handle as retrieved by *ti_sci_get_handle
570 * @id: Device Identifier
571 *
572 * Return: 0 if all went fine and the device ID is valid, else return
573 * appropriate error.
574 */
575static int ti_sci_cmd_dev_is_valid(const struct ti_sci_handle *handle, u32 id)
576{
577 u8 unused;
578
579 /* check the device state which will also tell us if the ID is valid */
580 return ti_sci_get_device_state(handle, id, NULL, NULL, NULL, &unused);
581}
582
583/**
584 * ti_sci_cmd_dev_get_clcnt() - Get context loss counter
585 * @handle: Pointer to TISCI handle
586 * @id: Device Identifier
587 * @count: Pointer to Context Loss counter to populate
588 *
589 * Return: 0 if all went fine, else return appropriate error.
590 */
591static int ti_sci_cmd_dev_get_clcnt(const struct ti_sci_handle *handle, u32 id,
592 u32 *count)
593{
594 return ti_sci_get_device_state(handle, id, count, NULL, NULL, NULL);
595}
596
597/**
598 * ti_sci_cmd_dev_is_idle() - Check if the device is requested to be idle
599 * @handle: Pointer to TISCI handle
600 * @id: Device Identifier
601 * @r_state: true if requested to be idle
602 *
603 * Return: 0 if all went fine, else return appropriate error.
604 */
605static int ti_sci_cmd_dev_is_idle(const struct ti_sci_handle *handle, u32 id,
606 bool *r_state)
607{
608 int ret;
609 u8 state;
610
611 if (!r_state)
612 return -EINVAL;
613
614 ret = ti_sci_get_device_state(handle, id, NULL, NULL, &state, NULL);
615 if (ret)
616 return ret;
617
618 *r_state = (state == MSG_DEVICE_SW_STATE_RETENTION);
619
620 return 0;
621}
622
623/**
624 * ti_sci_cmd_dev_is_stop() - Check if the device is requested to be stopped
625 * @handle: Pointer to TISCI handle
626 * @id: Device Identifier
627 * @r_state: true if requested to be stopped
628 * @curr_state: true if currently stopped.
629 *
630 * Return: 0 if all went fine, else return appropriate error.
631 */
632static int ti_sci_cmd_dev_is_stop(const struct ti_sci_handle *handle, u32 id,
633 bool *r_state, bool *curr_state)
634{
635 int ret;
636 u8 p_state, c_state;
637
638 if (!r_state && !curr_state)
639 return -EINVAL;
640
641 ret =
642 ti_sci_get_device_state(handle, id, NULL, NULL, &p_state, &c_state);
643 if (ret)
644 return ret;
645
646 if (r_state)
647 *r_state = (p_state == MSG_DEVICE_SW_STATE_AUTO_OFF);
648 if (curr_state)
649 *curr_state = (c_state == MSG_DEVICE_HW_STATE_OFF);
650
651 return 0;
652}
653
654/**
655 * ti_sci_cmd_dev_is_on() - Check if the device is requested to be ON
656 * @handle: Pointer to TISCI handle
657 * @id: Device Identifier
658 * @r_state: true if requested to be ON
659 * @curr_state: true if currently ON and active
660 *
661 * Return: 0 if all went fine, else return appropriate error.
662 */
663static int ti_sci_cmd_dev_is_on(const struct ti_sci_handle *handle, u32 id,
664 bool *r_state, bool *curr_state)
665{
666 int ret;
667 u8 p_state, c_state;
668
669 if (!r_state && !curr_state)
670 return -EINVAL;
671
672 ret =
673 ti_sci_get_device_state(handle, id, NULL, NULL, &p_state, &c_state);
674 if (ret)
675 return ret;
676
677 if (r_state)
678 *r_state = (p_state == MSG_DEVICE_SW_STATE_ON);
679 if (curr_state)
680 *curr_state = (c_state == MSG_DEVICE_HW_STATE_ON);
681
682 return 0;
683}
684
685/**
686 * ti_sci_cmd_dev_is_trans() - Check if the device is currently transitioning
687 * @handle: Pointer to TISCI handle
688 * @id: Device Identifier
689 * @curr_state: true if currently transitioning.
690 *
691 * Return: 0 if all went fine, else return appropriate error.
692 */
693static int ti_sci_cmd_dev_is_trans(const struct ti_sci_handle *handle, u32 id,
694 bool *curr_state)
695{
696 int ret;
697 u8 state;
698
699 if (!curr_state)
700 return -EINVAL;
701
702 ret = ti_sci_get_device_state(handle, id, NULL, NULL, NULL, &state);
703 if (ret)
704 return ret;
705
706 *curr_state = (state == MSG_DEVICE_HW_STATE_TRANS);
707
708 return 0;
709}
710
711/**
712 * ti_sci_cmd_set_device_resets() - command to set resets for device managed
713 * by TISCI
714 * @handle: Pointer to TISCI handle as retrieved by *ti_sci_get_handle
715 * @id: Device Identifier
716 * @reset_state: Device specific reset bit field
717 *
718 * Return: 0 if all went fine, else return appropriate error.
719 */
720static int ti_sci_cmd_set_device_resets(const struct ti_sci_handle *handle,
721 u32 id, u32 reset_state)
722{
723 struct ti_sci_msg_req_set_device_resets req;
724 struct ti_sci_msg_hdr *resp;
725 struct ti_sci_info *info;
726 struct ti_sci_xfer *xfer;
727 int ret = 0;
728
729 if (IS_ERR(handle))
730 return PTR_ERR(handle);
731 if (!handle)
732 return -EINVAL;
733
734 info = handle_to_ti_sci_info(handle);
735
736 xfer = ti_sci_setup_one_xfer(info, TI_SCI_MSG_SET_DEVICE_RESETS,
737 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
738 (u32 *)&req, sizeof(req), sizeof(*resp));
739 if (IS_ERR(xfer)) {
740 ret = PTR_ERR(xfer);
741 dev_err(info->dev, "Message alloc failed(%d)\n", ret);
742 return ret;
743 }
744 req.id = id;
745 req.resets = reset_state;
746
747 ret = ti_sci_do_xfer(info, xfer);
748 if (ret) {
749 dev_err(info->dev, "Mbox send fail %d\n", ret);
750 return ret;
751 }
752
753 resp = (struct ti_sci_msg_hdr *)xfer->tx_message.buf;
754
755 if (!ti_sci_is_response_ack(resp))
756 return -ENODEV;
757
758 return ret;
759}
760
761/**
762 * ti_sci_cmd_get_device_resets() - Get reset state for device managed
763 * by TISCI
764 * @handle: Pointer to TISCI handle
765 * @id: Device Identifier
766 * @reset_state: Pointer to reset state to populate
767 *
768 * Return: 0 if all went fine, else return appropriate error.
769 */
770static int ti_sci_cmd_get_device_resets(const struct ti_sci_handle *handle,
771 u32 id, u32 *reset_state)
772{
773 return ti_sci_get_device_state(handle, id, NULL, reset_state, NULL,
774 NULL);
775}
776
Andreas Dannenberg5299c4c2018-08-27 15:57:33 +0530777/*
778 * ti_sci_setup_ops() - Setup the operations structures
779 * @info: pointer to TISCI pointer
780 */
781static void ti_sci_setup_ops(struct ti_sci_info *info)
782{
783 struct ti_sci_ops *ops = &info->handle.ops;
784 struct ti_sci_board_ops *bops = &ops->board_ops;
Andreas Dannenberg24a4d5e2018-08-27 15:57:34 +0530785 struct ti_sci_dev_ops *dops = &ops->dev_ops;
Andreas Dannenberg5299c4c2018-08-27 15:57:33 +0530786
787 bops->board_config = ti_sci_cmd_set_board_config;
788 bops->board_config_rm = ti_sci_cmd_set_board_config_rm;
789 bops->board_config_security = ti_sci_cmd_set_board_config_security;
790 bops->board_config_pm = ti_sci_cmd_set_board_config_pm;
Andreas Dannenberg24a4d5e2018-08-27 15:57:34 +0530791
792 dops->get_device = ti_sci_cmd_get_device;
793 dops->idle_device = ti_sci_cmd_idle_device;
794 dops->put_device = ti_sci_cmd_put_device;
795 dops->is_valid = ti_sci_cmd_dev_is_valid;
796 dops->get_context_loss_count = ti_sci_cmd_dev_get_clcnt;
797 dops->is_idle = ti_sci_cmd_dev_is_idle;
798 dops->is_stop = ti_sci_cmd_dev_is_stop;
799 dops->is_on = ti_sci_cmd_dev_is_on;
800 dops->is_transitioning = ti_sci_cmd_dev_is_trans;
801 dops->set_device_resets = ti_sci_cmd_set_device_resets;
802 dops->get_device_resets = ti_sci_cmd_get_device_resets;
Andreas Dannenberg5299c4c2018-08-27 15:57:33 +0530803}
804
805/**
Lokesh Vutla5af02db2018-08-27 15:57:32 +0530806 * ti_sci_get_handle_from_sysfw() - Get the TI SCI handle of the SYSFW
807 * @dev: Pointer to the SYSFW device
808 *
809 * Return: pointer to handle if successful, else EINVAL if invalid conditions
810 * are encountered.
811 */
812const
813struct ti_sci_handle *ti_sci_get_handle_from_sysfw(struct udevice *sci_dev)
814{
815 if (!sci_dev)
816 return ERR_PTR(-EINVAL);
817
818 struct ti_sci_info *info = dev_get_priv(sci_dev);
819
820 if (!info)
821 return ERR_PTR(-EINVAL);
822
823 struct ti_sci_handle *handle = &info->handle;
824
825 if (!handle)
826 return ERR_PTR(-EINVAL);
827
828 return handle;
829}
830
831/**
832 * ti_sci_get_handle() - Get the TI SCI handle for a device
833 * @dev: Pointer to device for which we want SCI handle
834 *
835 * Return: pointer to handle if successful, else EINVAL if invalid conditions
836 * are encountered.
837 */
838const struct ti_sci_handle *ti_sci_get_handle(struct udevice *dev)
839{
840 if (!dev)
841 return ERR_PTR(-EINVAL);
842
843 struct udevice *sci_dev = dev_get_parent(dev);
844
845 return ti_sci_get_handle_from_sysfw(sci_dev);
846}
847
848/**
849 * ti_sci_get_by_phandle() - Get the TI SCI handle using DT phandle
850 * @dev: device node
851 * @propname: property name containing phandle on TISCI node
852 *
853 * Return: pointer to handle if successful, else appropriate error value.
854 */
855const struct ti_sci_handle *ti_sci_get_by_phandle(struct udevice *dev,
856 const char *property)
857{
858 struct ti_sci_info *entry, *info = NULL;
859 u32 phandle, err;
860 ofnode node;
861
862 err = ofnode_read_u32(dev_ofnode(dev), property, &phandle);
863 if (err)
864 return ERR_PTR(err);
865
866 node = ofnode_get_by_phandle(phandle);
867 if (!ofnode_valid(node))
868 return ERR_PTR(-EINVAL);
869
870 list_for_each_entry(entry, &ti_sci_list, list)
871 if (ofnode_equal(dev_ofnode(entry->dev), node)) {
872 info = entry;
873 break;
874 }
875
876 if (!info)
877 return ERR_PTR(-ENODEV);
878
879 return &info->handle;
880}
881
882/**
883 * ti_sci_of_to_info() - generate private data from device tree
884 * @dev: corresponding system controller interface device
885 * @info: pointer to driver specific private data
886 *
887 * Return: 0 if all goes good, else appropriate error message.
888 */
889static int ti_sci_of_to_info(struct udevice *dev, struct ti_sci_info *info)
890{
891 int ret;
892
893 ret = mbox_get_by_name(dev, "tx", &info->chan_tx);
894 if (ret) {
895 dev_err(dev, "%s: Acquiring Tx channel failed. ret = %d\n",
896 __func__, ret);
897 return ret;
898 }
899
900 ret = mbox_get_by_name(dev, "rx", &info->chan_rx);
901 if (ret) {
902 dev_err(dev, "%s: Acquiring Rx channel failed. ret = %d\n",
903 __func__, ret);
904 return ret;
905 }
906
907 /* Notify channel is optional. Enable only if populated */
908 ret = mbox_get_by_name(dev, "notify", &info->chan_notify);
909 if (ret) {
910 dev_dbg(dev, "%s: Acquiring notify channel failed. ret = %d\n",
911 __func__, ret);
912 }
913
914 info->host_id = dev_read_u32_default(dev, "ti,host-id",
915 info->desc->host_id);
916
917 info->is_secure = dev_read_bool(dev, "ti,secure-host");
918
919 return 0;
920}
921
922/**
923 * ti_sci_probe() - Basic probe
924 * @dev: corresponding system controller interface device
925 *
926 * Return: 0 if all goes good, else appropriate error message.
927 */
928static int ti_sci_probe(struct udevice *dev)
929{
930 struct ti_sci_info *info;
931 int ret;
932
933 debug("%s(dev=%p)\n", __func__, dev);
934
935 info = dev_get_priv(dev);
936 info->desc = (void *)dev_get_driver_data(dev);
937
938 ret = ti_sci_of_to_info(dev, info);
939 if (ret) {
940 dev_err(dev, "%s: Probe failed with error %d\n", __func__, ret);
941 return ret;
942 }
943
944 info->dev = dev;
945 info->seq = 0xA;
946
947 list_add_tail(&info->list, &ti_sci_list);
Andreas Dannenberg5299c4c2018-08-27 15:57:33 +0530948 ti_sci_setup_ops(info);
Lokesh Vutla5af02db2018-08-27 15:57:32 +0530949
950 ret = ti_sci_cmd_get_revision(&info->handle);
951
952 return ret;
953}
954
955/* Description for AM654 */
956static const struct ti_sci_desc ti_sci_sysfw_am654_desc = {
957 .host_id = 4,
958 .max_rx_timeout_us = 1000000,
959 .max_msg_size = 60,
960};
961
962static const struct udevice_id ti_sci_ids[] = {
963 {
964 .compatible = "ti,k2g-sci",
965 .data = (ulong)&ti_sci_sysfw_am654_desc
966 },
967 { /* Sentinel */ },
968};
969
970U_BOOT_DRIVER(ti_sci) = {
971 .name = "ti_sci",
972 .id = UCLASS_FIRMWARE,
973 .of_match = ti_sci_ids,
974 .probe = ti_sci_probe,
975 .priv_auto_alloc_size = sizeof(struct ti_sci_info),
976};