1 /*
2  * K2HK: secure kernel command file
3  *
4  * (C) Copyright 2012-2014
5  *     Texas Instruments Incorporated, <www.ti.com>
6  *
7  * SPDX-License-Identifier:     GPL-2.0+
8  */
9 
10 #include <common.h>
11 #include <command.h>
12 asm(".arch_extension sec\n\t");
13 
14 static int mon_install(u32 addr, u32 dpsc, u32 freq)
15 {
16 	int result;
17 
18 	__asm__ __volatile__ (
19 		"stmfd r13!, {lr}\n"
20 		"mov r0, %1\n"
21 		"mov r1, %2\n"
22 		"mov r2, %3\n"
23 		"blx r0\n"
24 		"ldmfd r13!, {lr}\n"
25 		: "=&r" (result)
26 		: "r" (addr), "r" (dpsc), "r" (freq)
27 		: "cc", "r0", "r1", "r2", "memory");
28 	return result;
29 }
30 
31 static int do_mon_install(cmd_tbl_t *cmdtp, int flag, int argc,
32 			  char * const argv[])
33 {
34 	u32 addr, dpsc_base = 0x1E80000, freq;
35 	int     rcode = 0;
36 
37 	if (argc < 2)
38 		return CMD_RET_USAGE;
39 
40 	freq = clk_get_rate(sys_clk0_6_clk);
41 
42 	addr = simple_strtoul(argv[1], NULL, 16);
43 
44 	rcode = mon_install(addr, dpsc_base, freq);
45 	printf("## installed monitor, freq [%d], status %d\n",
46 	       freq, rcode);
47 
48 	return 0;
49 }
50 
51 U_BOOT_CMD(mon_install, 2, 0, do_mon_install,
52 	   "Install boot kernel at 'addr'",
53 	   ""
54 );
55 
56 static void core_spin(void)
57 {
58 	while (1)
59 		; /* forever */;
60 }
61 
62 int mon_power_on(int core_id, void *ep)
63 {
64 	int result;
65 
66 	asm volatile (
67 		"stmfd  r13!, {lr}\n"
68 		"mov r1, %1\n"
69 		"mov r2, %2\n"
70 		"mov r0, #0\n"
71 		"smc	#0\n"
72 		"ldmfd  r13!, {lr}\n"
73 		: "=&r" (result)
74 		: "r" (core_id), "r" (ep)
75 		: "cc", "r0", "r1", "r2", "memory");
76 	return  result;
77 }
78 
79 int mon_power_off(int core_id)
80 {
81 	int result;
82 
83 	asm volatile (
84 		"stmfd  r13!, {lr}\n"
85 		"mov r1, %1\n"
86 		"mov r0, #1\n"
87 		"smc	#1\n"
88 		"ldmfd  r13!, {lr}\n"
89 		: "=&r" (result)
90 		: "r" (core_id)
91 		: "cc", "r0", "r1", "memory");
92 	return  result;
93 }
94 
95 int do_mon_power(cmd_tbl_t *cmdtp, int flag, int argc,
96 			char * const argv[])
97 {
98 	int     rcode = 0, core_id, on;
99 	void (*fn)(void);
100 
101 	fn = core_spin;
102 
103 	if (argc < 3)
104 		return CMD_RET_USAGE;
105 
106 	core_id = simple_strtoul(argv[1], NULL, 16);
107 	on = simple_strtoul(argv[2], NULL, 16);
108 
109 	if (on)
110 		rcode = mon_power_on(core_id, fn);
111 	else
112 		rcode = mon_power_off(core_id);
113 
114 	if (on) {
115 		if (!rcode)
116 			printf("core %d powered on successfully\n", core_id);
117 		else
118 			printf("core %d power on failure\n", core_id);
119 	} else {
120 		printf("core %d powered off successfully\n", core_id);
121 	}
122 
123 	return 0;
124 }
125 
126 U_BOOT_CMD(mon_power, 3, 0, do_mon_power,
127 	   "Power On/Off secondary core",
128 	   "mon_power <coreid> <oper>\n"
129 	   "- coreid (1-3) and oper (1 - ON, 0 - OFF)\n"
130 	   ""
131 );
132