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/ssi/ssi.h"
29 #include "migration/vmstate.h"
30 #include "qemu/bitops.h"
32 #include "qemu/module.h"
33 #include "qemu/error-report.h"
34 #include "qapi/error.h"
37 /* Fields for FlashPartInfo->flags */
39 /* erase capabilities */
42 /* set to allow the page program command to write 0s back to 1. Useful for
43 * modelling EEPROM with SPI flash command set
47 /* 16 MiB max in 3 byte address mode */
48 #define MAX_3BYTES_SIZE 0x1000000
50 #define SPI_NOR_MAX_ID_LEN 6
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_ENABLED (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_ENABLED (1 << 6)
147 #define NVCFG_QUAD_IO_MASK (1 << 3)
148 #define EVCFG_QUAD_IO_ENABLED (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("mx25l2005a", 0xc22012, 0, 64 << 10, 4, ER_4K
) },
213 { INFO("mx25l4005a", 0xc22013, 0, 64 << 10, 8, ER_4K
) },
214 { INFO("mx25l8005", 0xc22014, 0, 64 << 10, 16, 0) },
215 { INFO("mx25l1606e", 0xc22015, 0, 64 << 10, 32, ER_4K
) },
216 { INFO("mx25l3205d", 0xc22016, 0, 64 << 10, 64, 0) },
217 { INFO("mx25l6405d", 0xc22017, 0, 64 << 10, 128, 0) },
218 { INFO("mx25l12805d", 0xc22018, 0, 64 << 10, 256, 0) },
219 { INFO("mx25l12855e", 0xc22618, 0, 64 << 10, 256, 0) },
220 { INFO("mx25l25635e", 0xc22019, 0, 64 << 10, 512, 0) },
221 { INFO("mx25l25655e", 0xc22619, 0, 64 << 10, 512, 0) },
222 { INFO("mx66u51235f", 0xc2253a, 0, 64 << 10, 1024, ER_4K
| ER_32K
) },
223 { INFO("mx66u1g45g", 0xc2253b, 0, 64 << 10, 2048, ER_4K
| ER_32K
) },
224 { INFO("mx66l1g45g", 0xc2201b, 0, 64 << 10, 2048, ER_4K
| ER_32K
) },
227 { INFO("n25q032a11", 0x20bb16, 0, 64 << 10, 64, ER_4K
) },
228 { INFO("n25q032a13", 0x20ba16, 0, 64 << 10, 64, ER_4K
) },
229 { INFO("n25q064a11", 0x20bb17, 0, 64 << 10, 128, ER_4K
) },
230 { INFO("n25q064a13", 0x20ba17, 0, 64 << 10, 128, ER_4K
) },
231 { INFO("n25q128a11", 0x20bb18, 0, 64 << 10, 256, ER_4K
) },
232 { INFO("n25q128a13", 0x20ba18, 0, 64 << 10, 256, ER_4K
) },
233 { INFO("n25q256a11", 0x20bb19, 0, 64 << 10, 512, ER_4K
) },
234 { INFO("n25q256a13", 0x20ba19, 0, 64 << 10, 512, ER_4K
) },
235 { INFO("n25q512a11", 0x20bb20, 0, 64 << 10, 1024, ER_4K
) },
236 { INFO("n25q512a13", 0x20ba20, 0, 64 << 10, 1024, ER_4K
) },
237 { INFO("n25q128", 0x20ba18, 0, 64 << 10, 256, 0) },
238 { INFO("n25q256a", 0x20ba19, 0, 64 << 10, 512, ER_4K
) },
239 { INFO("n25q512a", 0x20ba20, 0, 64 << 10, 1024, ER_4K
) },
240 { INFO_STACKED("n25q00", 0x20ba21, 0x1000, 64 << 10, 2048, ER_4K
, 4) },
241 { INFO_STACKED("n25q00a", 0x20bb21, 0x1000, 64 << 10, 2048, ER_4K
, 4) },
242 { INFO_STACKED("mt25ql01g", 0x20ba21, 0x1040, 64 << 10, 2048, ER_4K
, 2) },
243 { INFO_STACKED("mt25qu01g", 0x20bb21, 0x1040, 64 << 10, 2048, ER_4K
, 2) },
245 /* Spansion -- single (large) sector size only, at least
246 * for the chips listed here (without boot sectors).
248 { INFO("s25sl032p", 0x010215, 0x4d00, 64 << 10, 64, ER_4K
) },
249 { INFO("s25sl064p", 0x010216, 0x4d00, 64 << 10, 128, ER_4K
) },
250 { INFO("s25fl256s0", 0x010219, 0x4d00, 256 << 10, 128, 0) },
251 { INFO("s25fl256s1", 0x010219, 0x4d01, 64 << 10, 512, 0) },
252 { INFO6("s25fl512s", 0x010220, 0x4d0080, 256 << 10, 256, 0) },
253 { INFO6("s70fl01gs", 0x010221, 0x4d0080, 256 << 10, 512, 0) },
254 { INFO("s25sl12800", 0x012018, 0x0300, 256 << 10, 64, 0) },
255 { INFO("s25sl12801", 0x012018, 0x0301, 64 << 10, 256, 0) },
256 { INFO("s25fl129p0", 0x012018, 0x4d00, 256 << 10, 64, 0) },
257 { INFO("s25fl129p1", 0x012018, 0x4d01, 64 << 10, 256, 0) },
258 { INFO("s25sl004a", 0x010212, 0, 64 << 10, 8, 0) },
259 { INFO("s25sl008a", 0x010213, 0, 64 << 10, 16, 0) },
260 { INFO("s25sl016a", 0x010214, 0, 64 << 10, 32, 0) },
261 { INFO("s25sl032a", 0x010215, 0, 64 << 10, 64, 0) },
262 { INFO("s25sl064a", 0x010216, 0, 64 << 10, 128, 0) },
263 { INFO("s25fl016k", 0xef4015, 0, 64 << 10, 32, ER_4K
| ER_32K
) },
264 { INFO("s25fl064k", 0xef4017, 0, 64 << 10, 128, ER_4K
| ER_32K
) },
266 /* Spansion -- boot sectors support */
267 { INFO6("s25fs512s", 0x010220, 0x4d0081, 256 << 10, 256, 0) },
268 { INFO6("s70fs01gs", 0x010221, 0x4d0081, 256 << 10, 512, 0) },
270 /* SST -- large erase sizes are "overlays", "sectors" are 4<< 10 */
271 { INFO("sst25vf040b", 0xbf258d, 0, 64 << 10, 8, ER_4K
) },
272 { INFO("sst25vf080b", 0xbf258e, 0, 64 << 10, 16, ER_4K
) },
273 { INFO("sst25vf016b", 0xbf2541, 0, 64 << 10, 32, ER_4K
) },
274 { INFO("sst25vf032b", 0xbf254a, 0, 64 << 10, 64, ER_4K
) },
275 { INFO("sst25wf512", 0xbf2501, 0, 64 << 10, 1, ER_4K
) },
276 { INFO("sst25wf010", 0xbf2502, 0, 64 << 10, 2, ER_4K
) },
277 { INFO("sst25wf020", 0xbf2503, 0, 64 << 10, 4, ER_4K
) },
278 { INFO("sst25wf040", 0xbf2504, 0, 64 << 10, 8, ER_4K
) },
279 { INFO("sst25wf080", 0xbf2505, 0, 64 << 10, 16, ER_4K
) },
281 /* ST Microelectronics -- newer production may have feature updates */
282 { INFO("m25p05", 0x202010, 0, 32 << 10, 2, 0) },
283 { INFO("m25p10", 0x202011, 0, 32 << 10, 4, 0) },
284 { INFO("m25p20", 0x202012, 0, 64 << 10, 4, 0) },
285 { INFO("m25p40", 0x202013, 0, 64 << 10, 8, 0) },
286 { INFO("m25p80", 0x202014, 0, 64 << 10, 16, 0) },
287 { INFO("m25p16", 0x202015, 0, 64 << 10, 32, 0) },
288 { INFO("m25p32", 0x202016, 0, 64 << 10, 64, 0) },
289 { INFO("m25p64", 0x202017, 0, 64 << 10, 128, 0) },
290 { INFO("m25p128", 0x202018, 0, 256 << 10, 64, 0) },
291 { INFO("n25q032", 0x20ba16, 0, 64 << 10, 64, 0) },
293 { INFO("m45pe10", 0x204011, 0, 64 << 10, 2, 0) },
294 { INFO("m45pe80", 0x204014, 0, 64 << 10, 16, 0) },
295 { INFO("m45pe16", 0x204015, 0, 64 << 10, 32, 0) },
297 { INFO("m25pe20", 0x208012, 0, 64 << 10, 4, 0) },
298 { INFO("m25pe80", 0x208014, 0, 64 << 10, 16, 0) },
299 { INFO("m25pe16", 0x208015, 0, 64 << 10, 32, ER_4K
) },
301 { INFO("m25px32", 0x207116, 0, 64 << 10, 64, ER_4K
) },
302 { INFO("m25px32-s0", 0x207316, 0, 64 << 10, 64, ER_4K
) },
303 { INFO("m25px32-s1", 0x206316, 0, 64 << 10, 64, ER_4K
) },
304 { INFO("m25px64", 0x207117, 0, 64 << 10, 128, 0) },
306 /* Winbond -- w25x "blocks" are 64k, "sectors" are 4KiB */
307 { INFO("w25x10", 0xef3011, 0, 64 << 10, 2, ER_4K
) },
308 { INFO("w25x20", 0xef3012, 0, 64 << 10, 4, ER_4K
) },
309 { INFO("w25x40", 0xef3013, 0, 64 << 10, 8, ER_4K
) },
310 { INFO("w25x80", 0xef3014, 0, 64 << 10, 16, ER_4K
) },
311 { INFO("w25x16", 0xef3015, 0, 64 << 10, 32, ER_4K
) },
312 { INFO("w25x32", 0xef3016, 0, 64 << 10, 64, ER_4K
) },
313 { INFO("w25q32", 0xef4016, 0, 64 << 10, 64, ER_4K
) },
314 { INFO("w25q32dw", 0xef6016, 0, 64 << 10, 64, ER_4K
) },
315 { INFO("w25x64", 0xef3017, 0, 64 << 10, 128, ER_4K
) },
316 { INFO("w25q64", 0xef4017, 0, 64 << 10, 128, ER_4K
) },
317 { INFO("w25q80", 0xef5014, 0, 64 << 10, 16, ER_4K
) },
318 { INFO("w25q80bl", 0xef4014, 0, 64 << 10, 16, ER_4K
) },
319 { INFO("w25q256", 0xef4019, 0, 64 << 10, 512, ER_4K
) },
320 { INFO("w25q512jv", 0xef4020, 0, 64 << 10, 1024, ER_4K
) },
332 BULK_ERASE_60
= 0x60,
364 ERASE4_SECTOR
= 0xdc,
366 EN_4BYTE_ADDR
= 0xB7,
367 EX_4BYTE_ADDR
= 0xE9,
369 EXTEND_ADDR_READ
= 0xC8,
370 EXTEND_ADDR_WRITE
= 0xC5,
376 * Micron: 0x35 - enable QPI
377 * Spansion: 0x35 - read control register
398 STATE_COLLECTING_DATA
,
399 STATE_COLLECTING_VAR_LEN_DATA
,
412 #define M25P80_INTERNAL_DATA_BUFFER_SZ 16
414 typedef struct Flash
{
424 uint8_t data
[M25P80_INTERNAL_DATA_BUFFER_SZ
];
428 uint8_t needed_bytes
;
429 uint8_t cmd_in_progress
;
431 uint32_t nonvolatile_cfg
;
432 /* Configuration register for Macronix */
433 uint32_t volatile_cfg
;
434 uint32_t enh_volatile_cfg
;
435 /* Spansion cfg registers. */
436 uint8_t spansion_cr1nv
;
437 uint8_t spansion_cr2nv
;
438 uint8_t spansion_cr3nv
;
439 uint8_t spansion_cr4nv
;
440 uint8_t spansion_cr1v
;
441 uint8_t spansion_cr2v
;
442 uint8_t spansion_cr3v
;
443 uint8_t spansion_cr4v
;
445 bool four_bytes_address_mode
;
452 const FlashPartInfo
*pi
;
456 typedef struct M25P80Class
{
457 SSISlaveClass parent_class
;
461 #define TYPE_M25P80 "m25p80-generic"
462 #define M25P80(obj) \
463 OBJECT_CHECK(Flash, (obj), TYPE_M25P80)
464 #define M25P80_CLASS(klass) \
465 OBJECT_CLASS_CHECK(M25P80Class, (klass), TYPE_M25P80)
466 #define M25P80_GET_CLASS(obj) \
467 OBJECT_GET_CLASS(M25P80Class, (obj), TYPE_M25P80)
469 static inline Manufacturer
get_man(Flash
*s
)
471 switch (s
->pi
->id
[0]) {
487 static void blk_sync_complete(void *opaque
, int ret
)
489 QEMUIOVector
*iov
= opaque
;
491 qemu_iovec_destroy(iov
);
494 /* do nothing. Masters do not directly interact with the backing store,
495 * only the working copy so no mutexing required.
499 static void flash_sync_page(Flash
*s
, int page
)
503 if (!s
->blk
|| blk_is_read_only(s
->blk
)) {
507 iov
= g_new(QEMUIOVector
, 1);
508 qemu_iovec_init(iov
, 1);
509 qemu_iovec_add(iov
, s
->storage
+ page
* s
->pi
->page_size
,
511 blk_aio_pwritev(s
->blk
, page
* s
->pi
->page_size
, iov
, 0,
512 blk_sync_complete
, iov
);
515 static inline void flash_sync_area(Flash
*s
, int64_t off
, int64_t len
)
519 if (!s
->blk
|| blk_is_read_only(s
->blk
)) {
523 assert(!(len
% BDRV_SECTOR_SIZE
));
524 iov
= g_new(QEMUIOVector
, 1);
525 qemu_iovec_init(iov
, 1);
526 qemu_iovec_add(iov
, s
->storage
+ off
, len
);
527 blk_aio_pwritev(s
->blk
, off
, iov
, 0, blk_sync_complete
, iov
);
530 static void flash_erase(Flash
*s
, int offset
, FlashCMD cmd
)
533 uint8_t capa_to_assert
= 0;
539 capa_to_assert
= ER_4K
;
544 capa_to_assert
= ER_32K
;
548 len
= s
->pi
->sector_size
;
554 if (s
->pi
->die_cnt
) {
555 len
= s
->size
/ s
->pi
->die_cnt
;
556 offset
= offset
& (~(len
- 1));
558 qemu_log_mask(LOG_GUEST_ERROR
, "M25P80: die erase is not supported"
567 trace_m25p80_flash_erase(s
, offset
, len
);
569 if ((s
->pi
->flags
& capa_to_assert
) != capa_to_assert
) {
570 qemu_log_mask(LOG_GUEST_ERROR
, "M25P80: %d erase size not supported by"
574 if (!s
->write_enable
) {
575 qemu_log_mask(LOG_GUEST_ERROR
, "M25P80: erase with write protect!\n");
578 memset(s
->storage
+ offset
, 0xff, len
);
579 flash_sync_area(s
, offset
, len
);
582 static inline void flash_sync_dirty(Flash
*s
, int64_t newpage
)
584 if (s
->dirty_page
>= 0 && s
->dirty_page
!= newpage
) {
585 flash_sync_page(s
, s
->dirty_page
);
586 s
->dirty_page
= newpage
;
591 void flash_write8(Flash
*s
, uint32_t addr
, uint8_t data
)
593 uint32_t page
= addr
/ s
->pi
->page_size
;
594 uint8_t prev
= s
->storage
[s
->cur_addr
];
596 if (!s
->write_enable
) {
597 qemu_log_mask(LOG_GUEST_ERROR
, "M25P80: write with write protect!\n");
600 if ((prev
^ data
) & data
) {
601 trace_m25p80_programming_zero_to_one(s
, addr
, prev
, data
);
604 if (s
->pi
->flags
& EEPROM
) {
605 s
->storage
[s
->cur_addr
] = data
;
607 s
->storage
[s
->cur_addr
] &= data
;
610 flash_sync_dirty(s
, page
);
611 s
->dirty_page
= page
;
614 static inline int get_addr_length(Flash
*s
)
616 /* check if eeprom is in use */
617 if (s
->pi
->flags
== EEPROM
) {
621 switch (s
->cmd_in_progress
) {
636 return s
->four_bytes_address_mode
? 4 : 3;
640 static void complete_collecting_data(Flash
*s
)
644 n
= get_addr_length(s
);
645 s
->cur_addr
= (n
== 3 ? s
->ear
: 0);
646 for (i
= 0; i
< n
; ++i
) {
648 s
->cur_addr
|= s
->data
[i
];
651 s
->cur_addr
&= s
->size
- 1;
653 s
->state
= STATE_IDLE
;
655 trace_m25p80_complete_collecting(s
, s
->cmd_in_progress
, n
, s
->ear
,
658 switch (s
->cmd_in_progress
) {
665 s
->state
= STATE_PAGE_PROGRAM
;
679 s
->state
= STATE_READ
;
688 flash_erase(s
, s
->cur_addr
, s
->cmd_in_progress
);
691 switch (get_man(s
)) {
693 s
->quad_enable
= !!(s
->data
[1] & 0x02);
696 s
->quad_enable
= extract32(s
->data
[0], 6, 1);
698 s
->volatile_cfg
= s
->data
[1];
699 s
->four_bytes_address_mode
= extract32(s
->data
[1], 5, 1);
705 if (s
->write_enable
) {
706 s
->write_enable
= false;
710 case EXTEND_ADDR_WRITE
:
714 s
->nonvolatile_cfg
= s
->data
[0] | (s
->data
[1] << 8);
717 s
->volatile_cfg
= s
->data
[0];
720 s
->enh_volatile_cfg
= s
->data
[0];
724 if (get_man(s
) == MAN_SST
) {
725 if (s
->cur_addr
<= 1) {
727 s
->data
[0] = s
->pi
->id
[2];
728 s
->data
[1] = s
->pi
->id
[0];
730 s
->data
[0] = s
->pi
->id
[0];
731 s
->data
[1] = s
->pi
->id
[2];
735 s
->data_read_loop
= true;
736 s
->state
= STATE_READING_DATA
;
738 qemu_log_mask(LOG_GUEST_ERROR
,
739 "M25P80: Invalid read id address\n");
742 qemu_log_mask(LOG_GUEST_ERROR
,
743 "M25P80: Read id (command 0x90/0xAB) is not supported"
752 static void reset_memory(Flash
*s
)
754 s
->cmd_in_progress
= NOP
;
757 s
->four_bytes_address_mode
= false;
761 s
->state
= STATE_IDLE
;
762 s
->write_enable
= false;
763 s
->reset_enable
= false;
764 s
->quad_enable
= false;
766 switch (get_man(s
)) {
769 s
->volatile_cfg
|= VCFG_DUMMY
;
770 s
->volatile_cfg
|= VCFG_WRAP_SEQUENTIAL
;
771 if ((s
->nonvolatile_cfg
& NVCFG_XIP_MODE_MASK
)
772 != NVCFG_XIP_MODE_DISABLED
) {
773 s
->volatile_cfg
|= VCFG_XIP_MODE_ENABLED
;
775 s
->volatile_cfg
|= deposit32(s
->volatile_cfg
,
778 extract32(s
->nonvolatile_cfg
,
783 s
->enh_volatile_cfg
= 0;
784 s
->enh_volatile_cfg
|= EVCFG_OUT_DRIVER_STRENGTH_DEF
;
785 s
->enh_volatile_cfg
|= EVCFG_VPP_ACCELERATOR
;
786 s
->enh_volatile_cfg
|= EVCFG_RESET_HOLD_ENABLED
;
787 if (s
->nonvolatile_cfg
& NVCFG_DUAL_IO_MASK
) {
788 s
->enh_volatile_cfg
|= EVCFG_DUAL_IO_ENABLED
;
790 if (s
->nonvolatile_cfg
& NVCFG_QUAD_IO_MASK
) {
791 s
->enh_volatile_cfg
|= EVCFG_QUAD_IO_ENABLED
;
793 if (!(s
->nonvolatile_cfg
& NVCFG_4BYTE_ADDR_MASK
)) {
794 s
->four_bytes_address_mode
= true;
796 if (!(s
->nonvolatile_cfg
& NVCFG_LOWER_SEGMENT_MASK
)) {
797 s
->ear
= s
->size
/ MAX_3BYTES_SIZE
- 1;
801 s
->volatile_cfg
= 0x7;
804 s
->spansion_cr1v
= s
->spansion_cr1nv
;
805 s
->spansion_cr2v
= s
->spansion_cr2nv
;
806 s
->spansion_cr3v
= s
->spansion_cr3nv
;
807 s
->spansion_cr4v
= s
->spansion_cr4nv
;
808 s
->quad_enable
= extract32(s
->spansion_cr1v
,
809 SPANSION_QUAD_CFG_POS
,
810 SPANSION_QUAD_CFG_LEN
812 s
->four_bytes_address_mode
= extract32(s
->spansion_cr2v
,
813 SPANSION_ADDR_LEN_POS
,
814 SPANSION_ADDR_LEN_LEN
821 trace_m25p80_reset_done(s
);
824 static void decode_fast_read_cmd(Flash
*s
)
826 s
->needed_bytes
= get_addr_length(s
);
827 switch (get_man(s
)) {
828 /* Dummy cycles - modeled with bytes writes instead of bits */
830 s
->needed_bytes
+= 8;
833 s
->needed_bytes
+= extract32(s
->volatile_cfg
, 4, 4);
836 if (extract32(s
->volatile_cfg
, 6, 2) == 1) {
837 s
->needed_bytes
+= 6;
839 s
->needed_bytes
+= 8;
843 s
->needed_bytes
+= extract32(s
->spansion_cr2v
,
844 SPANSION_DUMMY_CLK_POS
,
845 SPANSION_DUMMY_CLK_LEN
853 s
->state
= STATE_COLLECTING_DATA
;
856 static void decode_dio_read_cmd(Flash
*s
)
858 s
->needed_bytes
= get_addr_length(s
);
859 /* Dummy cycles modeled with bytes writes instead of bits */
860 switch (get_man(s
)) {
862 s
->needed_bytes
+= WINBOND_CONTINUOUS_READ_MODE_CMD_LEN
;
865 s
->needed_bytes
+= SPANSION_CONTINUOUS_READ_MODE_CMD_LEN
;
866 s
->needed_bytes
+= extract32(s
->spansion_cr2v
,
867 SPANSION_DUMMY_CLK_POS
,
868 SPANSION_DUMMY_CLK_LEN
872 s
->needed_bytes
+= extract32(s
->volatile_cfg
, 4, 4);
875 switch (extract32(s
->volatile_cfg
, 6, 2)) {
877 s
->needed_bytes
+= 6;
880 s
->needed_bytes
+= 8;
883 s
->needed_bytes
+= 4;
892 s
->state
= STATE_COLLECTING_DATA
;
895 static void decode_qio_read_cmd(Flash
*s
)
897 s
->needed_bytes
= get_addr_length(s
);
898 /* Dummy cycles modeled with bytes writes instead of bits */
899 switch (get_man(s
)) {
901 s
->needed_bytes
+= WINBOND_CONTINUOUS_READ_MODE_CMD_LEN
;
902 s
->needed_bytes
+= 4;
905 s
->needed_bytes
+= SPANSION_CONTINUOUS_READ_MODE_CMD_LEN
;
906 s
->needed_bytes
+= extract32(s
->spansion_cr2v
,
907 SPANSION_DUMMY_CLK_POS
,
908 SPANSION_DUMMY_CLK_LEN
912 s
->needed_bytes
+= extract32(s
->volatile_cfg
, 4, 4);
915 switch (extract32(s
->volatile_cfg
, 6, 2)) {
917 s
->needed_bytes
+= 4;
920 s
->needed_bytes
+= 8;
923 s
->needed_bytes
+= 6;
932 s
->state
= STATE_COLLECTING_DATA
;
935 static void decode_new_cmd(Flash
*s
, uint32_t value
)
939 s
->cmd_in_progress
= value
;
940 trace_m25p80_command_decoded(s
, value
);
942 if (value
!= RESET_MEMORY
) {
943 s
->reset_enable
= false;
965 s
->needed_bytes
= get_addr_length(s
);
968 s
->state
= STATE_COLLECTING_DATA
;
977 decode_fast_read_cmd(s
);
982 decode_dio_read_cmd(s
);
987 decode_qio_read_cmd(s
);
991 if (s
->write_enable
) {
992 switch (get_man(s
)) {
995 s
->state
= STATE_COLLECTING_DATA
;
999 s
->state
= STATE_COLLECTING_VAR_LEN_DATA
;
1002 s
->needed_bytes
= 1;
1003 s
->state
= STATE_COLLECTING_DATA
;
1010 s
->write_enable
= false;
1013 s
->write_enable
= true;
1017 s
->data
[0] = (!!s
->write_enable
) << 1;
1018 if (get_man(s
) == MAN_MACRONIX
) {
1019 s
->data
[0] |= (!!s
->quad_enable
) << 6;
1023 s
->data_read_loop
= true;
1024 s
->state
= STATE_READING_DATA
;
1028 s
->data
[0] = FSR_FLASH_READY
;
1029 if (s
->four_bytes_address_mode
) {
1030 s
->data
[0] |= FSR_4BYTE_ADDR_MODE_ENABLED
;
1034 s
->data_read_loop
= true;
1035 s
->state
= STATE_READING_DATA
;
1039 trace_m25p80_populated_jedec(s
);
1040 for (i
= 0; i
< s
->pi
->id_len
; i
++) {
1041 s
->data
[i
] = s
->pi
->id
[i
];
1043 for (; i
< SPI_NOR_MAX_ID_LEN
; i
++) {
1047 s
->len
= SPI_NOR_MAX_ID_LEN
;
1049 s
->state
= STATE_READING_DATA
;
1053 s
->data
[0] = s
->volatile_cfg
& 0xFF;
1054 s
->data
[0] |= (!!s
->four_bytes_address_mode
) << 5;
1057 s
->state
= STATE_READING_DATA
;
1062 if (s
->write_enable
) {
1063 trace_m25p80_chip_erase(s
);
1064 flash_erase(s
, 0, BULK_ERASE
);
1066 qemu_log_mask(LOG_GUEST_ERROR
, "M25P80: chip erase with write "
1073 s
->four_bytes_address_mode
= true;
1076 s
->four_bytes_address_mode
= false;
1079 case EXTEND_ADDR_READ
:
1080 s
->data
[0] = s
->ear
;
1083 s
->state
= STATE_READING_DATA
;
1086 case EXTEND_ADDR_WRITE
:
1087 if (s
->write_enable
) {
1088 s
->needed_bytes
= 1;
1091 s
->state
= STATE_COLLECTING_DATA
;
1095 s
->data
[0] = s
->nonvolatile_cfg
& 0xFF;
1096 s
->data
[1] = (s
->nonvolatile_cfg
>> 8) & 0xFF;
1099 s
->state
= STATE_READING_DATA
;
1102 if (s
->write_enable
&& get_man(s
) == MAN_NUMONYX
) {
1103 s
->needed_bytes
= 2;
1106 s
->state
= STATE_COLLECTING_DATA
;
1110 s
->data
[0] = s
->volatile_cfg
& 0xFF;
1113 s
->state
= STATE_READING_DATA
;
1116 if (s
->write_enable
) {
1117 s
->needed_bytes
= 1;
1120 s
->state
= STATE_COLLECTING_DATA
;
1124 s
->data
[0] = s
->enh_volatile_cfg
& 0xFF;
1127 s
->state
= STATE_READING_DATA
;
1130 if (s
->write_enable
) {
1131 s
->needed_bytes
= 1;
1134 s
->state
= STATE_COLLECTING_DATA
;
1138 s
->reset_enable
= true;
1141 if (s
->reset_enable
) {
1146 switch (get_man(s
)) {
1148 s
->data
[0] = (!!s
->quad_enable
) << 1;
1151 s
->state
= STATE_READING_DATA
;
1154 s
->quad_enable
= true;
1161 s
->quad_enable
= false;
1166 s
->state
= STATE_READING_DATA
;
1167 s
->data_read_loop
= true;
1169 qemu_log_mask(LOG_GUEST_ERROR
, "M25P80: Unknown cmd %x\n", value
);
1174 static int m25p80_cs(SSISlave
*ss
, bool select
)
1176 Flash
*s
= M25P80(ss
);
1179 if (s
->state
== STATE_COLLECTING_VAR_LEN_DATA
) {
1180 complete_collecting_data(s
);
1184 s
->state
= STATE_IDLE
;
1185 flash_sync_dirty(s
, -1);
1186 s
->data_read_loop
= false;
1189 trace_m25p80_select(s
, select
? "de" : "");
1194 static uint32_t m25p80_transfer8(SSISlave
*ss
, uint32_t tx
)
1196 Flash
*s
= M25P80(ss
);
1199 trace_m25p80_transfer(s
, s
->state
, s
->len
, s
->needed_bytes
, s
->pos
,
1200 s
->cur_addr
, (uint8_t)tx
);
1204 case STATE_PAGE_PROGRAM
:
1205 trace_m25p80_page_program(s
, s
->cur_addr
, (uint8_t)tx
);
1206 flash_write8(s
, s
->cur_addr
, (uint8_t)tx
);
1207 s
->cur_addr
= (s
->cur_addr
+ 1) & (s
->size
- 1);
1211 r
= s
->storage
[s
->cur_addr
];
1212 trace_m25p80_read_byte(s
, s
->cur_addr
, (uint8_t)r
);
1213 s
->cur_addr
= (s
->cur_addr
+ 1) & (s
->size
- 1);
1216 case STATE_COLLECTING_DATA
:
1217 case STATE_COLLECTING_VAR_LEN_DATA
:
1219 if (s
->len
>= M25P80_INTERNAL_DATA_BUFFER_SZ
) {
1220 qemu_log_mask(LOG_GUEST_ERROR
,
1221 "M25P80: Write overrun internal data buffer. "
1222 "SPI controller (QEMU emulator or guest driver) "
1223 "is misbehaving\n");
1224 s
->len
= s
->pos
= 0;
1225 s
->state
= STATE_IDLE
;
1229 s
->data
[s
->len
] = (uint8_t)tx
;
1232 if (s
->len
== s
->needed_bytes
) {
1233 complete_collecting_data(s
);
1237 case STATE_READING_DATA
:
1239 if (s
->pos
>= M25P80_INTERNAL_DATA_BUFFER_SZ
) {
1240 qemu_log_mask(LOG_GUEST_ERROR
,
1241 "M25P80: Read overrun internal data buffer. "
1242 "SPI controller (QEMU emulator or guest driver) "
1243 "is misbehaving\n");
1244 s
->len
= s
->pos
= 0;
1245 s
->state
= STATE_IDLE
;
1249 r
= s
->data
[s
->pos
];
1250 trace_m25p80_read_data(s
, s
->pos
, (uint8_t)r
);
1252 if (s
->pos
== s
->len
) {
1254 if (!s
->data_read_loop
) {
1255 s
->state
= STATE_IDLE
;
1262 decode_new_cmd(s
, (uint8_t)tx
);
1269 static void m25p80_realize(SSISlave
*ss
, Error
**errp
)
1271 Flash
*s
= M25P80(ss
);
1272 M25P80Class
*mc
= M25P80_GET_CLASS(s
);
1277 s
->size
= s
->pi
->sector_size
* s
->pi
->n_sectors
;
1281 uint64_t perm
= BLK_PERM_CONSISTENT_READ
|
1282 (blk_is_read_only(s
->blk
) ? 0 : BLK_PERM_WRITE
);
1283 ret
= blk_set_perm(s
->blk
, perm
, BLK_PERM_ALL
, errp
);
1288 trace_m25p80_binding(s
);
1289 s
->storage
= blk_blockalign(s
->blk
, s
->size
);
1291 if (blk_pread(s
->blk
, 0, s
->storage
, s
->size
) != s
->size
) {
1292 error_setg(errp
, "failed to read the initial flash content");
1296 trace_m25p80_binding_no_bdrv(s
);
1297 s
->storage
= blk_blockalign(NULL
, s
->size
);
1298 memset(s
->storage
, 0xFF, s
->size
);
1302 static void m25p80_reset(DeviceState
*d
)
1304 Flash
*s
= M25P80(d
);
1309 static int m25p80_pre_save(void *opaque
)
1311 flash_sync_dirty((Flash
*)opaque
, -1);
1316 static Property m25p80_properties
[] = {
1317 /* This is default value for Micron flash */
1318 DEFINE_PROP_UINT32("nonvolatile-cfg", Flash
, nonvolatile_cfg
, 0x8FFF),
1319 DEFINE_PROP_UINT8("spansion-cr1nv", Flash
, spansion_cr1nv
, 0x0),
1320 DEFINE_PROP_UINT8("spansion-cr2nv", Flash
, spansion_cr2nv
, 0x8),
1321 DEFINE_PROP_UINT8("spansion-cr3nv", Flash
, spansion_cr3nv
, 0x2),
1322 DEFINE_PROP_UINT8("spansion-cr4nv", Flash
, spansion_cr4nv
, 0x10),
1323 DEFINE_PROP_DRIVE("drive", Flash
, blk
),
1324 DEFINE_PROP_END_OF_LIST(),
1327 static int m25p80_pre_load(void *opaque
)
1329 Flash
*s
= (Flash
*)opaque
;
1331 s
->data_read_loop
= false;
1335 static bool m25p80_data_read_loop_needed(void *opaque
)
1337 Flash
*s
= (Flash
*)opaque
;
1339 return s
->data_read_loop
;
1342 static const VMStateDescription vmstate_m25p80_data_read_loop
= {
1343 .name
= "m25p80/data_read_loop",
1345 .minimum_version_id
= 1,
1346 .needed
= m25p80_data_read_loop_needed
,
1347 .fields
= (VMStateField
[]) {
1348 VMSTATE_BOOL(data_read_loop
, Flash
),
1349 VMSTATE_END_OF_LIST()
1353 static const VMStateDescription vmstate_m25p80
= {
1356 .minimum_version_id
= 0,
1357 .pre_save
= m25p80_pre_save
,
1358 .pre_load
= m25p80_pre_load
,
1359 .fields
= (VMStateField
[]) {
1360 VMSTATE_UINT8(state
, Flash
),
1361 VMSTATE_UINT8_ARRAY(data
, Flash
, M25P80_INTERNAL_DATA_BUFFER_SZ
),
1362 VMSTATE_UINT32(len
, Flash
),
1363 VMSTATE_UINT32(pos
, Flash
),
1364 VMSTATE_UINT8(needed_bytes
, Flash
),
1365 VMSTATE_UINT8(cmd_in_progress
, Flash
),
1366 VMSTATE_UINT32(cur_addr
, Flash
),
1367 VMSTATE_BOOL(write_enable
, Flash
),
1368 VMSTATE_BOOL(reset_enable
, Flash
),
1369 VMSTATE_UINT8(ear
, Flash
),
1370 VMSTATE_BOOL(four_bytes_address_mode
, Flash
),
1371 VMSTATE_UINT32(nonvolatile_cfg
, Flash
),
1372 VMSTATE_UINT32(volatile_cfg
, Flash
),
1373 VMSTATE_UINT32(enh_volatile_cfg
, Flash
),
1374 VMSTATE_BOOL(quad_enable
, Flash
),
1375 VMSTATE_UINT8(spansion_cr1nv
, Flash
),
1376 VMSTATE_UINT8(spansion_cr2nv
, Flash
),
1377 VMSTATE_UINT8(spansion_cr3nv
, Flash
),
1378 VMSTATE_UINT8(spansion_cr4nv
, Flash
),
1379 VMSTATE_END_OF_LIST()
1381 .subsections
= (const VMStateDescription
* []) {
1382 &vmstate_m25p80_data_read_loop
,
1387 static void m25p80_class_init(ObjectClass
*klass
, void *data
)
1389 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1390 SSISlaveClass
*k
= SSI_SLAVE_CLASS(klass
);
1391 M25P80Class
*mc
= M25P80_CLASS(klass
);
1393 k
->realize
= m25p80_realize
;
1394 k
->transfer
= m25p80_transfer8
;
1395 k
->set_cs
= m25p80_cs
;
1396 k
->cs_polarity
= SSI_CS_LOW
;
1397 dc
->vmsd
= &vmstate_m25p80
;
1398 device_class_set_props(dc
, m25p80_properties
);
1399 dc
->reset
= m25p80_reset
;
1403 static const TypeInfo m25p80_info
= {
1404 .name
= TYPE_M25P80
,
1405 .parent
= TYPE_SSI_SLAVE
,
1406 .instance_size
= sizeof(Flash
),
1407 .class_size
= sizeof(M25P80Class
),
1411 static void m25p80_register_types(void)
1415 type_register_static(&m25p80_info
);
1416 for (i
= 0; i
< ARRAY_SIZE(known_devices
); ++i
) {
1418 .name
= known_devices
[i
].part_name
,
1419 .parent
= TYPE_M25P80
,
1420 .class_init
= m25p80_class_init
,
1421 .class_data
= (void *)&known_devices
[i
],
1427 type_init(m25p80_register_types
)