cmd_bootldr.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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] == 0xA0)
  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. #define ZEROFILL 0x0001
  47. #define RESVECT 0x0002
  48. #define INIT 0x0008
  49. #define IGNORE 0x0010
  50. #define FINAL 0x8000
  51. static void ldr_load(uint8_t *base_addr)
  52. {
  53. #if defined(__ADSPBF531__) || defined(__ADSPBF532__) || defined(__ADSPBF533__) || \
  54. /*defined(__ADSPBF534__) || defined(__ADSPBF536__) || defined(__ADSPBF537__) ||*/\
  55. defined(__ADSPBF538__) || defined(__ADSPBF539__) || defined(__ADSPBF561__)
  56. uint32_t addr;
  57. uint32_t count;
  58. uint16_t flags;
  59. /* the bf56x has a 4 byte global header ... but it is useless to
  60. * us when booting an LDR from a memory address, so skip it
  61. */
  62. # ifdef __ADSPBF561__
  63. base_addr += 4;
  64. # endif
  65. memmove(&flags, base_addr + 8, sizeof(flags));
  66. bfin_write_EVT1(flags & RESVECT ? 0xFFA00000 : 0xFFA08000);
  67. do {
  68. /* block header may not be aligned */
  69. memmove(&addr, base_addr, sizeof(addr));
  70. memmove(&count, base_addr+4, sizeof(count));
  71. memmove(&flags, base_addr+8, sizeof(flags));
  72. base_addr += sizeof(addr) + sizeof(count) + sizeof(flags);
  73. printf("loading to 0x%08x (0x%x bytes) flags: 0x%04x\n",
  74. addr, count, flags);
  75. if (!(flags & IGNORE)) {
  76. if (flags & ZEROFILL)
  77. memset((void *)addr, 0x00, count);
  78. else
  79. memcpy((void *)addr, base_addr, count);
  80. if (flags & INIT) {
  81. void (*init)(void) = (void *)addr;
  82. init();
  83. }
  84. }
  85. if (!(flags & ZEROFILL))
  86. base_addr += count;
  87. } while (!(flags & FINAL));
  88. #endif
  89. }
  90. /* For BF537, we use the _BOOTROM_BOOT_DXE_FLASH funky ROM function.
  91. * For all other BF53x/BF56x, we just call the entry point.
  92. * For everything else (newer), we use _BOOTROM_MEMBOOT ROM function.
  93. */
  94. static void ldr_exec(void *addr)
  95. {
  96. #if defined(__ADSPBF534__) || defined(__ADSPBF536__) || defined(__ADSPBF537__)
  97. /* restore EVT1 to reset value as this is what the bootrom uses as
  98. * the default entry point when booting the final block of LDRs
  99. */
  100. bfin_write_EVT1(L1_INST_SRAM);
  101. __asm__("call (%0);" : : "a"(_BOOTROM_MEMBOOT), "q7"(addr) : "RETS", "memory");
  102. #elif defined(__ADSPBF531__) || defined(__ADSPBF532__) || defined(__ADSPBF533__) || \
  103. defined(__ADSPBF538__) || defined(__ADSPBF539__) || defined(__ADSPBF561__)
  104. void (*ldr_entry)(void) = (void *)bfin_read_EVT1();
  105. ldr_entry();
  106. #else
  107. int32_t (*BOOTROM_MEM)(void *, int32_t, int32_t, void *) = (void *)_BOOTROM_MEMBOOT;
  108. BOOTROM_MEM(addr, 0, 0, NULL);
  109. #endif
  110. }
  111. /*
  112. * the bootldr command loads an address, checks to see if there
  113. * is a Boot stream that the on-chip BOOTROM can understand,
  114. * and loads it via the BOOTROM Callback. It is possible
  115. * to also add booting from SPI, or TWI, but this function does
  116. * not currently support that.
  117. */
  118. int do_bootldr(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  119. {
  120. void *addr;
  121. /* Get the address */
  122. if (argc < 2)
  123. addr = (void *)load_addr;
  124. else
  125. addr = (void *)simple_strtoul(argv[1], NULL, 16);
  126. /* Check if it is a LDR file */
  127. if (ldr_valid_signature(addr)) {
  128. printf("## Booting ldr image at 0x%p ...\n", addr);
  129. ldr_load(addr);
  130. icache_disable();
  131. dcache_disable();
  132. ldr_exec(addr);
  133. } else
  134. printf("## No ldr image at address 0x%p\n", addr);
  135. return 0;
  136. }
  137. U_BOOT_CMD(bootldr, 2, 0, do_bootldr,
  138. "boot ldr image from memory",
  139. "[addr]\n"
  140. ""
  141. );