blob: 2d4d6efe5ab6e57fb65fabf71fefa6fb9c6727c5 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Troy Kisky124a06d2012-08-15 10:31:20 +00002/*
3 * Copyright (C) 2012 Boundary Devices Inc.
Troy Kisky124a06d2012-08-15 10:31:20 +00004 */
5#include <common.h>
Masahiro Yamada1221ce42016-09-21 11:28:55 +09006#include <linux/errno.h>
Troy Kisky124a06d2012-08-15 10:31:20 +00007#include <asm/io.h>
Stefano Babic552a8482017-06-29 10:16:06 +02008#include <asm/mach-imx/boot_mode.h>
Troy Kisky124a06d2012-08-15 10:31:20 +00009#include <malloc.h>
Marek Vasut6c7c9462012-10-12 10:27:04 +000010#include <command.h>
Troy Kisky124a06d2012-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;
54 return size;
55}
56
57static int do_boot_mode(cmd_tbl_t *cmdtp, int flag, int argc,
58 char * const argv[])
59{
60 const struct boot_mode *p;
61 int reset_requested = 1;
62
63 if (argc < 2)
64 return CMD_RET_USAGE;
65 p = search_modes(argv[1]);
66 if (!p)
67 return CMD_RET_USAGE;
68 if (argc == 3) {
69 if (strcmp(argv[2], "noreset"))
70 return CMD_RET_USAGE;
71 reset_requested = 0;
72 }
73
74 boot_mode_apply(p->cfg_val);
75 if (reset_requested && p->cfg_val)
76 do_reset(NULL, 0, 0, NULL);
77 return 0;
78}
79
80U_BOOT_CMD(
81 bmode, 3, 0, do_boot_mode,
82 NULL,
83 "");
84
85void add_board_boot_modes(const struct boot_mode *p)
86{
87 int size;
88 char *dest;
89
Marek Vasut6c7c9462012-10-12 10:27:04 +000090 cmd_tbl_t *entry = ll_entry_get(cmd_tbl_t, bmode, cmd);
91
92 if (entry->usage) {
93 free(entry->usage);
94 entry->usage = NULL;
Troy Kisky124a06d2012-08-15 10:31:20 +000095 }
96
97 modes[0] = p;
98 modes[1] = soc_boot_modes;
99 size = create_usage(NULL);
100 dest = malloc(size);
101 if (dest) {
102 create_usage(dest);
Marek Vasut6c7c9462012-10-12 10:27:04 +0000103 entry->usage = dest;
Troy Kisky124a06d2012-08-15 10:31:20 +0000104 }
105}