spl.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2014-2016 Stefan Roese <sr@denx.de>
  4. */
  5. #include <common.h>
  6. #include <dm.h>
  7. #include <debug_uart.h>
  8. #include <fdtdec.h>
  9. #include <hang.h>
  10. #include <image.h>
  11. #include <init.h>
  12. #include <log.h>
  13. #include <spl.h>
  14. #include <asm/global_data.h>
  15. #include <asm/io.h>
  16. #include <asm/arch/cpu.h>
  17. #include <asm/arch/soc.h>
  18. #if defined(CONFIG_SPL_SPI_FLASH_SUPPORT) || defined(CONFIG_SPL_MMC_SUPPORT) || defined(CONFIG_SPL_SATA_SUPPORT)
  19. /*
  20. * When loading U-Boot via SPL from SPI NOR, CONFIG_SYS_SPI_U_BOOT_OFFS must
  21. * point to the offset of kwbimage main header which is always at offset zero
  22. * (defined by BootROM). Therefore other values of CONFIG_SYS_SPI_U_BOOT_OFFS
  23. * makes U-Boot non-bootable.
  24. */
  25. #ifdef CONFIG_SPL_SPI_FLASH_SUPPORT
  26. #if defined(CONFIG_SYS_SPI_U_BOOT_OFFS) && CONFIG_SYS_SPI_U_BOOT_OFFS != 0
  27. #error CONFIG_SYS_SPI_U_BOOT_OFFS must be set to 0
  28. #endif
  29. #endif
  30. /*
  31. * When loading U-Boot via SPL from eMMC (in Marvell terminology SDIO), the
  32. * kwbimage main header is stored at sector 0. U-Boot SPL needs to parse this
  33. * header and figure out at which sector the U-Boot proper binary is stored.
  34. * Partition booting is therefore not supported and CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR
  35. * and CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_DATA_PART_OFFSET need to point to the
  36. * kwbimage main header.
  37. */
  38. #ifdef CONFIG_SPL_MMC_SUPPORT
  39. #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION
  40. #error CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION is unsupported
  41. #endif
  42. #if defined(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR) && CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR != 0
  43. #error CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR must be set to 0
  44. #endif
  45. #if defined(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_DATA_PART_OFFSET) && CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_DATA_PART_OFFSET != 0
  46. #error CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_DATA_PART_OFFSET must be set to 0
  47. #endif
  48. #endif
  49. /*
  50. * When loading U-Boot via SPL from SATA disk, the kwbimage main header is
  51. * stored at sector 1. Therefore CONFIG_SPL_SATA_RAW_U_BOOT_SECTOR must be
  52. * set to 1. Otherwise U-Boot SPL would not be able to load U-Boot proper.
  53. */
  54. #ifdef CONFIG_SPL_SATA_SUPPORT
  55. #if !defined(CONFIG_SPL_SATA_RAW_U_BOOT_USE_SECTOR) || !defined(CONFIG_SPL_SATA_RAW_U_BOOT_SECTOR) || CONFIG_SPL_SATA_RAW_U_BOOT_SECTOR != 1
  56. #error CONFIG_SPL_SATA_RAW_U_BOOT_SECTOR must be set to 1
  57. #endif
  58. #endif
  59. /* Boot Type - block ID */
  60. #define IBR_HDR_I2C_ID 0x4D
  61. #define IBR_HDR_SPI_ID 0x5A
  62. #define IBR_HDR_NAND_ID 0x8B
  63. #define IBR_HDR_SATA_ID 0x78
  64. #define IBR_HDR_PEX_ID 0x9C
  65. #define IBR_HDR_UART_ID 0x69
  66. #define IBR_HDR_SDIO_ID 0xAE
  67. /* Structure of the main header, version 1 (Armada 370/38x/XP) */
  68. struct kwbimage_main_hdr_v1 {
  69. uint8_t blockid; /* 0x0 */
  70. uint8_t flags; /* 0x1 */
  71. uint16_t reserved2; /* 0x2-0x3 */
  72. uint32_t blocksize; /* 0x4-0x7 */
  73. uint8_t version; /* 0x8 */
  74. uint8_t headersz_msb; /* 0x9 */
  75. uint16_t headersz_lsb; /* 0xA-0xB */
  76. uint32_t srcaddr; /* 0xC-0xF */
  77. uint32_t destaddr; /* 0x10-0x13 */
  78. uint32_t execaddr; /* 0x14-0x17 */
  79. uint8_t options; /* 0x18 */
  80. uint8_t nandblocksize; /* 0x19 */
  81. uint8_t nandbadblklocation; /* 0x1A */
  82. uint8_t reserved4; /* 0x1B */
  83. uint16_t reserved5; /* 0x1C-0x1D */
  84. uint8_t ext; /* 0x1E */
  85. uint8_t checksum; /* 0x1F */
  86. } __packed;
  87. #ifdef CONFIG_SPL_MMC_SUPPORT
  88. u32 spl_mmc_boot_mode(const u32 boot_device)
  89. {
  90. return MMCSD_MODE_RAW;
  91. }
  92. #endif
  93. int spl_parse_board_header(struct spl_image_info *spl_image,
  94. const void *image_header, size_t size)
  95. {
  96. const struct kwbimage_main_hdr_v1 *mhdr = image_header;
  97. if (size < sizeof(*mhdr)) {
  98. /* This should be compile time assert */
  99. printf("FATAL ERROR: Image header size is too small\n");
  100. hang();
  101. }
  102. /*
  103. * Very basic check for image validity. We cannot check mhdr->checksum
  104. * as it is calculated also from variable length extended headers
  105. * (including SPL content) which is not included in U-Boot image_header.
  106. */
  107. if (mhdr->version != 1 ||
  108. ((mhdr->headersz_msb << 16) | mhdr->headersz_lsb) < sizeof(*mhdr) ||
  109. (
  110. #ifdef CONFIG_SPL_SPI_FLASH_SUPPORT
  111. mhdr->blockid != IBR_HDR_SPI_ID &&
  112. #endif
  113. #ifdef CONFIG_SPL_SATA_SUPPORT
  114. mhdr->blockid != IBR_HDR_SATA_ID &&
  115. #endif
  116. #ifdef CONFIG_SPL_MMC_SUPPORT
  117. mhdr->blockid != IBR_HDR_SDIO_ID &&
  118. #endif
  119. 1
  120. )) {
  121. printf("ERROR: Not valid SPI/NAND/SATA/SDIO kwbimage v1\n");
  122. return -EINVAL;
  123. }
  124. spl_image->offset = mhdr->srcaddr;
  125. #ifdef CONFIG_SPL_SATA_SUPPORT
  126. /*
  127. * For SATA srcaddr is specified in number of sectors.
  128. * The main header is must be stored at sector number 1.
  129. * This expects that sector size is 512 bytes and recalculates
  130. * data offset to bytes relative to the main header.
  131. */
  132. if (mhdr->blockid == IBR_HDR_SATA_ID) {
  133. if (spl_image->offset < 1) {
  134. printf("ERROR: Wrong SATA srcaddr in kwbimage\n");
  135. return -EINVAL;
  136. }
  137. spl_image->offset -= 1;
  138. spl_image->offset *= 512;
  139. }
  140. #endif
  141. #ifdef CONFIG_SPL_MMC_SUPPORT
  142. /*
  143. * For SDIO (eMMC) srcaddr is specified in number of sectors.
  144. * This expects that sector size is 512 bytes and recalculates
  145. * data offset to bytes.
  146. */
  147. if (mhdr->blockid == IBR_HDR_SDIO_ID)
  148. spl_image->offset *= 512;
  149. #endif
  150. spl_image->size = mhdr->blocksize;
  151. spl_image->entry_point = mhdr->execaddr;
  152. spl_image->load_addr = mhdr->destaddr;
  153. spl_image->os = IH_OS_U_BOOT;
  154. spl_image->name = "U-Boot";
  155. return 0;
  156. }
  157. static u32 get_boot_device(void)
  158. {
  159. u32 val;
  160. u32 boot_device;
  161. /*
  162. * First check, if UART boot-mode is active. This can only
  163. * be done, via the bootrom error register. Here the
  164. * MSB marks if the UART mode is active.
  165. */
  166. val = readl(CONFIG_BOOTROM_ERR_REG);
  167. boot_device = (val & BOOTROM_ERR_MODE_MASK) >> BOOTROM_ERR_MODE_OFFS;
  168. debug("BOOTROM_REG=0x%08x boot_device=0x%x\n", val, boot_device);
  169. if (boot_device == BOOTROM_ERR_MODE_UART)
  170. return BOOT_DEVICE_UART;
  171. #ifdef CONFIG_ARMADA_38X
  172. /*
  173. * If the bootrom error code contains any other than zeros it's an
  174. * error condition and the bootROM has fallen back to UART boot
  175. */
  176. boot_device = (val & BOOTROM_ERR_CODE_MASK) >> BOOTROM_ERR_CODE_OFFS;
  177. if (boot_device)
  178. return BOOT_DEVICE_UART;
  179. #endif
  180. /*
  181. * Now check the SAR register for the strapped boot-device
  182. */
  183. val = readl(CONFIG_SAR_REG); /* SAR - Sample At Reset */
  184. boot_device = (val & BOOT_DEV_SEL_MASK) >> BOOT_DEV_SEL_OFFS;
  185. debug("SAR_REG=0x%08x boot_device=0x%x\n", val, boot_device);
  186. switch (boot_device) {
  187. #ifdef BOOT_FROM_NAND
  188. case BOOT_FROM_NAND:
  189. return BOOT_DEVICE_NAND;
  190. #endif
  191. #ifdef BOOT_FROM_MMC
  192. case BOOT_FROM_MMC:
  193. case BOOT_FROM_MMC_ALT:
  194. return BOOT_DEVICE_MMC1;
  195. #endif
  196. case BOOT_FROM_UART:
  197. #ifdef BOOT_FROM_UART_ALT
  198. case BOOT_FROM_UART_ALT:
  199. #endif
  200. return BOOT_DEVICE_UART;
  201. #ifdef BOOT_FROM_SATA
  202. case BOOT_FROM_SATA:
  203. case BOOT_FROM_SATA_ALT:
  204. return BOOT_DEVICE_SATA;
  205. #endif
  206. case BOOT_FROM_SPI:
  207. return BOOT_DEVICE_SPI;
  208. default:
  209. return BOOT_DEVICE_BOOTROM;
  210. };
  211. }
  212. #else
  213. static u32 get_boot_device(void)
  214. {
  215. return BOOT_DEVICE_BOOTROM;
  216. }
  217. #endif
  218. u32 spl_boot_device(void)
  219. {
  220. u32 boot_device = get_boot_device();
  221. switch (boot_device) {
  222. /*
  223. * Return to the BootROM to continue the Marvell xmodem
  224. * UART boot protocol. As initiated by the kwboot tool.
  225. *
  226. * This can only be done by the BootROM since the beginning
  227. * of the image is already read and interpreted by the BootROM.
  228. * SPL has no chance to receive this information. So we
  229. * need to return to the BootROM to enable this xmodem
  230. * UART download. Use SPL infrastructure to return to BootROM.
  231. */
  232. case BOOT_DEVICE_UART:
  233. return BOOT_DEVICE_BOOTROM;
  234. /*
  235. * If SPL is compiled with chosen boot_device support
  236. * then use SPL driver for loading U-Boot proper.
  237. */
  238. #ifdef CONFIG_SPL_MMC_SUPPORT
  239. case BOOT_DEVICE_MMC1:
  240. return BOOT_DEVICE_MMC1;
  241. #endif
  242. #ifdef CONFIG_SPL_SATA_SUPPORT
  243. case BOOT_FROM_SATA:
  244. return BOOT_FROM_SATA;
  245. #endif
  246. #ifdef CONFIG_SPL_SPI_FLASH_SUPPORT
  247. case BOOT_DEVICE_SPI:
  248. return BOOT_DEVICE_SPI;
  249. #endif
  250. /*
  251. * If SPL is not compiled with chosen boot_device support
  252. * then return to the BootROM. BootROM supports loading
  253. * U-Boot proper from any valid boot_device present in SAR
  254. * register.
  255. */
  256. default:
  257. return BOOT_DEVICE_BOOTROM;
  258. }
  259. }
  260. int board_return_to_bootrom(struct spl_image_info *spl_image,
  261. struct spl_boot_device *bootdev)
  262. {
  263. u32 *regs = *(u32 **)CONFIG_SPL_BOOTROM_SAVE;
  264. printf("Returning to BootROM (return address 0x%08x)...\n", regs[13]);
  265. return_to_bootrom();
  266. /* NOTREACHED - return_to_bootrom() does not return */
  267. hang();
  268. }
  269. void board_init_f(ulong dummy)
  270. {
  271. int ret;
  272. /*
  273. * Pin muxing needs to be done before UART output, since
  274. * on A38x the UART pins need some re-muxing for output
  275. * to work.
  276. */
  277. board_early_init_f();
  278. /* Example code showing how to enable the debug UART on MVEBU */
  279. #ifdef EARLY_UART
  280. /*
  281. * Debug UART can be used from here if required:
  282. *
  283. * debug_uart_init();
  284. * printch('a');
  285. * printhex8(0x1234);
  286. * printascii("string");
  287. */
  288. #endif
  289. /*
  290. * Use special translation offset for SPL. This needs to be
  291. * configured *before* spl_init() is called as this function
  292. * calls dm_init() which calls the bind functions of the
  293. * device drivers. Here the base address needs to be configured
  294. * (translated) correctly.
  295. */
  296. gd->translation_offset = 0xd0000000 - 0xf1000000;
  297. ret = spl_init();
  298. if (ret) {
  299. debug("spl_init() failed: %d\n", ret);
  300. hang();
  301. }
  302. preloader_console_init();
  303. timer_init();
  304. /* Armada 375 does not support SerDes and DDR3 init yet */
  305. #if !defined(CONFIG_ARMADA_375)
  306. /* First init the serdes PHY's */
  307. serdes_phy_config();
  308. /* Setup DDR */
  309. ddr3_init();
  310. #endif
  311. /* Initialize Auto Voltage Scaling */
  312. mv_avs_init();
  313. /* Update read timing control for PCIe */
  314. mv_rtc_config();
  315. }