blob: 5b2f4686230c195ffac433cb38fd6de0f5277272 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Troy Kisky0ca618c2012-08-15 10:31:20 +00002/*
3 * Copyright (C) 2012 Boundary Devices Inc.
Troy Kisky0ca618c2012-08-15 10:31:20 +00004 */
5#include <common.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +09006#include <linux/errno.h>
Troy Kisky0ca618c2012-08-15 10:31:20 +00007#include <asm/io.h>
Stefano Babic33731bc2017-06-29 10:16:06 +02008#include <asm/mach-imx/boot_mode.h>
Troy Kisky0ca618c2012-08-15 10:31:20 +00009#include <malloc.h>
Marek Vasut69c5e342012-10-12 10:27:04 +000010#include <command.h>
Troy Kisky0ca618c2012-08-15 10:31:20 +000011
12static const struct boot_mode *modes[2];
13
14static const struct boot_mode *search_modes(char *arg)
15{
16 int i;
17
18 for (i = 0; i < ARRAY_SIZE(modes); i++) {
19 const struct boot_mode *p = modes[i];
20 if (p) {
21 while (p->name) {
22 if (!strcmp(p->name, arg))
23 return p;
24 p++;
25 }
26 }
27 }
28 return NULL;
29}
30
31static int create_usage(char *dest)
32{
33 int i;
34 int size = 0;
35
36 for (i = 0; i < ARRAY_SIZE(modes); i++) {
37 const struct boot_mode *p = modes[i];
38 if (p) {
39 while (p->name) {
40 int len = strlen(p->name);
41 if (dest) {
42 memcpy(dest, p->name, len);
43 dest += len;
44 *dest++ = '|';
45 }
46 size += len + 1;
47 p++;
48 }
49 }
50 }
51 if (dest)
52 memcpy(dest - 1, " [noreset]", 11); /* include trailing 0 */
53 size += 10;
Marek Vasut81e514f2020-08-05 15:34:06 +020054
55 if (dest)
56 memcpy(dest - 1, "\nbmode - getprisec", 19);
57 size += 18;
58
Troy Kisky0ca618c2012-08-15 10:31:20 +000059 return size;
60}
61
Marek Vasut81e514f2020-08-05 15:34:06 +020062__weak int boot_mode_getprisec(void)
63{
64 return 0;
65}
66
Simon Glassed38aef2020-05-10 11:40:03 -060067static int do_boot_mode(struct cmd_tbl *cmdtp, int flag, int argc,
68 char *const argv[])
Troy Kisky0ca618c2012-08-15 10:31:20 +000069{
70 const struct boot_mode *p;
71 int reset_requested = 1;
72
73 if (argc < 2)
74 return CMD_RET_USAGE;
Marek Vasut81e514f2020-08-05 15:34:06 +020075 if (!strcmp(argv[1], "getprisec"))
76 return boot_mode_getprisec();
Troy Kisky0ca618c2012-08-15 10:31:20 +000077 p = search_modes(argv[1]);
78 if (!p)
79 return CMD_RET_USAGE;
80 if (argc == 3) {
81 if (strcmp(argv[2], "noreset"))
82 return CMD_RET_USAGE;
83 reset_requested = 0;
84 }
85
Fedor Rossc0b94842023-10-16 18:16:15 +020086/* No longer applicable to i.MX8M */
87#if IS_ENABLED(CONFIG_MX53) || IS_ENABLED(CONFIG_MX6) || IS_ENABLED(CONFIG_MX7)
Troy Kisky0ca618c2012-08-15 10:31:20 +000088 boot_mode_apply(p->cfg_val);
Fedor Rossc0b94842023-10-16 18:16:15 +020089#endif
90
Troy Kisky0ca618c2012-08-15 10:31:20 +000091 if (reset_requested && p->cfg_val)
92 do_reset(NULL, 0, 0, NULL);
93 return 0;
94}
95
96U_BOOT_CMD(
97 bmode, 3, 0, do_boot_mode,
98 NULL,
99 "");
100
101void add_board_boot_modes(const struct boot_mode *p)
102{
103 int size;
104 char *dest;
105
Simon Glassed38aef2020-05-10 11:40:03 -0600106 struct cmd_tbl *entry = ll_entry_get(struct cmd_tbl, bmode, cmd);
Marek Vasut69c5e342012-10-12 10:27:04 +0000107
108 if (entry->usage) {
109 free(entry->usage);
110 entry->usage = NULL;
Troy Kisky0ca618c2012-08-15 10:31:20 +0000111 }
112
113 modes[0] = p;
114 modes[1] = soc_boot_modes;
115 size = create_usage(NULL);
116 dest = malloc(size);
117 if (dest) {
118 create_usage(dest);
Marek Vasut69c5e342012-10-12 10:27:04 +0000119 entry->usage = dest;
Troy Kisky0ca618c2012-08-15 10:31:20 +0000120 }
121}