1 // SPDX-License-Identifier: GPL-2.0+
3 * Texas Instruments' K3 Secure proxy Driver
5 * Copyright (C) 2017-2018 Texas Instruments Incorporated - http://www.ti.com/
13 #include <dm/device_compat.h>
14 #include <linux/types.h>
15 #include <linux/bitops.h>
16 #include <linux/soc/ti/k3-sec-proxy.h>
18 #include <mailbox-uclass.h>
20 DECLARE_GLOBAL_DATA_PTR;
22 /* SEC PROXY RT THREAD STATUS */
23 #define RT_THREAD_STATUS 0x0
24 #define RT_THREAD_THRESHOLD 0x4
25 #define RT_THREAD_STATUS_ERROR_SHIFT 31
26 #define RT_THREAD_STATUS_ERROR_MASK BIT(31)
27 #define RT_THREAD_STATUS_CUR_CNT_SHIFT 0
28 #define RT_THREAD_STATUS_CUR_CNT_MASK GENMASK(7, 0)
30 /* SEC PROXY SCFG THREAD CTRL */
31 #define SCFG_THREAD_CTRL 0x1000
32 #define SCFG_THREAD_CTRL_DIR_SHIFT 31
33 #define SCFG_THREAD_CTRL_DIR_MASK BIT(31)
35 #define SEC_PROXY_THREAD(base, x) ((base) + (0x1000 * (x)))
36 #define THREAD_IS_RX 1
37 #define THREAD_IS_TX 0
40 * struct k3_sec_proxy_desc - Description of secure proxy integration.
41 * @thread_count: Number of Threads.
42 * @max_msg_size: Message size in bytes.
43 * @data_start_offset: Offset of the First data register of the thread
44 * @data_end_offset: Offset of the Last data register of the thread
45 * @valid_threads: List of Valid threads that the processor can access
46 * @num_valid_threads: Number of valid threads.
48 struct k3_sec_proxy_desc {
51 u16 data_start_offset;
53 const u32 *valid_threads;
54 u32 num_valid_threads;
58 * struct k3_sec_proxy_thread - Description of a secure proxy Thread
60 * @data: Thread Data path region for target
61 * @scfg: Secure Config Region for Thread
62 * @rt: RealTime Region for Thread
63 * @rx_buf: Receive buffer data, max message size.
65 struct k3_sec_proxy_thread {
74 * struct k3_sec_proxy_mbox - Description of a Secure Proxy Instance
75 * @chan: Mailbox Channel
76 * @desc: Description of the SoC integration
77 * @chans: Array for valid thread instances
78 * @target_data: Secure Proxy region for Target Data
79 * @scfg: Secure Proxy Region for Secure configuration.
80 * @rt: Secure proxy Region for Real Time Region.
82 struct k3_sec_proxy_mbox {
83 struct mbox_chan chan;
84 struct k3_sec_proxy_desc *desc;
85 struct k3_sec_proxy_thread *chans;
86 phys_addr_t target_data;
91 static inline u32 sp_readl(void __iomem *addr, unsigned int offset)
93 return readl(addr + offset);
96 static inline void sp_writel(void __iomem *addr, unsigned int offset, u32 data)
98 writel(data, addr + offset);
102 * k3_sec_proxy_of_xlate() - Translation of phandle to channel
103 * @chan: Mailbox channel
104 * @args: Phandle Pointer
106 * Translates the phandle args and fills up the Mailbox channel from client.
107 * Return: 0 if all goes good, else return corresponding error message.
109 static int k3_sec_proxy_of_xlate(struct mbox_chan *chan,
110 struct ofnode_phandle_args *args)
112 struct k3_sec_proxy_mbox *spm = dev_get_priv(chan->dev);
115 debug("%s(chan=%p)\n", __func__, chan);
117 if (args->args_count != 1) {
118 debug("Invaild args_count: %d\n", args->args_count);
123 for (i = 0; i < spm->desc->num_valid_threads; i++)
124 if (spm->chans[i].id == ind) {
126 chan->con_priv = &spm->chans[i];
130 dev_err(chan->dev, "%s: Invalid Thread ID %d\n", __func__, ind);
135 * k3_sec_proxy_request() - Request for mailbox channel
136 * @chan: Channel Pointer
138 static int k3_sec_proxy_request(struct mbox_chan *chan)
140 debug("%s(chan=%p)\n", __func__, chan);
146 * k3_sec_proxy_free() - Free the mailbox channel
147 * @chan: Channel Pointer
149 static int k3_sec_proxy_free(struct mbox_chan *chan)
151 debug("%s(chan=%p)\n", __func__, chan);
157 * k3_sec_proxy_verify_thread() - Verify thread status before
158 * sending/receiving data.
159 * @spt: pointer to secure proxy thread description
160 * @dir: Direction of the thread
162 * Return: 0 if all goes good, else appropriate error message.
164 static inline int k3_sec_proxy_verify_thread(struct k3_sec_proxy_thread *spt,
167 /* Check for any errors already available */
168 if (sp_readl(spt->rt, RT_THREAD_STATUS) &
169 RT_THREAD_STATUS_ERROR_MASK) {
170 printf("%s: Thread %d is corrupted, cannot send data.\n",
175 /* Make sure thread is configured for right direction */
176 if ((sp_readl(spt->scfg, SCFG_THREAD_CTRL)
177 & SCFG_THREAD_CTRL_DIR_MASK) >> SCFG_THREAD_CTRL_DIR_SHIFT != dir) {
179 printf("%s: Trying to receive data on tx Thread %d\n",
182 printf("%s: Trying to send data on rx Thread %d\n",
187 /* Check the message queue before sending/receiving data */
188 if (!(sp_readl(spt->rt, RT_THREAD_STATUS) &
189 RT_THREAD_STATUS_CUR_CNT_MASK))
196 * k3_sec_proxy_send() - Send data via mailbox channel
197 * @chan: Channel Pointer
198 * @data: Pointer to k3_sec_proxy_msg
200 * Return: 0 if all goes good, else appropriate error message.
202 static int k3_sec_proxy_send(struct mbox_chan *chan, const void *data)
204 const struct k3_sec_proxy_msg *msg = (struct k3_sec_proxy_msg *)data;
205 struct k3_sec_proxy_mbox *spm = dev_get_priv(chan->dev);
206 struct k3_sec_proxy_thread *spt = chan->con_priv;
207 int num_words, trail_bytes, ret;
208 void __iomem *data_reg;
211 debug("%s(chan=%p, data=%p)\n", __func__, chan, data);
213 ret = k3_sec_proxy_verify_thread(spt, THREAD_IS_TX);
215 dev_err(dev, "%s: Thread%d verification failed. ret = %d\n",
216 __func__, spt->id, ret);
220 /* Check the message size. */
221 if (msg->len > spm->desc->max_msg_size) {
222 printf("%s: Thread %ld message length %zu > max msg size %d\n",
223 __func__, chan->id, msg->len, spm->desc->max_msg_size);
227 /* Send the message */
228 data_reg = spt->data + spm->desc->data_start_offset;
229 for (num_words = msg->len / sizeof(u32), word_data = (u32 *)msg->buf;
231 num_words--, data_reg += sizeof(u32), word_data++)
232 writel(*word_data, data_reg);
234 trail_bytes = msg->len % sizeof(u32);
236 u32 data_trail = *word_data;
238 /* Ensure all unused data is 0 */
239 data_trail &= 0xFFFFFFFF >> (8 * (sizeof(u32) - trail_bytes));
240 writel(data_trail, data_reg);
245 * 'data_reg' indicates next register to write. If we did not already
246 * write on tx complete reg(last reg), we must do so for transmit
248 if (data_reg <= (spt->data + spm->desc->data_end_offset))
249 sp_writel(spt->data, spm->desc->data_end_offset, 0);
251 debug("%s: Message successfully sent on thread %ld\n",
258 * k3_sec_proxy_recv() - Receive data via mailbox channel
259 * @chan: Channel Pointer
260 * @data: Pointer to k3_sec_proxy_msg
262 * Return: 0 if all goes good, else appropriate error message.
264 static int k3_sec_proxy_recv(struct mbox_chan *chan, void *data)
266 struct k3_sec_proxy_mbox *spm = dev_get_priv(chan->dev);
267 struct k3_sec_proxy_thread *spt = chan->con_priv;
268 struct k3_sec_proxy_msg *msg = data;
269 void __iomem *data_reg;
273 debug("%s(chan=%p, data=%p)\n", __func__, chan, data);
275 ret = k3_sec_proxy_verify_thread(spt, THREAD_IS_RX);
279 msg->len = spm->desc->max_msg_size;
280 msg->buf = spt->rx_buf;
281 data_reg = spt->data + spm->desc->data_start_offset;
282 word_data = spt->rx_buf;
283 for (num_words = spm->desc->max_msg_size / sizeof(u32);
285 num_words--, data_reg += sizeof(u32), word_data++)
286 *word_data = readl(data_reg);
288 debug("%s: Message successfully received from thread %ld\n",
294 struct mbox_ops k3_sec_proxy_mbox_ops = {
295 .of_xlate = k3_sec_proxy_of_xlate,
296 .request = k3_sec_proxy_request,
297 .rfree = k3_sec_proxy_free,
298 .send = k3_sec_proxy_send,
299 .recv = k3_sec_proxy_recv,
303 * k3_sec_proxy_of_to_priv() - generate private data from device tree
304 * @dev: corresponding k3 secure proxy device
305 * @spm: pointer to driver specific private data
307 * Return: 0 if all went ok, else corresponding error message.
309 static int k3_sec_proxy_of_to_priv(struct udevice *dev,
310 struct k3_sec_proxy_mbox *spm)
312 const void *blob = gd->fdt_blob;
315 debug("'%s' no dt?\n", dev->name);
319 spm->target_data = devfdt_get_addr_name(dev, "target_data");
320 if (spm->target_data == FDT_ADDR_T_NONE) {
321 dev_err(dev, "No reg property for target data base\n");
325 spm->scfg = devfdt_get_addr_name(dev, "scfg");
326 if (spm->rt == FDT_ADDR_T_NONE) {
327 dev_err(dev, "No reg property for Secure Cfg base\n");
331 spm->rt = devfdt_get_addr_name(dev, "rt");
332 if (spm->rt == FDT_ADDR_T_NONE) {
333 dev_err(dev, "No reg property for Real Time Cfg base\n");
341 * k3_sec_proxy_thread_setup - Initialize the parameters for all valid threads
342 * @spm: Mailbox instance for which threads needs to be initialized
344 * Return: 0 if all went ok, else corresponding error message
346 static int k3_sec_proxy_thread_setup(struct k3_sec_proxy_mbox *spm)
348 struct k3_sec_proxy_thread *spt;
351 for (i = 0; i < spm->desc->num_valid_threads; i++) {
352 spt = &spm->chans[i];
353 ind = spm->desc->valid_threads[i];
355 spt->data = (void *)SEC_PROXY_THREAD(spm->target_data, ind);
356 spt->scfg = (void *)SEC_PROXY_THREAD(spm->scfg, ind);
357 spt->rt = (void *)SEC_PROXY_THREAD(spm->rt, ind);
358 spt->rx_buf = calloc(1, spm->desc->max_msg_size);
367 * k3_sec_proxy_probe() - Basic probe
368 * @dev: corresponding mailbox device
370 * Return: 0 if all went ok, else corresponding error message
372 static int k3_sec_proxy_probe(struct udevice *dev)
374 struct k3_sec_proxy_mbox *spm = dev_get_priv(dev);
377 debug("%s(dev=%p)\n", __func__, dev);
379 ret = k3_sec_proxy_of_to_priv(dev, spm);
383 spm->desc = (void *)dev_get_driver_data(dev);
384 spm->chans = calloc(spm->desc->num_valid_threads,
385 sizeof(struct k3_sec_proxy_thread));
389 ret = k3_sec_proxy_thread_setup(spm);
391 debug("%s: secure proxy thread setup failed\n", __func__);
398 static int k3_sec_proxy_remove(struct udevice *dev)
400 struct k3_sec_proxy_mbox *spm = dev_get_priv(dev);
402 debug("%s(dev=%p)\n", __func__, dev);
410 * Thread ID #4: ROM request
411 * Thread ID #5: ROM response, SYSFW notify
412 * Thread ID #6: SYSFW request response
413 * Thread ID #7: SYSFW request high priority
414 * Thread ID #8: SYSFW request low priority
415 * Thread ID #9: SYSFW notify response
417 static const u32 am6x_valid_threads[] = { 4, 5, 6, 7, 8, 9, 11, 13 };
419 static const struct k3_sec_proxy_desc am654_desc = {
422 .data_start_offset = 0x4,
423 .data_end_offset = 0x3C,
424 .valid_threads = am6x_valid_threads,
425 .num_valid_threads = ARRAY_SIZE(am6x_valid_threads),
428 static const struct udevice_id k3_sec_proxy_ids[] = {
429 { .compatible = "ti,am654-secure-proxy", .data = (ulong)&am654_desc},
433 U_BOOT_DRIVER(k3_sec_proxy) = {
434 .name = "k3-secure-proxy",
435 .id = UCLASS_MAILBOX,
436 .of_match = k3_sec_proxy_ids,
437 .probe = k3_sec_proxy_probe,
438 .remove = k3_sec_proxy_remove,
439 .priv_auto_alloc_size = sizeof(struct k3_sec_proxy_mbox),
440 .ops = &k3_sec_proxy_mbox_ops,