1 // SPDX-License-Identifier: GPL-2.0+
3 * Texas Instruments' K3 System Controller Driver
5 * Copyright (C) 2017-2018 Texas Instruments Incorporated - http://www.ti.com/
6 * Lokesh Vutla <lokeshvutla@ti.com>
11 #include <remoteproc.h>
14 #include <dm/device_compat.h>
15 #include <linux/soc/ti/k3-sec-proxy.h>
17 #define K3_MSG_R5_TO_M3_M3FW 0x8105
18 #define K3_MSG_M3_TO_R5_CERT_RESULT 0x8805
19 #define K3_MSG_M3_TO_R5_BOOT_NOTIFICATION 0x000A
21 #define K3_FLAGS_MSG_CERT_AUTH_PASS 0x555555
22 #define K3_FLAGS_MSG_CERT_AUTH_FAIL 0xffffff
25 * struct k3_sysctrler_msg_hdr - Generic Header for Messages and responses.
26 * @cmd_id: Message ID. One of K3_MSG_*
27 * @host_id: Host ID of the message
28 * @seq_ne: Message identifier indicating a transfer sequence.
29 * @flags: Flags for the message.
31 struct k3_sysctrler_msg_hdr {
39 * struct k3_sysctrler_load_msg - Message format for Firmware loading
40 * @hdr: Generic message hdr
41 * @buffer_address: Address at which firmware is located.
42 * @buffer_size: Size of the firmware.
44 struct k3_sysctrler_load_msg {
45 struct k3_sysctrler_msg_hdr hdr;
51 * struct k3_sysctrler_boot_notification_msg - Message format for boot
53 * @checksum: Checksum for the entire message
54 * @reserved: Reserved for future use.
55 * @hdr: Generic message hdr
57 struct k3_sysctrler_boot_notification_msg {
60 struct k3_sysctrler_msg_hdr hdr;
64 * struct k3_sysctrler_desc - Description of SoC integration.
65 * @host_id: Host identifier representing the compute entity
66 * @max_rx_timeout_ms: Timeout for communication with SoC (in Milliseconds)
67 * @max_msg_size: Maximum size of data per message that can be handled.
69 struct k3_sysctrler_desc {
71 int max_rx_timeout_us;
76 * struct k3_sysctrler_privdata - Structure representing System Controller data.
77 * @chan_tx: Transmit mailbox channel
78 * @chan_rx: Receive mailbox channel
79 * @desc: SoC description for this instance
80 * @seq_nr: Counter for number of messages sent.
82 struct k3_sysctrler_privdata {
83 struct mbox_chan chan_tx;
84 struct mbox_chan chan_rx;
85 struct k3_sysctrler_desc *desc;
90 void k3_sysctrler_load_msg_setup(struct k3_sysctrler_load_msg *fw,
91 struct k3_sysctrler_privdata *priv,
92 ulong addr, ulong size)
94 fw->hdr.cmd_id = K3_MSG_R5_TO_M3_M3FW;
95 fw->hdr.host_id = priv->desc->host_id;
96 fw->hdr.seq_nr = priv->seq_nr++;
98 fw->buffer_address = addr;
99 fw->buffer_size = size;
102 static int k3_sysctrler_load_response(u32 *buf)
104 struct k3_sysctrler_load_msg *fw;
106 fw = (struct k3_sysctrler_load_msg *)buf;
108 /* Check for proper response ID */
109 if (fw->hdr.cmd_id != K3_MSG_M3_TO_R5_CERT_RESULT) {
110 dev_err(dev, "%s: Command expected 0x%x, but received 0x%x\n",
111 __func__, K3_MSG_M3_TO_R5_CERT_RESULT, fw->hdr.cmd_id);
115 /* Check for certificate authentication result */
116 if (fw->hdr.flags == K3_FLAGS_MSG_CERT_AUTH_FAIL) {
117 dev_err(dev, "%s: Firmware certificate authentication failed\n",
120 } else if (fw->hdr.flags != K3_FLAGS_MSG_CERT_AUTH_PASS) {
121 dev_err(dev, "%s: Firmware Load response Invalid %d\n",
122 __func__, fw->hdr.flags);
126 debug("%s: Firmware authentication passed\n", __func__);
131 static int k3_sysctrler_boot_notification_response(u32 *buf)
133 struct k3_sysctrler_boot_notification_msg *boot;
135 boot = (struct k3_sysctrler_boot_notification_msg *)buf;
137 /* ToDo: Verify checksum */
139 /* Check for proper response ID */
140 if (boot->hdr.cmd_id != K3_MSG_M3_TO_R5_BOOT_NOTIFICATION) {
141 dev_err(dev, "%s: Command expected 0x%x, but received 0x%x\n",
142 __func__, K3_MSG_M3_TO_R5_BOOT_NOTIFICATION,
147 debug("%s: Boot notification received\n", __func__);
153 * k3_sysctrler_load() - Loadup the K3 remote processor
154 * @dev: corresponding K3 remote processor device
155 * @addr: Address in memory where image binary is stored
156 * @size: Size in bytes of the image binary
158 * Return: 0 if all goes good, else appropriate error message.
160 static int k3_sysctrler_load(struct udevice *dev, ulong addr, ulong size)
162 struct k3_sysctrler_privdata *priv = dev_get_priv(dev);
163 struct k3_sysctrler_load_msg firmware;
164 struct k3_sec_proxy_msg msg;
167 debug("%s: Loading binary from 0x%08lX, size 0x%08lX\n",
168 __func__, addr, size);
170 memset(&firmware, 0, sizeof(firmware));
171 memset(&msg, 0, sizeof(msg));
173 /* Setup the message */
174 k3_sysctrler_load_msg_setup(&firmware, priv, addr, size);
175 msg.len = sizeof(firmware);
176 msg.buf = (u32 *)&firmware;
178 /* Send the message */
179 ret = mbox_send(&priv->chan_tx, &msg);
181 dev_err(dev, "%s: Firmware Loading failed. ret = %d\n",
186 /* Receive the response */
187 ret = mbox_recv(&priv->chan_rx, &msg, priv->desc->max_rx_timeout_us);
189 dev_err(dev, "%s: Firmware Load response failed. ret = %d\n",
194 /* Process the response */
195 ret = k3_sysctrler_load_response(msg.buf);
199 debug("%s: Firmware Loaded successfully on dev %s\n",
200 __func__, dev->name);
206 * k3_sysctrler_start() - Start the remote processor
207 * Note that while technically the K3 system controller starts up
208 * automatically after its firmware got loaded we still want to
209 * utilize the rproc start operation for other startup-related
211 * @dev: device to operate upon
213 * Return: 0 if all went ok, else return appropriate error
215 static int k3_sysctrler_start(struct udevice *dev)
217 struct k3_sysctrler_privdata *priv = dev_get_priv(dev);
218 struct k3_sec_proxy_msg msg;
221 debug("%s(dev=%p)\n", __func__, dev);
223 /* Receive the boot notification. Note that it is sent only once. */
224 ret = mbox_recv(&priv->chan_rx, &msg, priv->desc->max_rx_timeout_us);
226 dev_err(dev, "%s: Boot Notification response failed. ret = %d\n",
231 /* Process the response */
232 ret = k3_sysctrler_boot_notification_response(msg.buf);
236 debug("%s: Boot notification received successfully on dev %s\n",
237 __func__, dev->name);
242 static const struct dm_rproc_ops k3_sysctrler_ops = {
243 .load = k3_sysctrler_load,
244 .start = k3_sysctrler_start,
248 * k3_of_to_priv() - generate private data from device tree
249 * @dev: corresponding k3 remote processor device
250 * @priv: pointer to driver specific private data
252 * Return: 0 if all goes good, else appropriate error message.
254 static int k3_of_to_priv(struct udevice *dev,
255 struct k3_sysctrler_privdata *priv)
259 ret = mbox_get_by_name(dev, "tx", &priv->chan_tx);
261 dev_err(dev, "%s: Acquiring Tx channel failed. ret = %d\n",
266 ret = mbox_get_by_name(dev, "rx", &priv->chan_rx);
268 dev_err(dev, "%s: Acquiring Rx channel failed. ret = %d\n",
277 * k3_sysctrler_probe() - Basic probe
278 * @dev: corresponding k3 remote processor device
280 * Return: 0 if all goes good, else appropriate error message.
282 static int k3_sysctrler_probe(struct udevice *dev)
284 struct k3_sysctrler_privdata *priv;
287 debug("%s(dev=%p)\n", __func__, dev);
289 priv = dev_get_priv(dev);
291 ret = k3_of_to_priv(dev, priv);
293 dev_err(dev, "%s: Probe failed with error %d\n", __func__, ret);
297 priv->desc = (void *)dev_get_driver_data(dev);
303 static const struct k3_sysctrler_desc k3_sysctrler_am654_desc = {
304 .host_id = 4, /* HOST_ID_R5_1 */
305 .max_rx_timeout_us = 800000,
309 static const struct udevice_id k3_sysctrler_ids[] = {
311 .compatible = "ti,am654-system-controller",
312 .data = (ulong)&k3_sysctrler_am654_desc,
317 U_BOOT_DRIVER(k3_sysctrler) = {
318 .name = "k3_system_controller",
319 .of_match = k3_sysctrler_ids,
320 .id = UCLASS_REMOTEPROC,
321 .ops = &k3_sysctrler_ops,
322 .probe = k3_sysctrler_probe,
323 .priv_auto_alloc_size = sizeof(struct k3_sysctrler_privdata),