Skip to content

Commit 7f2df52

Browse files
gatecatrobtaylor
authored andcommitted
Update SPI flash driver for new QSPI core
Signed-off-by: gatecat <gatecat@ds0.me>
1 parent c88f931 commit 7f2df52

File tree

3 files changed

+51
-57
lines changed

3 files changed

+51
-57
lines changed

chipflow_lib/software/drivers/spiflash.S

Lines changed: 38 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,57 @@
1-
21
.global flashio_worker_begin
32
.global flashio_worker_end
43

54
.balign 4
65

76
flashio_worker_begin:
8-
# a0 ... address of SPI ctrl reg
7+
# a0 ... flash base address
98
# a1 ... data pointer
109
# a2 ... data length
1110
# a3 ... optional WREN cmd (0 = disable)
1211

13-
# Set CS high, IO0 is output
14-
li t1, 0x120
15-
sh t1, 0(a0)
16-
17-
# Enable Manual SPI Ctrl
18-
sb zero, 3(a0)
19-
20-
# Send optional WREN cmd
21-
beqz a3, flashio_worker_L1
22-
li t5, 8
23-
andi t2, a3, 0xff
24-
flashio_worker_L4:
25-
srli t4, t2, 7
26-
sb t4, 0(a0)
27-
ori t4, t4, 0x10
28-
sb t4, 0(a0)
29-
slli t2, t2, 1
30-
andi t2, t2, 0xff
31-
addi t5, t5, -1
32-
bnez t5, flashio_worker_L4
33-
sb t1, 0(a0)
34-
35-
# SPI transfer
36-
flashio_worker_L1:
37-
beqz a2, flashio_worker_L3
38-
li t5, 8
39-
lbu t2, 0(a1)
40-
flashio_worker_L2:
41-
srli t4, t2, 7
42-
sb t4, 0(a0)
43-
ori t4, t4, 0x10
44-
sb t4, 0(a0)
45-
lbu t4, 0(a0)
46-
andi t4, t4, 2
47-
srli t4, t4, 1
48-
slli t2, t2, 1
49-
or t2, t2, t4
50-
andi t2, t2, 0xff
51-
addi t5, t5, -1
52-
bnez t5, flashio_worker_L2
53-
sb t2, 0(a1)
12+
mv t3, ra
13+
14+
# address of SPI ctrl reg
15+
li a0, 0xb0000000
16+
# enter bypass mode
17+
lbu t1, 0(a0)
18+
ori t1, t1, 0x1
19+
sb t1, 0(a0)
20+
call flashio_wait_bypass_ready
21+
22+
beqz a3, flashio_xfer
23+
24+
sb a3, 8(a0) # send wren
25+
call flashio_wait_bypass_ready
26+
li t1, 2 # deselect
27+
sb t1, 4(a0)
28+
call flashio_wait_bypass_ready
29+
30+
flashio_xfer:
31+
beqz a2, flashio_done
32+
lbu t1, 0(a1)
33+
sb t1, 8(a0) # tx data
34+
call flashio_wait_bypass_ready
35+
lbu t1, 12(a0) # rx data
36+
sb t1, 0(a1)
5437
addi a1, a1, 1
5538
addi a2, a2, -1
56-
j flashio_worker_L1
57-
flashio_worker_L3:
39+
j flashio_xfer
5840

59-
# Back to MEMIO mode
60-
li t1, 0x80
61-
sb t1, 3(a0)
41+
flashio_done:
42+
# exit bypass mode
43+
lbu t1, 0(a0)
44+
andi t1, t1, 0xFE
45+
sb t1, 0(a0)
6246

6347
fence.i
48+
mv ra, t3
49+
ret
6450

51+
flashio_wait_bypass_ready:
52+
lbu t1, 4(a0)
53+
andi t1, t1, 0x1
54+
beqz t1, flashio_wait_bypass_ready
6555
ret
6656

6757
.balign 4

chipflow_lib/software/drivers/spiflash.c

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,18 @@ extern uint32_t flashio_worker_end;
77

88
void spiflash_io(volatile spiflash_regs_t *flash, uint8_t *data, int len, uint8_t wrencmd) {
99
// Flash can't be accessed during IO, so copy to RAM and run that
10-
volatile uint32_t func[&flashio_worker_end - &flashio_worker_begin];
10+
volatile uint32_t func[&flashio_worker_end - &flashio_worker_begin];
1111

12-
uint32_t *src_ptr = &flashio_worker_begin;
13-
volatile uint32_t *dst_ptr = func;
12+
// Can't execute off flash while talking to it, so copy IO code to SRAM
13+
uint32_t *src_ptr = &flashio_worker_begin;
14+
volatile uint32_t *dst_ptr = func;
1415

15-
while (src_ptr != &flashio_worker_end)
16-
*(dst_ptr++) = *(src_ptr++);
16+
while (src_ptr != &flashio_worker_end)
17+
*(dst_ptr++) = *(src_ptr++);
1718

18-
__asm__ volatile ("fence.i" : : : "memory");
19+
__asm__ volatile ("fence.i" : : : "memory");
1920

20-
((void(*)(volatile spiflash_regs_t *, uint8_t*, uint32_t, uint32_t))func)(flash, data, len, wrencmd);
21+
((void(*)(uint8_t*, uint8_t*, uint32_t, uint32_t))func)((uint8_t*)flash, data, len, wrencmd);
2122
}
2223

2324
uint32_t spiflash_read_id(volatile spiflash_regs_t *flash) {
@@ -69,5 +70,5 @@ void spiflash_set_qspi_flag(volatile spiflash_regs_t *flash) {
6970
}
7071

7172
void spiflash_set_quad_mode(volatile spiflash_regs_t *flash) {
72-
flash->ctrl = (flash->ctrl & ~0x007f0000) | 0x00240000;
73+
flash->config = (0x3U << 3U) | (0x03U << 1U); // 3 dummy byte, X4 mode
7374
}

chipflow_lib/software/drivers/spiflash.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88
#define ISSI_ID 0x60
99

1010
typedef struct __attribute__((packed, aligned(4))) {
11-
uint32_t ctrl;
11+
uint32_t config;
12+
uint32_t raw_control;
13+
uint32_t raw_tx_data;
14+
uint32_t raw_rx_data;
1215
} spiflash_regs_t;
1316

1417
void spiflash_io(volatile spiflash_regs_t *flash, uint8_t *data, int len, uint8_t wrencmd);

0 commit comments

Comments
 (0)