1 |
58 |
dgisselq |
////////////////////////////////////////////////////////////////////////////////
|
2 |
|
|
//
|
3 |
|
|
// Filename: eqspiflashsim.h
|
4 |
|
|
//
|
5 |
|
|
// Project: OpenArty, an entirely open SoC based upon the Arty platform
|
6 |
|
|
//
|
7 |
|
|
// Purpose: This library simulates the operation of an Extended Quad-SPI
|
8 |
|
|
// commanded flash, such as the N25Q128A used on the Arty
|
9 |
|
|
// development board by Digilent. As such, it is defined by
|
10 |
|
|
// 16 MBytes of memory (4 MWords).
|
11 |
|
|
//
|
12 |
|
|
// Creator: Dan Gisselquist, Ph.D.
|
13 |
|
|
// Gisselquist Technology, LLC
|
14 |
|
|
//
|
15 |
|
|
////////////////////////////////////////////////////////////////////////////////
|
16 |
|
|
//
|
17 |
|
|
// Copyright (C) 2015,2017, Gisselquist Technology, LLC
|
18 |
|
|
//
|
19 |
|
|
// This program is free software (firmware): you can redistribute it and/or
|
20 |
|
|
// modify it under the terms of the GNU General Public License as published
|
21 |
|
|
// by the Free Software Foundation, either version 3 of the License, or (at
|
22 |
|
|
// your option) any later version.
|
23 |
|
|
//
|
24 |
|
|
// This program is distributed in the hope that it will be useful, but WITHOUT
|
25 |
|
|
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
|
26 |
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
27 |
|
|
// for more details.
|
28 |
|
|
//
|
29 |
|
|
// You should have received a copy of the GNU General Public License along
|
30 |
|
|
// with this program. (It's in the $(ROOT)/doc directory. Run make with no
|
31 |
|
|
// target there if the PDF file isn't present.) If not, see
|
32 |
|
|
// <http://www.gnu.org/licenses/> for a copy.
|
33 |
|
|
//
|
34 |
|
|
// License: GPL, v3, as defined and found on www.gnu.org,
|
35 |
|
|
// http://www.gnu.org/licenses/gpl.html
|
36 |
|
|
//
|
37 |
|
|
//
|
38 |
|
|
////////////////////////////////////////////////////////////////////////////////
|
39 |
|
|
//
|
40 |
|
|
//
|
41 |
|
|
#ifndef EQSPIFLASHSIM_H
|
42 |
|
|
#define EQSPIFLASHSIM_H
|
43 |
|
|
|
44 |
|
|
#define EQSPIF_WIP_FLAG 0x0001
|
45 |
|
|
#define EQSPIF_WEL_FLAG 0x0002
|
46 |
|
|
#define EQSPIF_DEEP_POWER_DOWN_FLAG 0x0200
|
47 |
|
|
class EQSPIFLASHSIM {
|
48 |
|
|
typedef enum {
|
49 |
|
|
EQSPIF_IDLE,
|
50 |
|
|
EQSPIF_XIP,
|
51 |
|
|
EQSPIF_RDSR,
|
52 |
|
|
EQSPIF_RDCR,
|
53 |
|
|
EQSPIF_RDNVCONFIG,
|
54 |
|
|
EQSPIF_RDEVCONFIG,
|
55 |
|
|
EQSPIF_WRSR,
|
56 |
|
|
EQSPIF_WRCR,
|
57 |
|
|
EQSPIF_WRNVCONFIG,
|
58 |
|
|
EQSPIF_WREVCONFIG,
|
59 |
|
|
EQSPIF_RDFLAGS,
|
60 |
|
|
EQSPIF_CLRFLAGS,
|
61 |
|
|
EQSPIF_RDLOCK,
|
62 |
|
|
EQSPIF_WRLOCK,
|
63 |
|
|
EQSPIF_RDID,
|
64 |
|
|
EQSPIF_RELEASE,
|
65 |
|
|
EQSPIF_FAST_READ,
|
66 |
|
|
EQSPIF_QUAD_OREAD_CMD,
|
67 |
|
|
EQSPIF_QUAD_IOREAD_CMD,
|
68 |
|
|
EQSPIF_QUAD_READ,
|
69 |
|
|
EQSPIF_PP,
|
70 |
|
|
EQSPIF_QPP,
|
71 |
|
|
// Erase states
|
72 |
|
|
EQSPIF_SUBSECTOR_ERASE,
|
73 |
|
|
EQSPIF_SECTOR_ERASE,
|
74 |
|
|
EQSPIF_BULK_ERASE,
|
75 |
|
|
// OTP memory
|
76 |
|
|
EQSPIF_PROGRAM_OTP,
|
77 |
|
|
EQSPIF_READ_OTP,
|
78 |
|
|
//
|
79 |
|
|
EQSPIF_INVALID
|
80 |
|
|
} EQSPIF_STATE;
|
81 |
|
|
|
82 |
|
|
EQSPIF_STATE m_state;
|
83 |
|
|
char *m_mem, *m_pmem, *m_otp, *m_lockregs;
|
84 |
|
|
int m_last_sck;
|
85 |
|
|
unsigned m_write_count, m_ireg, m_oreg, m_sreg, m_addr,
|
86 |
|
|
m_count, m_vconfig, m_mode_byte, m_creg, m_membytes,
|
87 |
|
|
m_memmask, m_nvconfig, m_evconfig, m_flagreg, m_nxtout[4];
|
88 |
|
|
bool mode, m_debug, m_otp_wp;
|
89 |
|
|
|
90 |
|
|
typedef enum {
|
91 |
|
|
EQSPIF_QMODE_SPI = 0,
|
92 |
|
|
EQSPIF_QMODE_QSPI_ADDR,
|
93 |
|
|
EQSPIF_QMODE_SPI_ADDR
|
94 |
|
|
} QUAD_MODE;
|
95 |
|
|
QUAD_MODE m_quad_mode;
|
96 |
|
|
|
97 |
|
|
public:
|
98 |
|
|
EQSPIFLASHSIM(const int lglen = 24, bool debug = false);
|
99 |
|
|
void load(const char *fname) { load(0, fname); }
|
100 |
|
|
void load(const unsigned addr, const char *fname);
|
101 |
|
|
void load(const uint32_t offset, const char *data, const uint32_t len);
|
102 |
|
|
void debug(const bool dbg) { m_debug = dbg; }
|
103 |
|
|
bool debug(void) const { return m_debug; }
|
104 |
|
|
bool write_enabled(void) const { return m_debug; }
|
105 |
|
|
unsigned counts_till_idle(void) const {
|
106 |
|
|
return m_write_count; }
|
107 |
|
|
unsigned operator[](const int index) {
|
108 |
|
|
unsigned char *cptr = (unsigned char *)&m_mem[index<<2];
|
109 |
|
|
unsigned v;
|
110 |
|
|
v = (*cptr++);
|
111 |
|
|
v = (v<<8)|(*cptr++);
|
112 |
|
|
v = (v<<8)|(*cptr++);
|
113 |
|
|
v = (v<<8)|(*cptr);
|
114 |
|
|
|
115 |
|
|
return v; }
|
116 |
|
|
void set(const unsigned addr, const unsigned val) {
|
117 |
|
|
unsigned char *cptr = (unsigned char *)&m_mem[addr<<2];
|
118 |
|
|
*cptr++ = (val>>24);
|
119 |
|
|
*cptr++ = (val>>16);
|
120 |
|
|
*cptr++ = (val>> 8);
|
121 |
|
|
*cptr = (val);
|
122 |
|
|
return;}
|
123 |
|
|
int operator()(const int csn, const int sck, const int dat);
|
124 |
|
|
};
|
125 |
|
|
|
126 |
|
|
#endif
|