1 |
2 |
dgisselq |
///////////////////////////////////////////////////////////////////////////
|
2 |
|
|
//
|
3 |
|
|
//
|
4 |
|
|
// Filename: spiflashsim.cpp
|
5 |
|
|
//
|
6 |
|
|
// Project: Wishbone Controlled Quad SPI Flash Controller
|
7 |
|
|
//
|
8 |
|
|
// Purpose: This library simulates the operation of a Quad-SPI commanded
|
9 |
|
|
// flash, such as the S25FL032P used on the Basys-3 development
|
10 |
|
|
// board by Digilent. As such, it is defined by 32 Mbits of
|
11 |
|
|
// memory (4 Mbyte).
|
12 |
|
|
//
|
13 |
|
|
// This simulator is useful for testing in a Verilator/C++
|
14 |
|
|
// environment, where this simulator can be used in place of
|
15 |
|
|
// the actual hardware.
|
16 |
|
|
//
|
17 |
|
|
// Creator: Dan Gisselquist
|
18 |
|
|
// Gisselquist Technology, LLC
|
19 |
|
|
//
|
20 |
|
|
///////////////////////////////////////////////////////////////////////////
|
21 |
|
|
//
|
22 |
|
|
// Copyright (C) 2015, Gisselquist Technology, LLC
|
23 |
|
|
//
|
24 |
|
|
// This program is free software (firmware): you can redistribute it and/or
|
25 |
|
|
// modify it under the terms of the GNU General Public License as published
|
26 |
|
|
// by the Free Software Foundation, either version 3 of the License, or (at
|
27 |
|
|
// your option) any later version.
|
28 |
|
|
//
|
29 |
|
|
// This program is distributed in the hope that it will be useful, but WITHOUT
|
30 |
|
|
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
|
31 |
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
32 |
|
|
// for more details.
|
33 |
|
|
//
|
34 |
|
|
// You should have received a copy of the GNU General Public License along
|
35 |
|
|
// with this program. (It's in the $(ROOT)/doc directory, run make with no
|
36 |
|
|
// target there if the PDF file isn't present.) If not, see
|
37 |
|
|
// <http://www.gnu.org/licenses/> for a copy.
|
38 |
|
|
//
|
39 |
|
|
// License: GPL, v3, as defined and found on www.gnu.org,
|
40 |
|
|
// http://www.gnu.org/licenses/gpl.html
|
41 |
|
|
//
|
42 |
|
|
//
|
43 |
|
|
///////////////////////////////////////////////////////////////////////////
|
44 |
|
|
#include <stdio.h>
|
45 |
|
|
#include <string.h>
|
46 |
|
|
#include <assert.h>
|
47 |
|
|
#include <stdlib.h>
|
48 |
|
|
|
49 |
10 |
dgisselq |
#include "regdefs.h"
|
50 |
2 |
dgisselq |
#include "qspiflashsim.h"
|
51 |
|
|
|
52 |
10 |
dgisselq |
#define MEMBYTES (FLASHWORDS<<2)
|
53 |
2 |
dgisselq |
|
54 |
|
|
static const unsigned DEVID = 0x0115,
|
55 |
|
|
DEVESD = 0x014,
|
56 |
|
|
MICROSECONDS = 100,
|
57 |
|
|
MILLISECONDS = MICROSECONDS * 1000,
|
58 |
|
|
SECONDS = MILLISECONDS * 1000,
|
59 |
|
|
tW = 50 * MICROSECONDS, // write config cycle time
|
60 |
|
|
tBE = 32 * SECONDS,
|
61 |
|
|
tDP = 10 * SECONDS,
|
62 |
|
|
tRES = 30 * SECONDS,
|
63 |
|
|
// Shall we artificially speed up this process?
|
64 |
|
|
tPP = 12 * MICROSECONDS,
|
65 |
|
|
tSE = 15 * MILLISECONDS;
|
66 |
|
|
// or keep it at the original speed
|
67 |
|
|
// tPP = 1200 * MICROSECONDS,
|
68 |
|
|
// tSE = 1500 * MILLISECONDS;
|
69 |
|
|
|
70 |
|
|
QSPIFLASHSIM::QSPIFLASHSIM(void) {
|
71 |
|
|
m_mem = new char[MEMBYTES];
|
72 |
|
|
m_pmem = new char[256];
|
73 |
|
|
m_state = QSPIF_IDLE;
|
74 |
|
|
m_last_sck = 1;
|
75 |
|
|
m_write_count = 0;
|
76 |
|
|
m_ireg = m_oreg = 0;
|
77 |
|
|
m_sreg = 0x01c;
|
78 |
|
|
m_creg = 0x001; // Iinitial creg on delivery
|
79 |
|
|
m_quad_mode = false;
|
80 |
|
|
m_mode_byte = 0;
|
81 |
|
|
|
82 |
|
|
memset(m_mem, 0x0ff, MEMBYTES);
|
83 |
|
|
}
|
84 |
|
|
|
85 |
|
|
void QSPIFLASHSIM::load(const unsigned addr, const char *fname) {
|
86 |
|
|
FILE *fp;
|
87 |
|
|
size_t len;
|
88 |
|
|
|
89 |
|
|
if (addr >= MEMBYTES)
|
90 |
|
|
return;
|
91 |
|
|
len = MEMBYTES-addr*4;
|
92 |
|
|
|
93 |
|
|
if (NULL != (fp = fopen(fname, "r"))) {
|
94 |
|
|
int nr = 0;
|
95 |
|
|
nr = fread(&m_mem[addr], sizeof(char), len, fp);
|
96 |
|
|
fclose(fp);
|
97 |
|
|
if (nr == 0) {
|
98 |
|
|
fprintf(stderr, "SPI-FLASH: Could not read %s\n", fname);
|
99 |
|
|
perror("O/S Err:");
|
100 |
|
|
}
|
101 |
|
|
} else {
|
102 |
|
|
fprintf(stderr, "SPI-FLASH: Could not open %s\n", fname);
|
103 |
|
|
perror("O/S Err:");
|
104 |
|
|
}
|
105 |
|
|
}
|
106 |
|
|
|
107 |
10 |
dgisselq |
void QSPIFLASHSIM::write(const unsigned addr, const unsigned len, const uint32_t *buf) {
|
108 |
|
|
char *ptr;
|
109 |
|
|
printf("FLASH: Copying into memory at S6Add4 %08x, my addr %08x, %d values\n",
|
110 |
|
|
addr, (addr-SPIFLASH)<<2, len<<2);
|
111 |
|
|
ptr = &m_mem[(addr-SPIFLASH)<<2];
|
112 |
|
|
memcpy(ptr, buf, len<<2);
|
113 |
|
|
printf("%02x %02x %02x %02x\n", ptr[0]&0x0ff, ptr[1]&0x0ff, ptr[2]&0x0ff, ptr[3]&0x0ff);
|
114 |
|
|
}
|
115 |
|
|
|
116 |
2 |
dgisselq |
#define QOREG(A) m_oreg = ((m_oreg & (~0x0ff))|(A&0x0ff))
|
117 |
|
|
|
118 |
|
|
int QSPIFLASHSIM::operator()(const int csn, const int sck, const int dat) {
|
119 |
|
|
// Keep track of a timer to determine when page program and erase
|
120 |
|
|
// cycles complete.
|
121 |
|
|
|
122 |
|
|
if (m_write_count > 0) {
|
123 |
|
|
if (0 == (--m_write_count)) {// When done with erase/page pgm,
|
124 |
|
|
m_sreg &= 0x0fc; // Clear the write in progress bit
|
125 |
|
|
if (m_debug) printf("Write complete, clearing WIP (inside SIM)\n");
|
126 |
|
|
}
|
127 |
|
|
}
|
128 |
|
|
|
129 |
|
|
if (csn) {
|
130 |
|
|
m_last_sck = 1;
|
131 |
|
|
m_ireg = 0; m_oreg = 0;
|
132 |
|
|
m_count= 0;
|
133 |
|
|
|
134 |
|
|
if ((QSPIF_PP == m_state)||(QSPIF_QPP == m_state)) {
|
135 |
|
|
// Start a page program
|
136 |
|
|
if (m_debug) printf("QSPI: Page Program write cycle begins\n");
|
137 |
|
|
if (m_debug) printf("CK = %d & 7 = %d\n", m_count, m_count & 0x07);
|
138 |
|
|
if (m_debug) printf("QSPI: pmem = %08lx\n", (unsigned long)m_pmem);
|
139 |
|
|
m_write_count = tPP;
|
140 |
|
|
m_state = QSPIF_IDLE;
|
141 |
|
|
m_sreg &= (~QSPIF_WEL_FLAG);
|
142 |
|
|
m_sreg |= (QSPIF_WIP_FLAG);
|
143 |
|
|
for(int i=0; i<256; i++) {
|
144 |
|
|
/*
|
145 |
|
|
if (m_debug) printf("%02x: m_mem[%02x] = %02x &= %02x = %02x\n",
|
146 |
|
|
i, (m_addr&(~0x0ff))+i,
|
147 |
|
|
m_mem[(m_addr&(~0x0ff))+i]&0x0ff, m_pmem[i]&0x0ff,
|
148 |
|
|
m_mem[(m_addr&(~0x0ff))+i]& m_pmem[i]&0x0ff);
|
149 |
|
|
*/
|
150 |
|
|
m_mem[(m_addr&(~0x0ff))+i] &= m_pmem[i];
|
151 |
|
|
}
|
152 |
|
|
m_quad_mode = false;
|
153 |
|
|
} else if (m_state == QSPIF_SECTOR_ERASE) {
|
154 |
|
|
if (m_debug) printf("Actually Erasing sector, from %08x\n", m_addr);
|
155 |
|
|
m_write_count = tSE;
|
156 |
|
|
m_state = QSPIF_IDLE;
|
157 |
|
|
m_sreg &= (~QSPIF_WEL_FLAG);
|
158 |
|
|
m_sreg |= (QSPIF_WIP_FLAG);
|
159 |
|
|
m_addr &= (-1<<16);
|
160 |
|
|
for(int i=0; i<(1<<16); i++)
|
161 |
|
|
m_mem[m_addr + i] = 0x0ff;
|
162 |
|
|
if (m_debug) printf("Now waiting %d ticks delay\n", m_write_count);
|
163 |
|
|
} else if (QSPIF_WRSR == m_state) {
|
164 |
|
|
if (m_debug) printf("Actually writing status register\n");
|
165 |
|
|
m_write_count = tW;
|
166 |
|
|
m_state = QSPIF_IDLE;
|
167 |
|
|
m_sreg &= (~QSPIF_WEL_FLAG);
|
168 |
|
|
m_sreg |= (QSPIF_WIP_FLAG);
|
169 |
|
|
} else if (QSPIF_CLSR == m_state) {
|
170 |
|
|
if (m_debug) printf("Actually clearing the status register bits\n");
|
171 |
|
|
m_state = QSPIF_IDLE;
|
172 |
|
|
m_sreg &= 0x09f;
|
173 |
|
|
} else if (m_state == QSPIF_BULK_ERASE) {
|
174 |
|
|
m_write_count = tBE;
|
175 |
|
|
m_state = QSPIF_IDLE;
|
176 |
|
|
m_sreg &= (~QSPIF_WEL_FLAG);
|
177 |
|
|
m_sreg |= (QSPIF_WIP_FLAG);
|
178 |
|
|
for(int i=0; i<MEMBYTES; i++)
|
179 |
|
|
m_mem[i] = 0x0ff;
|
180 |
|
|
} else if (m_state == QSPIF_DEEP_POWER_DOWN) {
|
181 |
|
|
m_write_count = tDP;
|
182 |
|
|
m_state = QSPIF_IDLE;
|
183 |
|
|
} else if (m_state == QSPIF_RELEASE) {
|
184 |
|
|
m_write_count = tRES;
|
185 |
|
|
m_state = QSPIF_IDLE;
|
186 |
|
|
} else if (m_state == QSPIF_QUAD_READ_CMD) {
|
187 |
|
|
if ((m_mode_byte & 0x0f0)!=0x0a0)
|
188 |
|
|
m_quad_mode = false;
|
189 |
|
|
else
|
190 |
|
|
m_state = QSPIF_QUAD_READ_IDLE;
|
191 |
|
|
} else if (m_state == QSPIF_QUAD_READ) {
|
192 |
|
|
if ((m_mode_byte & 0x0f0)!=0x0a0)
|
193 |
|
|
m_quad_mode = false;
|
194 |
|
|
else
|
195 |
|
|
m_state = QSPIF_QUAD_READ_IDLE;
|
196 |
|
|
} else if (m_state == QSPIF_QUAD_READ_IDLE) {
|
197 |
|
|
}
|
198 |
|
|
|
199 |
|
|
m_oreg = 0x0fe;
|
200 |
|
|
return dat;
|
201 |
|
|
} else if ((!m_last_sck)||(sck == m_last_sck)) {
|
202 |
|
|
// Only change on the falling clock edge
|
203 |
|
|
// printf("SFLASH-SKIP, CLK=%d -> %d\n", m_last_sck, sck);
|
204 |
|
|
m_last_sck = sck;
|
205 |
|
|
if (m_quad_mode)
|
206 |
|
|
return (m_oreg>>8)&0x0f;
|
207 |
|
|
else
|
208 |
|
|
// return ((m_oreg & 0x0100)?2:0) | (dat & 0x0d);
|
209 |
|
|
return (m_oreg & 0x0100)?2:0;
|
210 |
|
|
}
|
211 |
|
|
|
212 |
|
|
// We'll only get here if ...
|
213 |
|
|
// last_sck = 1, and sck = 0, thus transitioning on the
|
214 |
|
|
// negative edge as with everything else in this interface
|
215 |
|
|
if (m_quad_mode) {
|
216 |
|
|
m_ireg = (m_ireg << 4) | (dat & 0x0f);
|
217 |
|
|
m_count+=4;
|
218 |
|
|
m_oreg <<= 4;
|
219 |
|
|
} else {
|
220 |
|
|
m_ireg = (m_ireg << 1) | (dat & 1);
|
221 |
|
|
m_count++;
|
222 |
|
|
m_oreg <<= 1;
|
223 |
|
|
}
|
224 |
|
|
|
225 |
|
|
|
226 |
|
|
// printf("PROCESS, COUNT = %d, IREG = %02x\n", m_count, m_ireg);
|
227 |
|
|
if (m_state == QSPIF_QUAD_READ_IDLE) {
|
228 |
|
|
assert(m_quad_mode);
|
229 |
|
|
if (m_count == 24) {
|
230 |
|
|
if (m_debug) printf("QSPI: Entering from Quad-Read Idle to Quad-Read\n");
|
231 |
|
|
if (m_debug) printf("QSPI: QI/O Idle Addr = %02x\n", m_ireg&0x0ffffff);
|
232 |
|
|
m_addr = (m_ireg) & 0x0ffffff;
|
233 |
|
|
assert((m_addr & 0xfc00000)==0);
|
234 |
|
|
m_state = QSPIF_QUAD_READ;
|
235 |
|
|
} m_oreg = 0;
|
236 |
|
|
} else if (m_count == 8) {
|
237 |
|
|
QOREG(0x0a5);
|
238 |
|
|
// printf("SFLASH-CMD = %02x\n", m_ireg & 0x0ff);
|
239 |
|
|
// Figure out what command we've been given
|
240 |
|
|
if (m_debug) printf("SPI FLASH CMD %02x\n", m_ireg&0x0ff);
|
241 |
|
|
switch(m_ireg & 0x0ff) {
|
242 |
|
|
case 0x01: // Write status register
|
243 |
|
|
if (2 !=(m_sreg & 0x203)) {
|
244 |
|
|
if (m_debug) printf("QSPI: WEL not set, cannot write status reg\n");
|
245 |
|
|
m_state = QSPIF_INVALID;
|
246 |
|
|
} else
|
247 |
|
|
m_state = QSPIF_WRSR;
|
248 |
|
|
break;
|
249 |
|
|
case 0x02: // Page program
|
250 |
|
|
if (2 != (m_sreg & 0x203)) {
|
251 |
|
|
if (m_debug) printf("QSPI: Cannot program at this time, SREG = %x\n", m_sreg);
|
252 |
|
|
m_state = QSPIF_INVALID;
|
253 |
|
|
} else {
|
254 |
|
|
m_state = QSPIF_PP;
|
255 |
|
|
if (m_debug) printf("PAGE-PROGRAM COMMAND ACCEPTED\n");
|
256 |
|
|
}
|
257 |
|
|
break;
|
258 |
|
|
case 0x03: // Read data bytes
|
259 |
|
|
// Our clock won't support this command, so go
|
260 |
|
|
// to an invalid state
|
261 |
|
|
if (m_debug) printf("QSPI INVALID: This sim does not support slow reading\n");
|
262 |
|
|
m_state = QSPIF_INVALID;
|
263 |
|
|
break;
|
264 |
|
|
case 0x04: // Write disable
|
265 |
|
|
m_state = QSPIF_IDLE;
|
266 |
|
|
m_sreg &= (~QSPIF_WEL_FLAG);
|
267 |
|
|
break;
|
268 |
|
|
case 0x05: // Read status register
|
269 |
|
|
m_state = QSPIF_RDSR;
|
270 |
|
|
if (m_debug) printf("QSPI: READING STATUS REGISTER: %02x\n", m_sreg);
|
271 |
|
|
QOREG(m_sreg);
|
272 |
|
|
break;
|
273 |
|
|
case 0x06: // Write enable
|
274 |
|
|
m_state = QSPIF_IDLE;
|
275 |
|
|
m_sreg |= QSPIF_WEL_FLAG;
|
276 |
|
|
if (m_debug) printf("QSPI: WRITE-ENABLE COMMAND ACCEPTED\n");
|
277 |
|
|
break;
|
278 |
|
|
case 0x0b: // Here's the read that we support
|
279 |
|
|
if (m_debug) printf("QSPI: FAST-READ (single-bit)\n");
|
280 |
|
|
m_state = QSPIF_FAST_READ;
|
281 |
|
|
break;
|
282 |
|
|
case 0x30:
|
283 |
|
|
if (m_debug) printf("QSPI: CLEAR STATUS REGISTER COMMAND\n");
|
284 |
|
|
m_state = QSPIF_CLSR;
|
285 |
|
|
break;
|
286 |
|
|
case 0x32: // QUAD Page program, 4 bits at a time
|
287 |
|
|
if (2 != (m_sreg & 0x203)) {
|
288 |
|
|
if (m_debug) printf("QSPI: Cannot program at this time, SREG = %x\n", m_sreg);
|
289 |
|
|
m_state = QSPIF_INVALID;
|
290 |
|
|
} else {
|
291 |
|
|
m_state = QSPIF_QPP;
|
292 |
|
|
if (m_debug) printf("QSPI: QUAD-PAGE-PROGRAM COMMAND ACCEPTED\n");
|
293 |
|
|
if (m_debug) printf("QSPI: pmem = %08lx\n", (unsigned long)m_pmem);
|
294 |
|
|
}
|
295 |
|
|
break;
|
296 |
|
|
case 0x35: // Read configuration register
|
297 |
|
|
m_state = QSPIF_RDCR;
|
298 |
|
|
if (m_debug) printf("QSPI: READING CONFIGURATION REGISTER: %02x\n", m_creg);
|
299 |
|
|
QOREG(m_creg);
|
300 |
|
|
break;
|
301 |
|
|
case 0x9f: // Read ID
|
302 |
|
|
m_state = QSPIF_RDID;
|
303 |
|
|
if (m_debug) printf("QSPI: READING ID, %02x\n", (DEVID>>24)&0x0ff);
|
304 |
|
|
QOREG(0xfe);
|
305 |
|
|
break;
|
306 |
|
|
case 0xab: // Release from DEEP POWER DOWN
|
307 |
|
|
if (m_sreg & QSPIF_DEEP_POWER_DOWN_FLAG) {
|
308 |
|
|
if (m_debug) printf("QSPI: Release from deep power down\n");
|
309 |
|
|
m_sreg &= (~QSPIF_DEEP_POWER_DOWN_FLAG);
|
310 |
|
|
m_write_count = tRES;
|
311 |
|
|
} m_state = QSPIF_RELEASE;
|
312 |
|
|
break;
|
313 |
|
|
case 0xb9: // DEEP POWER DOWN
|
314 |
|
|
if (0 != (m_sreg & 0x01)) {
|
315 |
|
|
if (m_debug) printf("QSPI: Cannot enter DEEP POWER DOWN, in middle of write/erase\n");
|
316 |
|
|
m_state = QSPIF_INVALID;
|
317 |
|
|
} else {
|
318 |
|
|
m_sreg |= QSPIF_DEEP_POWER_DOWN_FLAG;
|
319 |
|
|
m_state = QSPIF_IDLE;
|
320 |
|
|
}
|
321 |
|
|
break;
|
322 |
|
|
case 0xc7: // Bulk Erase
|
323 |
|
|
if (2 != (m_sreg & 0x203)) {
|
324 |
|
|
if (m_debug) printf("QSPI: WEL not set, cannot erase device\n");
|
325 |
|
|
m_state = QSPIF_INVALID;
|
326 |
|
|
} else
|
327 |
|
|
m_state = QSPIF_BULK_ERASE;
|
328 |
|
|
break;
|
329 |
|
|
case 0xd8: // Sector Erase
|
330 |
|
|
if (2 != (m_sreg & 0x203)) {
|
331 |
|
|
if (m_debug) printf("QSPI: WEL not set, cannot erase sector\n");
|
332 |
|
|
m_state = QSPIF_INVALID;
|
333 |
|
|
} else {
|
334 |
|
|
m_state = QSPIF_SECTOR_ERASE;
|
335 |
|
|
if (m_debug) printf("QSPI: SECTOR_ERASE COMMAND\n");
|
336 |
|
|
}
|
337 |
|
|
break;
|
338 |
|
|
case 0x0eb: // Here's the (other) read that we support
|
339 |
|
|
// printf("QSPI: QUAD-I/O-READ\n");
|
340 |
|
|
m_state = QSPIF_QUAD_READ_CMD;
|
341 |
|
|
m_quad_mode = true;
|
342 |
|
|
break;
|
343 |
|
|
default:
|
344 |
|
|
printf("QSPI: UNRECOGNIZED SPI FLASH CMD: %02x\n", m_ireg&0x0ff);
|
345 |
|
|
m_state = QSPIF_INVALID;
|
346 |
|
|
assert(0 && "Unrecognized command\n");
|
347 |
|
|
break;
|
348 |
|
|
}
|
349 |
|
|
} else if ((0 == (m_count&0x07))&&(m_count != 0)) {
|
350 |
|
|
QOREG(0);
|
351 |
|
|
switch(m_state) {
|
352 |
|
|
case QSPIF_IDLE:
|
353 |
|
|
printf("TOO MANY CLOCKS, SPIF in IDLE\n");
|
354 |
|
|
break;
|
355 |
|
|
case QSPIF_WRSR:
|
356 |
|
|
if (m_count == 16) {
|
357 |
|
|
m_sreg = (m_sreg & 0x061) | (m_ireg & 0x09c);
|
358 |
|
|
if (m_debug) printf("Request to set sreg to 0x%02x\n",
|
359 |
|
|
m_ireg&0x0ff);
|
360 |
|
|
} else if (m_count == 24) {
|
361 |
|
|
m_creg = (m_creg & 0x0fd) | (m_ireg & 0x02);
|
362 |
|
|
if (m_debug) printf("Request to set creg to 0x%02x\n",
|
363 |
|
|
m_ireg&0x0ff);
|
364 |
|
|
} else {
|
365 |
|
|
printf("TOO MANY CLOCKS FOR WRR!!!\n");
|
366 |
|
|
exit(-2);
|
367 |
|
|
m_state = QSPIF_IDLE;
|
368 |
|
|
}
|
369 |
|
|
break;
|
370 |
|
|
case QSPIF_CLSR:
|
371 |
|
|
assert(0 && "Too many clocks for CLSR command!!\n");
|
372 |
|
|
break;
|
373 |
|
|
case QSPIF_RDID:
|
374 |
|
|
if (m_count == 32) {
|
375 |
|
|
m_addr = m_ireg & 0x0ffffff;
|
376 |
|
|
if (m_debug) printf("READID, ADDR = %08x\n", m_addr);
|
377 |
|
|
QOREG((DEVID>>8));
|
378 |
|
|
if (m_debug) printf("QSPI: READING ID, %02x\n", (DEVID>>8)&0x0ff);
|
379 |
|
|
} else if (m_count > 32) {
|
380 |
|
|
if (((m_count-32)>>3)&1)
|
381 |
|
|
QOREG((DEVID));
|
382 |
|
|
else
|
383 |
|
|
QOREG((DEVID>>8));
|
384 |
|
|
if (m_debug) printf("QSPI: READING ID, %02x -- DONE\n", 0x00);
|
385 |
|
|
}
|
386 |
|
|
// m_oreg = (DEVID >> (2-(m_count>>3)-1)) & 0x0ff;
|
387 |
|
|
break;
|
388 |
|
|
case QSPIF_RDSR:
|
389 |
|
|
// printf("Read SREG = %02x, wait = %08x\n", m_sreg,
|
390 |
|
|
// m_write_count);
|
391 |
|
|
QOREG(m_sreg);
|
392 |
|
|
break;
|
393 |
|
|
case QSPIF_RDCR:
|
394 |
|
|
if (m_debug) printf("Read CREG = %02x\n", m_creg);
|
395 |
|
|
QOREG(m_creg);
|
396 |
|
|
break;
|
397 |
|
|
case QSPIF_FAST_READ:
|
398 |
|
|
if (m_count == 32) {
|
399 |
|
|
m_addr = m_ireg & 0x0ffffff;
|
400 |
|
|
if (m_debug) printf("FAST READ, ADDR = %08x\n", m_addr);
|
401 |
|
|
QOREG(0x0c3);
|
402 |
|
|
assert((m_addr & 0xfc00000)==0);
|
403 |
|
|
} else if ((m_count >= 40)&&(0 == (m_sreg&0x01))) {
|
404 |
|
|
//if (m_count == 40)
|
405 |
|
|
//printf("DUMMY BYTE COMPLETE ...\n");
|
406 |
|
|
QOREG(m_mem[m_addr++]);
|
407 |
|
|
// if (m_debug) printf("SPIF[%08x] = %02x\n", m_addr-1, m_oreg);
|
408 |
|
|
} else m_oreg = 0;
|
409 |
|
|
break;
|
410 |
|
|
case QSPIF_QUAD_READ_CMD:
|
411 |
|
|
// The command to go into quad read mode took 8 bits
|
412 |
|
|
// that changes the timings, else we'd use quad_Read
|
413 |
|
|
// below
|
414 |
|
|
if (m_count == 32) {
|
415 |
|
|
m_addr = m_ireg & 0x0ffffff;
|
416 |
|
|
// printf("FAST READ, ADDR = %08x\n", m_addr);
|
417 |
|
|
// printf("QSPI: QUAD READ, ADDR = %06x\n", m_addr);
|
418 |
|
|
assert((m_addr & 0xfc00000)==0);
|
419 |
|
|
} else if (m_count == 32+24) {
|
420 |
|
|
m_mode_byte = (m_ireg>>16) & 0x0ff;
|
421 |
|
|
// printf("QSPI: MODE BYTE = %02x\n", m_mode_byte);
|
422 |
|
|
} else if ((m_count > 32+24)&&(0 == (m_sreg&0x01))) {
|
423 |
|
|
QOREG(m_mem[m_addr++]);
|
424 |
|
|
// printf("QSPIF[%08x]/QR = %02x\n",
|
425 |
|
|
// m_addr-1, m_oreg);
|
426 |
|
|
} else m_oreg = 0;
|
427 |
|
|
break;
|
428 |
|
|
case QSPIF_QUAD_READ:
|
429 |
|
|
if (m_count == 32) {
|
430 |
|
|
m_mode_byte = (m_ireg & 0x0ff);
|
431 |
|
|
// printf("QSPI/QR: MODE BYTE = %02x\n", m_mode_byte);
|
432 |
|
|
} else if ((m_count >= 32+16)&&(0 == (m_sreg&0x01))) {
|
433 |
|
|
QOREG(m_mem[m_addr++]);
|
434 |
|
|
// printf("QSPIF[%08x]/QR = %02x\n", m_addr-1, m_oreg & 0x0ff);
|
435 |
|
|
} else m_oreg = 0;
|
436 |
|
|
break;
|
437 |
|
|
case QSPIF_PP:
|
438 |
|
|
if (m_count == 32) {
|
439 |
|
|
m_addr = m_ireg & 0x0ffffff;
|
440 |
|
|
if (m_debug) printf("QSPI: PAGE-PROGRAM ADDR = %06x\n", m_addr);
|
441 |
|
|
assert((m_addr & 0xfc00000)==0);
|
442 |
|
|
// m_page = m_addr >> 8;
|
443 |
|
|
for(int i=0; i<256; i++)
|
444 |
|
|
m_pmem[i] = 0x0ff;
|
445 |
|
|
} else if (m_count >= 40) {
|
446 |
|
|
m_pmem[m_addr & 0x0ff] = m_ireg & 0x0ff;
|
447 |
|
|
// printf("QSPI: PMEM[%02x] = 0x%02x -> %02x\n", m_addr & 0x0ff, m_ireg & 0x0ff, (m_pmem[(m_addr & 0x0ff)]&0x0ff));
|
448 |
|
|
m_addr = (m_addr & (~0x0ff)) | ((m_addr+1)&0x0ff);
|
449 |
|
|
} break;
|
450 |
|
|
case QSPIF_QPP:
|
451 |
|
|
if (m_count == 32) {
|
452 |
|
|
m_addr = m_ireg & 0x0ffffff;
|
453 |
|
|
m_quad_mode = true;
|
454 |
|
|
if (m_debug) printf("QSPI/QR: PAGE-PROGRAM ADDR = %06x\n", m_addr);
|
455 |
|
|
assert((m_addr & 0xfc00000)==0);
|
456 |
|
|
// m_page = m_addr >> 8;
|
457 |
|
|
for(int i=0; i<256; i++)
|
458 |
|
|
m_pmem[i] = 0x0ff;
|
459 |
|
|
} else if (m_count >= 40) {
|
460 |
|
|
m_pmem[m_addr & 0x0ff] = m_ireg & 0x0ff;
|
461 |
|
|
// printf("QSPI/QR: PMEM[%02x] = 0x%02x -> %02x\n", m_addr & 0x0ff, m_ireg & 0x0ff, (m_pmem[(m_addr & 0x0ff)]&0x0ff));
|
462 |
|
|
m_addr = (m_addr & (~0x0ff)) | ((m_addr+1)&0x0ff);
|
463 |
|
|
} break;
|
464 |
|
|
case QSPIF_SECTOR_ERASE:
|
465 |
|
|
if (m_count == 32) {
|
466 |
|
|
m_addr = m_ireg & 0x0ffc000;
|
467 |
|
|
if (m_debug) printf("SECTOR_ERASE ADDRESS = %08x\n", m_addr);
|
468 |
|
|
assert((m_addr & 0xfc00000)==0);
|
469 |
|
|
} break;
|
470 |
|
|
case QSPIF_RELEASE:
|
471 |
|
|
if (m_count >= 32) {
|
472 |
|
|
QOREG(DEVESD);
|
473 |
|
|
} break;
|
474 |
|
|
default:
|
475 |
|
|
break;
|
476 |
|
|
}
|
477 |
|
|
} // else printf("SFLASH->count = %d\n", m_count);
|
478 |
|
|
|
479 |
|
|
m_last_sck = sck;
|
480 |
|
|
if (m_quad_mode)
|
481 |
|
|
return (m_oreg>>8)&0x0f;
|
482 |
|
|
else
|
483 |
|
|
// return ((m_oreg & 0x0100)?2:0) | (dat & 0x0d);
|
484 |
|
|
return (m_oreg & 0x0100)?2:0;
|
485 |
|
|
}
|
486 |
|
|
|