blob: 8a8d394a5f0cb745ebd6821c9e9a1873bd7d0635 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Alexey Brodkinb628c012014-02-04 12:56:15 +04002/*
3 * Copyright (C) 2013-2014 Synopsys, Inc. All rights reserved.
Alexey Brodkinb628c012014-02-04 12:56:15 +04004 */
5
Simon Glass1ea97892020-05-10 11:40:00 -06006#include <common.h>
7#include <bootstage.h>
Simon Glassed38aef2020-05-10 11:40:03 -06008#include <env.h>
Simon Glass2dc9c342020-05-10 11:40:01 -06009#include <image.h>
Simon Glass8f3f7612019-11-14 12:57:42 -070010#include <irq_func.h>
Simon Glass2dc9c342020-05-10 11:40:01 -060011#include <lmb.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Eugeniy Paltsev67fd56a2018-03-21 15:59:02 +030013#include <asm/cache.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060014#include <asm/global_data.h>
Alexey Brodkinb628c012014-02-04 12:56:15 +040015
16DECLARE_GLOBAL_DATA_PTR;
17
18static ulong get_sp(void)
19{
20 ulong ret;
21
22 asm("mov %0, sp" : "=r"(ret) : );
23 return ret;
24}
25
26void arch_lmb_reserve(struct lmb *lmb)
27{
28 ulong sp;
29
30 /*
31 * Booting a (Linux) kernel image
32 *
33 * Allocate space for command line and board info - the
34 * address should be as high as possible within the reach of
35 * the kernel (see CONFIG_SYS_BOOTMAPSZ settings), but in unused
36 * memory, which means far enough below the current stack
37 * pointer.
38 */
39 sp = get_sp();
40 debug("## Current stack ends at 0x%08lx ", sp);
41
42 /* adjust sp by 4K to be safe */
43 sp -= 4096;
44 lmb_reserve(lmb, sp, (CONFIG_SYS_SDRAM_BASE + gd->ram_size - sp));
45}
46
47static int cleanup_before_linux(void)
48{
49 disable_interrupts();
Eugeniy Paltsev67fd56a2018-03-21 15:59:02 +030050 sync_n_cleanup_cache_all();
Alexey Brodkinb628c012014-02-04 12:56:15 +040051
52 return 0;
53}
54
Eugeniy Paltsev01f45cc2018-03-23 15:35:03 +030055__weak int board_prep_linux(bootm_headers_t *images) { return 0; }
56
Alexey Brodkinb628c012014-02-04 12:56:15 +040057/* Subcommand: PREP */
Eugeniy Paltsev01f45cc2018-03-23 15:35:03 +030058static int boot_prep_linux(bootm_headers_t *images)
Alexey Brodkinb628c012014-02-04 12:56:15 +040059{
Eugeniy Paltsev01f45cc2018-03-23 15:35:03 +030060 int ret;
61
62 ret = image_setup_linux(images);
63 if (ret)
64 return ret;
65
66 return board_prep_linux(images);
Alexey Brodkinb628c012014-02-04 12:56:15 +040067}
68
Eugeniy Paltsev01f45cc2018-03-23 15:35:03 +030069/* Generic implementation for single core CPU */
70__weak void board_jump_and_run(ulong entry, int zero, int arch, uint params)
71{
72 void (*kernel_entry)(int zero, int arch, uint params);
73
74 kernel_entry = (void (*)(int, int, uint))entry;
75
76 kernel_entry(zero, arch, params);
77}
Alexey Brodkincf9cafd2015-04-13 13:37:05 +030078
Alexey Brodkinb628c012014-02-04 12:56:15 +040079/* Subcommand: GO */
80static void boot_jump_linux(bootm_headers_t *images, int flag)
81{
Eugeniy Paltsev01f45cc2018-03-23 15:35:03 +030082 ulong kernel_entry;
Alexey Brodkinb628c012014-02-04 12:56:15 +040083 unsigned int r0, r2;
84 int fake = (flag & BOOTM_STATE_OS_FAKE_GO);
85
Eugeniy Paltsev01f45cc2018-03-23 15:35:03 +030086 kernel_entry = images->ep;
Alexey Brodkinb628c012014-02-04 12:56:15 +040087
88 debug("## Transferring control to Linux (at address %08lx)...\n",
Eugeniy Paltsev01f45cc2018-03-23 15:35:03 +030089 kernel_entry);
Alexey Brodkinb628c012014-02-04 12:56:15 +040090 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
91
92 printf("\nStarting kernel ...%s\n\n", fake ?
93 "(fake run for tracing)" : "");
94 bootstage_mark_name(BOOTSTAGE_ID_BOOTM_HANDOFF, "start_kernel");
95
Alexey Brodkinb628c012014-02-04 12:56:15 +040096 if (IMAGE_ENABLE_OF_LIBFDT && images->ft_len) {
97 r0 = 2;
98 r2 = (unsigned int)images->ft_addr;
99 } else {
100 r0 = 1;
Simon Glass64b723f2017-08-03 12:22:12 -0600101 r2 = (unsigned int)env_get("bootargs");
Alexey Brodkinb628c012014-02-04 12:56:15 +0400102 }
103
Eugeniy Paltsev01f45cc2018-03-23 15:35:03 +0300104 cleanup_before_linux();
105
106 if (!fake)
107 board_jump_and_run(kernel_entry, r0, 0, r2);
Alexey Brodkinb628c012014-02-04 12:56:15 +0400108}
109
110int do_bootm_linux(int flag, int argc, char *argv[], bootm_headers_t *images)
111{
112 /* No need for those on ARC */
113 if ((flag & BOOTM_STATE_OS_BD_T) || (flag & BOOTM_STATE_OS_CMDLINE))
114 return -1;
115
Eugeniy Paltsev01f45cc2018-03-23 15:35:03 +0300116 if (flag & BOOTM_STATE_OS_PREP)
117 return boot_prep_linux(images);
Alexey Brodkinb628c012014-02-04 12:56:15 +0400118
119 if (flag & (BOOTM_STATE_OS_GO | BOOTM_STATE_OS_FAKE_GO)) {
120 boot_jump_linux(images, flag);
121 return 0;
122 }
123
Eugeniy Paltsev01f45cc2018-03-23 15:35:03 +0300124 return -1;
Alexey Brodkinb628c012014-02-04 12:56:15 +0400125}