blob: c20e80725f8ba93fafa8346acbd8088faddda326 [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 */
Masahiro Yamada56a931c2016-09-21 11:28:55 +09005#include <linux/errno.h>
Troy Kisky0ca618c2012-08-15 10:31:20 +00006#include <asm/io.h>
Stefano Babic33731bc2017-06-29 10:16:06 +02007#include <asm/mach-imx/boot_mode.h>
Troy Kisky0ca618c2012-08-15 10:31:20 +00008#include <malloc.h>
Marek Vasut69c5e342012-10-12 10:27:04 +00009#include <command.h>
Troy Kisky0ca618c2012-08-15 10:31:20 +000010
11static const struct boot_mode *modes[2];
12
13static const struct boot_mode *search_modes(char *arg)
14{
15 int i;
16
17 for (i = 0; i < ARRAY_SIZE(modes); i++) {
18 const struct boot_mode *p = modes[i];
19 if (p) {
20 while (p->name) {
21 if (!strcmp(p->name, arg))
22 return p;
23 p++;
24 }
25 }
26 }
27 return NULL;
28}
29
30static int create_usage(char *dest)
31{
32 int i;
33 int size = 0;
34
35 for (i = 0; i < ARRAY_SIZE(modes); i++) {
36 const struct boot_mode *p = modes[i];
37 if (p) {
38 while (p->name) {
39 int len = strlen(p->name);
40 if (dest) {
41 memcpy(dest, p->name, len);
42 dest += len;
43 *dest++ = '|';
44 }
45 size += len + 1;
46 p++;
47 }
48 }
49 }
50 if (dest)
51 memcpy(dest - 1, " [noreset]", 11); /* include trailing 0 */
52 size += 10;
Marek Vasut81e514f2020-08-05 15:34:06 +020053
54 if (dest)
55 memcpy(dest - 1, "\nbmode - getprisec", 19);
56 size += 18;
57
Troy Kisky0ca618c2012-08-15 10:31:20 +000058 return size;
59}
60
Marek Vasut81e514f2020-08-05 15:34:06 +020061__weak int boot_mode_getprisec(void)
62{
63 return 0;
64}
65
Simon Glassed38aef2020-05-10 11:40:03 -060066static int do_boot_mode(struct cmd_tbl *cmdtp, int flag, int argc,
67 char *const argv[])
Troy Kisky0ca618c2012-08-15 10:31:20 +000068{
69 const struct boot_mode *p;
70 int reset_requested = 1;
71
72 if (argc < 2)
73 return CMD_RET_USAGE;
Marek Vasut81e514f2020-08-05 15:34:06 +020074 if (!strcmp(argv[1], "getprisec"))
75 return boot_mode_getprisec();
Troy Kisky0ca618c2012-08-15 10:31:20 +000076 p = search_modes(argv[1]);
77 if (!p)
78 return CMD_RET_USAGE;
79 if (argc == 3) {
80 if (strcmp(argv[2], "noreset"))
81 return CMD_RET_USAGE;
82 reset_requested = 0;
83 }
84
Fedor Rossc0b94842023-10-16 18:16:15 +020085/* No longer applicable to i.MX8M */
86#if IS_ENABLED(CONFIG_MX53) || IS_ENABLED(CONFIG_MX6) || IS_ENABLED(CONFIG_MX7)
Troy Kisky0ca618c2012-08-15 10:31:20 +000087 boot_mode_apply(p->cfg_val);
Fedor Rossc0b94842023-10-16 18:16:15 +020088#endif
89
Troy Kisky0ca618c2012-08-15 10:31:20 +000090 if (reset_requested && p->cfg_val)
91 do_reset(NULL, 0, 0, NULL);
92 return 0;
93}
94
95U_BOOT_CMD(
96 bmode, 3, 0, do_boot_mode,
97 NULL,
98 "");
99
100void add_board_boot_modes(const struct boot_mode *p)
101{
102 int size;
103 char *dest;
104
Simon Glassed38aef2020-05-10 11:40:03 -0600105 struct cmd_tbl *entry = ll_entry_get(struct cmd_tbl, bmode, cmd);
Marek Vasut69c5e342012-10-12 10:27:04 +0000106
107 if (entry->usage) {
108 free(entry->usage);
109 entry->usage = NULL;
Troy Kisky0ca618c2012-08-15 10:31:20 +0000110 }
111
112 modes[0] = p;
113 modes[1] = soc_boot_modes;
114 size = create_usage(NULL);
115 dest = malloc(size);
116 if (dest) {
117 create_usage(dest);
Marek Vasut69c5e342012-10-12 10:27:04 +0000118 entry->usage = dest;
Troy Kisky0ca618c2012-08-15 10:31:20 +0000119 }
120}