blob: 4f017b3f34e80c5d75dbc99e55dc461eaf744197 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
#include <stdio.h>
#include <stdlib.h>
#include "encoding.h"
#include "platform.h"
#include "eos.h"
#include "soc/timer.h"
#include "flash.h"
#define IDLE_TICKS 10
__attribute__ ((section (".itim.flash")))
static void send(uint8_t data) {
while (SPI0_REG(SPI_REG_TXFIFO) & SPI_TXFIFO_FULL);
SPI0_REG(SPI_REG_TXFIFO) = data;
}
__attribute__ ((section (".itim.flash")))
static uint8_t xfer(uint8_t data) {
volatile uint32_t x = 0;
send(data);
while ((x = SPI0_REG(SPI_REG_RXFIFO)) & SPI_RXFIFO_EMPTY);
return x;
}
void eos_flash_init(void) {
SPI0_REG(SPI_REG_FMT) |= SPI_FMT_DIR(SPI_DIR_TX);
SPI0_REG(SPI_REG_TXCTRL) = SPI_TXWM(1);
eos_flash_norm();
}
__attribute__ ((section (".itim.flash")))
void eos_flash_norm(void) {
volatile uint64_t *mtime = (uint64_t *) (CLINT_CTRL_ADDR + CLINT_MTIME);
uint32_t mtime0;
clear_csr(mstatus, MSTATUS_MIE);
SPI0_REG(SPI_REG_FCTRL) = 0;
SPI0_REG(SPI_REG_SCKDIV) = 3;
if (SPI0_REG(SPI_REG_FMT) & SPI_FMT_PROTO(SPI_PROTO_Q)) {
send(EOS_FLASH_QPIDI);
while (!(SPI0_REG(SPI_REG_IP) & SPI_IP_TXWM));
}
SPI0_REG(SPI_REG_FMT) = \
SPI_FMT_PROTO(SPI_PROTO_S) |
SPI_FMT_ENDIAN(SPI_ENDIAN_MSB) |
SPI_FMT_DIR(SPI_DIR_TX) |
SPI_FMT_LEN(8);
SPI0_REG(SPI_REG_FFMT) =
SPI_INSN_CMD_EN |
SPI_INSN_ADDR_LEN(3) |
SPI_INSN_PAD_CNT(0) |
SPI_INSN_CMD_PROTO(SPI_PROTO_S) |
SPI_INSN_ADDR_PROTO(SPI_PROTO_S) |
SPI_INSN_DATA_PROTO(SPI_PROTO_S) |
SPI_INSN_CMD_CODE(EOS_FLASH_NORD) |
SPI_INSN_PAD_CODE(0x00);
mtime0 = *mtime;
while ((*mtime - mtime0) < IDLE_TICKS);
SPI0_REG(SPI_REG_FCTRL) = SPI_FCTRL_EN;
set_csr(mstatus, MSTATUS_MIE);
}
__attribute__ ((section (".itim.flash")))
void eos_flash_fast(void) {
volatile uint64_t *mtime = (uint64_t *) (CLINT_CTRL_ADDR + CLINT_MTIME);
uint32_t mtime0;
clear_csr(mstatus, MSTATUS_MIE);
SPI0_REG(SPI_REG_FCTRL) = 0;
SPI0_REG(SPI_REG_SCKDIV) = 2;
if (!(SPI0_REG(SPI_REG_FMT) & SPI_FMT_PROTO(SPI_PROTO_Q))) {
send(EOS_FLASH_QPIEN);
while (!(SPI0_REG(SPI_REG_IP) & SPI_IP_TXWM));
}
SPI0_REG(SPI_REG_FMT) = \
SPI_FMT_PROTO(SPI_PROTO_Q) |
SPI_FMT_ENDIAN(SPI_ENDIAN_MSB) |
SPI_FMT_DIR(SPI_DIR_TX) |
SPI_FMT_LEN(8);
SPI0_REG(SPI_REG_FFMT) =
SPI_INSN_CMD_EN |
SPI_INSN_ADDR_LEN(3) |
SPI_INSN_PAD_CNT(6) |
SPI_INSN_CMD_PROTO(SPI_PROTO_Q) |
SPI_INSN_ADDR_PROTO(SPI_PROTO_Q) |
SPI_INSN_DATA_PROTO(SPI_PROTO_Q) |
SPI_INSN_CMD_CODE(EOS_FLASH_FRD) |
SPI_INSN_PAD_CODE(0x00);
mtime0 = *mtime;
while ((*mtime - mtime0) < IDLE_TICKS);
SPI0_REG(SPI_REG_FCTRL) = SPI_FCTRL_EN;
set_csr(mstatus, MSTATUS_MIE);
}
__attribute__ ((section (".itim.flash")))
void eos_flash_wip(void) {
uint8_t status;
do {
SPI0_REG(SPI_REG_CSMODE) = SPI_CSMODE_HOLD;
xfer(EOS_FLASH_RDSR);
status = xfer(0);
SPI0_REG(SPI_REG_CSMODE) = SPI_CSMODE_AUTO;
} while (status & EOS_FLASH_WIP);
}
__attribute__ ((section (".itim.flash")))
void eos_flash_wren(void) {
uint8_t status;
xfer(EOS_FLASH_WREN);
#if 0
do {
SPI0_REG(SPI_REG_CSMODE) = SPI_CSMODE_HOLD;
xfer(EOS_FLASH_RDSR);
status = xfer(0);
SPI0_REG(SPI_REG_CSMODE) = SPI_CSMODE_AUTO;
} while (!(status & EOS_FLASH_WEL));
#endif
}
__attribute__ ((section (".itim.flash")))
void eos_flash_ser(uint32_t addr) {
SPI0_REG(SPI_REG_CSMODE) = SPI_CSMODE_HOLD;
xfer(EOS_FLASH_SER);
xfer(addr >> 16);
xfer(addr >> 8);
xfer(addr);
SPI0_REG(SPI_REG_CSMODE) = SPI_CSMODE_AUTO;
}
__attribute__ ((section (".itim.flash")))
void eos_flash_pp(uint32_t addr, uint8_t *buf) {
int i;
SPI0_REG(SPI_REG_FMT) |= SPI_FMT_DIR(SPI_DIR_TX);
SPI0_REG(SPI_REG_CSMODE) = SPI_CSMODE_HOLD;
send(EOS_FLASH_PP);
send(addr >> 16);
send(addr >> 8);
send(addr);
for (i=0; i<256; i++) {
send(buf[i]);
}
while (!(SPI0_REG(SPI_REG_IP) & SPI_IP_TXWM));
SPI0_REG(SPI_REG_CSMODE) = SPI_CSMODE_AUTO;
SPI0_REG(SPI_REG_FMT) &= ~SPI_FMT_DIR(SPI_DIR_TX);
}
|