Skip to content

Commit 93de2c9

Browse files
authored
Clean up fespi flashing code (#313)
* WIP upstream review feedback. See http://openocd.zylin.com/#/c/4656/ The main change is to get rid of macros that contain a return statement. Change-Id: Iff79a8aa7c40ee04a8d1f07d973f9b29d4899d5c * Remove unaligned head/tail code. From inspection it's not clear to me that this is necessary at all. I've been unable to make a test case that results in anything besides a 4-byte aligned flash to happen. Sections that aren't multiples of 4 are common, and appear to work fine. Change-Id: Idb6109ca015ae06b9d8f16bd883f9c8f5c51087d * Move fespi native code into contrib/loaders As suggested by http://openocd.zylin.com/#/c/4656/ Change-Id: I275012aa8a1ef6a0e8a2ec8ebe8643d87de24407 * Reenable hw mode if errors happen without it. Change-Id: I1220033c13d02e8a441992bd6daa0ec3b5acbfca * Default flash to not protected. Requested by upstream review. Change-Id: I61753bd9909d7f21ef6624037a865072c18bd1d8
1 parent 626df7d commit 93de2c9

File tree

4 files changed

+251
-260
lines changed

4 files changed

+251
-260
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
BIN2C = ../../../../src/helper/bin2char.sh
2+
3+
CROSS_COMPILE ?= riscv64-unknown-elf-
4+
5+
CC=$(CROSS_COMPILE)gcc
6+
OBJCOPY=$(CROSS_COMPILE)objcopy
7+
OBJDUMP=$(CROSS_COMPILE)objdump
8+
9+
CFLAGS = -march=rv32i -mabi=ilp32 -x assembler-with-cpp - -nostdlib -nostartfiles
10+
11+
all: fespi.inc
12+
13+
.PHONY: clean
14+
15+
%.elf: %.S
16+
$(CC) $(CFLAGS) $< -o $@
17+
18+
%.lst: %.elf
19+
$(OBJDUMP) -S $< > $@
20+
21+
%.bin: %.elf
22+
$(OBJCOPY) -Obinary $< $@
23+
24+
%.inc: %.bin
25+
$(BIN2C) < $< > $@
26+
27+
clean:
28+
-rm -f *.elf *.lst *.bin *.inc
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
#define SPIFLASH_READ_STATUS 0x05 // Read Status Register
2+
#define SPIFLASH_BSY_BIT 0x00000001 // WIP Bit of SPI SR on SMI SR
3+
4+
// Register offsets
5+
#define FESPI_REG_FMT 0x40
6+
#define FESPI_REG_TXFIFO 0x48
7+
#define FESPI_REG_RXFIFO 0x4c
8+
#define FESPI_REG_IP 0x74
9+
10+
// Fields
11+
#define FESPI_IP_TXWM 0x1
12+
#define FESPI_FMT_DIR(x) (((x) & 0x1) << 3)
13+
14+
// To enter, jump to the start of command_table (ie. offset 0).
15+
// a0 - FESPI base address
16+
// a1 - start address of buffer
17+
18+
// The buffer contains a "program" in byte sequences. The first byte in a
19+
// sequence determines the operation. Some operation will read more data from
20+
// the program, while some will not. The operation byte is the offset into
21+
// command_table, so eg. 4 means exit, 8 means transmit, and so on.
22+
23+
.global _start
24+
_start:
25+
command_table:
26+
j main // 0
27+
ebreak // 4
28+
j tx // 8
29+
j txwm_wait // 12
30+
j write_reg // 16
31+
j wip_wait // 20
32+
j set_dir // 24
33+
34+
// Execute the program.
35+
main:
36+
lbu t0, 0(a1)
37+
addi a1, a1, 1
38+
la t1, command_table
39+
add t0, t0, t1
40+
jr t0
41+
42+
// Read 1 byte the contains the number of bytes to transmit. Then read those
43+
// bytes from the program and transmit them one by one.
44+
tx:
45+
lbu t1, 0(a1) // read number of bytes to transmit
46+
addi a1, a1, 1
47+
1: lw t0, FESPI_REG_TXFIFO(a0) // wait for FIFO clear
48+
bltz t0, 1b
49+
lbu t0, 0(a1) // Load byte to write
50+
sw t0, FESPI_REG_TXFIFO(a0)
51+
addi a1, a1, 1
52+
addi t1, t1, -1
53+
bgtz t1, 1b
54+
j main
55+
56+
// Wait until TXWM is set.
57+
txwm_wait:
58+
1: lw t0, FESPI_REG_IP(a0)
59+
andi t0, t0, FESPI_IP_TXWM
60+
beqz t0, 1b
61+
j main
62+
63+
// Read 1 byte that contains the offset of the register to write, and 1 byte
64+
// that contains the data to write.
65+
write_reg:
66+
lbu t0, 0(a1) // read register to write
67+
add t0, t0, a0
68+
lbu t1, 1(a1) // read value to write
69+
addi a1, a1, 2
70+
sw t1, 0(t0)
71+
j main
72+
73+
wip_wait:
74+
li a2, SPIFLASH_READ_STATUS
75+
jal txrx_byte
76+
// discard first result
77+
1: li a2, 0
78+
jal txrx_byte
79+
andi t0, a2, SPIFLASH_BSY_BIT
80+
bnez t0, 1b
81+
j main
82+
83+
txrx_byte: // transmit the byte in a2, receive a bit into a2
84+
lw t0, FESPI_REG_TXFIFO(a0) // wait for FIFO clear
85+
bltz t0, txrx_byte
86+
sw a2, FESPI_REG_TXFIFO(a0)
87+
1: lw a2, FESPI_REG_RXFIFO(a0)
88+
bltz a2, 1b
89+
ret
90+
91+
set_dir:
92+
lw t0, FESPI_REG_FMT(a0)
93+
li t1, ~(FESPI_FMT_DIR(0xFFFFFFFF))
94+
and t0, t0, t1
95+
lbu t1, 0(a1) // read value to OR in
96+
addi a1, a1, 1
97+
or t0, t0, t1
98+
sw t0, FESPI_REG_FMT(a0)
99+
j main
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/* Autogenerated with ../../../../src/helper/bin2char.sh */
2+
0x6f,0x00,0xc0,0x01,0x73,0x00,0x10,0x00,0x6f,0x00,0xc0,0x02,0x6f,0x00,0x00,0x05,
3+
0x6f,0x00,0xc0,0x05,0x6f,0x00,0x00,0x07,0x6f,0x00,0x00,0x0a,0x83,0xc2,0x05,0x00,
4+
0x93,0x85,0x15,0x00,0x17,0x03,0x00,0x00,0x13,0x03,0xc3,0xfd,0xb3,0x82,0x62,0x00,
5+
0x67,0x80,0x02,0x00,0x03,0xc3,0x05,0x00,0x93,0x85,0x15,0x00,0x83,0x22,0x85,0x04,
6+
0xe3,0xce,0x02,0xfe,0x83,0xc2,0x05,0x00,0x23,0x24,0x55,0x04,0x93,0x85,0x15,0x00,
7+
0x13,0x03,0xf3,0xff,0xe3,0x44,0x60,0xfe,0x6f,0xf0,0x5f,0xfc,0x83,0x22,0x45,0x07,
8+
0x93,0xf2,0x12,0x00,0xe3,0x8c,0x02,0xfe,0x6f,0xf0,0x5f,0xfb,0x83,0xc2,0x05,0x00,
9+
0xb3,0x82,0xa2,0x00,0x03,0xc3,0x15,0x00,0x93,0x85,0x25,0x00,0x23,0xa0,0x62,0x00,
10+
0x6f,0xf0,0xdf,0xf9,0x13,0x06,0x50,0x00,0xef,0x00,0x80,0x01,0x13,0x06,0x00,0x00,
11+
0xef,0x00,0x00,0x01,0x93,0x72,0x16,0x00,0xe3,0x9a,0x02,0xfe,0x6f,0xf0,0x1f,0xf8,
12+
0x83,0x22,0x85,0x04,0xe3,0xce,0x02,0xfe,0x23,0x24,0xc5,0x04,0x03,0x26,0xc5,0x04,
13+
0xe3,0x4e,0x06,0xfe,0x67,0x80,0x00,0x00,0x83,0x22,0x05,0x04,0x13,0x03,0x70,0xff,
14+
0xb3,0xf2,0x62,0x00,0x03,0xc3,0x05,0x00,0x93,0x85,0x15,0x00,0xb3,0xe2,0x62,0x00,
15+
0x23,0x20,0x55,0x04,0x6f,0xf0,0x9f,0xf4,

0 commit comments

Comments
 (0)