bootldr.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * U-Boot - bootldr.c
  3. *
  4. * Copyright (c) 2005-2008 Analog Devices Inc.
  5. *
  6. * See file CREDITS for list of people who contributed to this
  7. * project.
  8. *
  9. * Licensed under the GPL-2 or later.
  10. */
  11. #include <config.h>
  12. #include <common.h>
  13. #include <command.h>
  14. #include <asm/blackfin.h>
  15. #include <asm/mach-common/bits/bootrom.h>
  16. /* Simple sanity check on the specified address to make sure it contains
  17. * an LDR image of some sort.
  18. */
  19. static bool ldr_valid_signature(uint8_t *data)
  20. {
  21. #if defined(__ADSPBF561__)
  22. /* BF56x has a 4 byte global header */
  23. if (data[3] == (GFLAG_56X_SIGN_MAGIC << (GFLAG_56X_SIGN_SHIFT - 24)))
  24. return true;
  25. #elif defined(__ADSPBF531__) || defined(__ADSPBF532__) || defined(__ADSPBF533__) || \
  26. defined(__ADSPBF534__) || defined(__ADSPBF536__) || defined(__ADSPBF537__) || \
  27. defined(__ADSPBF538__) || defined(__ADSPBF539__)
  28. /* all the BF53x should start at this address mask */
  29. uint32_t addr;
  30. memmove(&addr, data, sizeof(addr));
  31. if ((addr & 0xFF0FFF0F) == 0xFF000000)
  32. return true;
  33. #else
  34. /* everything newer has a magic byte */
  35. uint32_t count;
  36. memmove(&count, data + 8, sizeof(count));
  37. if (data[3] == 0xAD && count == 0)
  38. return true;
  39. #endif
  40. return false;
  41. }
  42. /* If the Blackfin is new enough, the Blackfin on-chip ROM supports loading
  43. * LDRs from random memory addresses. So whenever possible, use that. In
  44. * the older cases (BF53x/BF561), parse the LDR format ourselves.
  45. */
  46. static void ldr_load(uint8_t *base_addr)
  47. {
  48. #if defined(__ADSPBF531__) || defined(__ADSPBF532__) || defined(__ADSPBF533__) || \
  49. /*defined(__ADSPBF534__) || defined(__ADSPBF536__) || defined(__ADSPBF537__) ||*/\
  50. defined(__ADSPBF538__) || defined(__ADSPBF539__) || defined(__ADSPBF561__)
  51. uint32_t addr;
  52. uint32_t count;
  53. uint16_t flags;
  54. /* the bf56x has a 4 byte global header ... but it is useless to
  55. * us when booting an LDR from a memory address, so skip it
  56. */
  57. # ifdef __ADSPBF561__
  58. base_addr += 4;
  59. # endif
  60. memmove(&flags, base_addr + 8, sizeof(flags));
  61. bfin_write_EVT1(flags & BFLAG_53X_RESVECT ? 0xFFA00000 : 0xFFA08000);
  62. do {
  63. /* block header may not be aligned */
  64. memmove(&addr, base_addr, sizeof(addr));
  65. memmove(&count, base_addr+4, sizeof(count));
  66. memmove(&flags, base_addr+8, sizeof(flags));
  67. base_addr += sizeof(addr) + sizeof(count) + sizeof(flags);
  68. printf("loading to 0x%08x (%#x bytes) flags: 0x%04x\n",
  69. addr, count, flags);
  70. if (!(flags & BFLAG_53X_IGNORE)) {
  71. if (flags & BFLAG_53X_ZEROFILL)
  72. memset((void *)addr, 0x00, count);
  73. else
  74. memcpy((void *)addr, base_addr, count);
  75. if (flags & BFLAG_53X_INIT) {
  76. void (*init)(void) = (void *)addr;
  77. init();
  78. }
  79. }
  80. if (!(flags & BFLAG_53X_ZEROFILL))
  81. base_addr += count;
  82. } while (!(flags & BFLAG_53X_FINAL));
  83. #endif
  84. }
  85. /* For BF537, we use the _BOOTROM_BOOT_DXE_FLASH funky ROM function.
  86. * For all other BF53x/BF56x, we just call the entry point.
  87. * For everything else (newer), we use _BOOTROM_MEMBOOT ROM function.
  88. */
  89. static void ldr_exec(void *addr)
  90. {
  91. #if defined(__ADSPBF534__) || defined(__ADSPBF536__) || defined(__ADSPBF537__)
  92. /* restore EVT1 to reset value as this is what the bootrom uses as
  93. * the default entry point when booting the final block of LDRs
  94. */
  95. bfin_write_EVT1(L1_INST_SRAM);
  96. __asm__("call (%0);" : : "a"(_BOOTROM_MEMBOOT), "q7"(addr) : "RETS", "memory");
  97. #elif defined(__ADSPBF531__) || defined(__ADSPBF532__) || defined(__ADSPBF533__) || \
  98. defined(__ADSPBF538__) || defined(__ADSPBF539__) || defined(__ADSPBF561__)
  99. void (*ldr_entry)(void) = (void *)bfin_read_EVT1();
  100. ldr_entry();
  101. #else
  102. int32_t (*BOOTROM_MEM)(void *, int32_t, int32_t, void *) = (void *)_BOOTROM_MEMBOOT;
  103. BOOTROM_MEM(addr, 0, 0, NULL);
  104. #endif
  105. }
  106. /*
  107. * the bootldr command loads an address, checks to see if there
  108. * is a Boot stream that the on-chip BOOTROM can understand,
  109. * and loads it via the BOOTROM Callback. It is possible
  110. * to also add booting from SPI, or TWI, but this function does
  111. * not currently support that.
  112. */
  113. int do_bootldr(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  114. {
  115. void *addr;
  116. /* Get the address */
  117. if (argc < 2)
  118. addr = (void *)load_addr;
  119. else
  120. addr = (void *)simple_strtoul(argv[1], NULL, 16);
  121. /* Check if it is a LDR file */
  122. if (ldr_valid_signature(addr)) {
  123. printf("## Booting ldr image at 0x%p ...\n", addr);
  124. ldr_load(addr);
  125. icache_disable();
  126. dcache_disable();
  127. ldr_exec(addr);
  128. } else
  129. printf("## No ldr image at address 0x%p\n", addr);
  130. return 0;
  131. }
  132. U_BOOT_CMD(
  133. bootldr, 2, 0, do_bootldr,
  134. "boot ldr image from memory",
  135. "[addr]\n"
  136. ""
  137. );