2 * ST M25P80 emulator. Emulate all SPI flash devices based on the m25p80 command
3 * set. Known devices table current as of Jun/2012 and taken from linux.
4 * See drivers/mtd/devices/m25p80.c.
6 * Copyright (C) 2011 Edgar E. Iglesias <edgar.iglesias@gmail.com>
7 * Copyright (C) 2012 Peter A. G. Crosthwaite <peter.crosthwaite@petalogix.com>
8 * Copyright (C) 2012 PetaLogix
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation; either version 2 or
13 * (at your option) a later version of the License.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, see <http://www.gnu.org/licenses/>.
24 #include "qemu/osdep.h"
25 #include "qemu/units.h"
26 #include "sysemu/block-backend.h"
27 #include "hw/qdev-properties.h"
28 #include "hw/qdev-properties-system.h"
29 #include "hw/ssi/ssi.h"
30 #include "migration/vmstate.h"
31 #include "qemu/bitops.h"
33 #include "qemu/module.h"
34 #include "qemu/error-report.h"
35 #include "qapi/error.h"
37 #include "qom/object.h"
39 /* 16 MiB max in 3 byte address mode */
40 #define MAX_3BYTES_SIZE 0x1000000
41 #define SPI_NOR_MAX_ID_LEN 6
43 /* Fields for FlashPartInfo->flags */
44 enum spi_flash_option_flags
{
49 HAS_SR_BP3_BIT6
= BIT(4),
52 typedef struct FlashPartInfo
{
53 const char *part_name
;
55 * This array stores the ID bytes.
56 * The first three bytes are the JEDIC ID.
57 * JEDEC ID zero means "no ID" (mostly older chips).
59 uint8_t id
[SPI_NOR_MAX_ID_LEN
];
61 /* there is confusion between manufacturers as to what a sector is. In this
62 * device model, a "sector" is the size that is erased by the ERASE_SECTOR
63 * command (opcode 0xd8).
70 * Big sized spi nor are often stacked devices, thus sometime
71 * replace chip erase with die erase.
72 * This field inform how many die is in the chip.
77 /* adapted from linux */
78 /* Used when the "_ext_id" is two bytes at most */
79 #define INFO(_part_name, _jedec_id, _ext_id, _sector_size, _n_sectors, _flags)\
80 .part_name = _part_name,\
82 ((_jedec_id) >> 16) & 0xff,\
83 ((_jedec_id) >> 8) & 0xff,\
85 ((_ext_id) >> 8) & 0xff,\
88 .id_len = (!(_jedec_id) ? 0 : (3 + ((_ext_id) ? 2 : 0))),\
89 .sector_size = (_sector_size),\
90 .n_sectors = (_n_sectors),\
95 #define INFO6(_part_name, _jedec_id, _ext_id, _sector_size, _n_sectors, _flags)\
96 .part_name = _part_name,\
98 ((_jedec_id) >> 16) & 0xff,\
99 ((_jedec_id) >> 8) & 0xff,\
101 ((_ext_id) >> 16) & 0xff,\
102 ((_ext_id) >> 8) & 0xff,\
106 .sector_size = (_sector_size),\
107 .n_sectors = (_n_sectors),\
112 #define INFO_STACKED(_part_name, _jedec_id, _ext_id, _sector_size, _n_sectors,\
114 .part_name = _part_name,\
116 ((_jedec_id) >> 16) & 0xff,\
117 ((_jedec_id) >> 8) & 0xff,\
119 ((_ext_id) >> 8) & 0xff,\
122 .id_len = (!(_jedec_id) ? 0 : (3 + ((_ext_id) ? 2 : 0))),\
123 .sector_size = (_sector_size),\
124 .n_sectors = (_n_sectors),\
129 #define JEDEC_NUMONYX 0x20
130 #define JEDEC_WINBOND 0xEF
131 #define JEDEC_SPANSION 0x01
133 /* Numonyx (Micron) Configuration register macros */
134 #define VCFG_DUMMY 0x1
135 #define VCFG_WRAP_SEQUENTIAL 0x2
136 #define NVCFG_XIP_MODE_DISABLED (7 << 9)
137 #define NVCFG_XIP_MODE_MASK (7 << 9)
138 #define VCFG_XIP_MODE_DISABLED (1 << 3)
139 #define CFG_DUMMY_CLK_LEN 4
140 #define NVCFG_DUMMY_CLK_POS 12
141 #define VCFG_DUMMY_CLK_POS 4
142 #define EVCFG_OUT_DRIVER_STRENGTH_DEF 7
143 #define EVCFG_VPP_ACCELERATOR (1 << 3)
144 #define EVCFG_RESET_HOLD_ENABLED (1 << 4)
145 #define NVCFG_DUAL_IO_MASK (1 << 2)
146 #define EVCFG_DUAL_IO_DISABLED (1 << 6)
147 #define NVCFG_QUAD_IO_MASK (1 << 3)
148 #define EVCFG_QUAD_IO_DISABLED (1 << 7)
149 #define NVCFG_4BYTE_ADDR_MASK (1 << 0)
150 #define NVCFG_LOWER_SEGMENT_MASK (1 << 1)
152 /* Numonyx (Micron) Flag Status Register macros */
153 #define FSR_4BYTE_ADDR_MODE_ENABLED 0x1
154 #define FSR_FLASH_READY (1 << 7)
156 /* Spansion configuration registers macros. */
157 #define SPANSION_QUAD_CFG_POS 0
158 #define SPANSION_QUAD_CFG_LEN 1
159 #define SPANSION_DUMMY_CLK_POS 0
160 #define SPANSION_DUMMY_CLK_LEN 4
161 #define SPANSION_ADDR_LEN_POS 7
162 #define SPANSION_ADDR_LEN_LEN 1
165 * Spansion read mode command length in bytes,
166 * the mode is currently not supported.
169 #define SPANSION_CONTINUOUS_READ_MODE_CMD_LEN 1
170 #define WINBOND_CONTINUOUS_READ_MODE_CMD_LEN 1
172 static const FlashPartInfo known_devices
[] = {
173 /* Atmel -- some are (confusingly) marketed as "DataFlash" */
174 { INFO("at25fs010", 0x1f6601, 0, 32 << 10, 4, ER_4K
) },
175 { INFO("at25fs040", 0x1f6604, 0, 64 << 10, 8, ER_4K
) },
177 { INFO("at25df041a", 0x1f4401, 0, 64 << 10, 8, ER_4K
) },
178 { INFO("at25df321a", 0x1f4701, 0, 64 << 10, 64, ER_4K
) },
179 { INFO("at25df641", 0x1f4800, 0, 64 << 10, 128, ER_4K
) },
181 { INFO("at26f004", 0x1f0400, 0, 64 << 10, 8, ER_4K
) },
182 { INFO("at26df081a", 0x1f4501, 0, 64 << 10, 16, ER_4K
) },
183 { INFO("at26df161a", 0x1f4601, 0, 64 << 10, 32, ER_4K
) },
184 { INFO("at26df321", 0x1f4700, 0, 64 << 10, 64, ER_4K
) },
186 { INFO("at45db081d", 0x1f2500, 0, 64 << 10, 16, ER_4K
) },
188 /* Atmel EEPROMS - it is assumed, that don't care bit in command
189 * is set to 0. Block protection is not supported.
191 { INFO("at25128a-nonjedec", 0x0, 0, 1, 131072, EEPROM
) },
192 { INFO("at25256a-nonjedec", 0x0, 0, 1, 262144, EEPROM
) },
195 { INFO("en25f32", 0x1c3116, 0, 64 << 10, 64, ER_4K
) },
196 { INFO("en25p32", 0x1c2016, 0, 64 << 10, 64, 0) },
197 { INFO("en25q32b", 0x1c3016, 0, 64 << 10, 64, 0) },
198 { INFO("en25p64", 0x1c2017, 0, 64 << 10, 128, 0) },
199 { INFO("en25q64", 0x1c3017, 0, 64 << 10, 128, ER_4K
) },
202 { INFO("gd25q32", 0xc84016, 0, 64 << 10, 64, ER_4K
) },
203 { INFO("gd25q64", 0xc84017, 0, 64 << 10, 128, ER_4K
) },
205 /* Intel/Numonyx -- xxxs33b */
206 { INFO("160s33b", 0x898911, 0, 64 << 10, 32, 0) },
207 { INFO("320s33b", 0x898912, 0, 64 << 10, 64, 0) },
208 { INFO("640s33b", 0x898913, 0, 64 << 10, 128, 0) },
209 { INFO("n25q064", 0x20ba17, 0, 64 << 10, 128, 0) },
212 { INFO("is25lq040b", 0x9d4013, 0, 64 << 10, 8, ER_4K
) },
213 { INFO("is25lp080d", 0x9d6014, 0, 64 << 10, 16, ER_4K
) },
214 { INFO("is25lp016d", 0x9d6015, 0, 64 << 10, 32, ER_4K
) },
215 { INFO("is25lp032", 0x9d6016, 0, 64 << 10, 64, ER_4K
) },
216 { INFO("is25lp064", 0x9d6017, 0, 64 << 10, 128, ER_4K
) },
217 { INFO("is25lp128", 0x9d6018, 0, 64 << 10, 256, ER_4K
) },
218 { INFO("is25lp256", 0x9d6019, 0, 64 << 10, 512, ER_4K
) },
219 { INFO("is25wp032", 0x9d7016, 0, 64 << 10, 64, ER_4K
) },
220 { INFO("is25wp064", 0x9d7017, 0, 64 << 10, 128, ER_4K
) },
221 { INFO("is25wp128", 0x9d7018, 0, 64 << 10, 256, ER_4K
) },
222 { INFO("is25wp256", 0x9d7019, 0, 64 << 10, 512, ER_4K
) },
225 { INFO("mx25l2005a", 0xc22012, 0, 64 << 10, 4, ER_4K
) },
226 { INFO("mx25l4005a", 0xc22013, 0, 64 << 10, 8, ER_4K
) },
227 { INFO("mx25l8005", 0xc22014, 0, 64 << 10, 16, 0) },
228 { INFO("mx25l1606e", 0xc22015, 0, 64 << 10, 32, ER_4K
) },
229 { INFO("mx25l3205d", 0xc22016, 0, 64 << 10, 64, 0) },
230 { INFO("mx25l6405d", 0xc22017, 0, 64 << 10, 128, 0) },
231 { INFO("mx25l12805d", 0xc22018, 0, 64 << 10, 256, 0) },
232 { INFO("mx25l12855e", 0xc22618, 0, 64 << 10, 256, 0) },
233 { INFO6("mx25l25635e", 0xc22019, 0xc22019, 64 << 10, 512, 0) },
234 { INFO("mx25l25655e", 0xc22619, 0, 64 << 10, 512, 0) },
235 { INFO("mx66l51235f", 0xc2201a, 0, 64 << 10, 1024, ER_4K
| ER_32K
) },
236 { INFO("mx66u51235f", 0xc2253a, 0, 64 << 10, 1024, ER_4K
| ER_32K
) },
237 { INFO("mx66u1g45g", 0xc2253b, 0, 64 << 10, 2048, ER_4K
| ER_32K
) },
238 { INFO("mx66l1g45g", 0xc2201b, 0, 64 << 10, 2048, ER_4K
| ER_32K
) },
241 { INFO("n25q032a11", 0x20bb16, 0, 64 << 10, 64, ER_4K
) },
242 { INFO("n25q032a13", 0x20ba16, 0, 64 << 10, 64, ER_4K
) },
243 { INFO("n25q064a11", 0x20bb17, 0, 64 << 10, 128, ER_4K
) },
244 { INFO("n25q064a13", 0x20ba17, 0, 64 << 10, 128, ER_4K
) },
245 { INFO("n25q128a11", 0x20bb18, 0, 64 << 10, 256, ER_4K
) },
246 { INFO("n25q128a13", 0x20ba18, 0, 64 << 10, 256, ER_4K
) },
247 { INFO("n25q256a11", 0x20bb19, 0, 64 << 10, 512, ER_4K
) },
248 { INFO("n25q256a13", 0x20ba19, 0, 64 << 10, 512, ER_4K
) },
249 { INFO("n25q512a11", 0x20bb20, 0, 64 << 10, 1024, ER_4K
) },
250 { INFO("n25q512a13", 0x20ba20, 0, 64 << 10, 1024, ER_4K
) },
251 { INFO("n25q128", 0x20ba18, 0, 64 << 10, 256, 0) },
252 { INFO("n25q256a", 0x20ba19, 0, 64 << 10, 512,
253 ER_4K
| HAS_SR_BP3_BIT6
| HAS_SR_TB
) },
254 { INFO("n25q512a", 0x20ba20, 0, 64 << 10, 1024, ER_4K
) },
255 { INFO("n25q512ax3", 0x20ba20, 0x1000, 64 << 10, 1024, ER_4K
) },
256 { INFO("mt25ql512ab", 0x20ba20, 0x1044, 64 << 10, 1024, ER_4K
| ER_32K
) },
257 { INFO_STACKED("mt35xu01g", 0x2c5b1b, 0x104100, 128 << 10, 1024,
258 ER_4K
| ER_32K
, 2) },
259 { INFO_STACKED("n25q00", 0x20ba21, 0x1000, 64 << 10, 2048, ER_4K
, 4) },
260 { INFO_STACKED("n25q00a", 0x20bb21, 0x1000, 64 << 10, 2048, ER_4K
, 4) },
261 { INFO_STACKED("mt25ql01g", 0x20ba21, 0x1040, 64 << 10, 2048, ER_4K
, 2) },
262 { INFO_STACKED("mt25qu01g", 0x20bb21, 0x1040, 64 << 10, 2048, ER_4K
, 2) },
263 { INFO_STACKED("mt25ql02g", 0x20ba22, 0x1040, 64 << 10, 4096, ER_4K
| ER_32K
, 2) },
264 { INFO_STACKED("mt25qu02g", 0x20bb22, 0x1040, 64 << 10, 4096, ER_4K
| ER_32K
, 2) },
266 /* Spansion -- single (large) sector size only, at least
267 * for the chips listed here (without boot sectors).
269 { INFO("s25sl032p", 0x010215, 0x4d00, 64 << 10, 64, ER_4K
) },
270 { INFO("s25sl064p", 0x010216, 0x4d00, 64 << 10, 128, ER_4K
) },
271 { INFO("s25fl256s0", 0x010219, 0x4d00, 256 << 10, 128, 0) },
272 { INFO("s25fl256s1", 0x010219, 0x4d01, 64 << 10, 512, 0) },
273 { INFO6("s25fl512s", 0x010220, 0x4d0080, 256 << 10, 256, 0) },
274 { INFO6("s70fl01gs", 0x010221, 0x4d0080, 256 << 10, 512, 0) },
275 { INFO("s25sl12800", 0x012018, 0x0300, 256 << 10, 64, 0) },
276 { INFO("s25sl12801", 0x012018, 0x0301, 64 << 10, 256, 0) },
277 { INFO("s25fl129p0", 0x012018, 0x4d00, 256 << 10, 64, 0) },
278 { INFO("s25fl129p1", 0x012018, 0x4d01, 64 << 10, 256, 0) },
279 { INFO("s25sl004a", 0x010212, 0, 64 << 10, 8, 0) },
280 { INFO("s25sl008a", 0x010213, 0, 64 << 10, 16, 0) },
281 { INFO("s25sl016a", 0x010214, 0, 64 << 10, 32, 0) },
282 { INFO("s25sl032a", 0x010215, 0, 64 << 10, 64, 0) },
283 { INFO("s25sl064a", 0x010216, 0, 64 << 10, 128, 0) },
284 { INFO("s25fl016k", 0xef4015, 0, 64 << 10, 32, ER_4K
| ER_32K
) },
285 { INFO("s25fl064k", 0xef4017, 0, 64 << 10, 128, ER_4K
| ER_32K
) },
287 /* Spansion -- boot sectors support */
288 { INFO6("s25fs512s", 0x010220, 0x4d0081, 256 << 10, 256, 0) },
289 { INFO6("s70fs01gs", 0x010221, 0x4d0081, 256 << 10, 512, 0) },
291 /* SST -- large erase sizes are "overlays", "sectors" are 4<< 10 */
292 { INFO("sst25vf040b", 0xbf258d, 0, 64 << 10, 8, ER_4K
) },
293 { INFO("sst25vf080b", 0xbf258e, 0, 64 << 10, 16, ER_4K
) },
294 { INFO("sst25vf016b", 0xbf2541, 0, 64 << 10, 32, ER_4K
) },
295 { INFO("sst25vf032b", 0xbf254a, 0, 64 << 10, 64, ER_4K
) },
296 { INFO("sst25wf512", 0xbf2501, 0, 64 << 10, 1, ER_4K
) },
297 { INFO("sst25wf010", 0xbf2502, 0, 64 << 10, 2, ER_4K
) },
298 { INFO("sst25wf020", 0xbf2503, 0, 64 << 10, 4, ER_4K
) },
299 { INFO("sst25wf040", 0xbf2504, 0, 64 << 10, 8, ER_4K
) },
300 { INFO("sst25wf080", 0xbf2505, 0, 64 << 10, 16, ER_4K
) },
302 /* ST Microelectronics -- newer production may have feature updates */
303 { INFO("m25p05", 0x202010, 0, 32 << 10, 2, 0) },
304 { INFO("m25p10", 0x202011, 0, 32 << 10, 4, 0) },
305 { INFO("m25p20", 0x202012, 0, 64 << 10, 4, 0) },
306 { INFO("m25p40", 0x202013, 0, 64 << 10, 8, 0) },
307 { INFO("m25p80", 0x202014, 0, 64 << 10, 16, 0) },
308 { INFO("m25p16", 0x202015, 0, 64 << 10, 32, 0) },
309 { INFO("m25p32", 0x202016, 0, 64 << 10, 64, 0) },
310 { INFO("m25p64", 0x202017, 0, 64 << 10, 128, 0) },
311 { INFO("m25p128", 0x202018, 0, 256 << 10, 64, 0) },
312 { INFO("n25q032", 0x20ba16, 0, 64 << 10, 64, 0) },
314 { INFO("m45pe10", 0x204011, 0, 64 << 10, 2, 0) },
315 { INFO("m45pe80", 0x204014, 0, 64 << 10, 16, 0) },
316 { INFO("m45pe16", 0x204015, 0, 64 << 10, 32, 0) },
318 { INFO("m25pe20", 0x208012, 0, 64 << 10, 4, 0) },
319 { INFO("m25pe80", 0x208014, 0, 64 << 10, 16, 0) },
320 { INFO("m25pe16", 0x208015, 0, 64 << 10, 32, ER_4K
) },
322 { INFO("m25px32", 0x207116, 0, 64 << 10, 64, ER_4K
) },
323 { INFO("m25px32-s0", 0x207316, 0, 64 << 10, 64, ER_4K
) },
324 { INFO("m25px32-s1", 0x206316, 0, 64 << 10, 64, ER_4K
) },
325 { INFO("m25px64", 0x207117, 0, 64 << 10, 128, 0) },
327 /* Winbond -- w25x "blocks" are 64k, "sectors" are 4KiB */
328 { INFO("w25x10", 0xef3011, 0, 64 << 10, 2, ER_4K
) },
329 { INFO("w25x20", 0xef3012, 0, 64 << 10, 4, ER_4K
) },
330 { INFO("w25x40", 0xef3013, 0, 64 << 10, 8, ER_4K
) },
331 { INFO("w25x80", 0xef3014, 0, 64 << 10, 16, ER_4K
) },
332 { INFO("w25x16", 0xef3015, 0, 64 << 10, 32, ER_4K
) },
333 { INFO("w25x32", 0xef3016, 0, 64 << 10, 64, ER_4K
) },
334 { INFO("w25q32", 0xef4016, 0, 64 << 10, 64, ER_4K
) },
335 { INFO("w25q32dw", 0xef6016, 0, 64 << 10, 64, ER_4K
) },
336 { INFO("w25x64", 0xef3017, 0, 64 << 10, 128, ER_4K
) },
337 { INFO("w25q64", 0xef4017, 0, 64 << 10, 128, ER_4K
) },
338 { INFO("w25q80", 0xef5014, 0, 64 << 10, 16, ER_4K
) },
339 { INFO("w25q80bl", 0xef4014, 0, 64 << 10, 16, ER_4K
) },
340 { INFO("w25q256", 0xef4019, 0, 64 << 10, 512, ER_4K
) },
341 { INFO("w25q512jv", 0xef4020, 0, 64 << 10, 1024, ER_4K
) },
342 { INFO("w25q01jvq", 0xef4021, 0, 64 << 10, 2048, ER_4K
) },
354 BULK_ERASE_60
= 0x60,
387 ERASE4_SECTOR
= 0xdc,
389 EN_4BYTE_ADDR
= 0xB7,
390 EX_4BYTE_ADDR
= 0xE9,
392 EXTEND_ADDR_READ
= 0xC8,
393 EXTEND_ADDR_WRITE
= 0xC5,
399 * Micron: 0x35 - enable QPI
400 * Spansion: 0x35 - read control register
421 STATE_COLLECTING_DATA
,
422 STATE_COLLECTING_VAR_LEN_DATA
,
442 #define M25P80_INTERNAL_DATA_BUFFER_SZ 16
445 SSIPeripheral parent_obj
;
454 uint8_t data
[M25P80_INTERNAL_DATA_BUFFER_SZ
];
458 uint8_t needed_bytes
;
459 uint8_t cmd_in_progress
;
461 uint32_t nonvolatile_cfg
;
462 /* Configuration register for Macronix */
463 uint32_t volatile_cfg
;
464 uint32_t enh_volatile_cfg
;
465 /* Spansion cfg registers. */
466 uint8_t spansion_cr1nv
;
467 uint8_t spansion_cr2nv
;
468 uint8_t spansion_cr3nv
;
469 uint8_t spansion_cr4nv
;
470 uint8_t spansion_cr1v
;
471 uint8_t spansion_cr2v
;
472 uint8_t spansion_cr3v
;
473 uint8_t spansion_cr4v
;
476 bool four_bytes_address_mode
;
485 bool status_register_write_disabled
;
490 const FlashPartInfo
*pi
;
495 SSIPeripheralClass parent_class
;
499 #define TYPE_M25P80 "m25p80-generic"
500 OBJECT_DECLARE_TYPE(Flash
, M25P80Class
, M25P80
)
502 static inline Manufacturer
get_man(Flash
*s
)
504 switch (s
->pi
->id
[0]) {
522 static void blk_sync_complete(void *opaque
, int ret
)
524 QEMUIOVector
*iov
= opaque
;
526 qemu_iovec_destroy(iov
);
529 /* do nothing. Masters do not directly interact with the backing store,
530 * only the working copy so no mutexing required.
534 static void flash_sync_page(Flash
*s
, int page
)
538 if (!s
->blk
|| !blk_is_writable(s
->blk
)) {
542 iov
= g_new(QEMUIOVector
, 1);
543 qemu_iovec_init(iov
, 1);
544 qemu_iovec_add(iov
, s
->storage
+ page
* s
->pi
->page_size
,
546 blk_aio_pwritev(s
->blk
, page
* s
->pi
->page_size
, iov
, 0,
547 blk_sync_complete
, iov
);
550 static inline void flash_sync_area(Flash
*s
, int64_t off
, int64_t len
)
554 if (!s
->blk
|| !blk_is_writable(s
->blk
)) {
558 assert(!(len
% BDRV_SECTOR_SIZE
));
559 iov
= g_new(QEMUIOVector
, 1);
560 qemu_iovec_init(iov
, 1);
561 qemu_iovec_add(iov
, s
->storage
+ off
, len
);
562 blk_aio_pwritev(s
->blk
, off
, iov
, 0, blk_sync_complete
, iov
);
565 static void flash_erase(Flash
*s
, int offset
, FlashCMD cmd
)
568 uint8_t capa_to_assert
= 0;
574 capa_to_assert
= ER_4K
;
579 capa_to_assert
= ER_32K
;
583 len
= s
->pi
->sector_size
;
589 if (s
->pi
->die_cnt
) {
590 len
= s
->size
/ s
->pi
->die_cnt
;
591 offset
= offset
& (~(len
- 1));
593 qemu_log_mask(LOG_GUEST_ERROR
, "M25P80: die erase is not supported"
602 trace_m25p80_flash_erase(s
, offset
, len
);
604 if ((s
->pi
->flags
& capa_to_assert
) != capa_to_assert
) {
605 qemu_log_mask(LOG_GUEST_ERROR
, "M25P80: %d erase size not supported by"
609 if (!s
->write_enable
) {
610 qemu_log_mask(LOG_GUEST_ERROR
, "M25P80: erase with write protect!\n");
613 memset(s
->storage
+ offset
, 0xff, len
);
614 flash_sync_area(s
, offset
, len
);
617 static inline void flash_sync_dirty(Flash
*s
, int64_t newpage
)
619 if (s
->dirty_page
>= 0 && s
->dirty_page
!= newpage
) {
620 flash_sync_page(s
, s
->dirty_page
);
621 s
->dirty_page
= newpage
;
626 void flash_write8(Flash
*s
, uint32_t addr
, uint8_t data
)
628 uint32_t page
= addr
/ s
->pi
->page_size
;
629 uint8_t prev
= s
->storage
[s
->cur_addr
];
630 uint32_t block_protect_value
= (s
->block_protect3
<< 3) |
631 (s
->block_protect2
<< 2) |
632 (s
->block_protect1
<< 1) |
633 (s
->block_protect0
<< 0);
635 if (!s
->write_enable
) {
636 qemu_log_mask(LOG_GUEST_ERROR
, "M25P80: write with write protect!\n");
640 if (block_protect_value
> 0) {
641 uint32_t num_protected_sectors
= 1 << (block_protect_value
- 1);
642 uint32_t sector
= addr
/ s
->pi
->sector_size
;
644 /* top_bottom_bit == 0 means TOP */
645 if (!s
->top_bottom_bit
) {
646 if (s
->pi
->n_sectors
<= sector
+ num_protected_sectors
) {
647 qemu_log_mask(LOG_GUEST_ERROR
,
648 "M25P80: write with write protect!\n");
652 if (sector
< num_protected_sectors
) {
653 qemu_log_mask(LOG_GUEST_ERROR
,
654 "M25P80: write with write protect!\n");
660 if ((prev
^ data
) & data
) {
661 trace_m25p80_programming_zero_to_one(s
, addr
, prev
, data
);
664 if (s
->pi
->flags
& EEPROM
) {
665 s
->storage
[s
->cur_addr
] = data
;
667 s
->storage
[s
->cur_addr
] &= data
;
670 flash_sync_dirty(s
, page
);
671 s
->dirty_page
= page
;
674 static inline int get_addr_length(Flash
*s
)
676 /* check if eeprom is in use */
677 if (s
->pi
->flags
== EEPROM
) {
681 switch (s
->cmd_in_progress
) {
696 return s
->four_bytes_address_mode
? 4 : 3;
700 static void complete_collecting_data(Flash
*s
)
704 n
= get_addr_length(s
);
705 s
->cur_addr
= (n
== 3 ? s
->ear
: 0);
706 for (i
= 0; i
< n
; ++i
) {
708 s
->cur_addr
|= s
->data
[i
];
711 s
->cur_addr
&= s
->size
- 1;
713 s
->state
= STATE_IDLE
;
715 trace_m25p80_complete_collecting(s
, s
->cmd_in_progress
, n
, s
->ear
,
718 switch (s
->cmd_in_progress
) {
725 s
->state
= STATE_PAGE_PROGRAM
;
728 /* AAI programming starts from the even address */
729 s
->cur_addr
&= ~BIT(0);
730 s
->state
= STATE_PAGE_PROGRAM
;
744 s
->state
= STATE_READ
;
753 flash_erase(s
, s
->cur_addr
, s
->cmd_in_progress
);
756 s
->status_register_write_disabled
= extract32(s
->data
[0], 7, 1);
757 s
->block_protect0
= extract32(s
->data
[0], 2, 1);
758 s
->block_protect1
= extract32(s
->data
[0], 3, 1);
759 s
->block_protect2
= extract32(s
->data
[0], 4, 1);
760 if (s
->pi
->flags
& HAS_SR_TB
) {
761 s
->top_bottom_bit
= extract32(s
->data
[0], 5, 1);
763 if (s
->pi
->flags
& HAS_SR_BP3_BIT6
) {
764 s
->block_protect3
= extract32(s
->data
[0], 6, 1);
767 switch (get_man(s
)) {
769 s
->quad_enable
= !!(s
->data
[1] & 0x02);
772 s
->quad_enable
= extract32(s
->data
[0], 6, 1);
775 s
->quad_enable
= extract32(s
->data
[0], 6, 1);
777 s
->volatile_cfg
= s
->data
[1];
778 s
->four_bytes_address_mode
= extract32(s
->data
[1], 5, 1);
784 if (s
->write_enable
) {
785 s
->write_enable
= false;
789 case EXTEND_ADDR_WRITE
:
793 s
->nonvolatile_cfg
= s
->data
[0] | (s
->data
[1] << 8);
796 s
->volatile_cfg
= s
->data
[0];
799 s
->enh_volatile_cfg
= s
->data
[0];
803 if (get_man(s
) == MAN_SST
) {
804 if (s
->cur_addr
<= 1) {
806 s
->data
[0] = s
->pi
->id
[2];
807 s
->data
[1] = s
->pi
->id
[0];
809 s
->data
[0] = s
->pi
->id
[0];
810 s
->data
[1] = s
->pi
->id
[2];
814 s
->data_read_loop
= true;
815 s
->state
= STATE_READING_DATA
;
817 qemu_log_mask(LOG_GUEST_ERROR
,
818 "M25P80: Invalid read id address\n");
821 qemu_log_mask(LOG_GUEST_ERROR
,
822 "M25P80: Read id (command 0x90/0xAB) is not supported"
831 static void reset_memory(Flash
*s
)
833 s
->cmd_in_progress
= NOP
;
836 s
->four_bytes_address_mode
= false;
840 s
->state
= STATE_IDLE
;
841 s
->write_enable
= false;
842 s
->reset_enable
= false;
843 s
->quad_enable
= false;
844 s
->aai_enable
= false;
846 switch (get_man(s
)) {
849 s
->volatile_cfg
|= VCFG_DUMMY
;
850 s
->volatile_cfg
|= VCFG_WRAP_SEQUENTIAL
;
851 if ((s
->nonvolatile_cfg
& NVCFG_XIP_MODE_MASK
)
852 == NVCFG_XIP_MODE_DISABLED
) {
853 s
->volatile_cfg
|= VCFG_XIP_MODE_DISABLED
;
855 s
->volatile_cfg
|= deposit32(s
->volatile_cfg
,
858 extract32(s
->nonvolatile_cfg
,
863 s
->enh_volatile_cfg
= 0;
864 s
->enh_volatile_cfg
|= EVCFG_OUT_DRIVER_STRENGTH_DEF
;
865 s
->enh_volatile_cfg
|= EVCFG_VPP_ACCELERATOR
;
866 s
->enh_volatile_cfg
|= EVCFG_RESET_HOLD_ENABLED
;
867 if (s
->nonvolatile_cfg
& NVCFG_DUAL_IO_MASK
) {
868 s
->enh_volatile_cfg
|= EVCFG_DUAL_IO_DISABLED
;
870 if (s
->nonvolatile_cfg
& NVCFG_QUAD_IO_MASK
) {
871 s
->enh_volatile_cfg
|= EVCFG_QUAD_IO_DISABLED
;
873 if (!(s
->nonvolatile_cfg
& NVCFG_4BYTE_ADDR_MASK
)) {
874 s
->four_bytes_address_mode
= true;
876 if (!(s
->nonvolatile_cfg
& NVCFG_LOWER_SEGMENT_MASK
)) {
877 s
->ear
= s
->size
/ MAX_3BYTES_SIZE
- 1;
881 s
->volatile_cfg
= 0x7;
884 s
->spansion_cr1v
= s
->spansion_cr1nv
;
885 s
->spansion_cr2v
= s
->spansion_cr2nv
;
886 s
->spansion_cr3v
= s
->spansion_cr3nv
;
887 s
->spansion_cr4v
= s
->spansion_cr4nv
;
888 s
->quad_enable
= extract32(s
->spansion_cr1v
,
889 SPANSION_QUAD_CFG_POS
,
890 SPANSION_QUAD_CFG_LEN
892 s
->four_bytes_address_mode
= extract32(s
->spansion_cr2v
,
893 SPANSION_ADDR_LEN_POS
,
894 SPANSION_ADDR_LEN_LEN
901 trace_m25p80_reset_done(s
);
904 static uint8_t numonyx_mode(Flash
*s
)
906 if (!(s
->enh_volatile_cfg
& EVCFG_QUAD_IO_DISABLED
)) {
908 } else if (!(s
->enh_volatile_cfg
& EVCFG_DUAL_IO_DISABLED
)) {
915 static uint8_t numonyx_extract_cfg_num_dummies(Flash
*s
)
919 assert(get_man(s
) == MAN_NUMONYX
);
921 mode
= numonyx_mode(s
);
922 num_dummies
= extract32(s
->volatile_cfg
, 4, 4);
924 if (num_dummies
== 0x0 || num_dummies
== 0xf) {
925 switch (s
->cmd_in_progress
) {
931 num_dummies
= (mode
== MODE_QIO
) ? 10 : 8;
939 static void decode_fast_read_cmd(Flash
*s
)
941 s
->needed_bytes
= get_addr_length(s
);
942 switch (get_man(s
)) {
943 /* Dummy cycles - modeled with bytes writes instead of bits */
945 s
->needed_bytes
+= 1;
948 s
->needed_bytes
+= 8;
951 s
->needed_bytes
+= numonyx_extract_cfg_num_dummies(s
);
954 if (extract32(s
->volatile_cfg
, 6, 2) == 1) {
955 s
->needed_bytes
+= 6;
957 s
->needed_bytes
+= 8;
961 s
->needed_bytes
+= extract32(s
->spansion_cr2v
,
962 SPANSION_DUMMY_CLK_POS
,
963 SPANSION_DUMMY_CLK_LEN
968 * The Fast Read instruction code is followed by address bytes and
969 * dummy cycles, transmitted via the SI line.
971 * The number of dummy cycles is configurable but this is currently
972 * unmodeled, hence the default value 8 is used.
974 * QPI (Quad Peripheral Interface) mode has different default value
975 * of dummy cycles, but this is unsupported at the time being.
977 s
->needed_bytes
+= 1;
984 s
->state
= STATE_COLLECTING_DATA
;
987 static void decode_dio_read_cmd(Flash
*s
)
989 s
->needed_bytes
= get_addr_length(s
);
990 /* Dummy cycles modeled with bytes writes instead of bits */
991 switch (get_man(s
)) {
993 s
->needed_bytes
+= WINBOND_CONTINUOUS_READ_MODE_CMD_LEN
;
996 s
->needed_bytes
+= SPANSION_CONTINUOUS_READ_MODE_CMD_LEN
;
997 s
->needed_bytes
+= extract32(s
->spansion_cr2v
,
998 SPANSION_DUMMY_CLK_POS
,
999 SPANSION_DUMMY_CLK_LEN
1003 s
->needed_bytes
+= numonyx_extract_cfg_num_dummies(s
);
1006 switch (extract32(s
->volatile_cfg
, 6, 2)) {
1008 s
->needed_bytes
+= 6;
1011 s
->needed_bytes
+= 8;
1014 s
->needed_bytes
+= 4;
1020 * The Fast Read Dual I/O instruction code is followed by address bytes
1021 * and dummy cycles, transmitted via the IO1 and IO0 line.
1023 * The number of dummy cycles is configurable but this is currently
1024 * unmodeled, hence the default value 4 is used.
1026 s
->needed_bytes
+= 1;
1033 s
->state
= STATE_COLLECTING_DATA
;
1036 static void decode_qio_read_cmd(Flash
*s
)
1038 s
->needed_bytes
= get_addr_length(s
);
1039 /* Dummy cycles modeled with bytes writes instead of bits */
1040 switch (get_man(s
)) {
1042 s
->needed_bytes
+= WINBOND_CONTINUOUS_READ_MODE_CMD_LEN
;
1043 s
->needed_bytes
+= 4;
1046 s
->needed_bytes
+= SPANSION_CONTINUOUS_READ_MODE_CMD_LEN
;
1047 s
->needed_bytes
+= extract32(s
->spansion_cr2v
,
1048 SPANSION_DUMMY_CLK_POS
,
1049 SPANSION_DUMMY_CLK_LEN
1053 s
->needed_bytes
+= numonyx_extract_cfg_num_dummies(s
);
1056 switch (extract32(s
->volatile_cfg
, 6, 2)) {
1058 s
->needed_bytes
+= 4;
1061 s
->needed_bytes
+= 8;
1064 s
->needed_bytes
+= 6;
1070 * The Fast Read Quad I/O instruction code is followed by address bytes
1071 * and dummy cycles, transmitted via the IO3, IO2, IO1 and IO0 line.
1073 * The number of dummy cycles is configurable but this is currently
1074 * unmodeled, hence the default value 6 is used.
1076 * QPI (Quad Peripheral Interface) mode has different default value
1077 * of dummy cycles, but this is unsupported at the time being.
1079 s
->needed_bytes
+= 3;
1086 s
->state
= STATE_COLLECTING_DATA
;
1089 static bool is_valid_aai_cmd(uint32_t cmd
)
1091 return cmd
== AAI_WP
|| cmd
== WRDI
|| cmd
== RDSR
;
1094 static void decode_new_cmd(Flash
*s
, uint32_t value
)
1098 s
->cmd_in_progress
= value
;
1099 trace_m25p80_command_decoded(s
, value
);
1101 if (value
!= RESET_MEMORY
) {
1102 s
->reset_enable
= false;
1105 if (get_man(s
) == MAN_SST
&& s
->aai_enable
&& !is_valid_aai_cmd(value
)) {
1106 qemu_log_mask(LOG_GUEST_ERROR
,
1107 "M25P80: Invalid cmd within AAI programming sequence");
1123 s
->needed_bytes
= get_addr_length(s
);
1126 s
->state
= STATE_COLLECTING_DATA
;
1130 if (get_man(s
) != MAN_NUMONYX
|| numonyx_mode(s
) == MODE_STD
) {
1131 s
->needed_bytes
= get_addr_length(s
);
1134 s
->state
= STATE_COLLECTING_DATA
;
1136 qemu_log_mask(LOG_GUEST_ERROR
, "M25P80: Cannot execute cmd %x in "
1137 "DIO or QIO mode\n", s
->cmd_in_progress
);
1141 if (get_man(s
) != MAN_NUMONYX
|| numonyx_mode(s
) != MODE_QIO
) {
1142 s
->needed_bytes
= get_addr_length(s
);
1145 s
->state
= STATE_COLLECTING_DATA
;
1147 qemu_log_mask(LOG_GUEST_ERROR
, "M25P80: Cannot execute cmd %x in "
1148 "QIO mode\n", s
->cmd_in_progress
);
1154 if (get_man(s
) != MAN_NUMONYX
|| numonyx_mode(s
) != MODE_DIO
) {
1155 s
->needed_bytes
= get_addr_length(s
);
1158 s
->state
= STATE_COLLECTING_DATA
;
1160 qemu_log_mask(LOG_GUEST_ERROR
, "M25P80: Cannot execute cmd %x in "
1161 "DIO mode\n", s
->cmd_in_progress
);
1167 decode_fast_read_cmd(s
);
1171 if (get_man(s
) != MAN_NUMONYX
|| numonyx_mode(s
) != MODE_QIO
) {
1172 decode_fast_read_cmd(s
);
1174 qemu_log_mask(LOG_GUEST_ERROR
, "M25P80: Cannot execute cmd %x in "
1175 "QIO mode\n", s
->cmd_in_progress
);
1180 if (get_man(s
) != MAN_NUMONYX
|| numonyx_mode(s
) != MODE_DIO
) {
1181 decode_fast_read_cmd(s
);
1183 qemu_log_mask(LOG_GUEST_ERROR
, "M25P80: Cannot execute cmd %x in "
1184 "DIO mode\n", s
->cmd_in_progress
);
1190 if (get_man(s
) != MAN_NUMONYX
|| numonyx_mode(s
) != MODE_QIO
) {
1191 decode_dio_read_cmd(s
);
1193 qemu_log_mask(LOG_GUEST_ERROR
, "M25P80: Cannot execute cmd %x in "
1194 "QIO mode\n", s
->cmd_in_progress
);
1200 if (get_man(s
) != MAN_NUMONYX
|| numonyx_mode(s
) != MODE_DIO
) {
1201 decode_qio_read_cmd(s
);
1203 qemu_log_mask(LOG_GUEST_ERROR
, "M25P80: Cannot execute cmd %x in "
1204 "DIO mode\n", s
->cmd_in_progress
);
1210 * If WP# is low and status_register_write_disabled is high,
1211 * status register writes are disabled.
1212 * This is also called "hardware protected mode" (HPM). All other
1213 * combinations of the two states are called "software protected mode"
1214 * (SPM), and status register writes are permitted.
1216 if ((s
->wp_level
== 0 && s
->status_register_write_disabled
)
1217 || !s
->write_enable
) {
1218 qemu_log_mask(LOG_GUEST_ERROR
,
1219 "M25P80: Status register write is disabled!\n");
1223 switch (get_man(s
)) {
1225 s
->needed_bytes
= 2;
1226 s
->state
= STATE_COLLECTING_DATA
;
1229 s
->needed_bytes
= 2;
1230 s
->state
= STATE_COLLECTING_VAR_LEN_DATA
;
1233 s
->needed_bytes
= 1;
1234 s
->state
= STATE_COLLECTING_DATA
;
1240 s
->write_enable
= false;
1241 if (get_man(s
) == MAN_SST
) {
1242 s
->aai_enable
= false;
1246 s
->write_enable
= true;
1250 s
->data
[0] = (!!s
->write_enable
) << 1;
1251 s
->data
[0] |= (!!s
->status_register_write_disabled
) << 7;
1252 s
->data
[0] |= (!!s
->block_protect0
) << 2;
1253 s
->data
[0] |= (!!s
->block_protect1
) << 3;
1254 s
->data
[0] |= (!!s
->block_protect2
) << 4;
1255 if (s
->pi
->flags
& HAS_SR_TB
) {
1256 s
->data
[0] |= (!!s
->top_bottom_bit
) << 5;
1258 if (s
->pi
->flags
& HAS_SR_BP3_BIT6
) {
1259 s
->data
[0] |= (!!s
->block_protect3
) << 6;
1262 if (get_man(s
) == MAN_MACRONIX
|| get_man(s
) == MAN_ISSI
) {
1263 s
->data
[0] |= (!!s
->quad_enable
) << 6;
1265 if (get_man(s
) == MAN_SST
) {
1266 s
->data
[0] |= (!!s
->aai_enable
) << 6;
1271 s
->data_read_loop
= true;
1272 s
->state
= STATE_READING_DATA
;
1276 s
->data
[0] = FSR_FLASH_READY
;
1277 if (s
->four_bytes_address_mode
) {
1278 s
->data
[0] |= FSR_4BYTE_ADDR_MODE_ENABLED
;
1282 s
->data_read_loop
= true;
1283 s
->state
= STATE_READING_DATA
;
1287 if (get_man(s
) != MAN_NUMONYX
|| numonyx_mode(s
) == MODE_STD
) {
1288 trace_m25p80_populated_jedec(s
);
1289 for (i
= 0; i
< s
->pi
->id_len
; i
++) {
1290 s
->data
[i
] = s
->pi
->id
[i
];
1292 for (; i
< SPI_NOR_MAX_ID_LEN
; i
++) {
1296 s
->len
= SPI_NOR_MAX_ID_LEN
;
1298 s
->state
= STATE_READING_DATA
;
1300 qemu_log_mask(LOG_GUEST_ERROR
, "M25P80: Cannot execute JEDEC read "
1301 "in DIO or QIO mode\n");
1306 s
->data
[0] = s
->volatile_cfg
& 0xFF;
1307 s
->data
[0] |= (!!s
->four_bytes_address_mode
) << 5;
1310 s
->state
= STATE_READING_DATA
;
1315 if (s
->write_enable
) {
1316 trace_m25p80_chip_erase(s
);
1317 flash_erase(s
, 0, BULK_ERASE
);
1319 qemu_log_mask(LOG_GUEST_ERROR
, "M25P80: chip erase with write "
1326 s
->four_bytes_address_mode
= true;
1329 s
->four_bytes_address_mode
= false;
1332 case EXTEND_ADDR_READ
:
1333 s
->data
[0] = s
->ear
;
1336 s
->state
= STATE_READING_DATA
;
1339 case EXTEND_ADDR_WRITE
:
1340 if (s
->write_enable
) {
1341 s
->needed_bytes
= 1;
1344 s
->state
= STATE_COLLECTING_DATA
;
1348 s
->data
[0] = s
->nonvolatile_cfg
& 0xFF;
1349 s
->data
[1] = (s
->nonvolatile_cfg
>> 8) & 0xFF;
1352 s
->state
= STATE_READING_DATA
;
1355 if (s
->write_enable
&& get_man(s
) == MAN_NUMONYX
) {
1356 s
->needed_bytes
= 2;
1359 s
->state
= STATE_COLLECTING_DATA
;
1363 s
->data
[0] = s
->volatile_cfg
& 0xFF;
1366 s
->state
= STATE_READING_DATA
;
1369 if (s
->write_enable
) {
1370 s
->needed_bytes
= 1;
1373 s
->state
= STATE_COLLECTING_DATA
;
1377 s
->data
[0] = s
->enh_volatile_cfg
& 0xFF;
1380 s
->state
= STATE_READING_DATA
;
1383 if (s
->write_enable
) {
1384 s
->needed_bytes
= 1;
1387 s
->state
= STATE_COLLECTING_DATA
;
1391 s
->reset_enable
= true;
1394 if (s
->reset_enable
) {
1399 switch (get_man(s
)) {
1401 s
->data
[0] = (!!s
->quad_enable
) << 1;
1404 s
->state
= STATE_READING_DATA
;
1407 s
->quad_enable
= true;
1414 s
->quad_enable
= false;
1417 if (get_man(s
) == MAN_SST
) {
1418 if (s
->write_enable
) {
1419 if (s
->aai_enable
) {
1420 s
->state
= STATE_PAGE_PROGRAM
;
1422 s
->aai_enable
= true;
1423 s
->needed_bytes
= get_addr_length(s
);
1424 s
->state
= STATE_COLLECTING_DATA
;
1427 qemu_log_mask(LOG_GUEST_ERROR
,
1428 "M25P80: AAI_WP with write protect\n");
1431 qemu_log_mask(LOG_GUEST_ERROR
, "M25P80: Unknown cmd %x\n", value
);
1437 s
->state
= STATE_READING_DATA
;
1438 s
->data_read_loop
= true;
1440 qemu_log_mask(LOG_GUEST_ERROR
, "M25P80: Unknown cmd %x\n", value
);
1445 static int m25p80_cs(SSIPeripheral
*ss
, bool select
)
1447 Flash
*s
= M25P80(ss
);
1450 if (s
->state
== STATE_COLLECTING_VAR_LEN_DATA
) {
1451 complete_collecting_data(s
);
1455 s
->state
= STATE_IDLE
;
1456 flash_sync_dirty(s
, -1);
1457 s
->data_read_loop
= false;
1460 trace_m25p80_select(s
, select
? "de" : "");
1465 static uint32_t m25p80_transfer8(SSIPeripheral
*ss
, uint32_t tx
)
1467 Flash
*s
= M25P80(ss
);
1470 trace_m25p80_transfer(s
, s
->state
, s
->len
, s
->needed_bytes
, s
->pos
,
1471 s
->cur_addr
, (uint8_t)tx
);
1475 case STATE_PAGE_PROGRAM
:
1476 trace_m25p80_page_program(s
, s
->cur_addr
, (uint8_t)tx
);
1477 flash_write8(s
, s
->cur_addr
, (uint8_t)tx
);
1478 s
->cur_addr
= (s
->cur_addr
+ 1) & (s
->size
- 1);
1480 if (get_man(s
) == MAN_SST
&& s
->aai_enable
&& s
->cur_addr
== 0) {
1482 * There is no wrap mode during AAI programming once the highest
1483 * unprotected memory address is reached. The Write-Enable-Latch
1484 * bit is automatically reset, and AAI programming mode aborts.
1486 s
->write_enable
= false;
1487 s
->aai_enable
= false;
1493 r
= s
->storage
[s
->cur_addr
];
1494 trace_m25p80_read_byte(s
, s
->cur_addr
, (uint8_t)r
);
1495 s
->cur_addr
= (s
->cur_addr
+ 1) & (s
->size
- 1);
1498 case STATE_COLLECTING_DATA
:
1499 case STATE_COLLECTING_VAR_LEN_DATA
:
1501 if (s
->len
>= M25P80_INTERNAL_DATA_BUFFER_SZ
) {
1502 qemu_log_mask(LOG_GUEST_ERROR
,
1503 "M25P80: Write overrun internal data buffer. "
1504 "SPI controller (QEMU emulator or guest driver) "
1505 "is misbehaving\n");
1506 s
->len
= s
->pos
= 0;
1507 s
->state
= STATE_IDLE
;
1511 s
->data
[s
->len
] = (uint8_t)tx
;
1514 if (s
->len
== s
->needed_bytes
) {
1515 complete_collecting_data(s
);
1519 case STATE_READING_DATA
:
1521 if (s
->pos
>= M25P80_INTERNAL_DATA_BUFFER_SZ
) {
1522 qemu_log_mask(LOG_GUEST_ERROR
,
1523 "M25P80: Read overrun internal data buffer. "
1524 "SPI controller (QEMU emulator or guest driver) "
1525 "is misbehaving\n");
1526 s
->len
= s
->pos
= 0;
1527 s
->state
= STATE_IDLE
;
1531 r
= s
->data
[s
->pos
];
1532 trace_m25p80_read_data(s
, s
->pos
, (uint8_t)r
);
1534 if (s
->pos
== s
->len
) {
1536 if (!s
->data_read_loop
) {
1537 s
->state
= STATE_IDLE
;
1544 decode_new_cmd(s
, (uint8_t)tx
);
1551 static void m25p80_write_protect_pin_irq_handler(void *opaque
, int n
, int level
)
1553 Flash
*s
= M25P80(opaque
);
1554 /* WP# is just a single pin. */
1556 s
->wp_level
= !!level
;
1559 static void m25p80_realize(SSIPeripheral
*ss
, Error
**errp
)
1561 Flash
*s
= M25P80(ss
);
1562 M25P80Class
*mc
= M25P80_GET_CLASS(s
);
1567 s
->size
= s
->pi
->sector_size
* s
->pi
->n_sectors
;
1571 uint64_t perm
= BLK_PERM_CONSISTENT_READ
|
1572 (blk_supports_write_perm(s
->blk
) ? BLK_PERM_WRITE
: 0);
1573 ret
= blk_set_perm(s
->blk
, perm
, BLK_PERM_ALL
, errp
);
1578 trace_m25p80_binding(s
);
1579 s
->storage
= blk_blockalign(s
->blk
, s
->size
);
1581 if (blk_pread(s
->blk
, 0, s
->size
, s
->storage
, 0) < 0) {
1582 error_setg(errp
, "failed to read the initial flash content");
1586 trace_m25p80_binding_no_bdrv(s
);
1587 s
->storage
= blk_blockalign(NULL
, s
->size
);
1588 memset(s
->storage
, 0xFF, s
->size
);
1591 qdev_init_gpio_in_named(DEVICE(s
),
1592 m25p80_write_protect_pin_irq_handler
, "WP#", 1);
1595 static void m25p80_reset(DeviceState
*d
)
1597 Flash
*s
= M25P80(d
);
1600 s
->status_register_write_disabled
= false;
1601 s
->block_protect0
= false;
1602 s
->block_protect1
= false;
1603 s
->block_protect2
= false;
1604 s
->block_protect3
= false;
1605 s
->top_bottom_bit
= false;
1610 static int m25p80_pre_save(void *opaque
)
1612 flash_sync_dirty((Flash
*)opaque
, -1);
1617 static Property m25p80_properties
[] = {
1618 /* This is default value for Micron flash */
1619 DEFINE_PROP_BOOL("write-enable", Flash
, write_enable
, false),
1620 DEFINE_PROP_UINT32("nonvolatile-cfg", Flash
, nonvolatile_cfg
, 0x8FFF),
1621 DEFINE_PROP_UINT8("spansion-cr1nv", Flash
, spansion_cr1nv
, 0x0),
1622 DEFINE_PROP_UINT8("spansion-cr2nv", Flash
, spansion_cr2nv
, 0x8),
1623 DEFINE_PROP_UINT8("spansion-cr3nv", Flash
, spansion_cr3nv
, 0x2),
1624 DEFINE_PROP_UINT8("spansion-cr4nv", Flash
, spansion_cr4nv
, 0x10),
1625 DEFINE_PROP_DRIVE("drive", Flash
, blk
),
1626 DEFINE_PROP_END_OF_LIST(),
1629 static int m25p80_pre_load(void *opaque
)
1631 Flash
*s
= (Flash
*)opaque
;
1633 s
->data_read_loop
= false;
1637 static bool m25p80_data_read_loop_needed(void *opaque
)
1639 Flash
*s
= (Flash
*)opaque
;
1641 return s
->data_read_loop
;
1644 static const VMStateDescription vmstate_m25p80_data_read_loop
= {
1645 .name
= "m25p80/data_read_loop",
1647 .minimum_version_id
= 1,
1648 .needed
= m25p80_data_read_loop_needed
,
1649 .fields
= (VMStateField
[]) {
1650 VMSTATE_BOOL(data_read_loop
, Flash
),
1651 VMSTATE_END_OF_LIST()
1655 static bool m25p80_aai_enable_needed(void *opaque
)
1657 Flash
*s
= (Flash
*)opaque
;
1659 return s
->aai_enable
;
1662 static const VMStateDescription vmstate_m25p80_aai_enable
= {
1663 .name
= "m25p80/aai_enable",
1665 .minimum_version_id
= 1,
1666 .needed
= m25p80_aai_enable_needed
,
1667 .fields
= (VMStateField
[]) {
1668 VMSTATE_BOOL(aai_enable
, Flash
),
1669 VMSTATE_END_OF_LIST()
1673 static bool m25p80_wp_level_srwd_needed(void *opaque
)
1675 Flash
*s
= (Flash
*)opaque
;
1677 return !s
->wp_level
|| s
->status_register_write_disabled
;
1680 static const VMStateDescription vmstate_m25p80_write_protect
= {
1681 .name
= "m25p80/write_protect",
1683 .minimum_version_id
= 1,
1684 .needed
= m25p80_wp_level_srwd_needed
,
1685 .fields
= (VMStateField
[]) {
1686 VMSTATE_BOOL(wp_level
, Flash
),
1687 VMSTATE_BOOL(status_register_write_disabled
, Flash
),
1688 VMSTATE_END_OF_LIST()
1692 static bool m25p80_block_protect_needed(void *opaque
)
1694 Flash
*s
= (Flash
*)opaque
;
1696 return s
->block_protect0
||
1697 s
->block_protect1
||
1698 s
->block_protect2
||
1699 s
->block_protect3
||
1703 static const VMStateDescription vmstate_m25p80_block_protect
= {
1704 .name
= "m25p80/block_protect",
1706 .minimum_version_id
= 1,
1707 .needed
= m25p80_block_protect_needed
,
1708 .fields
= (VMStateField
[]) {
1709 VMSTATE_BOOL(block_protect0
, Flash
),
1710 VMSTATE_BOOL(block_protect1
, Flash
),
1711 VMSTATE_BOOL(block_protect2
, Flash
),
1712 VMSTATE_BOOL(block_protect3
, Flash
),
1713 VMSTATE_BOOL(top_bottom_bit
, Flash
),
1714 VMSTATE_END_OF_LIST()
1718 static const VMStateDescription vmstate_m25p80
= {
1721 .minimum_version_id
= 0,
1722 .pre_save
= m25p80_pre_save
,
1723 .pre_load
= m25p80_pre_load
,
1724 .fields
= (VMStateField
[]) {
1725 VMSTATE_UINT8(state
, Flash
),
1726 VMSTATE_UINT8_ARRAY(data
, Flash
, M25P80_INTERNAL_DATA_BUFFER_SZ
),
1727 VMSTATE_UINT32(len
, Flash
),
1728 VMSTATE_UINT32(pos
, Flash
),
1729 VMSTATE_UINT8(needed_bytes
, Flash
),
1730 VMSTATE_UINT8(cmd_in_progress
, Flash
),
1731 VMSTATE_UINT32(cur_addr
, Flash
),
1732 VMSTATE_BOOL(write_enable
, Flash
),
1733 VMSTATE_BOOL(reset_enable
, Flash
),
1734 VMSTATE_UINT8(ear
, Flash
),
1735 VMSTATE_BOOL(four_bytes_address_mode
, Flash
),
1736 VMSTATE_UINT32(nonvolatile_cfg
, Flash
),
1737 VMSTATE_UINT32(volatile_cfg
, Flash
),
1738 VMSTATE_UINT32(enh_volatile_cfg
, Flash
),
1739 VMSTATE_BOOL(quad_enable
, Flash
),
1740 VMSTATE_UINT8(spansion_cr1nv
, Flash
),
1741 VMSTATE_UINT8(spansion_cr2nv
, Flash
),
1742 VMSTATE_UINT8(spansion_cr3nv
, Flash
),
1743 VMSTATE_UINT8(spansion_cr4nv
, Flash
),
1744 VMSTATE_END_OF_LIST()
1746 .subsections
= (const VMStateDescription
* []) {
1747 &vmstate_m25p80_data_read_loop
,
1748 &vmstate_m25p80_aai_enable
,
1749 &vmstate_m25p80_write_protect
,
1750 &vmstate_m25p80_block_protect
,
1755 static void m25p80_class_init(ObjectClass
*klass
, void *data
)
1757 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1758 SSIPeripheralClass
*k
= SSI_PERIPHERAL_CLASS(klass
);
1759 M25P80Class
*mc
= M25P80_CLASS(klass
);
1761 k
->realize
= m25p80_realize
;
1762 k
->transfer
= m25p80_transfer8
;
1763 k
->set_cs
= m25p80_cs
;
1764 k
->cs_polarity
= SSI_CS_LOW
;
1765 dc
->vmsd
= &vmstate_m25p80
;
1766 device_class_set_props(dc
, m25p80_properties
);
1767 dc
->reset
= m25p80_reset
;
1771 static const TypeInfo m25p80_info
= {
1772 .name
= TYPE_M25P80
,
1773 .parent
= TYPE_SSI_PERIPHERAL
,
1774 .instance_size
= sizeof(Flash
),
1775 .class_size
= sizeof(M25P80Class
),
1779 static void m25p80_register_types(void)
1783 type_register_static(&m25p80_info
);
1784 for (i
= 0; i
< ARRAY_SIZE(known_devices
); ++i
) {
1786 .name
= known_devices
[i
].part_name
,
1787 .parent
= TYPE_M25P80
,
1788 .class_init
= m25p80_class_init
,
1789 .class_data
= (void *)&known_devices
[i
],
1795 type_init(m25p80_register_types
)