]> git.sur5r.net Git - u-boot/blob - arch/arm/cpu/armv7/omap-common/spl.c
d1776522b7fa5d00e936eb07f9050a547baeffe6
[u-boot] / 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 <mmc.h>
30 #include <fat.h>
31 #include <timestamp_autogenerated.h>
32 #include <version_autogenerated.h>
33 #include <asm/omap_common.h>
34 #include <asm/arch/mmc_host_def.h>
35 #include <i2c.h>
36 #include <image.h>
37
38 DECLARE_GLOBAL_DATA_PTR;
39
40 /* Define global data structure pointer to it*/
41 static gd_t gdata __attribute__ ((section(".data")));
42 static bd_t bdata __attribute__ ((section(".data")));
43 static const char *image_name;
44 static u8 image_os;
45 static u32 image_load_addr;
46 static u32 image_entry_point;
47 static u32 image_size;
48
49 inline void hang(void)
50 {
51         puts("### ERROR ### Please RESET the board ###\n");
52         for (;;)
53                 ;
54 }
55
56 void board_init_f(ulong dummy)
57 {
58         /*
59          * We call relocate_code() with relocation target same as the
60          * CONFIG_SYS_SPL_TEXT_BASE. This will result in relocation getting
61          * skipped. Instead, only .bss initialization will happen. That's
62          * all we need
63          */
64         debug(">>board_init_f()\n");
65         relocate_code(CONFIG_SPL_STACK, &gdata, CONFIG_SPL_TEXT_BASE);
66 }
67
68 #ifdef CONFIG_GENERIC_MMC
69 int board_mmc_init(bd_t *bis)
70 {
71         switch (omap_boot_device()) {
72         case BOOT_DEVICE_MMC1:
73                 omap_mmc_init(0);
74                 break;
75         case BOOT_DEVICE_MMC2:
76                 omap_mmc_init(1);
77                 break;
78         }
79         return 0;
80 }
81 #endif
82
83 static void parse_image_header(const struct image_header *header)
84 {
85         u32 header_size = sizeof(struct image_header);
86
87         if (__be32_to_cpu(header->ih_magic) == IH_MAGIC) {
88                 image_size = __be32_to_cpu(header->ih_size) + header_size;
89                 image_entry_point = __be32_to_cpu(header->ih_load);
90                 /* Load including the header */
91                 image_load_addr = image_entry_point - header_size;
92                 image_os = header->ih_os;
93                 image_name = (const char *)&header->ih_name;
94                 debug("spl: payload image: %s load addr: 0x%x size: %d\n",
95                         image_name, image_load_addr, image_size);
96         } else {
97                 /* Signature not found - assume u-boot.bin */
98                 printf("mkimage signature not found - ih_magic = %x\n",
99                         header->ih_magic);
100                 puts("Assuming u-boot.bin ..\n");
101                 /* Let's assume U-Boot will not be more than 200 KB */
102                 image_size = 200 * 1024;
103                 image_entry_point = CONFIG_SYS_TEXT_BASE;
104                 image_load_addr = CONFIG_SYS_TEXT_BASE;
105                 image_os = IH_OS_U_BOOT;
106                 image_name = "U-Boot";
107         }
108 }
109
110 static void mmc_load_image_raw(struct mmc *mmc)
111 {
112         u32 image_size_sectors, err;
113         const struct image_header *header;
114
115         header = (struct image_header *)(CONFIG_SYS_TEXT_BASE -
116                                                 sizeof(struct image_header));
117
118         /* read image header to find the image size & load address */
119         err = mmc->block_dev.block_read(0,
120                         CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR, 1,
121                         (void *)header);
122
123         if (err <= 0)
124                 goto end;
125
126         parse_image_header(header);
127
128         /* convert size to sectors - round up */
129         image_size_sectors = (image_size + MMCSD_SECTOR_SIZE - 1) /
130                                 MMCSD_SECTOR_SIZE;
131
132         /* Read the header too to avoid extra memcpy */
133         err = mmc->block_dev.block_read(0,
134                         CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR,
135                         image_size_sectors, (void *)image_load_addr);
136
137 end:
138         if (err <= 0) {
139                 printf("spl: mmc blk read err - %d\n", err);
140                 hang();
141         }
142 }
143
144 static void mmc_load_image_fat(struct mmc *mmc)
145 {
146         s32 err;
147         struct image_header *header;
148
149         header = (struct image_header *)(CONFIG_SYS_TEXT_BASE -
150                                                 sizeof(struct image_header));
151
152         err = fat_register_device(&mmc->block_dev,
153                                 CONFIG_SYS_MMC_SD_FAT_BOOT_PARTITION);
154         if (err) {
155                 printf("spl: fat register err - %d\n", err);
156                 hang();
157         }
158
159         err = file_fat_read(CONFIG_SPL_FAT_LOAD_PAYLOAD_NAME,
160                                 (u8 *)header, sizeof(struct image_header));
161         if (err <= 0)
162                 goto end;
163
164         parse_image_header(header);
165
166         err = file_fat_read(CONFIG_SPL_FAT_LOAD_PAYLOAD_NAME,
167                                 (u8 *)image_load_addr, 0);
168
169 end:
170         if (err <= 0) {
171                 printf("spl: error reading image %s, err - %d\n",
172                         CONFIG_SPL_FAT_LOAD_PAYLOAD_NAME, err);
173                 hang();
174         }
175 }
176
177 static void mmc_load_image(void)
178 {
179         struct mmc *mmc;
180         int err;
181         u32 boot_mode;
182
183         mmc_initialize(gd->bd);
184         /* We register only one device. So, the dev id is always 0 */
185         mmc = find_mmc_device(0);
186         if (!mmc) {
187                 puts("spl: mmc device not found!!\n");
188                 hang();
189         }
190
191         err = mmc_init(mmc);
192         if (err) {
193                 printf("spl: mmc init failed: err - %d\n", err);
194                 hang();
195         }
196
197         boot_mode = omap_boot_mode();
198         if (boot_mode == MMCSD_MODE_RAW) {
199                 debug("boot mode - RAW\n");
200                 mmc_load_image_raw(mmc);
201         } else if (boot_mode == MMCSD_MODE_FAT) {
202                 debug("boot mode - FAT\n");
203                 mmc_load_image_fat(mmc);
204         } else {
205                 puts("spl: wrong MMC boot mode\n");
206                 hang();
207         }
208 }
209
210 void jump_to_image_no_args(void)
211 {
212         typedef void (*image_entry_noargs_t)(void)__attribute__ ((noreturn));
213         image_entry_noargs_t image_entry =
214                         (image_entry_noargs_t) image_entry_point;
215
216         image_entry();
217 }
218
219 void jump_to_image_no_args(void) __attribute__ ((noreturn));
220 void board_init_r(gd_t *id, ulong dummy)
221 {
222         u32 boot_device;
223         debug(">>spl:board_init_r()\n");
224
225         timer_init();
226         i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
227
228         boot_device = omap_boot_device();
229         debug("boot device - %d\n", boot_device);
230         switch (boot_device) {
231         case BOOT_DEVICE_MMC1:
232         case BOOT_DEVICE_MMC2:
233                 mmc_load_image();
234                 break;
235         default:
236                 printf("SPL: Un-supported Boot Device - %d!!!\n", boot_device);
237                 hang();
238                 break;
239         }
240
241         switch (image_os) {
242         case IH_OS_U_BOOT:
243                 debug("Jumping to U-Boot\n");
244                 jump_to_image_no_args();
245                 break;
246         default:
247                 puts("Unsupported OS image.. Jumping nevertheless..\n");
248                 jump_to_image_no_args();
249         }
250 }
251
252 void preloader_console_init(void)
253 {
254         const char *u_boot_rev = U_BOOT_VERSION;
255         char rev_string_buffer[50];
256
257         gd = &gdata;
258         gd->bd = &bdata;
259         gd->flags |= GD_FLG_RELOC;
260         gd->baudrate = CONFIG_BAUDRATE;
261
262         setup_clocks_for_console();
263         serial_init();          /* serial communications setup */
264
265         /* Avoid a second "U-Boot" coming from this string */
266         u_boot_rev = &u_boot_rev[7];
267
268         printf("\nU-Boot SPL %s (%s - %s)\n", u_boot_rev, U_BOOT_DATE,
269                 U_BOOT_TIME);
270         omap_rev_string(rev_string_buffer);
271         printf("Texas Instruments %s\n", rev_string_buffer);
272 }