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