omap-common: add nand spl support
[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
198         boot_mode = omap_boot_mode();
199         if (boot_mode == MMCSD_MODE_RAW) {
200                 debug("boot mode - RAW\n");
201                 mmc_load_image_raw(mmc);
202         } else if (boot_mode == MMCSD_MODE_FAT) {
203                 debug("boot mode - FAT\n");
204                 mmc_load_image_fat(mmc);
205         } else {
206                 puts("spl: wrong MMC boot mode\n");
207                 hang();
208         }
209 }
210
211 #ifdef CONFIG_SPL_NAND_SUPPORT
212 static void nand_load_image(void) __attribute__ ((unused));
213 static void nand_load_image(void)
214 {
215         struct image_header *header;
216
217         gpmc_init();
218         nand_init();
219
220         /*use CONFIG_SYS_TEXT_BASE as temporary storage area */
221         header = (struct image_header *)(CONFIG_SYS_TEXT_BASE);
222
223 #ifdef CONFIG_NAND_ENV_DST
224         nand_spl_load_image(CONFIG_ENV_OFFSET,
225                 CONFIG_SYS_NAND_PAGE_SIZE, (void *)header);
226         parse_image_header(header);
227         nand_spl_load_image(CONFIG_ENV_OFFSET, image_size,
228                 (void *)image_load_addr);
229 #ifdef CONFIG_ENV_OFFSET_REDUND
230         nand_spl_load_image(CONFIG_ENV_OFFSET_REDUND,
231                 CONFIG_SYS_NAND_PAGE_SIZE, (void *)header);
232         parse_image_header(header);
233         nand_spl_load_image(CONFIG_ENV_OFFSET_REDUND, image_size,
234                 (void *)image_load_addr);
235 #endif
236 #endif
237         /* Load u-boot */
238         nand_spl_load_image(CONFIG_SYS_NAND_U_BOOT_OFFS,
239                 CONFIG_SYS_NAND_PAGE_SIZE, (void *)header);
240         parse_image_header(header);
241         nand_spl_load_image(CONFIG_SYS_NAND_U_BOOT_OFFS,
242                 image_size, (void *)image_load_addr);
243         nand_deselect();
244 }
245 #endif /* CONFIG_SPL_NAND_SUPPORT */
246 void jump_to_image_no_args(void)
247 {
248         typedef void (*image_entry_noargs_t)(void)__attribute__ ((noreturn));
249         image_entry_noargs_t image_entry =
250                         (image_entry_noargs_t) image_entry_point;
251
252         debug("image entry point: 0x%X\n", image_entry_point);
253         image_entry();
254 }
255
256 void jump_to_image_no_args(void) __attribute__ ((noreturn));
257 void board_init_r(gd_t *id, ulong dummy)
258 {
259         u32 boot_device;
260         debug(">>spl:board_init_r()\n");
261
262         timer_init();
263         i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
264
265         boot_device = omap_boot_device();
266         debug("boot device - %d\n", boot_device);
267         switch (boot_device) {
268 #ifdef CONFIG_SPL_MMC_SUPPORT
269         case BOOT_DEVICE_MMC1:
270         case BOOT_DEVICE_MMC2:
271                 mmc_load_image();
272                 break;
273 #endif
274 #ifdef CONFIG_SPL_NAND_SUPPORT
275         case BOOT_DEVICE_NAND:
276                 nand_load_image();
277                 break;
278 #endif
279         default:
280                 printf("SPL: Un-supported Boot Device - %d!!!\n", boot_device);
281                 hang();
282                 break;
283         }
284
285         switch (image_os) {
286         case IH_OS_U_BOOT:
287                 debug("Jumping to U-Boot\n");
288                 jump_to_image_no_args();
289                 break;
290         default:
291                 puts("Unsupported OS image.. Jumping nevertheless..\n");
292                 jump_to_image_no_args();
293         }
294 }
295
296 /* This requires UART clocks to be enabled */
297 void preloader_console_init(void)
298 {
299         const char *u_boot_rev = U_BOOT_VERSION;
300         char rev_string_buffer[50];
301
302         gd = &gdata;
303         gd->bd = &bdata;
304         gd->flags |= GD_FLG_RELOC;
305         gd->baudrate = CONFIG_BAUDRATE;
306
307         serial_init();          /* serial communications setup */
308
309         /* Avoid a second "U-Boot" coming from this string */
310         u_boot_rev = &u_boot_rev[7];
311
312         printf("\nU-Boot SPL %s (%s - %s)\n", u_boot_rev, U_BOOT_DATE,
313                 U_BOOT_TIME);
314         omap_rev_string(rev_string_buffer);
315         printf("Texas Instruments %s\n", rev_string_buffer);
316 }