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 <linux/soc/ti/k3-sec-proxy.h>
16 #define K3_MSG_R5_TO_M3_M3FW 0x8105
17 #define K3_MSG_M3_TO_R5_CERT_RESULT 0x8805
18 #define K3_MSG_M3_TO_R5_BOOT_NOTIFICATION 0x000A
20 #define K3_FLAGS_MSG_CERT_AUTH_PASS 0x555555
21 #define K3_FLAGS_MSG_CERT_AUTH_FAIL 0xffffff
24 * struct k3_sysctrler_msg_hdr - Generic Header for Messages and responses.
25 * @cmd_id: Message ID. One of K3_MSG_*
26 * @host_id: Host ID of the message
27 * @seq_ne: Message identifier indicating a transfer sequence.
28 * @flags: Flags for the message.
30 struct k3_sysctrler_msg_hdr {
38 * struct k3_sysctrler_load_msg - Message format for Firmware loading
39 * @hdr: Generic message hdr
40 * @buffer_address: Address at which firmware is located.
41 * @buffer_size: Size of the firmware.
43 struct k3_sysctrler_load_msg {
44 struct k3_sysctrler_msg_hdr hdr;
50 * struct k3_sysctrler_boot_notification_msg - Message format for boot
52 * @checksum: Checksum for the entire message
53 * @reserved: Reserved for future use.
54 * @hdr: Generic message hdr
56 struct k3_sysctrler_boot_notification_msg {
59 struct k3_sysctrler_msg_hdr hdr;
63 * struct k3_sysctrler_desc - Description of SoC integration.
64 * @host_id: Host identifier representing the compute entity
65 * @max_rx_timeout_ms: Timeout for communication with SoC (in Milliseconds)
66 * @max_msg_size: Maximum size of data per message that can be handled.
68 struct k3_sysctrler_desc {
70 int max_rx_timeout_us;
75 * struct k3_sysctrler_privdata - Structure representing System Controller data.
76 * @chan_tx: Transmit mailbox channel
77 * @chan_rx: Receive mailbox channel
78 * @desc: SoC description for this instance
79 * @seq_nr: Counter for number of messages sent.
81 struct k3_sysctrler_privdata {
82 struct mbox_chan chan_tx;
83 struct mbox_chan chan_rx;
84 struct k3_sysctrler_desc *desc;
89 void k3_sysctrler_load_msg_setup(struct k3_sysctrler_load_msg *fw,
90 struct k3_sysctrler_privdata *priv,
91 ulong addr, ulong size)
93 fw->hdr.cmd_id = K3_MSG_R5_TO_M3_M3FW;
94 fw->hdr.host_id = priv->desc->host_id;
95 fw->hdr.seq_nr = priv->seq_nr++;
97 fw->buffer_address = addr;
98 fw->buffer_size = size;
101 static int k3_sysctrler_load_response(u32 *buf)
103 struct k3_sysctrler_load_msg *fw;
105 fw = (struct k3_sysctrler_load_msg *)buf;
107 /* Check for proper response ID */
108 if (fw->hdr.cmd_id != K3_MSG_M3_TO_R5_CERT_RESULT) {
109 dev_err(dev, "%s: Command expected 0x%x, but received 0x%x\n",
110 __func__, K3_MSG_M3_TO_R5_CERT_RESULT, fw->hdr.cmd_id);
114 /* Check for certificate authentication result */
115 if (fw->hdr.flags == K3_FLAGS_MSG_CERT_AUTH_FAIL) {
116 dev_err(dev, "%s: Firmware certificate authentication failed\n",
119 } else if (fw->hdr.flags != K3_FLAGS_MSG_CERT_AUTH_PASS) {
120 dev_err(dev, "%s: Firmware Load response Invalid %d\n",
121 __func__, fw->hdr.flags);
125 debug("%s: Firmware authentication passed\n", __func__);
130 static int k3_sysctrler_boot_notification_response(u32 *buf)
132 struct k3_sysctrler_boot_notification_msg *boot;
134 boot = (struct k3_sysctrler_boot_notification_msg *)buf;
136 /* ToDo: Verify checksum */
138 /* Check for proper response ID */
139 if (boot->hdr.cmd_id != K3_MSG_M3_TO_R5_BOOT_NOTIFICATION) {
140 dev_err(dev, "%s: Command expected 0x%x, but received 0x%x\n",
141 __func__, K3_MSG_M3_TO_R5_BOOT_NOTIFICATION,
146 debug("%s: Boot notification received\n", __func__);
152 * k3_sysctrler_load() - Loadup the K3 remote processor
153 * @dev: corresponding K3 remote processor device
154 * @addr: Address in memory where image binary is stored
155 * @size: Size in bytes of the image binary
157 * Return: 0 if all goes good, else appropriate error message.
159 static int k3_sysctrler_load(struct udevice *dev, ulong addr, ulong size)
161 struct k3_sysctrler_privdata *priv = dev_get_priv(dev);
162 struct k3_sysctrler_load_msg firmware;
163 struct k3_sec_proxy_msg msg;
166 debug("%s: Loading binary from 0x%08lX, size 0x%08lX\n",
167 __func__, addr, size);
169 memset(&firmware, 0, sizeof(firmware));
170 memset(&msg, 0, sizeof(msg));
172 /* Setup the message */
173 k3_sysctrler_load_msg_setup(&firmware, priv, addr, size);
174 msg.len = sizeof(firmware);
175 msg.buf = (u32 *)&firmware;
177 /* Send the message */
178 ret = mbox_send(&priv->chan_tx, &msg);
180 dev_err(dev, "%s: Firmware Loading failed. ret = %d\n",
185 /* Receive the response */
186 ret = mbox_recv(&priv->chan_rx, &msg, priv->desc->max_rx_timeout_us);
188 dev_err(dev, "%s: Firmware Load response failed. ret = %d\n",
193 /* Process the response */
194 ret = k3_sysctrler_load_response(msg.buf);
198 debug("%s: Firmware Loaded successfully on dev %s\n",
199 __func__, dev->name);
205 * k3_sysctrler_start() - Start the remote processor
206 * Note that while technically the K3 system controller starts up
207 * automatically after its firmware got loaded we still want to
208 * utilize the rproc start operation for other startup-related
210 * @dev: device to operate upon
212 * Return: 0 if all went ok, else return appropriate error
214 static int k3_sysctrler_start(struct udevice *dev)
216 struct k3_sysctrler_privdata *priv = dev_get_priv(dev);
217 struct k3_sec_proxy_msg msg;
220 debug("%s(dev=%p)\n", __func__, dev);
222 /* Receive the boot notification. Note that it is sent only once. */
223 ret = mbox_recv(&priv->chan_rx, &msg, priv->desc->max_rx_timeout_us);
225 dev_err(dev, "%s: Boot Notification response failed. ret = %d\n",
230 /* Process the response */
231 ret = k3_sysctrler_boot_notification_response(msg.buf);
235 debug("%s: Boot notification received successfully on dev %s\n",
236 __func__, dev->name);
241 static const struct dm_rproc_ops k3_sysctrler_ops = {
242 .load = k3_sysctrler_load,
243 .start = k3_sysctrler_start,
247 * k3_of_to_priv() - generate private data from device tree
248 * @dev: corresponding k3 remote processor device
249 * @priv: pointer to driver specific private data
251 * Return: 0 if all goes good, else appropriate error message.
253 static int k3_of_to_priv(struct udevice *dev,
254 struct k3_sysctrler_privdata *priv)
258 ret = mbox_get_by_name(dev, "tx", &priv->chan_tx);
260 dev_err(dev, "%s: Acquiring Tx channel failed. ret = %d\n",
265 ret = mbox_get_by_name(dev, "rx", &priv->chan_rx);
267 dev_err(dev, "%s: Acquiring Rx channel failed. ret = %d\n",
276 * k3_sysctrler_probe() - Basic probe
277 * @dev: corresponding k3 remote processor device
279 * Return: 0 if all goes good, else appropriate error message.
281 static int k3_sysctrler_probe(struct udevice *dev)
283 struct k3_sysctrler_privdata *priv;
286 debug("%s(dev=%p)\n", __func__, dev);
288 priv = dev_get_priv(dev);
290 ret = k3_of_to_priv(dev, priv);
292 dev_err(dev, "%s: Probe failed with error %d\n", __func__, ret);
296 priv->desc = (void *)dev_get_driver_data(dev);
302 static const struct k3_sysctrler_desc k3_sysctrler_am654_desc = {
303 .host_id = 4, /* HOST_ID_R5_1 */
304 .max_rx_timeout_us = 800000,
308 static const struct udevice_id k3_sysctrler_ids[] = {
310 .compatible = "ti,am654-system-controller",
311 .data = (ulong)&k3_sysctrler_am654_desc,
316 U_BOOT_DRIVER(k3_sysctrler) = {
317 .name = "k3_system_controller",
318 .of_match = k3_sysctrler_ids,
319 .id = UCLASS_REMOTEPROC,
320 .ops = &k3_sysctrler_ops,
321 .probe = k3_sysctrler_probe,
322 .priv_auto_alloc_size = sizeof(struct k3_sysctrler_privdata),