blob: b34191753b1becccc95dc9fd0e009fc2f9025757 [file] [log] [blame]
Peng Fanad9d40a2018-10-18 14:28:12 +02001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright 2018 NXP
4 *
5 * Peng Fan <peng.fan@nxp.com>
6 */
7
8#include <common.h>
Simon Glassdb41d652019-12-28 10:45:07 -07009#include <hang.h>
Peng Fanad9d40a2018-10-18 14:28:12 +020010#include <asm/io.h>
11#include <dm.h>
12#include <asm/arch/sci/sci.h>
13#include <misc.h>
14
15DECLARE_GLOBAL_DATA_PTR;
16
Peng Fan4d817552019-09-23 10:12:31 +000017#define B2U8(X) (((X) != SC_FALSE) ? (u8)(0x01U) : (u8)(0x00U))
18
Peng Fanad9d40a2018-10-18 14:28:12 +020019/* CLK and PM */
20int sc_pm_set_clock_rate(sc_ipc_t ipc, sc_rsrc_t resource, sc_pm_clk_t clk,
21 sc_pm_clock_rate_t *rate)
22{
23 struct udevice *dev = gd->arch.scu_dev;
24 int size = sizeof(struct sc_rpc_msg_s);
25 struct sc_rpc_msg_s msg;
26 int ret;
27
28 RPC_VER(&msg) = SC_RPC_VERSION;
29 RPC_SVC(&msg) = (u8)SC_RPC_SVC_PM;
30 RPC_FUNC(&msg) = (u8)PM_FUNC_SET_CLOCK_RATE;
31 RPC_U32(&msg, 0U) = *(u32 *)rate;
32 RPC_U16(&msg, 4U) = (u16)resource;
33 RPC_U8(&msg, 6U) = (u8)clk;
34 RPC_SIZE(&msg) = 3U;
35
36 ret = misc_call(dev, SC_FALSE, &msg, size, &msg, size);
37 if (ret)
38 printf("%s: rate:%u resource:%u: clk:%u res:%d\n",
39 __func__, *rate, resource, clk, RPC_R8(&msg));
40
41 *rate = RPC_U32(&msg, 0U);
42
43 return ret;
44}
45
46int sc_pm_get_clock_rate(sc_ipc_t ipc, sc_rsrc_t resource, sc_pm_clk_t clk,
47 sc_pm_clock_rate_t *rate)
48{
49 struct udevice *dev = gd->arch.scu_dev;
50 int size = sizeof(struct sc_rpc_msg_s);
51 struct sc_rpc_msg_s msg;
52 int ret;
53
54 RPC_VER(&msg) = SC_RPC_VERSION;
55 RPC_SVC(&msg) = (u8)SC_RPC_SVC_PM;
56 RPC_FUNC(&msg) = (u8)PM_FUNC_GET_CLOCK_RATE;
57 RPC_U16(&msg, 0U) = (u16)resource;
58 RPC_U8(&msg, 2U) = (u8)clk;
59 RPC_SIZE(&msg) = 2U;
60
61 ret = misc_call(dev, SC_FALSE, &msg, size, &msg, size);
62 if (ret) {
63 printf("%s: resource:%d clk:%d: res:%d\n",
64 __func__, resource, clk, RPC_R8(&msg));
65 return ret;
66 }
67
68 if (rate)
69 *rate = RPC_U32(&msg, 0U);
70
71 return 0;
72}
73
74int sc_pm_clock_enable(sc_ipc_t ipc, sc_rsrc_t resource, sc_pm_clk_t clk,
75 sc_bool_t enable, sc_bool_t autog)
76{
77 struct udevice *dev = gd->arch.scu_dev;
78 int size = sizeof(struct sc_rpc_msg_s);
79 struct sc_rpc_msg_s msg;
80 int ret;
81
82 RPC_VER(&msg) = SC_RPC_VERSION;
83 RPC_SVC(&msg) = (u8)SC_RPC_SVC_PM;
84 RPC_FUNC(&msg) = (u8)PM_FUNC_CLOCK_ENABLE;
85 RPC_U16(&msg, 0U) = (u16)resource;
86 RPC_U8(&msg, 2U) = (u8)clk;
87 RPC_U8(&msg, 3U) = (u8)enable;
88 RPC_U8(&msg, 4U) = (u8)autog;
89 RPC_SIZE(&msg) = 3U;
90
91 ret = misc_call(dev, SC_FALSE, &msg, size, &msg, size);
92 if (ret)
93 printf("%s: resource:%d clk:%d: enable:%d autog: %d, res:%d\n",
94 __func__, resource, clk, enable, autog, RPC_R8(&msg));
95
96 return ret;
97}
98
Peng Fan4d817552019-09-23 10:12:31 +000099int sc_pm_set_clock_parent(sc_ipc_t ipc, sc_rsrc_t resource,
100 sc_pm_clk_t clk, sc_pm_clk_parent_t parent)
101{
102 struct udevice *dev = gd->arch.scu_dev;
103 int size = sizeof(struct sc_rpc_msg_s);
104 struct sc_rpc_msg_s msg;
105 int ret;
106
107 RPC_VER(&msg) = SC_RPC_VERSION;
108 RPC_SVC(&msg) = (u8)SC_RPC_SVC_PM;
109 RPC_FUNC(&msg) = (u8)PM_FUNC_SET_CLOCK_PARENT;
110 RPC_U16(&msg, 0U) = (u16)resource;
111 RPC_U8(&msg, 2U) = (u8)clk;
112 RPC_U8(&msg, 3U) = (u8)parent;
113 RPC_SIZE(&msg) = 2U;
114
115 ret = misc_call(dev, SC_FALSE, &msg, size, &msg, size);
116 if (ret)
117 printf("%s: resource:%d clk:%d: parent clk: %d, res:%d\n",
118 __func__, resource, clk, parent, RPC_R8(&msg));
119
120 return ret;
121}
122
Peng Fanad9d40a2018-10-18 14:28:12 +0200123int sc_pm_set_resource_power_mode(sc_ipc_t ipc, sc_rsrc_t resource,
124 sc_pm_power_mode_t mode)
125{
126 struct udevice *dev = gd->arch.scu_dev;
127 int size = sizeof(struct sc_rpc_msg_s);
128 struct sc_rpc_msg_s msg;
129 int ret;
130
131 if (!dev)
132 hang();
133
134 RPC_VER(&msg) = SC_RPC_VERSION;
135 RPC_SVC(&msg) = (u8)SC_RPC_SVC_PM;
136 RPC_FUNC(&msg) = (u8)PM_FUNC_SET_RESOURCE_POWER_MODE;
137 RPC_U16(&msg, 0U) = (u16)resource;
138 RPC_U8(&msg, 2U) = (u8)mode;
139 RPC_SIZE(&msg) = 2U;
140
141 ret = misc_call(dev, SC_FALSE, &msg, size, &msg, size);
142 if (ret)
143 printf("%s: resource:%d mode:%d: res:%d\n",
144 __func__, resource, mode, RPC_R8(&msg));
145
146 return ret;
147}
148
Peng Fan8cacd782019-08-26 08:12:16 +0000149sc_bool_t sc_pm_is_partition_started(sc_ipc_t ipc, sc_rm_pt_t pt)
150{
151 struct udevice *dev = gd->arch.scu_dev;
152 int size = sizeof(struct sc_rpc_msg_s);
153 struct sc_rpc_msg_s msg;
154 int ret;
155 u8 result;
156
157 RPC_VER(&msg) = SC_RPC_VERSION;
158 RPC_SVC(&msg) = (u8)(SC_RPC_SVC_PM);
159 RPC_FUNC(&msg) = (u8)(PM_FUNC_IS_PARTITION_STARTED);
160 RPC_U8(&msg, 0U) = (u8)(pt);
161 RPC_SIZE(&msg) = 2U;
162
163 ret = misc_call(dev, SC_FALSE, &msg, size, &msg, size);
164
165 result = RPC_R8(&msg);
166 if (result != 0 && result != 1) {
167 printf("%s: partition:%d res:%d\n",
168 __func__, pt, RPC_R8(&msg));
169 if (ret)
170 printf("%s: partition:%d res:%d\n", __func__, pt,
171 RPC_R8(&msg));
172 }
173 return !!result;
174}
175
Peng Fanad9d40a2018-10-18 14:28:12 +0200176/* PAD */
177int sc_pad_set(sc_ipc_t ipc, sc_pad_t pad, u32 val)
178{
179 struct udevice *dev = gd->arch.scu_dev;
180 int size = sizeof(struct sc_rpc_msg_s);
181 struct sc_rpc_msg_s msg;
182 int ret;
183
184 if (!dev)
185 hang();
186
187 RPC_VER(&msg) = SC_RPC_VERSION;
188 RPC_SVC(&msg) = (u8)SC_RPC_SVC_PAD;
189 RPC_FUNC(&msg) = (u8)PAD_FUNC_SET;
190 RPC_U32(&msg, 0U) = (u32)val;
191 RPC_U16(&msg, 4U) = (u16)pad;
192 RPC_SIZE(&msg) = 3U;
193
194 ret = misc_call(dev, SC_FALSE, &msg, size, &msg, size);
195 if (ret)
196 printf("%s: val:%d pad:%d: res:%d\n",
197 __func__, val, pad, RPC_R8(&msg));
198
199 return ret;
200}
201
202/* MISC */
Peng Fan4d817552019-09-23 10:12:31 +0000203int sc_misc_set_control(sc_ipc_t ipc, sc_rsrc_t resource,
204 sc_ctrl_t ctrl, u32 val)
205{
206 struct udevice *dev = gd->arch.scu_dev;
207 int size = sizeof(struct sc_rpc_msg_s);
208 struct sc_rpc_msg_s msg;
209 int ret;
210
211 if (!dev)
212 hang();
213
214 RPC_VER(&msg) = SC_RPC_VERSION;
215 RPC_SVC(&msg) = (u8)SC_RPC_SVC_MISC;
216 RPC_FUNC(&msg) = (u8)MISC_FUNC_SET_CONTROL;
217 RPC_U32(&msg, 0U) = (u32)ctrl;
218 RPC_U32(&msg, 4U) = (u32)val;
219 RPC_U16(&msg, 8U) = (u16)resource;
220 RPC_SIZE(&msg) = 4U;
221
222 ret = misc_call(dev, SC_FALSE, &msg, size, &msg, size);
223 if (ret)
224 printf("%s: ctrl:%d resource:%d: res:%d\n",
225 __func__, ctrl, resource, RPC_R8(&msg));
226
227 return ret;
228}
229
Peng Fanad9d40a2018-10-18 14:28:12 +0200230int sc_misc_get_control(sc_ipc_t ipc, sc_rsrc_t resource, sc_ctrl_t ctrl,
231 u32 *val)
232{
233 struct udevice *dev = gd->arch.scu_dev;
234 int size = sizeof(struct sc_rpc_msg_s);
235 struct sc_rpc_msg_s msg;
236 int ret;
237
238 if (!dev)
239 hang();
240
241 RPC_VER(&msg) = SC_RPC_VERSION;
242 RPC_SVC(&msg) = (u8)SC_RPC_SVC_MISC;
243 RPC_FUNC(&msg) = (u8)MISC_FUNC_GET_CONTROL;
244 RPC_U32(&msg, 0U) = (u32)ctrl;
245 RPC_U16(&msg, 4U) = (u16)resource;
246 RPC_SIZE(&msg) = 3U;
247
248 ret = misc_call(dev, SC_FALSE, &msg, size, &msg, size);
249 if (ret)
250 printf("%s: ctrl:%d resource:%d: res:%d\n",
251 __func__, ctrl, resource, RPC_R8(&msg));
252
Peng Fanecab65e2018-12-15 12:19:49 +0000253 if (val)
Peng Fanad9d40a2018-10-18 14:28:12 +0200254 *val = RPC_U32(&msg, 0U);
255
256 return ret;
257}
258
Peng Fan4d817552019-09-23 10:12:31 +0000259int sc_rm_set_master_sid(sc_ipc_t ipc, sc_rsrc_t resource, sc_rm_sid_t sid)
260{
261 struct udevice *dev = gd->arch.scu_dev;
262 struct sc_rpc_msg_s msg;
263 int size = sizeof(struct sc_rpc_msg_s);
264 int ret;
265
266 RPC_VER(&msg) = SC_RPC_VERSION;
267 RPC_SVC(&msg) = (u8)SC_RPC_SVC_RM;
268 RPC_FUNC(&msg) = (u8)RM_FUNC_SET_MASTER_SID;
269 RPC_U16(&msg, 0U) = (u16)resource;
270 RPC_U16(&msg, 2U) = (u16)sid;
271 RPC_SIZE(&msg) = 2U;
272
273 ret = misc_call(dev, SC_FALSE, &msg, size, &msg, size);
274 if (ret)
275 printf("%s: resource:%d sid:%d: res:%d\n",
276 __func__, resource, sid, RPC_R8(&msg));
277
278 return ret;
279}
280
Peng Fanad9d40a2018-10-18 14:28:12 +0200281void sc_misc_get_boot_dev(sc_ipc_t ipc, sc_rsrc_t *boot_dev)
282{
283 struct udevice *dev = gd->arch.scu_dev;
284 int size = sizeof(struct sc_rpc_msg_s);
285 struct sc_rpc_msg_s msg;
286 int ret;
287
288 if (!dev)
289 hang();
290
291 RPC_VER(&msg) = SC_RPC_VERSION;
292 RPC_SVC(&msg) = (u8)SC_RPC_SVC_MISC;
293 RPC_FUNC(&msg) = (u8)MISC_FUNC_GET_BOOT_DEV;
294 RPC_SIZE(&msg) = 1U;
295
296 ret = misc_call(dev, SC_FALSE, &msg, size, &msg, size);
297 if (ret)
298 printf("%s: res:%d\n", __func__, RPC_R8(&msg));
299
Peng Fanecab65e2018-12-15 12:19:49 +0000300 if (boot_dev)
Peng Fanad9d40a2018-10-18 14:28:12 +0200301 *boot_dev = RPC_U16(&msg, 0U);
302}
303
304void sc_misc_boot_status(sc_ipc_t ipc, sc_misc_boot_status_t status)
305{
306 struct udevice *dev = gd->arch.scu_dev;
307 int size = sizeof(struct sc_rpc_msg_s);
308 struct sc_rpc_msg_s msg;
309 int ret;
310
311 if (!dev)
312 hang();
313
314 RPC_VER(&msg) = SC_RPC_VERSION;
315 RPC_SVC(&msg) = (u8)SC_RPC_SVC_MISC;
316 RPC_FUNC(&msg) = (u8)MISC_FUNC_BOOT_STATUS;
317 RPC_U8(&msg, 0U) = (u8)status;
318 RPC_SIZE(&msg) = 2U;
319
320 ret = misc_call(dev, SC_TRUE, &msg, size, &msg, size);
321 if (ret)
322 printf("%s: status:%d res:%d\n",
323 __func__, status, RPC_R8(&msg));
324}
325
326void sc_misc_build_info(sc_ipc_t ipc, u32 *build, u32 *commit)
327{
328 struct udevice *dev = gd->arch.scu_dev;
329 int size = sizeof(struct sc_rpc_msg_s);
330 struct sc_rpc_msg_s msg;
331 int ret;
332
333 if (!dev)
334 hang();
335
336 RPC_VER(&msg) = SC_RPC_VERSION;
337 RPC_SVC(&msg) = SC_RPC_SVC_MISC;
338 RPC_FUNC(&msg) = MISC_FUNC_BUILD_INFO;
339 RPC_SIZE(&msg) = 1;
340
341 ret = misc_call(dev, SC_FALSE, &msg, size, &msg, size);
342 if (ret < 0) {
343 printf("%s: err: %d\n", __func__, ret);
344 return;
345 }
346
347 if (build)
348 *build = RPC_U32(&msg, 0);
349 if (commit)
350 *commit = RPC_U32(&msg, 4);
351}
352
353int sc_misc_otp_fuse_read(sc_ipc_t ipc, u32 word, u32 *val)
354{
355 struct udevice *dev = gd->arch.scu_dev;
356 int size = sizeof(struct sc_rpc_msg_s);
357 struct sc_rpc_msg_s msg;
358 int ret;
359
360 if (!dev)
361 hang();
362
363 RPC_VER(&msg) = SC_RPC_VERSION;
364 RPC_SVC(&msg) = SC_RPC_SVC_MISC;
365 RPC_FUNC(&msg) = MISC_FUNC_OTP_FUSE_READ;
366 RPC_U32(&msg, 0) = word;
367 RPC_SIZE(&msg) = 2;
368
369 ret = misc_call(dev, SC_FALSE, &msg, size, &msg, size);
370 if (ret < 0)
371 return ret;
372
373 if (val)
374 *val = RPC_U32(&msg, 0U);
375
376 return 0;
377}
378
Peng Fan7752a0f2019-05-05 13:23:51 +0000379int sc_misc_get_temp(sc_ipc_t ipc, sc_rsrc_t resource, sc_misc_temp_t temp,
380 s16 *celsius, s8 *tenths)
381{
382 struct udevice *dev = gd->arch.scu_dev;
383 int size = sizeof(struct sc_rpc_msg_s);
384 struct sc_rpc_msg_s msg;
385 int ret;
386
387 RPC_VER(&msg) = SC_RPC_VERSION;
388 RPC_SVC(&msg) = (u8)SC_RPC_SVC_MISC;
389 RPC_FUNC(&msg) = (u8)MISC_FUNC_GET_TEMP;
390 RPC_U16(&msg, 0U) = (u16)resource;
391 RPC_U8(&msg, 2U) = (u8)temp;
392 RPC_SIZE(&msg) = 2U;
393
394 ret = misc_call(dev, SC_FALSE, &msg, size, &msg, size);
395 if (ret < 0)
396 return ret;
397
398 if (celsius)
399 *celsius = RPC_I16(&msg, 0U);
400
401 if (tenths)
402 *tenths = RPC_I8(&msg, 2U);
403
404 return 0;
405}
406
Peng Fanad9d40a2018-10-18 14:28:12 +0200407/* RM */
408sc_bool_t sc_rm_is_memreg_owned(sc_ipc_t ipc, sc_rm_mr_t mr)
409{
410 struct udevice *dev = gd->arch.scu_dev;
411 int size = sizeof(struct sc_rpc_msg_s);
412 struct sc_rpc_msg_s msg;
413 int ret;
414 sc_err_t result;
415
416 if (!dev)
417 hang();
418
419 RPC_VER(&msg) = SC_RPC_VERSION;
420 RPC_SVC(&msg) = (u8)SC_RPC_SVC_RM;
421 RPC_FUNC(&msg) = (u8)RM_FUNC_IS_MEMREG_OWNED;
422 RPC_U8(&msg, 0U) = (u8)mr;
423 RPC_SIZE(&msg) = 2U;
424
425 ret = misc_call(dev, SC_FALSE, &msg, size, &msg, size);
426 result = RPC_R8(&msg);
427
428 if (result != 0 && result != 1) {
429 printf("%s: mr:%d res:%d\n", __func__, mr, RPC_R8(&msg));
430 if (ret)
431 printf("%s: mr:%d res:%d\n", __func__, mr,
432 RPC_R8(&msg));
433 }
434
435 return (sc_bool_t)result;
436}
437
Peng Fan4d817552019-09-23 10:12:31 +0000438int sc_rm_find_memreg(sc_ipc_t ipc, sc_rm_mr_t *mr, sc_faddr_t addr_start,
439 sc_faddr_t addr_end)
Peng Fan26864472019-08-26 08:12:03 +0000440{
441 struct udevice *dev = gd->arch.scu_dev;
Peng Fan26864472019-08-26 08:12:03 +0000442 int size = sizeof(struct sc_rpc_msg_s);
Peng Fan4d817552019-09-23 10:12:31 +0000443 struct sc_rpc_msg_s msg;
Peng Fan26864472019-08-26 08:12:03 +0000444 int ret;
445
Peng Fan4d817552019-09-23 10:12:31 +0000446 if (!dev)
447 hang();
448
Peng Fan26864472019-08-26 08:12:03 +0000449 RPC_VER(&msg) = SC_RPC_VERSION;
Peng Fan4d817552019-09-23 10:12:31 +0000450 RPC_SVC(&msg) = (u8)(SC_RPC_SVC_RM);
451 RPC_FUNC(&msg) = (u8)(RM_FUNC_FIND_MEMREG);
452 RPC_U32(&msg, 0U) = (u32)(addr_start >> 32ULL);
453 RPC_U32(&msg, 4U) = (u32)(addr_start);
454 RPC_U32(&msg, 8U) = (u32)(addr_end >> 32ULL);
455 RPC_U32(&msg, 12U) = (u32)(addr_end);
456 RPC_SIZE(&msg) = 5U;
Peng Fan26864472019-08-26 08:12:03 +0000457
458 ret = misc_call(dev, SC_FALSE, &msg, size, &msg, size);
459 if (ret)
Peng Fan4d817552019-09-23 10:12:31 +0000460 printf("%s: start:0x%llx, end:0x%llx res:%d\n", __func__, addr_start, addr_end, RPC_R8(&msg));
461
462 if (mr)
463 *mr = RPC_U8(&msg, 0U);
464
465 return ret;
466}
467
468int sc_rm_set_memreg_permissions(sc_ipc_t ipc, sc_rm_mr_t mr,
469 sc_rm_pt_t pt, sc_rm_perm_t perm)
470{
471 struct udevice *dev = gd->arch.scu_dev;
472 int size = sizeof(struct sc_rpc_msg_s);
473 struct sc_rpc_msg_s msg;
474 int ret;
475
476 if (!dev)
477 hang();
478
479 RPC_VER(&msg) = SC_RPC_VERSION;
480 RPC_SVC(&msg) = (u8)(SC_RPC_SVC_RM);
481 RPC_FUNC(&msg) = (u8)(RM_FUNC_SET_MEMREG_PERMISSIONS);
482 RPC_U8(&msg, 0U) = (u8)(mr);
483 RPC_U8(&msg, 1U) = (u8)(pt);
484 RPC_U8(&msg, 2U) = (u8)(perm);
485 RPC_SIZE(&msg) = 2U;
486
487 ret = misc_call(dev, SC_FALSE, &msg, size, &msg, size);
488 if (ret) {
489 printf("%s: mr:%u, pt:%u, perm:%u, res:%d\n", __func__,
490 mr, pt, perm, RPC_R8(&msg));
491 }
Peng Fan26864472019-08-26 08:12:03 +0000492
493 return ret;
494}
495
Peng Fanad9d40a2018-10-18 14:28:12 +0200496int sc_rm_get_memreg_info(sc_ipc_t ipc, sc_rm_mr_t mr, sc_faddr_t *addr_start,
497 sc_faddr_t *addr_end)
498{
499 struct udevice *dev = gd->arch.scu_dev;
500 int size = sizeof(struct sc_rpc_msg_s);
501 struct sc_rpc_msg_s msg;
502 int ret;
503
504 if (!dev)
505 hang();
506
507 RPC_VER(&msg) = SC_RPC_VERSION;
508 RPC_SVC(&msg) = (u8)SC_RPC_SVC_RM;
509 RPC_FUNC(&msg) = (u8)RM_FUNC_GET_MEMREG_INFO;
510 RPC_U8(&msg, 0U) = (u8)mr;
511 RPC_SIZE(&msg) = 2U;
512
513 ret = misc_call(dev, SC_FALSE, &msg, size, &msg, size);
514 if (ret)
515 printf("%s: mr:%d res:%d\n", __func__, mr, RPC_R8(&msg));
516
517 if (addr_start)
518 *addr_start = ((u64)RPC_U32(&msg, 0U) << 32U) |
519 RPC_U32(&msg, 4U);
520
521 if (addr_end)
522 *addr_end = ((u64)RPC_U32(&msg, 8U) << 32U) |
523 RPC_U32(&msg, 12U);
524
525 return ret;
526}
527
528sc_bool_t sc_rm_is_resource_owned(sc_ipc_t ipc, sc_rsrc_t resource)
529{
530 struct udevice *dev = gd->arch.scu_dev;
531 int size = sizeof(struct sc_rpc_msg_s);
532 struct sc_rpc_msg_s msg;
533 int ret;
534 u8 result;
535
536 if (!dev)
537 hang();
538
539 RPC_VER(&msg) = SC_RPC_VERSION;
540 RPC_SVC(&msg) = (u8)SC_RPC_SVC_RM;
541 RPC_FUNC(&msg) = (u8)RM_FUNC_IS_RESOURCE_OWNED;
542 RPC_U16(&msg, 0U) = (u16)resource;
543 RPC_SIZE(&msg) = 2U;
544
545 ret = misc_call(dev, SC_FALSE, &msg, size, &msg, size);
546 result = RPC_R8(&msg);
547 if (result != 0 && result != 1) {
548 printf("%s: resource:%d res:%d\n",
549 __func__, resource, RPC_R8(&msg));
550 if (ret)
551 printf("%s: res:%d res:%d\n", __func__, resource,
552 RPC_R8(&msg));
553 }
554
555 return !!result;
556}
Peng Fan4d817552019-09-23 10:12:31 +0000557
558int sc_rm_partition_alloc(sc_ipc_t ipc, sc_rm_pt_t *pt, sc_bool_t secure,
559 sc_bool_t isolated, sc_bool_t restricted,
560 sc_bool_t grant, sc_bool_t coherent)
561{
562 struct udevice *dev = gd->arch.scu_dev;
563 struct sc_rpc_msg_s msg;
564 int size = sizeof(struct sc_rpc_msg_s);
565 int ret;
566
567 RPC_VER(&msg) = SC_RPC_VERSION;
568 RPC_SVC(&msg) = (u8)SC_RPC_SVC_RM;
569 RPC_FUNC(&msg) = (u8)RM_FUNC_PARTITION_ALLOC;
570 RPC_U8(&msg, 0U) = B2U8(secure);
571 RPC_U8(&msg, 1U) = B2U8(isolated);
572 RPC_U8(&msg, 2U) = B2U8(restricted);
573 RPC_U8(&msg, 3U) = B2U8(grant);
574 RPC_U8(&msg, 4U) = B2U8(coherent);
575 RPC_SIZE(&msg) = 3U;
576
577 ret = misc_call(dev, SC_FALSE, &msg, size, &msg, size);
578 if (ret) {
579 printf("%s: secure:%u isolated:%u restricted:%u grant:%u coherent:%u res:%d\n",
580 __func__, secure, isolated, restricted, grant, coherent,
581 RPC_R8(&msg));
582 }
583
584 if (pt)
585 *pt = RPC_U8(&msg, 0U);
586
587 return ret;
588}
589
590int sc_rm_partition_free(sc_ipc_t ipc, sc_rm_pt_t pt)
591{
592 struct udevice *dev = gd->arch.scu_dev;
593 struct sc_rpc_msg_s msg;
594 int size = sizeof(struct sc_rpc_msg_s);
595 int ret;
596
597 RPC_VER(&msg) = SC_RPC_VERSION;
598 RPC_SVC(&msg) = (u8)SC_RPC_SVC_RM;
599 RPC_FUNC(&msg) = (u8)RM_FUNC_PARTITION_FREE;
600 RPC_U8(&msg, 0U) = (u8)pt;
601 RPC_SIZE(&msg) = 2U;
602
603 ret = misc_call(dev, SC_FALSE, &msg, size, &msg, size);
604 if (ret) {
605 printf("%s: pt:%u res:%d\n",
606 __func__, pt, RPC_R8(&msg));
607 }
608
609 return ret;
610}
611
612int sc_rm_get_partition(sc_ipc_t ipc, sc_rm_pt_t *pt)
613{
614 struct udevice *dev = gd->arch.scu_dev;
615 struct sc_rpc_msg_s msg;
616 int size = sizeof(struct sc_rpc_msg_s);
617 int ret;
618
619 RPC_VER(&msg) = SC_RPC_VERSION;
620 RPC_SVC(&msg) = (u8)SC_RPC_SVC_RM;
621 RPC_FUNC(&msg) = (u8)RM_FUNC_GET_PARTITION;
622 RPC_SIZE(&msg) = 1U;
623
624 ret = misc_call(dev, SC_FALSE, &msg, size, &msg, size);
625 if (ret)
626 printf("%s: res:%d\n", __func__, RPC_R8(&msg));
627
628 if (pt)
629 *pt = RPC_U8(&msg, 0U);
630
631 return ret;
632}
633
634int sc_rm_set_parent(sc_ipc_t ipc, sc_rm_pt_t pt, sc_rm_pt_t pt_parent)
635{
636 struct udevice *dev = gd->arch.scu_dev;
637 struct sc_rpc_msg_s msg;
638 int size = sizeof(struct sc_rpc_msg_s);
639 int ret;
640
641 RPC_VER(&msg) = SC_RPC_VERSION;
642 RPC_SVC(&msg) = (u8)SC_RPC_SVC_RM;
643 RPC_FUNC(&msg) = (u8)RM_FUNC_SET_PARENT;
644 RPC_U8(&msg, 0U) = (u8)pt;
645 RPC_U8(&msg, 1U) = (u8)pt_parent;
646 RPC_SIZE(&msg) = 2U;
647
648 ret = misc_call(dev, SC_FALSE, &msg, size, &msg, size);
649 if (ret) {
650 printf("%s: pt:%u, pt_parent:%u, res:%d\n",
651 __func__, pt, pt_parent, RPC_R8(&msg));
652 }
653
654 return ret;
655}
656
657int sc_rm_assign_resource(sc_ipc_t ipc, sc_rm_pt_t pt, sc_rsrc_t resource)
658{
659 struct udevice *dev = gd->arch.scu_dev;
660 struct sc_rpc_msg_s msg;
661 int size = sizeof(struct sc_rpc_msg_s);
662 int ret;
663
664 RPC_VER(&msg) = SC_RPC_VERSION;
665 RPC_SVC(&msg) = (u8)SC_RPC_SVC_RM;
666 RPC_FUNC(&msg) = (u8)RM_FUNC_ASSIGN_RESOURCE;
667 RPC_U16(&msg, 0U) = (u16)resource;
668 RPC_U8(&msg, 2U) = (u8)pt;
669 RPC_SIZE(&msg) = 2U;
670
671 ret = misc_call(dev, SC_FALSE, &msg, size, &msg, size);
672 if (ret) {
673 printf("%s: pt:%u, resource:%u, res:%d\n",
674 __func__, pt, resource, RPC_R8(&msg));
675 }
676
677 return ret;
678}
679
680int sc_rm_assign_pad(sc_ipc_t ipc, sc_rm_pt_t pt, sc_pad_t pad)
681{
682 struct udevice *dev = gd->arch.scu_dev;
683 struct sc_rpc_msg_s msg;
684 int size = sizeof(struct sc_rpc_msg_s);
685 int ret;
686
687 RPC_VER(&msg) = SC_RPC_VERSION;
688 RPC_SVC(&msg) = (u8)SC_RPC_SVC_RM;
689 RPC_FUNC(&msg) = (u8)RM_FUNC_ASSIGN_PAD;
690 RPC_U16(&msg, 0U) = (u16)pad;
691 RPC_U8(&msg, 2U) = (u8)pt;
692 RPC_SIZE(&msg) = 2U;
693
694 ret = misc_call(dev, SC_FALSE, &msg, size, &msg, size);
695 if (ret) {
696 printf("%s: pt:%u, pad:%u, res:%d\n",
697 __func__, pt, pad, RPC_R8(&msg));
698 }
699
700 return ret;
701}
702
703sc_bool_t sc_rm_is_pad_owned(sc_ipc_t ipc, sc_pad_t pad)
704{
705 struct udevice *dev = gd->arch.scu_dev;
706 struct sc_rpc_msg_s msg;
707 int size = sizeof(struct sc_rpc_msg_s);
708 int ret;
709 u8 result;
710
711 RPC_VER(&msg) = SC_RPC_VERSION;
712 RPC_SVC(&msg) = (u8)SC_RPC_SVC_RM;
713 RPC_FUNC(&msg) = (u8)RM_FUNC_IS_PAD_OWNED;
714 RPC_U8(&msg, 0U) = (u8)pad;
715 RPC_SIZE(&msg) = 2U;
716
717 ret = misc_call(dev, SC_FALSE, &msg, size, &msg, size);
718 result = RPC_R8(&msg);
719 if (result != 0 && result != 1) {
720 printf("%s: pad:%d res:%d\n", __func__, pad, RPC_R8(&msg));
721 if (ret) {
722 printf("%s: pad:%d res:%d\n", __func__,
723 pad, RPC_R8(&msg));
724 }
725 }
726
727 return !!result;
728}
729
730int sc_rm_get_resource_owner(sc_ipc_t ipc, sc_rsrc_t resource,
731 sc_rm_pt_t *pt)
732{
733 struct udevice *dev = gd->arch.scu_dev;
734 struct sc_rpc_msg_s msg;
735 int size = sizeof(struct sc_rpc_msg_s);
736 int ret;
737
738 RPC_VER(&msg) = SC_RPC_VERSION;
739 RPC_SVC(&msg) = (u8)SC_RPC_SVC_RM;
740 RPC_FUNC(&msg) = (u8)RM_FUNC_GET_RESOURCE_OWNER;
741 RPC_U16(&msg, 0U) = (u16)resource;
742 RPC_SIZE(&msg) = 2U;
743
744 ret = misc_call(dev, SC_FALSE, &msg, size, &msg, size);
745 if (pt)
746 *pt = RPC_U8(&msg, 0U);
747
748 return ret;
749}
750
751int sc_pm_cpu_start(sc_ipc_t ipc, sc_rsrc_t resource, sc_bool_t enable,
752 sc_faddr_t address)
753{
754 struct udevice *dev = gd->arch.scu_dev;
755 struct sc_rpc_msg_s msg;
756 int size = sizeof(struct sc_rpc_msg_s);
757 int ret;
758
759 RPC_VER(&msg) = SC_RPC_VERSION;
760 RPC_SVC(&msg) = (u8)SC_RPC_SVC_PM;
761 RPC_FUNC(&msg) = (u8)PM_FUNC_CPU_START;
762 RPC_U32(&msg, 0U) = (u32)(address >> 32ULL);
763 RPC_U32(&msg, 4U) = (u32)address;
764 RPC_U16(&msg, 8U) = (u16)resource;
765 RPC_U8(&msg, 10U) = B2U8(enable);
766 RPC_SIZE(&msg) = 4U;
767
768 ret = misc_call(dev, SC_FALSE, &msg, size, &msg, size);
769 if (ret) {
770 printf("%s: resource:%d address:0x%llx: res:%d\n",
771 __func__, resource, address, RPC_R8(&msg));
772 }
773
774 return ret;
775}
776
777int sc_pm_get_resource_power_mode(sc_ipc_t ipc, sc_rsrc_t resource,
778 sc_pm_power_mode_t *mode)
779{
780 struct udevice *dev = gd->arch.scu_dev;
781 struct sc_rpc_msg_s msg;
782 int size = sizeof(struct sc_rpc_msg_s);
783 int ret;
784
785 RPC_VER(&msg) = SC_RPC_VERSION;
786 RPC_SVC(&msg) = (u8)SC_RPC_SVC_PM;
787 RPC_FUNC(&msg) = (u8)PM_FUNC_GET_RESOURCE_POWER_MODE;
788 RPC_U16(&msg, 0U) = (u16)resource;
789 RPC_SIZE(&msg) = 2U;
790
791 ret = misc_call(dev, SC_FALSE, &msg, size, &msg, size);
792 if (ret) {
793 printf("%s: resource:%d: res:%d\n",
794 __func__, resource, RPC_R8(&msg));
795 }
796
797 if (mode)
798 *mode = RPC_U8(&msg, 0U);
799
800 return ret;
801}
802
803int sc_seco_authenticate(sc_ipc_t ipc, sc_seco_auth_cmd_t cmd,
804 sc_faddr_t addr)
805{
806 struct udevice *dev = gd->arch.scu_dev;
807 struct sc_rpc_msg_s msg;
808 int size = sizeof(struct sc_rpc_msg_s);
809 int ret;
810
811 RPC_VER(&msg) = SC_RPC_VERSION;
812 RPC_SVC(&msg) = (u8)SC_RPC_SVC_SECO;
813 RPC_FUNC(&msg) = (u8)SECO_FUNC_AUTHENTICATE;
814 RPC_U32(&msg, 0U) = (u32)(addr >> 32ULL);
815 RPC_U32(&msg, 4U) = (u32)addr;
816 RPC_U8(&msg, 8U) = (u8)cmd;
817 RPC_SIZE(&msg) = 4U;
818
819 ret = misc_call(dev, SC_FALSE, &msg, size, &msg, size);
820 if (ret)
821 printf("%s: res:%d\n", __func__, RPC_R8(&msg));
822
823 return ret;
824}
825
826int sc_seco_forward_lifecycle(sc_ipc_t ipc, u32 change)
827{
828 struct udevice *dev = gd->arch.scu_dev;
829 struct sc_rpc_msg_s msg;
830 int size = sizeof(struct sc_rpc_msg_s);
831 int ret;
832
833 RPC_VER(&msg) = SC_RPC_VERSION;
834 RPC_SVC(&msg) = (u8)SC_RPC_SVC_SECO;
835 RPC_FUNC(&msg) = (u8)SECO_FUNC_FORWARD_LIFECYCLE;
836 RPC_U32(&msg, 0U) = (u32)change;
837 RPC_SIZE(&msg) = 2U;
838
839 ret = misc_call(dev, SC_FALSE, &msg, size, &msg, size);
840 if (ret) {
841 printf("%s: change:%u, res:%d\n", __func__,
842 change, RPC_R8(&msg));
843 }
844
845 return ret;
846}
847
848int sc_seco_chip_info(sc_ipc_t ipc, u16 *lc, u16 *monotonic, u32 *uid_l,
849 u32 *uid_h)
850{
851 struct udevice *dev = gd->arch.scu_dev;
852 struct sc_rpc_msg_s msg;
853 int size = sizeof(struct sc_rpc_msg_s);
854 int ret;
855
856 RPC_VER(&msg) = SC_RPC_VERSION;
857 RPC_SVC(&msg) = (u8)SC_RPC_SVC_SECO;
858 RPC_FUNC(&msg) = (u8)SECO_FUNC_CHIP_INFO;
859 RPC_SIZE(&msg) = 1U;
860
861 ret = misc_call(dev, SC_FALSE, &msg, size, &msg, size);
862 if (ret)
863 printf("%s: res:%d\n", __func__, RPC_R8(&msg));
864
865 if (uid_l)
866 *uid_l = RPC_U32(&msg, 0U);
867
868 if (uid_h)
869 *uid_h = RPC_U32(&msg, 4U);
870
871 if (lc)
872 *lc = RPC_U16(&msg, 8U);
873
874 if (monotonic)
875 *monotonic = RPC_U16(&msg, 10U);
876
877 return ret;
878}
879
880void sc_seco_build_info(sc_ipc_t ipc, u32 *version, u32 *commit)
881{
882 struct udevice *dev = gd->arch.scu_dev;
883 struct sc_rpc_msg_s msg;
884 int size = sizeof(struct sc_rpc_msg_s);
885
886 RPC_VER(&msg) = SC_RPC_VERSION;
887 RPC_SVC(&msg) = (u8)(SC_RPC_SVC_SECO);
888 RPC_FUNC(&msg) = (u8)(SECO_FUNC_BUILD_INFO);
889 RPC_SIZE(&msg) = 1U;
890
891 misc_call(dev, SC_FALSE, &msg, size, &msg, size);
892
893 if (version)
894 *version = RPC_U32(&msg, 0U);
895
896 if (commit)
897 *commit = RPC_U32(&msg, 4U);
898}
899
900int sc_seco_get_event(sc_ipc_t ipc, u8 idx, u32 *event)
901{
902 struct udevice *dev = gd->arch.scu_dev;
903 struct sc_rpc_msg_s msg;
904 int size = sizeof(struct sc_rpc_msg_s);
905 int ret;
906
907 RPC_VER(&msg) = SC_RPC_VERSION;
908 RPC_SVC(&msg) = (u8)SC_RPC_SVC_SECO;
909 RPC_FUNC(&msg) = (u8)SECO_FUNC_GET_EVENT;
910 RPC_U8(&msg, 0U) = (u8)idx;
911 RPC_SIZE(&msg) = 2U;
912
913 ret = misc_call(dev, SC_FALSE, &msg, size, &msg, size);
914 if (ret)
915 printf("%s: idx: %u, res:%d\n", __func__, idx, RPC_R8(&msg));
916
917 if (event)
918 *event = RPC_U32(&msg, 0U);
919
920 return ret;
921}
922
923int sc_seco_gen_key_blob(sc_ipc_t ipc, u32 id, sc_faddr_t load_addr,
924 sc_faddr_t export_addr, u16 max_size)
925{
926 struct udevice *dev = gd->arch.scu_dev;
927 struct sc_rpc_msg_s msg;
928 int size = sizeof(struct sc_rpc_msg_s);
929 int ret;
930
931 RPC_VER(&msg) = SC_RPC_VERSION;
932 RPC_SVC(&msg) = (u8)SC_RPC_SVC_SECO;
933 RPC_FUNC(&msg) = (u8)SECO_FUNC_GEN_KEY_BLOB;
934 RPC_U32(&msg, 0U) = (u32)(load_addr >> 32ULL);
935 RPC_U32(&msg, 4U) = (u32)load_addr;
936 RPC_U32(&msg, 8U) = (u32)(export_addr >> 32ULL);
937 RPC_U32(&msg, 12U) = (u32)export_addr;
938 RPC_U32(&msg, 16U) = (u32)id;
939 RPC_U16(&msg, 20U) = (u16)max_size;
940 RPC_SIZE(&msg) = 7U;
941
942 ret = misc_call(dev, SC_FALSE, &msg, size, &msg, size);
943 if (ret) {
944 printf("%s: id: %u, load_addr 0x%llx, export_addr 0x%llx, res:%d\n",
945 __func__, id, load_addr, export_addr, RPC_R8(&msg));
946 }
947
948 return ret;
949}