44
55#include "boot/trap.h"
66#include "hal/gpio.h"
7+ #include "hal/mmio.h"
78#include "hal/mocha.h"
89#include "hal/spi_device.h"
910#include "hal/uart.h"
@@ -32,7 +33,7 @@ int main(void)
3233 spi_boot_strap (console );
3334
3435 enum { BootAddress = 0x10004080 };
35- uprintf (console , "\nJumping to: 0x%0x \n" , BootAddress );
36+ uprintf (console , "\nJumping to: 0x%x \n" , BootAddress );
3637
3738 boot (BootAddress );
3839 uprintf (console , "\nFailed to boot?\n" );
@@ -53,9 +54,9 @@ bool spi_boot_strap(uart_t console)
5354
5455 spi_device_t spid = mocha_system_spi_device ();
5556 spi_device_init (spid );
56- spi_device_enable_set (spid , true );
57- spi_device_flash_status_set ( spid , 0 ) ;
58-
57+ spi_device_flash_mode_set (spid , spi_device_flash_mode_flash );
58+ const spi_device_flash_status clear_status = { 0 } ;
59+ spi_device_flash_status_set ( spid , clear_status );
5960 uint32_t received_resets = 0 ;
6061 size_t count = 0 ;
6162
@@ -66,45 +67,47 @@ bool spi_boot_strap(uart_t console)
6667 count = 0 ;
6768 }
6869
69- spi_device_cmd_t cmd = spi_device_cmd_get_non_blocking (spid );
70- if (cmd .status != spi_device_status_ready ) {
71- if (cmd .status == spi_device_status_overflow ) {
70+ spi_device_software_command command ;
71+ enum spi_device_status command_status =
72+ spi_device_software_command_get_non_blocking (spid , & command );
73+ if (command_status != spi_device_status_ready ) {
74+ if (command_status == spi_device_status_overflow ) {
7275 uprintf (console , "SPI payload overflow\n" );
73- spi_device_flash_status_set (spid , 0 );
76+ spi_device_flash_status_busy_set (spid , false);
77+ continue ;
7478 }
75- continue ;
7679 }
7780
78- switch (cmd .opcode ) {
79- case SPI_DEVICE_OPCODE_SECTOR_ERASE :
81+ switch (command .opcode ) {
82+ case spi_device_opcode_sector_erase :
8083 // No need to erase SRAM.
8184 break ;
82- case SPI_DEVICE_OPCODE_PAGE_PROGRAM :
83- if (cmd .payload_byte_count > 0 ) {
84- page_program (console , spid , cmd .address , cmd .payload_byte_count );
85+ case spi_device_opcode_page_program :
86+ if (command . has_address && command .payload_byte_count > 0 ) {
87+ page_program (console , spid , command .address , command .payload_byte_count );
8588 }
8689 break ;
87- case SPI_DEVICE_OPCODE_RESET :
90+ case spi_device_opcode_reset :
8891 // This is a workaround to openFPGALoader that starts with a reset.
8992 if (received_resets ++ > 0 ) {
9093 // Exit boot strap
91- spi_device_enable_set (spid , false );
94+ spi_device_flash_mode_set (spid , spi_device_flash_mode_disabled );
9295 return true;
9396 }
94- uprintf (console , "\nFirst reset" );
97+ uprintf (console , "\nFirst reset\n " );
9598 break ;
9699 default :
97- uprintf (console , "\nUnsupported command: 0x%0x " , cmd .opcode );
100+ uprintf (console , "\nUnsupported command: 0x%x " , command .opcode );
98101 break ;
99102 }
100103 // Finished processing the write, clear the busy bit.
101- spi_device_flash_status_set (spid , 0 );
104+ spi_device_flash_status_busy_set (spid , false );
102105 }
103106
104107 return true;
105108}
106109
107- static inline bool is_overriding_me (uintptr_t addr )
110+ static inline bool is_overwriting_me (uintptr_t addr )
108111{
109112 return addr >= (uintptr_t )_program_start && addr < (uintptr_t )_program_end ;
110113}
@@ -114,24 +117,28 @@ void page_program(uart_t console, spi_device_t spid, uint32_t offset, uint32_t b
114117 // TODO: Enable the spi flash 4 bytes addressing.
115118 enum { SramOffset = 0x10000000 };
116119 uintptr_t ptr = SramOffset + offset ;
117- uint32_t payload_offset = 0 ;
120+ uprintf (console , "page program: addr: 0x%x len: 0x%x bytes\n" , (uint32_t )ptr , bytes );
121+ size_t num_words = bytes / 4 ;
122+ if (bytes % 4 != 0 ) {
123+ num_words ++ ;
124+ }
118125
119- if (bytes > SPI_DEVICE_PAYLOAD_AREA_NUM_BYTES ) {
126+ if (bytes > spi_device_ingress_buffer_size_payload_fifo * sizeof ( uint32_t ) ) {
120127 uprintf (console , "\npage program size out of bounds" );
121128 return ;
122129 }
123130
124131 // TODO: Now only SRAM is supported, but when 4 bytes addressing is enabled and the HW supports
125132 // DRAM and ROM, then we need to check that the offset is valid within a memory address space.
126- if (is_overriding_me (ptr ) || is_overriding_me (ptr + bytes )) {
127- uprintf (console , "\nPlease don't override the bootROM." );
133+ if (is_overwriting_me (ptr ) || is_overwriting_me (ptr + bytes )) {
134+ uprintf (console , "\nPlease don't overwrite the bootROM." );
128135 return ;
129136 }
130137
131- while ( payload_offset < bytes ) {
132- * (( volatile uint64_t * ) ptr ) = spi_device_flash_payload_buffer_read64 ( spid , payload_offset );
133- ptr += sizeof ( uint64_t );
134- payload_offset += sizeof ( uint64_t ) ;
138+ for ( size_t i = 0 ; i < num_words ; i ++ ) {
139+ uint32_t word =
140+ VOLATILE_READ ( spid -> ingress_buffer [ spi_device_ingress_buffer_offset_payload_fifo + i ] );
141+ (( uint32_t * ) ptr )[ i ] = word ;
135142 }
136143}
137144
0 commit comments