spl.c 8.1 KB

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