77#include "constants.h"
88#include "hal/gpio.h"
99#include "hal/hart.h"
10+ #include "hal/mmio.h"
1011#include "hal/mocha.h"
1112#include "hal/spi_device.h"
1213#include "hal/uart.h"
@@ -88,7 +89,7 @@ void boot(uintptr_t addr)
8889void clear_slots ()
8990{
9091 for (size_t i = 0 ; i < ARRAY_LEN (boot_slots ); i ++ ) {
91- DEV_WRITE (boot_slots [i ], 0x00 );
92+ DEV_WRITE (boot_slots [i ], 0 );
9293 }
9394}
9495
@@ -111,41 +112,41 @@ bool spi_boot_strap(struct boot_context *ctx)
111112
112113 spi_device_t spid = mocha_system_spi_device ();
113114 spi_device_init (spid );
114- spi_device_enable_set (spid , true);
115- spi_device_flash_status_set (spid , 0 );
115+ spi_device_flash_mode_set (spid , spi_device_flash_mode_flash );
116116
117117 while (true) {
118118 led_animation_run (ctx );
119119
120- spi_device_cmd_t cmd = spi_device_cmd_get_non_blocking (spid );
121- if (cmd .status != spi_device_status_ready ) {
122- if (cmd .status == spi_device_status_overflow ) {
120+ spi_device_software_command cmd ;
121+ enum spi_device_status status = spi_device_software_command_get_non_blocking (spid , & cmd );
122+ if (status != spi_device_status_ready ) {
123+ if (status == spi_device_status_overflow ) {
123124 uprintf (ctx -> console , "SPI payload overflow\n" );
124- spi_device_flash_status_set (spid , 0 );
125+ spi_device_flash_status_busy_set (spid , false );
125126 }
126127 continue ;
127128 }
128129
129130 switch (cmd .opcode ) {
130- case SPI_DEVICE_OPCODE_SECTOR_ERASE4B :
131- case SPI_DEVICE_OPCODE_SECTOR_ERASE :
131+ case spi_device_opcode_sector_erase :
132+ case spi_device_opcode_sector_erase_4b :
132133 // No need to erase SRAM.
133134 break ;
134- case SPI_DEVICE_OPCODE_PAGE_PROGRAM4B :
135- case SPI_DEVICE_OPCODE_PAGE_PROGRAM :
135+ case spi_device_opcode_page_program :
136+ case spi_device_opcode_page_program_4b :
136137 if (cmd .payload_byte_count > 0 ) {
137138 page_program (ctx -> console , spid , cmd .address , cmd .payload_byte_count );
138139 }
139140 break ;
140- case SPI_DEVICE_OPCODE_RESET :
141+ case spi_device_opcode_reset :
141142 // Exit boot strap
142143 return true;
143144 default :
144- uprintf (ctx -> console , "\nUnsupported command: 0x%0x " , cmd .opcode );
145+ uprintf (ctx -> console , "Unsupported command: 0x%x\n " , cmd .opcode );
145146 break ;
146147 }
147148 // Finished processing the write, clear the busy bit.
148- spi_device_flash_status_set (spid , 0 );
149+ spi_device_flash_status_busy_set (spid , false );
149150 }
150151
151152 return true;
@@ -159,23 +160,24 @@ static inline bool is_overriding_me(uintptr_t addr)
159160void page_program (uart_t console , spi_device_t spid , uint32_t offset , uint32_t bytes )
160161{
161162 uintptr_t ptr = offset ;
162- uint32_t payload_offset = 0 ;
163163
164- if (bytes > SPI_DEVICE_PAYLOAD_AREA_NUM_BYTES ) {
165- uprintf (console , "\npage program size out of bounds" );
164+ if (bytes > spi_device_ingress_buffer_size_payload_fifo * sizeof ( uint32_t ) ) {
165+ uprintf (console , "page program size out of bounds\n " );
166166 return ;
167167 }
168168
169169 // TODO: we need to check that the offset is valid within a memory address space.
170170 if (is_overriding_me (ptr ) || is_overriding_me (ptr + bytes )) {
171- uprintf (console , "\nPlease don't override the bootrom's ram." );
171+ uprintf (console , "Please don't override the bootrom's ram.\n " );
172172 return ;
173173 }
174174
175- while (payload_offset < bytes ) {
176- * ((volatile uint64_t * )ptr ) = spi_device_flash_payload_buffer_read64 (spid , payload_offset );
177- ptr += sizeof (uint64_t );
178- payload_offset += sizeof (uint64_t );
175+ uint32_t num_words = (bytes / 4 );
176+ if (bytes % 4 != 0 ) { num_words ++ ; }
177+ for (size_t i = 0 ; i < num_words ; i ++ ) {
178+ uint32_t word =
179+ VOLATILE_READ (spid -> ingress_buffer [spi_device_ingress_buffer_offset_payload_fifo + i ]);
180+ ((uint32_t * )ptr )[i ] = word ;
179181 }
180182}
181183
0 commit comments