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>
12 #include <remoteproc.h>
15 #include <dm/device_compat.h>
16 #include <linux/soc/ti/k3-sec-proxy.h>
18 #define K3_MSG_R5_TO_M3_M3FW 0x8105
19 #define K3_MSG_M3_TO_R5_CERT_RESULT 0x8805
20 #define K3_MSG_M3_TO_R5_BOOT_NOTIFICATION 0x000A
22 #define K3_FLAGS_MSG_CERT_AUTH_PASS 0x555555
23 #define K3_FLAGS_MSG_CERT_AUTH_FAIL 0xffffff
26 * struct k3_sysctrler_msg_hdr - Generic Header for Messages and responses.
27 * @cmd_id: Message ID. One of K3_MSG_*
28 * @host_id: Host ID of the message
29 * @seq_ne: Message identifier indicating a transfer sequence.
30 * @flags: Flags for the message.
32 struct k3_sysctrler_msg_hdr {
40 * struct k3_sysctrler_load_msg - Message format for Firmware loading
41 * @hdr: Generic message hdr
42 * @buffer_address: Address at which firmware is located.
43 * @buffer_size: Size of the firmware.
45 struct k3_sysctrler_load_msg {
46 struct k3_sysctrler_msg_hdr hdr;
52 * struct k3_sysctrler_boot_notification_msg - Message format for boot
54 * @checksum: Checksum for the entire message
55 * @reserved: Reserved for future use.
56 * @hdr: Generic message hdr
58 struct k3_sysctrler_boot_notification_msg {
61 struct k3_sysctrler_msg_hdr hdr;
65 * struct k3_sysctrler_desc - Description of SoC integration.
66 * @host_id: Host identifier representing the compute entity
67 * @max_rx_timeout_ms: Timeout for communication with SoC (in Milliseconds)
68 * @max_msg_size: Maximum size of data per message that can be handled.
70 struct k3_sysctrler_desc {
72 int max_rx_timeout_us;
77 * struct k3_sysctrler_privdata - Structure representing System Controller data.
78 * @chan_tx: Transmit mailbox channel
79 * @chan_rx: Receive mailbox channel
80 * @desc: SoC description for this instance
81 * @seq_nr: Counter for number of messages sent.
83 struct k3_sysctrler_privdata {
84 struct mbox_chan chan_tx;
85 struct mbox_chan chan_rx;
86 struct k3_sysctrler_desc *desc;
91 void k3_sysctrler_load_msg_setup(struct k3_sysctrler_load_msg *fw,
92 struct k3_sysctrler_privdata *priv,
93 ulong addr, ulong size)
95 fw->hdr.cmd_id = K3_MSG_R5_TO_M3_M3FW;
96 fw->hdr.host_id = priv->desc->host_id;
97 fw->hdr.seq_nr = priv->seq_nr++;
99 fw->buffer_address = addr;
100 fw->buffer_size = size;
103 static int k3_sysctrler_load_response(u32 *buf)
105 struct k3_sysctrler_load_msg *fw;
107 fw = (struct k3_sysctrler_load_msg *)buf;
109 /* Check for proper response ID */
110 if (fw->hdr.cmd_id != K3_MSG_M3_TO_R5_CERT_RESULT) {
111 dev_err(dev, "%s: Command expected 0x%x, but received 0x%x\n",
112 __func__, K3_MSG_M3_TO_R5_CERT_RESULT, fw->hdr.cmd_id);
116 /* Check for certificate authentication result */
117 if (fw->hdr.flags == K3_FLAGS_MSG_CERT_AUTH_FAIL) {
118 dev_err(dev, "%s: Firmware certificate authentication failed\n",
121 } else if (fw->hdr.flags != K3_FLAGS_MSG_CERT_AUTH_PASS) {
122 dev_err(dev, "%s: Firmware Load response Invalid %d\n",
123 __func__, fw->hdr.flags);
127 debug("%s: Firmware authentication passed\n", __func__);
132 static int k3_sysctrler_boot_notification_response(u32 *buf)
134 struct k3_sysctrler_boot_notification_msg *boot;
136 boot = (struct k3_sysctrler_boot_notification_msg *)buf;
138 /* ToDo: Verify checksum */
140 /* Check for proper response ID */
141 if (boot->hdr.cmd_id != K3_MSG_M3_TO_R5_BOOT_NOTIFICATION) {
142 dev_err(dev, "%s: Command expected 0x%x, but received 0x%x\n",
143 __func__, K3_MSG_M3_TO_R5_BOOT_NOTIFICATION,
148 debug("%s: Boot notification received\n", __func__);
154 * k3_sysctrler_load() - Loadup the K3 remote processor
155 * @dev: corresponding K3 remote processor device
156 * @addr: Address in memory where image binary is stored
157 * @size: Size in bytes of the image binary
159 * Return: 0 if all goes good, else appropriate error message.
161 static int k3_sysctrler_load(struct udevice *dev, ulong addr, ulong size)
163 struct k3_sysctrler_privdata *priv = dev_get_priv(dev);
164 struct k3_sysctrler_load_msg firmware;
165 struct k3_sec_proxy_msg msg;
168 debug("%s: Loading binary from 0x%08lX, size 0x%08lX\n",
169 __func__, addr, size);
171 memset(&firmware, 0, sizeof(firmware));
172 memset(&msg, 0, sizeof(msg));
174 /* Setup the message */
175 k3_sysctrler_load_msg_setup(&firmware, priv, addr, size);
176 msg.len = sizeof(firmware);
177 msg.buf = (u32 *)&firmware;
179 /* Send the message */
180 ret = mbox_send(&priv->chan_tx, &msg);
182 dev_err(dev, "%s: Firmware Loading failed. ret = %d\n",
187 /* Receive the response */
188 ret = mbox_recv(&priv->chan_rx, &msg, priv->desc->max_rx_timeout_us);
190 dev_err(dev, "%s: Firmware Load response failed. ret = %d\n",
195 /* Process the response */
196 ret = k3_sysctrler_load_response(msg.buf);
200 debug("%s: Firmware Loaded successfully on dev %s\n",
201 __func__, dev->name);
207 * k3_sysctrler_start() - Start the remote processor
208 * Note that while technically the K3 system controller starts up
209 * automatically after its firmware got loaded we still want to
210 * utilize the rproc start operation for other startup-related
212 * @dev: device to operate upon
214 * Return: 0 if all went ok, else return appropriate error
216 static int k3_sysctrler_start(struct udevice *dev)
218 struct k3_sysctrler_privdata *priv = dev_get_priv(dev);
219 struct k3_sec_proxy_msg msg;
222 debug("%s(dev=%p)\n", __func__, dev);
224 /* Receive the boot notification. Note that it is sent only once. */
225 ret = mbox_recv(&priv->chan_rx, &msg, priv->desc->max_rx_timeout_us);
227 dev_err(dev, "%s: Boot Notification response failed. ret = %d\n",
232 /* Process the response */
233 ret = k3_sysctrler_boot_notification_response(msg.buf);
237 debug("%s: Boot notification received successfully on dev %s\n",
238 __func__, dev->name);
243 static const struct dm_rproc_ops k3_sysctrler_ops = {
244 .load = k3_sysctrler_load,
245 .start = k3_sysctrler_start,
249 * k3_of_to_priv() - generate private data from device tree
250 * @dev: corresponding k3 remote processor device
251 * @priv: pointer to driver specific private data
253 * Return: 0 if all goes good, else appropriate error message.
255 static int k3_of_to_priv(struct udevice *dev,
256 struct k3_sysctrler_privdata *priv)
260 ret = mbox_get_by_name(dev, "tx", &priv->chan_tx);
262 dev_err(dev, "%s: Acquiring Tx channel failed. ret = %d\n",
267 ret = mbox_get_by_name(dev, "rx", &priv->chan_rx);
269 dev_err(dev, "%s: Acquiring Rx channel failed. ret = %d\n",
278 * k3_sysctrler_probe() - Basic probe
279 * @dev: corresponding k3 remote processor device
281 * Return: 0 if all goes good, else appropriate error message.
283 static int k3_sysctrler_probe(struct udevice *dev)
285 struct k3_sysctrler_privdata *priv;
288 debug("%s(dev=%p)\n", __func__, dev);
290 priv = dev_get_priv(dev);
292 ret = k3_of_to_priv(dev, priv);
294 dev_err(dev, "%s: Probe failed with error %d\n", __func__, ret);
298 priv->desc = (void *)dev_get_driver_data(dev);
304 static const struct k3_sysctrler_desc k3_sysctrler_am654_desc = {
305 .host_id = 4, /* HOST_ID_R5_1 */
306 .max_rx_timeout_us = 800000,
310 static const struct udevice_id k3_sysctrler_ids[] = {
312 .compatible = "ti,am654-system-controller",
313 .data = (ulong)&k3_sysctrler_am654_desc,
318 U_BOOT_DRIVER(k3_sysctrler) = {
319 .name = "k3_system_controller",
320 .of_match = k3_sysctrler_ids,
321 .id = UCLASS_REMOTEPROC,
322 .ops = &k3_sysctrler_ops,
323 .probe = k3_sysctrler_probe,
324 .priv_auto_alloc_size = sizeof(struct k3_sysctrler_privdata),