Skip to content

Commit f45de2f

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

9 files changed

Lines changed: 887 additions & 856 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 data_load.S ../devicetree/mocha.S)
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
"-T${CMAKE_CURRENT_SOURCE_DIR}/rom.ld"
1212
"-L${LDS_DIR}"

sw/device/bootrom/bootrom.c

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
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)
8889
void 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)
159160
void 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

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
@@ -120,7 +120,8 @@ i2c_t mocha_system_i2c(void)
120120
spi_device_t mocha_system_spi_device(void)
121121
{
122122
#if defined(__riscv_zcherihybrid)
123-
return (spi_device_t)create_mmio_capability(spi_device_base, 0x1FC0u);
123+
return (spi_device_t)create_mmio_capability(spi_device_base,
124+
sizeof(struct spi_device_memory_layout));
124125
#else /* !defined(__riscv_zcherihybrid) */
125126
return (spi_device_t)spi_device_base;
126127
#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)