omap3: implement boot parameter saving
[platform/kernel/u-boot.git] / arch / arm / cpu / armv7 / omap-common / spl.c
1 /*
2  * (C) Copyright 2010
3  * Texas Instruments, <www.ti.com>
4  *
5  * Aneesh V <aneesh@ti.com>
6  *
7  * See file CREDITS for list of people who contributed to this
8  * project.
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License as
12  * published by the Free Software Foundation; either version 2 of
13  * the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
23  * MA 02111-1307 USA
24  */
25 #include <common.h>
26 #include <asm/u-boot.h>
27 #include <asm/utils.h>
28 #include <asm/arch/sys_proto.h>
29 #include <nand.h>
30 #include <mmc.h>
31 #include <fat.h>
32 #include <timestamp_autogenerated.h>
33 #include <version_autogenerated.h>
34 #include <asm/omap_common.h>
35 #include <asm/arch/mmc_host_def.h>
36 #include <i2c.h>
37 #include <image.h>
38
39 DECLARE_GLOBAL_DATA_PTR;
40
41 /* Define global data structure pointer to it*/
42 static gd_t gdata __attribute__ ((section(".data")));
43 static bd_t bdata __attribute__ ((section(".data")));
44 static const char *image_name;
45 static u8 image_os;
46 static u32 image_load_addr;
47 static u32 image_entry_point;
48 static u32 image_size;
49
50 inline void hang(void)
51 {
52         puts("### ERROR ### Please RESET the board ###\n");
53         for (;;)
54                 ;
55 }
56
57 void board_init_f(ulong dummy)
58 {
59         /*
60          * We call relocate_code() with relocation target same as the
61          * CONFIG_SYS_SPL_TEXT_BASE. This will result in relocation getting
62          * skipped. Instead, only .bss initialization will happen. That's
63          * all we need
64          */
65         debug(">>board_init_f()\n");
66         relocate_code(CONFIG_SPL_STACK, &gdata, CONFIG_SPL_TEXT_BASE);
67 }
68
69 #ifdef CONFIG_GENERIC_MMC
70 int board_mmc_init(bd_t *bis)
71 {
72         switch (omap_boot_device()) {
73         case BOOT_DEVICE_MMC1:
74                 omap_mmc_init(0);
75                 break;
76         case BOOT_DEVICE_MMC2:
77                 omap_mmc_init(1);
78                 break;
79         }
80         return 0;
81 }
82 #endif
83
84 static void parse_image_header(const struct image_header *header)
85 {
86         u32 header_size = sizeof(struct image_header);
87
88         if (__be32_to_cpu(header->ih_magic) == IH_MAGIC) {
89                 image_size = __be32_to_cpu(header->ih_size) + header_size;
90                 image_entry_point = __be32_to_cpu(header->ih_load);
91                 /* Load including the header */
92                 image_load_addr = image_entry_point - header_size;
93                 image_os = header->ih_os;
94                 image_name = (const char *)&header->ih_name;
95                 debug("spl: payload image: %s load addr: 0x%x size: %d\n",
96                         image_name, image_load_addr, image_size);
97         } else {
98                 /* Signature not found - assume u-boot.bin */
99                 printf("mkimage signature not found - ih_magic = %x\n",
100                         header->ih_magic);
101                 puts("Assuming u-boot.bin ..\n");
102                 /* Let's assume U-Boot will not be more than 200 KB */
103                 image_size = 200 * 1024;
104                 image_entry_point = CONFIG_SYS_TEXT_BASE;
105                 image_load_addr = CONFIG_SYS_TEXT_BASE;
106                 image_os = IH_OS_U_BOOT;
107                 image_name = "U-Boot";
108         }
109 }
110
111 static void mmc_load_image_raw(struct mmc *mmc)
112 {
113         u32 image_size_sectors, err;
114         const struct image_header *header;
115
116         header = (struct image_header *)(CONFIG_SYS_TEXT_BASE -
117                                                 sizeof(struct image_header));
118
119         /* read image header to find the image size & load address */
120         err = mmc->block_dev.block_read(0,
121                         CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR, 1,
122                         (void *)header);
123
124         if (err <= 0)
125                 goto end;
126
127         parse_image_header(header);
128
129         /* convert size to sectors - round up */
130         image_size_sectors = (image_size + MMCSD_SECTOR_SIZE - 1) /
131                                 MMCSD_SECTOR_SIZE;
132
133         /* Read the header too to avoid extra memcpy */
134         err = mmc->block_dev.block_read(0,
135                         CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR,
136                         image_size_sectors, (void *)image_load_addr);
137
138 end:
139         if (err <= 0) {
140                 printf("spl: mmc blk read err - %d\n", err);
141                 hang();
142         }
143 }
144
145 static void mmc_load_image_fat(struct mmc *mmc)
146 {
147         s32 err;
148         struct image_header *header;
149
150         header = (struct image_header *)(CONFIG_SYS_TEXT_BASE -
151                                                 sizeof(struct image_header));
152
153         err = fat_register_device(&mmc->block_dev,
154                                 CONFIG_SYS_MMC_SD_FAT_BOOT_PARTITION);
155         if (err) {
156                 printf("spl: fat register err - %d\n", err);
157                 hang();
158         }
159
160         err = file_fat_read(CONFIG_SPL_FAT_LOAD_PAYLOAD_NAME,
161                                 (u8 *)header, sizeof(struct image_header));
162         if (err <= 0)
163                 goto end;
164
165         parse_image_header(header);
166
167         err = file_fat_read(CONFIG_SPL_FAT_LOAD_PAYLOAD_NAME,
168                                 (u8 *)image_load_addr, 0);
169
170 end:
171         if (err <= 0) {
172                 printf("spl: error reading image %s, err - %d\n",
173                         CONFIG_SPL_FAT_LOAD_PAYLOAD_NAME, err);
174                 hang();
175         }
176 }
177 static void mmc_load_image(void) __attribute__((unused));
178 static void mmc_load_image(void)
179 {
180         struct mmc *mmc;
181         int err;
182         u32 boot_mode;
183
184         mmc_initialize(gd->bd);
185         /* We register only one device. So, the dev id is always 0 */
186         mmc = find_mmc_device(0);
187         if (!mmc) {
188                 puts("spl: mmc device not found!!\n");
189                 hang();
190         }
191
192         err = mmc_init(mmc);
193         if (err) {
194                 printf("spl: mmc init failed: err - %d\n", err);
195                 hang();
196         }
197 /* For OMAP3 there is no automatic boot mode detection */
198 #ifdef CONFIG_OMAP34XX
199         boot_mode = CONFIG_SYS_MMC_SD_BOOTMODE;
200 #else
201         boot_mode = omap_boot_mode();
202 #endif
203         if (boot_mode == MMCSD_MODE_RAW) {
204                 debug("boot mode - RAW\n");
205                 mmc_load_image_raw(mmc);
206         } else if (boot_mode == MMCSD_MODE_FAT) {
207                 debug("boot mode - FAT\n");
208                 mmc_load_image_fat(mmc);
209         } else {
210                 puts("spl: wrong MMC boot mode\n");
211                 hang();
212         }
213 }
214
215 #ifdef CONFIG_SPL_NAND_SUPPORT
216 static void nand_load_image(void) __attribute__ ((unused));
217 static void nand_load_image(void)
218 {
219         struct image_header *header;
220
221         gpmc_init();
222         nand_init();
223
224         /*use CONFIG_SYS_TEXT_BASE as temporary storage area */
225         header = (struct image_header *)(CONFIG_SYS_TEXT_BASE);
226
227 #ifdef CONFIG_NAND_ENV_DST
228         nand_spl_load_image(CONFIG_ENV_OFFSET,
229                 CONFIG_SYS_NAND_PAGE_SIZE, (void *)header);
230         parse_image_header(header);
231         nand_spl_load_image(CONFIG_ENV_OFFSET, image_size,
232                 (void *)image_load_addr);
233 #ifdef CONFIG_ENV_OFFSET_REDUND
234         nand_spl_load_image(CONFIG_ENV_OFFSET_REDUND,
235                 CONFIG_SYS_NAND_PAGE_SIZE, (void *)header);
236         parse_image_header(header);
237         nand_spl_load_image(CONFIG_ENV_OFFSET_REDUND, image_size,
238                 (void *)image_load_addr);
239 #endif
240 #endif
241         /* Load u-boot */
242         nand_spl_load_image(CONFIG_SYS_NAND_U_BOOT_OFFS,
243                 CONFIG_SYS_NAND_PAGE_SIZE, (void *)header);
244         parse_image_header(header);
245         nand_spl_load_image(CONFIG_SYS_NAND_U_BOOT_OFFS,
246                 image_size, (void *)image_load_addr);
247         nand_deselect();
248 }
249 #endif /* CONFIG_SPL_NAND_SUPPORT */
250 void jump_to_image_no_args(void)
251 {
252         typedef void (*image_entry_noargs_t)(void)__attribute__ ((noreturn));
253         image_entry_noargs_t image_entry =
254                         (image_entry_noargs_t) image_entry_point;
255
256         debug("image entry point: 0x%X\n", image_entry_point);
257         image_entry();
258 }
259
260 void jump_to_image_no_args(void) __attribute__ ((noreturn));
261 void board_init_r(gd_t *id, ulong dummy)
262 {
263         u32 boot_device;
264         debug(">>spl:board_init_r()\n");
265
266         timer_init();
267         i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
268
269         boot_device = omap_boot_device();
270         debug("boot device - %d\n", boot_device);
271         switch (boot_device) {
272 #ifdef CONFIG_SPL_MMC_SUPPORT
273         case BOOT_DEVICE_MMC1:
274         case BOOT_DEVICE_MMC2:
275                 mmc_load_image();
276                 break;
277 #endif
278 #ifdef CONFIG_SPL_NAND_SUPPORT
279         case BOOT_DEVICE_NAND:
280                 nand_load_image();
281                 break;
282 #endif
283         default:
284                 printf("SPL: Un-supported Boot Device - %d!!!\n", boot_device);
285                 hang();
286                 break;
287         }
288
289         switch (image_os) {
290         case IH_OS_U_BOOT:
291                 debug("Jumping to U-Boot\n");
292                 jump_to_image_no_args();
293                 break;
294         default:
295                 puts("Unsupported OS image.. Jumping nevertheless..\n");
296                 jump_to_image_no_args();
297         }
298 }
299
300 /* This requires UART clocks to be enabled */
301 void preloader_console_init(void)
302 {
303         const char *u_boot_rev = U_BOOT_VERSION;
304         char rev_string_buffer[50];
305
306         gd = &gdata;
307         gd->bd = &bdata;
308         gd->flags |= GD_FLG_RELOC;
309         gd->baudrate = CONFIG_BAUDRATE;
310
311         serial_init();          /* serial communications setup */
312
313         /* Avoid a second "U-Boot" coming from this string */
314         u_boot_rev = &u_boot_rev[7];
315
316         printf("\nU-Boot SPL %s (%s - %s)\n", u_boot_rev, U_BOOT_DATE,
317                 U_BOOT_TIME);
318         omap_rev_string(rev_string_buffer);
319         printf("Texas Instruments %s\n", rev_string_buffer);
320 }