Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Troy Kisky | 0ca618c | 2012-08-15 10:31:20 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2012 Boundary Devices Inc. |
Troy Kisky | 0ca618c | 2012-08-15 10:31:20 +0000 | [diff] [blame] | 4 | */ |
Masahiro Yamada | 56a931c | 2016-09-21 11:28:55 +0900 | [diff] [blame] | 5 | #include <linux/errno.h> |
Troy Kisky | 0ca618c | 2012-08-15 10:31:20 +0000 | [diff] [blame] | 6 | #include <asm/io.h> |
Stefano Babic | 33731bc | 2017-06-29 10:16:06 +0200 | [diff] [blame] | 7 | #include <asm/mach-imx/boot_mode.h> |
Troy Kisky | 0ca618c | 2012-08-15 10:31:20 +0000 | [diff] [blame] | 8 | #include <malloc.h> |
Marek Vasut | 69c5e34 | 2012-10-12 10:27:04 +0000 | [diff] [blame] | 9 | #include <command.h> |
Troy Kisky | 0ca618c | 2012-08-15 10:31:20 +0000 | [diff] [blame] | 10 | |
| 11 | static const struct boot_mode *modes[2]; |
| 12 | |
| 13 | static 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 | |
| 30 | static 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 Vasut | 81e514f | 2020-08-05 15:34:06 +0200 | [diff] [blame] | 53 | |
| 54 | if (dest) |
| 55 | memcpy(dest - 1, "\nbmode - getprisec", 19); |
| 56 | size += 18; |
| 57 | |
Troy Kisky | 0ca618c | 2012-08-15 10:31:20 +0000 | [diff] [blame] | 58 | return size; |
| 59 | } |
| 60 | |
Marek Vasut | 81e514f | 2020-08-05 15:34:06 +0200 | [diff] [blame] | 61 | __weak int boot_mode_getprisec(void) |
| 62 | { |
| 63 | return 0; |
| 64 | } |
| 65 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 66 | static int do_boot_mode(struct cmd_tbl *cmdtp, int flag, int argc, |
| 67 | char *const argv[]) |
Troy Kisky | 0ca618c | 2012-08-15 10:31:20 +0000 | [diff] [blame] | 68 | { |
| 69 | const struct boot_mode *p; |
| 70 | int reset_requested = 1; |
| 71 | |
| 72 | if (argc < 2) |
| 73 | return CMD_RET_USAGE; |
Marek Vasut | 81e514f | 2020-08-05 15:34:06 +0200 | [diff] [blame] | 74 | if (!strcmp(argv[1], "getprisec")) |
| 75 | return boot_mode_getprisec(); |
Troy Kisky | 0ca618c | 2012-08-15 10:31:20 +0000 | [diff] [blame] | 76 | 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 Ross | c0b9484 | 2023-10-16 18:16:15 +0200 | [diff] [blame] | 85 | /* No longer applicable to i.MX8M */ |
| 86 | #if IS_ENABLED(CONFIG_MX53) || IS_ENABLED(CONFIG_MX6) || IS_ENABLED(CONFIG_MX7) |
Troy Kisky | 0ca618c | 2012-08-15 10:31:20 +0000 | [diff] [blame] | 87 | boot_mode_apply(p->cfg_val); |
Fedor Ross | c0b9484 | 2023-10-16 18:16:15 +0200 | [diff] [blame] | 88 | #endif |
| 89 | |
Troy Kisky | 0ca618c | 2012-08-15 10:31:20 +0000 | [diff] [blame] | 90 | if (reset_requested && p->cfg_val) |
| 91 | do_reset(NULL, 0, 0, NULL); |
| 92 | return 0; |
| 93 | } |
| 94 | |
| 95 | U_BOOT_CMD( |
| 96 | bmode, 3, 0, do_boot_mode, |
| 97 | NULL, |
| 98 | ""); |
| 99 | |
| 100 | void add_board_boot_modes(const struct boot_mode *p) |
| 101 | { |
| 102 | int size; |
| 103 | char *dest; |
| 104 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 105 | struct cmd_tbl *entry = ll_entry_get(struct cmd_tbl, bmode, cmd); |
Marek Vasut | 69c5e34 | 2012-10-12 10:27:04 +0000 | [diff] [blame] | 106 | |
| 107 | if (entry->usage) { |
| 108 | free(entry->usage); |
| 109 | entry->usage = NULL; |
Troy Kisky | 0ca618c | 2012-08-15 10:31:20 +0000 | [diff] [blame] | 110 | } |
| 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 Vasut | 69c5e34 | 2012-10-12 10:27:04 +0000 | [diff] [blame] | 118 | entry->usage = dest; |
Troy Kisky | 0ca618c | 2012-08-15 10:31:20 +0000 | [diff] [blame] | 119 | } |
| 120 | } |