|
| 1 | + /* Assembly program to go into the boot ROM |
| 2 | + For use with the simple_spi core and standard SPI flash |
| 3 | + interface-compatible parts (ST M25P16 for example.) |
| 4 | +
|
| 5 | + Loads an image in the U-boot uimage format from SPI Flash |
| 6 | + in the the RAM at the address specified in the uimage header. |
| 7 | + After loading is completed, it will jump to the entry point |
| 8 | + address specified in the uimage header. |
| 9 | +
|
| 10 | + To keep it simple, only basic sanity checks are made on the image |
| 11 | + */ |
| 12 | + |
| 13 | +/* Base address of the SPI controller used to communicate with the Flash */ |
| 14 | +#ifndef SPI_BASE |
| 15 | +#define SPI_BASE 0x80001040 |
| 16 | +#endif |
| 17 | + |
| 18 | +/* Base address of the SPI controller used to blink LED */ |
| 19 | +#ifndef GPIO_BASE |
| 20 | +#define GPIO_BASE 0x80001010 |
| 21 | +#endif |
| 22 | + |
| 23 | +/* 24-bit address in SPI Flash where application image is stored */ |
| 24 | +#ifndef BOOTROM_ADDR |
| 25 | +#define BOOTROM_ADDR 0x000000 |
| 26 | +#endif |
| 27 | + |
| 28 | +/* Flash needs 300 us to warm up from power-on. In practice, this is only an |
| 29 | + issue in simulations as FPGA boot is far longer than 300 us |
| 30 | +*/ |
| 31 | +#ifndef INIT_DELAY |
| 32 | +#define INIT_DELAY 700 |
| 33 | +#endif |
| 34 | + |
| 35 | +#define RAM_INIT_DONE 0x8000100A |
| 36 | + |
| 37 | +#define RETRIES 3 |
| 38 | + |
| 39 | +#define SPI_SPCR 0x00 |
| 40 | +#define SPI_SPSR 0x08 |
| 41 | +#define SPI_SPDR 0x10 |
| 42 | +#define SPI_SPER 0x18 |
| 43 | +#define SPI_SPSS 0x20 |
| 44 | + |
| 45 | +#define SPI_SPSS_INIT 0x1 |
| 46 | +#define SPI_SPSR_RX_CHECK 0x01 /* Check bit 0 is cleared, fifo !empty*/ |
| 47 | + |
| 48 | +.globl _start |
| 49 | +_start: |
| 50 | + /* Registers used |
| 51 | + ra link register |
| 52 | + t1 temp register |
| 53 | + t2 temp register |
| 54 | + s0 Image size |
| 55 | + s1 Load address |
| 56 | + s2 Reset vector |
| 57 | + s3 Retry counter |
| 58 | + gp SPI master base address |
| 59 | + a3 get_rx_data return value |
| 60 | + t3 temp register |
| 61 | + */ |
| 62 | + |
| 63 | +boot_init: |
| 64 | + |
| 65 | +#if INIT_DELAY |
| 66 | + li t1, INIT_DELAY |
| 67 | + and t2, zero, zero |
| 68 | +1: addi t2,t2,1 |
| 69 | + bne t1, t2, 1b |
| 70 | +#endif |
| 71 | + |
| 72 | + /* Wait until RAM initialization is done */ |
| 73 | + li t1, RAM_INIT_DONE |
| 74 | +1: lbu t2, 0(t1) |
| 75 | + beqz t2, 1b |
| 76 | + |
| 77 | + /* Load SPI base address to gp */ |
| 78 | + li gp, SPI_BASE |
| 79 | + |
| 80 | + li s3, RETRIES |
| 81 | +spi_init: |
| 82 | + /* Clear slave selects */ |
| 83 | + sb zero, SPI_SPSS(gp) |
| 84 | + fence |
| 85 | + |
| 86 | + /* Set clock divider to 4 (arbitrarily chosen value) |
| 87 | + and enable controller */ |
| 88 | + addi t1, zero, 0x40 | 0x01 |
| 89 | + sb t1, SPI_SPCR(gp) |
| 90 | + fence |
| 91 | + |
| 92 | + /* Set appropriate slave select */ |
| 93 | + addi t1, zero, 1 |
| 94 | + sb t1, SPI_SPSS(gp) |
| 95 | + fence |
| 96 | + |
| 97 | + /* Set command to READ at BOOTROM_ADDR */ |
| 98 | + li a0, ((BOOTROM_ADDR & 0xFF) <<24) | ((BOOTROM_ADDR & 0xFF00) << 8) | ((BOOTROM_ADDR & 0xFF0000) >> 8) | 0x3 |
| 99 | + jal spi_xfer |
| 100 | + |
| 101 | + /* Get magic word */ |
| 102 | + jal spi_xfer |
| 103 | + |
| 104 | + /* Verify that magic word is (endian-swapped) 0x27051956 |
| 105 | + Retry a couple of times before we give up */ |
| 106 | + addi s3, s3, -1 |
| 107 | + beqz s3, boot_fail |
| 108 | + li t1, 0x56190527 |
| 109 | + bne t1, a1, spi_init |
| 110 | + |
| 111 | + |
| 112 | +read_header: |
| 113 | + /* Dummy read two words */ |
| 114 | + jal spi_xfer |
| 115 | + jal spi_xfer |
| 116 | + |
| 117 | + /* Load image size to s0 */ |
| 118 | + jal spi_xfer |
| 119 | + jal endian_swap |
| 120 | + mv s0, a2 |
| 121 | + |
| 122 | + /* Load RAM base address to s1 */ |
| 123 | + jal spi_xfer |
| 124 | + jal endian_swap |
| 125 | + mv s1, a2 |
| 126 | + |
| 127 | + /* Load reset vector to s2 */ |
| 128 | + jal spi_xfer |
| 129 | + jal endian_swap |
| 130 | + mv s2, a2 |
| 131 | + |
| 132 | + /* Dummy read rest of header */ |
| 133 | + jal spi_xfer //dcrc |
| 134 | + jal spi_xfer //os, arch, type, comp |
| 135 | + |
| 136 | + jal spi_xfer |
| 137 | + jal spi_xfer |
| 138 | + jal spi_xfer |
| 139 | + jal spi_xfer |
| 140 | + jal spi_xfer |
| 141 | + jal spi_xfer |
| 142 | + jal spi_xfer |
| 143 | + jal spi_xfer |
| 144 | + |
| 145 | + /* Clear number of copied bytes */ |
| 146 | + addi s4, zero, 0 |
| 147 | +copy_to_ram: |
| 148 | + jal spi_xfer |
| 149 | + |
| 150 | + /* Set memory store address */ |
| 151 | + add t1, s1, s4 |
| 152 | + |
| 153 | + /* Write word to RAM */ |
| 154 | + sw a1, 0(t1) |
| 155 | + fence |
| 156 | + |
| 157 | + /* Increase counter */ |
| 158 | + addi s4, s4, 4 |
| 159 | + |
| 160 | + /* Check if file is completely copied */ |
| 161 | + bge s0, s4, copy_to_ram |
| 162 | + |
| 163 | + /* Jump to entry point */ |
| 164 | +goto_reset: |
| 165 | + jr s2 |
| 166 | + |
| 167 | + /* Spin here on boot failures */ |
| 168 | +boot_fail: j boot_fail |
| 169 | + |
| 170 | + /* Reads ddccbbaa from a1, stores aabbccdd to a2 */ |
| 171 | +endian_swap: |
| 172 | + slli a2, a1, 24 // a2 = aa000000 |
| 173 | + srli t1, a1, 8 // t1 = 00ddccbb |
| 174 | + andi t1, t1, 0xff // t1 = 000000bb |
| 175 | + slli t1, t1, 16 // t1 = 00bb0000 |
| 176 | + or a2, a2, t1 // a2 = aabb0000 |
| 177 | + |
| 178 | + srli t1, a1, 16 // t1 = 0000ddcc |
| 179 | + andi t1, t1, 0xff // t1 = 000000cc |
| 180 | + slli t1, t1, 8 // t1 = 0000cc00 |
| 181 | + or a2, a2, t1 // a2 = aabbcc00 |
| 182 | + |
| 183 | + srli t1, a1, 24 // t1 = 000000dd |
| 184 | + or a2, a2, t1 // a2 = aabbccdd |
| 185 | + |
| 186 | + ret |
| 187 | + |
| 188 | +spi_xfer: |
| 189 | + /* Loop four times */ |
| 190 | + addi t0, zero, 4 |
| 191 | + |
| 192 | +spi_xfer_loop: |
| 193 | + /* Send data in a0[7:0] */ |
| 194 | + sb a0, SPI_SPDR(gp) |
| 195 | + fence |
| 196 | + |
| 197 | +spi_xfer_poll: |
| 198 | + /* Wait for data in RX FIFO */ |
| 199 | + lbu t1, SPI_SPSR(gp) |
| 200 | + fence |
| 201 | + andi t1, t1, SPI_SPSR_RX_CHECK |
| 202 | + bnez t1, spi_xfer_poll |
| 203 | + |
| 204 | + srli a1, a1, 8 |
| 205 | + lbu t1, SPI_SPDR(gp) |
| 206 | + slli t1, t1, 24 |
| 207 | + or a1, a1, t1 |
| 208 | + |
| 209 | + srli a0, a0, 8 |
| 210 | + addi t0, t0, -1 |
| 211 | + bnez t0, spi_xfer_loop |
| 212 | + |
| 213 | + ret |
0 commit comments