Skip to content

Commit 2cfbaa7

Browse files
committed
sw: update SPI Device HAL to use new autogen interface
Signed-off-by: Alice Ziuziakowska <a.ziuziakowska@lowrisc.org>
1 parent 0323be6 commit 2cfbaa7

9 files changed

Lines changed: 898 additions & 846 deletions

File tree

sw/device/bootrom/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ set(NAME bootrom)
66
add_executable(${NAME} bootrom.c)
77
target_link_libraries(${NAME} hal_vanilla runtime_vanilla startup_vanilla)
88
target_compile_options(${NAME} PUBLIC ${VANILLA_FLAGS})
9-
target_link_options(${NAME} PUBLIC
9+
target_link_options(${NAME} PUBLIC
1010
"-nodefaultlibs"
1111
"-Wl,--defsym,BOOT_ROM_OFFSET=0x00"
1212
"-T${LDS}"

sw/device/bootrom/bootrom.c

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
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

sw/device/examples/spi.c

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,63 +15,67 @@ int main(void)
1515
spi_device_t spi_device = mocha_system_spi_device();
1616
uart_init(uart);
1717
spi_device_init(spi_device);
18+
spi_device_flash_mode_set(spi_device, spi_device_flash_mode_flash);
19+
const spi_device_flash_status clear_status = { 0 };
20+
spi_device_flash_status_set(spi_device, clear_status);
1821

1922
uprintf(uart, "Hello SPI in Mocha!\n");
2023

2124
// Poll and process SPI command
22-
spi_device_cmd_t cmd;
23-
while (1) {
25+
spi_device_software_command cmd;
26+
while (true) {
2427
// Now process SPI command (if any)
25-
cmd = spi_device_cmd_get(spi_device);
26-
if (cmd.status != 0) {
27-
uprintf(uart, "SPI payload overflow\n");
28-
spi_device_flash_status_set(spi_device, 0);
29-
continue;
28+
enum spi_device_status status = spi_device_software_command_get(spi_device, &cmd);
29+
if (status != spi_device_status_ready) {
30+
if (status == spi_device_status_overflow) {
31+
uprintf(uart, "SPI payload overflow\n");
32+
spi_device_flash_status_set(spi_device, clear_status);
33+
continue;
34+
}
3035
}
3136

3237
switch (cmd.opcode) {
33-
case SPI_DEVICE_OPCODE_CHIP_ERASE:
38+
case spi_device_opcode_chip_erase:
3439
uprintf(uart, "SPI CHIP ERASE");
3540
break;
36-
case SPI_DEVICE_OPCODE_SECTOR_ERASE:
41+
case spi_device_opcode_sector_erase:
3742
uprintf(uart, "SPI SECTOR ERASE");
3843
break;
39-
case SPI_DEVICE_OPCODE_PAGE_PROGRAM:
44+
case spi_device_opcode_page_program:
4045
uprintf(uart, "SPI PAGE PROGRAM");
4146
break;
42-
case SPI_DEVICE_OPCODE_RESET:
47+
case spi_device_opcode_reset:
4348
uprintf(uart, "SPI RESET");
4449
break;
4550
default:
4651
uprintf(uart, "SPI ??");
4752
break;
4853
}
4954

50-
if (cmd.address != UINT32_MAX) {
55+
if (cmd.has_address) {
5156
uprintf(uart, " addr: 0x%x\n", cmd.address);
5257
}
5358

5459
if (cmd.payload_byte_count > 0) {
5560
uprintf(uart, "payload bytes: 0x%x\n", cmd.payload_byte_count);
5661
uint32_t payload_word_count = ((uint32_t)cmd.payload_byte_count) / sizeof(uint32_t);
5762
if ((cmd.payload_byte_count % sizeof(uint32_t)) != 0) {
58-
++payload_word_count;
63+
payload_word_count++;
5964
}
6065

6166
uprintf(uart, "payload data:\n");
6267

6368
uint32_t word;
6469
for (uint32_t i = 0; i < payload_word_count; ++i) {
65-
word = spi_device_flash_payload_buffer_read(spi_device, i * sizeof(uint32_t));
66-
spi_device_flash_read_buffer_write(spi_device, cmd.address + i * sizeof(uint32_t),
67-
word);
68-
uprintf(uart, "0x%x\n", word);
70+
if (spi_device_flash_payload_buffer_read_word(spi_device, i, &word)) {
71+
uprintf(uart, "0x%x\n", word);
72+
}
6973
}
7074
}
7175

7276
uprintf(uart, "\n");
7377

74-
spi_device_flash_status_set(spi_device, 0);
78+
spi_device_flash_status_set(spi_device, clear_status);
7579
}
7680

7781
return 0;

sw/device/lib/hal/mocha.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ i2c_t mocha_system_i2c(void)
100100
spi_device_t mocha_system_spi_device(void)
101101
{
102102
#if defined(__riscv_zcherihybrid)
103-
return (spi_device_t)create_mmio_capability(spi_device_base, 0x1FC0u);
103+
return (spi_device_t)create_mmio_capability(spi_device_base,
104+
sizeof(struct spi_device_memory_layout));
104105
#else /* !defined(__riscv_zcherihybrid) */
105106
return (spi_device_t)spi_device_base;
106107
#endif /* defined(__riscv_zcherihybrid) */

sw/device/lib/hal/reg_field.h

Lines changed: 0 additions & 18 deletions
This file was deleted.

0 commit comments

Comments
 (0)