summaryrefslogtreecommitdiffstats
path: root/arch/arm/mach-at91/bootstrap.c
blob: 1baf7323fe396d964482f9371a5ff552f357223b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*
 * Copyright (C) 2012 Jean-Christophe PLAGNIOL-VILLARD <plagnio@jcrosoft.com>
 *
 * Under GPLv2
 */

#include <common.h>
#include <bootstrap.h>
#include <mach/bootstrap.h>
#include <linux/sizes.h>
#include <malloc.h>
#include <restart.h>
#include <init.h>
#include <menu.h>

#if defined(CONFIG_MCI_ATMEL)
#define is_mmc() 1
#else
#define is_mmc() 0
#endif

#ifdef CONFIG_NAND_ATMEL
#define is_nand() 1
#else
#define is_nand() 0
#endif

#ifdef CONFIG_MTD_M25P80
#define is_m25p80() 1
#else
#define is_m25p80() 0
#endif

#ifdef CONFIG_MTD_DATAFLASH
#define is_dataflash() 1
#else
#define is_dataflash() 0
#endif

#if defined(CONFIG_MENU) && !defined(CONFIG_NONE)
#define is_menu() 1
#else
#define is_menu() 0
#endif

static char* is_barebox_to_str(bool is_barebox)
{
	return is_barebox ? "barebox" : "unknown";
}

static void at91bootstrap_boot_m25p80(bool is_barebox)
{
	char *name = is_barebox_to_str(is_barebox);
	int (*func)(void) = NULL;

	func = bootstrap_board_read_m25p80();
	printf("Boot %s from m25p80\n", name);
	bootstrap_boot(func, is_barebox);
	bootstrap_err("... failed\n");
	free(func);
}

static void at91bootstrap_boot_dataflash(bool is_barebox)
{
	char *name = is_barebox_to_str(is_barebox);
	int (*func)(void) = NULL;

	printf("Boot %s from dataflash\n", name);
	func = bootstrap_board_read_dataflash();
	bootstrap_boot(func, is_barebox);
	bootstrap_err("... failed\n");
	free(func);
}

static void at91bootstrap_boot_nand(bool is_barebox)
{
	char *name = is_barebox_to_str(is_barebox);
	int (*func)(void) = NULL;

	printf("Boot %s from nand\n", name);
	func = bootstrap_read_devfs("nand0", true, SZ_128K, SZ_256K, SZ_1M);
	bootstrap_boot(func, is_barebox);
	bootstrap_err("... failed\n");
	free(func);
}

static void at91bootstrap_boot_mmc(void)
{
	int (*func)(void) = NULL;

	printf("Boot from mmc\n");
	func = bootstrap_read_disk("disk0.0", NULL);
	bootstrap_boot(func, false);
	bootstrap_err("... failed\n");
	free(func);
}

static void boot_nand_barebox_action(struct menu *m, struct menu_entry *me)
{
	at91bootstrap_boot_nand(true);

	getc();
}

static void boot_nand_action(struct menu *m, struct menu_entry *me)
{
	at91bootstrap_boot_nand(false);

	getc();
}

static void boot_m25p80_barebox_action(struct menu *m, struct menu_entry *me)
{
	at91bootstrap_boot_nand(true);

	getc();
}

static void boot_m25p80_action(struct menu *m, struct menu_entry *me)
{
	at91bootstrap_boot_nand(false);

	getc();
}

static void boot_dataflash_barebox_action(struct menu *m, struct menu_entry *me)
{
	at91bootstrap_boot_dataflash(true);

	getc();
}

static void boot_dataflash_action(struct menu *m, struct menu_entry *me)
{
	at91bootstrap_boot_dataflash(false);

	getc();
}

static void boot_mmc_disk_action(struct menu *m, struct menu_entry *me)
{
	at91bootstrap_boot_mmc();

	getc();
}

static void boot_reset_action(struct menu *m, struct menu_entry *me)
{
	restart_machine();
}

void at91_bootstrap_menu(void)
{
	struct menu *m;
	struct menu_entry *me;

	m = menu_alloc();
	m->display = m->name = "boot";

	menu_add(m);

	if (is_mmc()) {
		me = menu_entry_alloc();
		me->action = boot_mmc_disk_action;
		me->type = MENU_ENTRY_NORMAL;
		me->display = "mmc";
		menu_add_entry(m, me);
	}

	if (is_m25p80()) {
		me = menu_entry_alloc();
		me->action = boot_m25p80_barebox_action;
		me->type = MENU_ENTRY_NORMAL;
		me->display = "m25p80 (barebox)";
		menu_add_entry(m, me);

		me = menu_entry_alloc();
		me->action = boot_m25p80_action;
		me->type = MENU_ENTRY_NORMAL;
		me->display = "m25p80";
		menu_add_entry(m, me);
	}

	if (is_dataflash()) {
		me = menu_entry_alloc();
		me->action = boot_dataflash_barebox_action;
		me->type = MENU_ENTRY_NORMAL;
		me->display = "dataflash (barebox)";
		menu_add_entry(m, me);

		me = menu_entry_alloc();
		me->action = boot_dataflash_action;
		me->type = MENU_ENTRY_NORMAL;
		me->display = "dataflash";
		menu_add_entry(m, me);
	}

	if (is_nand()) {
		me = menu_entry_alloc();
		me->action = boot_nand_barebox_action;
		me->type = MENU_ENTRY_NORMAL;
		me->display = "nand (barebox)";
		menu_add_entry(m, me);

		me = menu_entry_alloc();
		me->action = boot_nand_action;
		me->type = MENU_ENTRY_NORMAL;
		me->display = "nand";
		menu_add_entry(m, me);
	}

	me = menu_entry_alloc();
	me->action = boot_reset_action;
	me->type = MENU_ENTRY_NORMAL;
	me->display = "reset";
	menu_add_entry(m, me);

	menu_show(m);
}

static void boot_seq(bool is_barebox)
{
	if (is_m25p80())
		at91bootstrap_boot_m25p80(is_barebox);

	if (is_dataflash())
		at91bootstrap_boot_dataflash(is_barebox);

	if (is_nand())
		at91bootstrap_boot_nand(is_barebox);
}

static int at91_bootstrap(void)
{
	if (is_menu()) {
		printf("press 'm' to start the menu\n");
		if (tstc() && getc() == 'm')
			at91_bootstrap_menu();
	}

	if (is_mmc())
		at91bootstrap_boot_mmc();

	/* First only bootstrap_boot a barebox */
	boot_seq(true);
	/* Second bootstrap_boot any */
	boot_seq(false);

	bootstrap_err("bootstrap_booting failed\n");
	while (1);

	return 0;
}

static int at91_set_bootstrap(void)
{
	barebox_main = at91_bootstrap;

	return 0;
}
late_initcall(at91_set_bootstrap);