]> git.proxmox.com Git - mirror_qemu.git/blob - hw/block/pflash_cfi01.c
8434a45cabb0b506a577400e30f75b21e5e79ed6
[mirror_qemu.git] / hw / block / pflash_cfi01.c
1 /*
2 * CFI parallel flash with Intel command set emulation
3 *
4 * Copyright (c) 2006 Thorsten Zitterell
5 * Copyright (c) 2005 Jocelyn Mayer
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19 */
20
21 /*
22 * For now, this code can emulate flashes of 1, 2 or 4 bytes width.
23 * Supported commands/modes are:
24 * - flash read
25 * - flash write
26 * - flash ID read
27 * - sector erase
28 * - CFI queries
29 *
30 * It does not support timings
31 * It does not support flash interleaving
32 * It does not implement software data protection as found in many real chips
33 * It does not implement erase suspend/resume commands
34 * It does not implement multiple sectors erase
35 *
36 * It does not implement much more ...
37 */
38
39 #include "qemu/osdep.h"
40 #include "hw/block/block.h"
41 #include "hw/block/flash.h"
42 #include "hw/qdev-properties.h"
43 #include "hw/qdev-properties-system.h"
44 #include "sysemu/block-backend.h"
45 #include "qapi/error.h"
46 #include "qemu/error-report.h"
47 #include "qemu/bitops.h"
48 #include "qemu/host-utils.h"
49 #include "qemu/log.h"
50 #include "qemu/module.h"
51 #include "qemu/option.h"
52 #include "hw/sysbus.h"
53 #include "migration/vmstate.h"
54 #include "sysemu/blockdev.h"
55 #include "sysemu/runstate.h"
56 #include "trace.h"
57
58 #define PFLASH_BE 0
59 #define PFLASH_SECURE 1
60
61 struct PFlashCFI01 {
62 /*< private >*/
63 SysBusDevice parent_obj;
64 /*< public >*/
65
66 BlockBackend *blk;
67 uint32_t nb_blocs;
68 uint64_t sector_len;
69 uint8_t bank_width;
70 uint8_t device_width; /* If 0, device width not specified. */
71 uint8_t max_device_width; /* max device width in bytes */
72 uint32_t features;
73 uint8_t wcycle; /* if 0, the flash is read normally */
74 bool ro;
75 uint8_t cmd;
76 uint8_t status;
77 uint16_t ident0;
78 uint16_t ident1;
79 uint16_t ident2;
80 uint16_t ident3;
81 uint8_t cfi_table[0x52];
82 uint64_t counter;
83 unsigned int writeblock_size;
84 MemoryRegion mem;
85 char *name;
86 void *storage;
87 VMChangeStateEntry *vmstate;
88 bool old_multiple_chip_handling;
89 };
90
91 static int pflash_post_load(void *opaque, int version_id);
92
93 static const VMStateDescription vmstate_pflash = {
94 .name = "pflash_cfi01",
95 .version_id = 1,
96 .minimum_version_id = 1,
97 .post_load = pflash_post_load,
98 .fields = (const VMStateField[]) {
99 VMSTATE_UINT8(wcycle, PFlashCFI01),
100 VMSTATE_UINT8(cmd, PFlashCFI01),
101 VMSTATE_UINT8(status, PFlashCFI01),
102 VMSTATE_UINT64(counter, PFlashCFI01),
103 VMSTATE_END_OF_LIST()
104 }
105 };
106
107 /*
108 * Perform a CFI query based on the bank width of the flash.
109 * If this code is called we know we have a device_width set for
110 * this flash.
111 */
112 static uint32_t pflash_cfi_query(PFlashCFI01 *pfl, hwaddr offset)
113 {
114 int i;
115 uint32_t resp = 0;
116 hwaddr boff;
117
118 /*
119 * Adjust incoming offset to match expected device-width
120 * addressing. CFI query addresses are always specified in terms of
121 * the maximum supported width of the device. This means that x8
122 * devices and x8/x16 devices in x8 mode behave differently. For
123 * devices that are not used at their max width, we will be
124 * provided with addresses that use higher address bits than
125 * expected (based on the max width), so we will shift them lower
126 * so that they will match the addresses used when
127 * device_width==max_device_width.
128 */
129 boff = offset >> (ctz32(pfl->bank_width) +
130 ctz32(pfl->max_device_width) - ctz32(pfl->device_width));
131
132 if (boff >= sizeof(pfl->cfi_table)) {
133 return 0;
134 }
135 /*
136 * Now we will construct the CFI response generated by a single
137 * device, then replicate that for all devices that make up the
138 * bus. For wide parts used in x8 mode, CFI query responses
139 * are different than native byte-wide parts.
140 */
141 resp = pfl->cfi_table[boff];
142 if (pfl->device_width != pfl->max_device_width) {
143 /* The only case currently supported is x8 mode for a
144 * wider part.
145 */
146 if (pfl->device_width != 1 || pfl->bank_width > 4) {
147 trace_pflash_unsupported_device_configuration(pfl->name,
148 pfl->device_width, pfl->max_device_width);
149 return 0;
150 }
151 /* CFI query data is repeated, rather than zero padded for
152 * wide devices used in x8 mode.
153 */
154 for (i = 1; i < pfl->max_device_width; i++) {
155 resp = deposit32(resp, 8 * i, 8, pfl->cfi_table[boff]);
156 }
157 }
158 /* Replicate responses for each device in bank. */
159 if (pfl->device_width < pfl->bank_width) {
160 for (i = pfl->device_width;
161 i < pfl->bank_width; i += pfl->device_width) {
162 resp = deposit32(resp, 8 * i, 8 * pfl->device_width, resp);
163 }
164 }
165
166 return resp;
167 }
168
169
170
171 /* Perform a device id query based on the bank width of the flash. */
172 static uint32_t pflash_devid_query(PFlashCFI01 *pfl, hwaddr offset)
173 {
174 int i;
175 uint32_t resp;
176 hwaddr boff;
177
178 /*
179 * Adjust incoming offset to match expected device-width
180 * addressing. Device ID read addresses are always specified in
181 * terms of the maximum supported width of the device. This means
182 * that x8 devices and x8/x16 devices in x8 mode behave
183 * differently. For devices that are not used at their max width,
184 * we will be provided with addresses that use higher address bits
185 * than expected (based on the max width), so we will shift them
186 * lower so that they will match the addresses used when
187 * device_width==max_device_width.
188 */
189 boff = offset >> (ctz32(pfl->bank_width) +
190 ctz32(pfl->max_device_width) - ctz32(pfl->device_width));
191
192 /*
193 * Mask off upper bits which may be used in to query block
194 * or sector lock status at other addresses.
195 * Offsets 2/3 are block lock status, is not emulated.
196 */
197 switch (boff & 0xFF) {
198 case 0:
199 resp = pfl->ident0;
200 trace_pflash_manufacturer_id(pfl->name, resp);
201 break;
202 case 1:
203 resp = pfl->ident1;
204 trace_pflash_device_id(pfl->name, resp);
205 break;
206 default:
207 trace_pflash_device_info(pfl->name, offset);
208 return 0;
209 }
210 /* Replicate responses for each device in bank. */
211 if (pfl->device_width < pfl->bank_width) {
212 for (i = pfl->device_width;
213 i < pfl->bank_width; i += pfl->device_width) {
214 resp = deposit32(resp, 8 * i, 8 * pfl->device_width, resp);
215 }
216 }
217
218 return resp;
219 }
220
221 static uint32_t pflash_data_read(PFlashCFI01 *pfl, hwaddr offset,
222 int width, int be)
223 {
224 uint8_t *p;
225 uint32_t ret;
226
227 p = pfl->storage;
228 if (be) {
229 ret = ldn_be_p(p + offset, width);
230 } else {
231 ret = ldn_le_p(p + offset, width);
232 }
233 trace_pflash_data_read(pfl->name, offset, width, ret);
234 return ret;
235 }
236
237 static uint32_t pflash_read(PFlashCFI01 *pfl, hwaddr offset,
238 int width, int be)
239 {
240 hwaddr boff;
241 uint32_t ret;
242
243 ret = -1;
244 switch (pfl->cmd) {
245 default:
246 /* This should never happen : reset state & treat it as a read */
247 trace_pflash_read_unknown_state(pfl->name, pfl->cmd);
248 pfl->wcycle = 0;
249 /*
250 * The command 0x00 is not assigned by the CFI open standard,
251 * but QEMU historically uses it for the READ_ARRAY command (0xff).
252 */
253 pfl->cmd = 0x00;
254 /* fall through to read code */
255 case 0x00: /* This model reset value for READ_ARRAY (not CFI compliant) */
256 /* Flash area read */
257 ret = pflash_data_read(pfl, offset, width, be);
258 break;
259 case 0x10: /* Single byte program */
260 case 0x20: /* Block erase */
261 case 0x28: /* Block erase */
262 case 0x40: /* single byte program */
263 case 0x50: /* Clear status register */
264 case 0x60: /* Block /un)lock */
265 case 0x70: /* Status Register */
266 case 0xe8: /* Write block */
267 /*
268 * Status register read. Return status from each device in
269 * bank.
270 */
271 ret = pfl->status;
272 if (pfl->device_width && width > pfl->device_width) {
273 int shift = pfl->device_width * 8;
274 while (shift + pfl->device_width * 8 <= width * 8) {
275 ret |= pfl->status << shift;
276 shift += pfl->device_width * 8;
277 }
278 } else if (!pfl->device_width && width > 2) {
279 /*
280 * Handle 32 bit flash cases where device width is not
281 * set. (Existing behavior before device width added.)
282 */
283 ret |= pfl->status << 16;
284 }
285 trace_pflash_read_status(pfl->name, ret);
286 break;
287 case 0x90:
288 if (!pfl->device_width) {
289 /* Preserve old behavior if device width not specified */
290 boff = offset & 0xFF;
291 if (pfl->bank_width == 2) {
292 boff = boff >> 1;
293 } else if (pfl->bank_width == 4) {
294 boff = boff >> 2;
295 }
296
297 switch (boff) {
298 case 0:
299 ret = pfl->ident0 << 8 | pfl->ident1;
300 trace_pflash_manufacturer_id(pfl->name, ret);
301 break;
302 case 1:
303 ret = pfl->ident2 << 8 | pfl->ident3;
304 trace_pflash_device_id(pfl->name, ret);
305 break;
306 default:
307 trace_pflash_device_info(pfl->name, boff);
308 ret = 0;
309 break;
310 }
311 } else {
312 /*
313 * If we have a read larger than the bank_width, combine multiple
314 * manufacturer/device ID queries into a single response.
315 */
316 int i;
317 for (i = 0; i < width; i += pfl->bank_width) {
318 ret = deposit32(ret, i * 8, pfl->bank_width * 8,
319 pflash_devid_query(pfl,
320 offset + i * pfl->bank_width));
321 }
322 }
323 break;
324 case 0x98: /* Query mode */
325 if (!pfl->device_width) {
326 /* Preserve old behavior if device width not specified */
327 boff = offset & 0xFF;
328 if (pfl->bank_width == 2) {
329 boff = boff >> 1;
330 } else if (pfl->bank_width == 4) {
331 boff = boff >> 2;
332 }
333
334 if (boff < sizeof(pfl->cfi_table)) {
335 ret = pfl->cfi_table[boff];
336 } else {
337 ret = 0;
338 }
339 } else {
340 /*
341 * If we have a read larger than the bank_width, combine multiple
342 * CFI queries into a single response.
343 */
344 int i;
345 for (i = 0; i < width; i += pfl->bank_width) {
346 ret = deposit32(ret, i * 8, pfl->bank_width * 8,
347 pflash_cfi_query(pfl,
348 offset + i * pfl->bank_width));
349 }
350 }
351
352 break;
353 }
354 trace_pflash_io_read(pfl->name, offset, width, ret, pfl->cmd, pfl->wcycle);
355
356 return ret;
357 }
358
359 /* update flash content on disk */
360 static void pflash_update(PFlashCFI01 *pfl, int offset,
361 int size)
362 {
363 int offset_end;
364 int ret;
365 if (pfl->blk) {
366 offset_end = offset + size;
367 /* widen to sector boundaries */
368 offset = QEMU_ALIGN_DOWN(offset, BDRV_SECTOR_SIZE);
369 offset_end = QEMU_ALIGN_UP(offset_end, BDRV_SECTOR_SIZE);
370 ret = blk_pwrite(pfl->blk, offset, offset_end - offset,
371 pfl->storage + offset, 0);
372 if (ret < 0) {
373 /* TODO set error bit in status */
374 error_report("Could not update PFLASH: %s", strerror(-ret));
375 }
376 }
377 }
378
379 static inline void pflash_data_write(PFlashCFI01 *pfl, hwaddr offset,
380 uint32_t value, int width, int be)
381 {
382 uint8_t *p;
383
384 trace_pflash_data_write(pfl->name, offset, width, value, pfl->counter);
385 p = pfl->storage + offset;
386
387 if (be) {
388 stn_be_p(p, width, value);
389 } else {
390 stn_le_p(p, width, value);
391 }
392 }
393
394 static void pflash_write(PFlashCFI01 *pfl, hwaddr offset,
395 uint32_t value, int width, int be)
396 {
397 uint8_t *p;
398 uint8_t cmd;
399
400 cmd = value;
401
402 trace_pflash_io_write(pfl->name, offset, width, value, pfl->wcycle);
403 if (!pfl->wcycle) {
404 /* Set the device in I/O access mode */
405 memory_region_rom_device_set_romd(&pfl->mem, false);
406 }
407
408 switch (pfl->wcycle) {
409 case 0:
410 /* read mode */
411 switch (cmd) {
412 case 0x00: /* This model reset value for READ_ARRAY (not CFI) */
413 goto mode_read_array;
414 case 0x10: /* Single Byte Program */
415 case 0x40: /* Single Byte Program */
416 trace_pflash_write(pfl->name, "single byte program (0)");
417 break;
418 case 0x20: /* Block erase */
419 p = pfl->storage;
420 offset &= ~(pfl->sector_len - 1);
421
422 trace_pflash_write_block_erase(pfl->name, offset, pfl->sector_len);
423
424 if (!pfl->ro) {
425 memset(p + offset, 0xff, pfl->sector_len);
426 pflash_update(pfl, offset, pfl->sector_len);
427 } else {
428 pfl->status |= 0x20; /* Block erase error */
429 }
430 pfl->status |= 0x80; /* Ready! */
431 break;
432 case 0x50: /* Clear status bits */
433 trace_pflash_write(pfl->name, "clear status bits");
434 pfl->status = 0x0;
435 goto mode_read_array;
436 case 0x60: /* Block (un)lock */
437 trace_pflash_write(pfl->name, "block unlock");
438 break;
439 case 0x70: /* Status Register */
440 trace_pflash_write(pfl->name, "read status register");
441 pfl->cmd = cmd;
442 return;
443 case 0x90: /* Read Device ID */
444 trace_pflash_write(pfl->name, "read device information");
445 pfl->cmd = cmd;
446 return;
447 case 0x98: /* CFI query */
448 trace_pflash_write(pfl->name, "CFI query");
449 break;
450 case 0xe8: /* Write to buffer */
451 trace_pflash_write(pfl->name, "write to buffer");
452 /* FIXME should save @offset, @width for case 1+ */
453 qemu_log_mask(LOG_UNIMP,
454 "%s: Write to buffer emulation is flawed\n",
455 __func__);
456 pfl->status |= 0x80; /* Ready! */
457 break;
458 case 0xf0: /* Probe for AMD flash */
459 trace_pflash_write(pfl->name, "probe for AMD flash");
460 goto mode_read_array;
461 case 0xff: /* Read Array */
462 trace_pflash_write(pfl->name, "read array mode");
463 goto mode_read_array;
464 default:
465 goto error_flash;
466 }
467 pfl->wcycle++;
468 pfl->cmd = cmd;
469 break;
470 case 1:
471 switch (pfl->cmd) {
472 case 0x10: /* Single Byte Program */
473 case 0x40: /* Single Byte Program */
474 trace_pflash_write(pfl->name, "single byte program (1)");
475 if (!pfl->ro) {
476 pflash_data_write(pfl, offset, value, width, be);
477 pflash_update(pfl, offset, width);
478 } else {
479 pfl->status |= 0x10; /* Programming error */
480 }
481 pfl->status |= 0x80; /* Ready! */
482 pfl->wcycle = 0;
483 break;
484 case 0x20: /* Block erase */
485 case 0x28:
486 if (cmd == 0xd0) { /* confirm */
487 pfl->wcycle = 0;
488 pfl->status |= 0x80;
489 } else if (cmd == 0xff) { /* Read Array */
490 goto mode_read_array;
491 } else
492 goto error_flash;
493
494 break;
495 case 0xe8:
496 /*
497 * Mask writeblock size based on device width, or bank width if
498 * device width not specified.
499 */
500 /* FIXME check @offset, @width */
501 if (pfl->device_width) {
502 value = extract32(value, 0, pfl->device_width * 8);
503 } else {
504 value = extract32(value, 0, pfl->bank_width * 8);
505 }
506 trace_pflash_write_block(pfl->name, value);
507 pfl->counter = value;
508 pfl->wcycle++;
509 break;
510 case 0x60:
511 if (cmd == 0xd0) {
512 pfl->wcycle = 0;
513 pfl->status |= 0x80;
514 } else if (cmd == 0x01) {
515 pfl->wcycle = 0;
516 pfl->status |= 0x80;
517 } else if (cmd == 0xff) { /* Read Array */
518 goto mode_read_array;
519 } else {
520 trace_pflash_write(pfl->name, "unknown (un)locking command");
521 goto mode_read_array;
522 }
523 break;
524 case 0x98:
525 if (cmd == 0xff) { /* Read Array */
526 goto mode_read_array;
527 } else {
528 trace_pflash_write(pfl->name, "leaving query mode");
529 }
530 break;
531 default:
532 goto error_flash;
533 }
534 break;
535 case 2:
536 switch (pfl->cmd) {
537 case 0xe8: /* Block write */
538 /* FIXME check @offset, @width */
539 if (!pfl->ro) {
540 /*
541 * FIXME writing straight to memory is *wrong*. We
542 * should write to a buffer, and flush it to memory
543 * only on confirm command (see below).
544 */
545 pflash_data_write(pfl, offset, value, width, be);
546 } else {
547 pfl->status |= 0x10; /* Programming error */
548 }
549
550 pfl->status |= 0x80;
551
552 if (!pfl->counter) {
553 hwaddr mask = pfl->writeblock_size - 1;
554 mask = ~mask;
555
556 trace_pflash_write(pfl->name, "block write finished");
557 pfl->wcycle++;
558 if (!pfl->ro) {
559 /* Flush the entire write buffer onto backing storage. */
560 /* FIXME premature! */
561 pflash_update(pfl, offset & mask, pfl->writeblock_size);
562 } else {
563 pfl->status |= 0x10; /* Programming error */
564 }
565 }
566
567 pfl->counter--;
568 break;
569 default:
570 goto error_flash;
571 }
572 break;
573 case 3: /* Confirm mode */
574 switch (pfl->cmd) {
575 case 0xe8: /* Block write */
576 if (cmd == 0xd0) {
577 /* FIXME this is where we should write out the buffer */
578 pfl->wcycle = 0;
579 pfl->status |= 0x80;
580 } else {
581 qemu_log_mask(LOG_UNIMP,
582 "%s: Aborting write to buffer not implemented,"
583 " the data is already written to storage!\n"
584 "Flash device reset into READ mode.\n",
585 __func__);
586 goto mode_read_array;
587 }
588 break;
589 default:
590 goto error_flash;
591 }
592 break;
593 default:
594 /* Should never happen */
595 trace_pflash_write(pfl->name, "invalid write state");
596 goto mode_read_array;
597 }
598 return;
599
600 error_flash:
601 qemu_log_mask(LOG_UNIMP, "%s: Unimplemented flash cmd sequence "
602 "(offset " HWADDR_FMT_plx ", wcycle 0x%x cmd 0x%x value 0x%x)"
603 "\n", __func__, offset, pfl->wcycle, pfl->cmd, value);
604
605 mode_read_array:
606 trace_pflash_mode_read_array(pfl->name);
607 memory_region_rom_device_set_romd(&pfl->mem, true);
608 pfl->wcycle = 0;
609 pfl->cmd = 0x00; /* This model reset value for READ_ARRAY (not CFI) */
610 }
611
612
613 static MemTxResult pflash_mem_read_with_attrs(void *opaque, hwaddr addr, uint64_t *value,
614 unsigned len, MemTxAttrs attrs)
615 {
616 PFlashCFI01 *pfl = opaque;
617 bool be = !!(pfl->features & (1 << PFLASH_BE));
618
619 if ((pfl->features & (1 << PFLASH_SECURE)) && !attrs.secure) {
620 *value = pflash_data_read(opaque, addr, len, be);
621 } else {
622 *value = pflash_read(opaque, addr, len, be);
623 }
624 return MEMTX_OK;
625 }
626
627 static MemTxResult pflash_mem_write_with_attrs(void *opaque, hwaddr addr, uint64_t value,
628 unsigned len, MemTxAttrs attrs)
629 {
630 PFlashCFI01 *pfl = opaque;
631 bool be = !!(pfl->features & (1 << PFLASH_BE));
632
633 if ((pfl->features & (1 << PFLASH_SECURE)) && !attrs.secure) {
634 return MEMTX_ERROR;
635 } else {
636 pflash_write(opaque, addr, value, len, be);
637 return MEMTX_OK;
638 }
639 }
640
641 static const MemoryRegionOps pflash_cfi01_ops = {
642 .read_with_attrs = pflash_mem_read_with_attrs,
643 .write_with_attrs = pflash_mem_write_with_attrs,
644 .endianness = DEVICE_NATIVE_ENDIAN,
645 };
646
647 static void pflash_cfi01_fill_cfi_table(PFlashCFI01 *pfl)
648 {
649 uint64_t blocks_per_device, sector_len_per_device, device_len;
650 int num_devices;
651
652 /*
653 * These are only used to expose the parameters of each device
654 * in the cfi_table[].
655 */
656 num_devices = pfl->device_width ? (pfl->bank_width / pfl->device_width) : 1;
657 if (pfl->old_multiple_chip_handling) {
658 blocks_per_device = pfl->nb_blocs / num_devices;
659 sector_len_per_device = pfl->sector_len;
660 } else {
661 blocks_per_device = pfl->nb_blocs;
662 sector_len_per_device = pfl->sector_len / num_devices;
663 }
664 device_len = sector_len_per_device * blocks_per_device;
665
666 /* Hardcoded CFI table */
667 /* Standard "QRY" string */
668 pfl->cfi_table[0x10] = 'Q';
669 pfl->cfi_table[0x11] = 'R';
670 pfl->cfi_table[0x12] = 'Y';
671 /* Command set (Intel) */
672 pfl->cfi_table[0x13] = 0x01;
673 pfl->cfi_table[0x14] = 0x00;
674 /* Primary extended table address (none) */
675 pfl->cfi_table[0x15] = 0x31;
676 pfl->cfi_table[0x16] = 0x00;
677 /* Alternate command set (none) */
678 pfl->cfi_table[0x17] = 0x00;
679 pfl->cfi_table[0x18] = 0x00;
680 /* Alternate extended table (none) */
681 pfl->cfi_table[0x19] = 0x00;
682 pfl->cfi_table[0x1A] = 0x00;
683 /* Vcc min */
684 pfl->cfi_table[0x1B] = 0x45;
685 /* Vcc max */
686 pfl->cfi_table[0x1C] = 0x55;
687 /* Vpp min (no Vpp pin) */
688 pfl->cfi_table[0x1D] = 0x00;
689 /* Vpp max (no Vpp pin) */
690 pfl->cfi_table[0x1E] = 0x00;
691 /* Reserved */
692 pfl->cfi_table[0x1F] = 0x07;
693 /* Timeout for min size buffer write */
694 pfl->cfi_table[0x20] = 0x07;
695 /* Typical timeout for block erase */
696 pfl->cfi_table[0x21] = 0x0a;
697 /* Typical timeout for full chip erase (4096 ms) */
698 pfl->cfi_table[0x22] = 0x00;
699 /* Reserved */
700 pfl->cfi_table[0x23] = 0x04;
701 /* Max timeout for buffer write */
702 pfl->cfi_table[0x24] = 0x04;
703 /* Max timeout for block erase */
704 pfl->cfi_table[0x25] = 0x04;
705 /* Max timeout for chip erase */
706 pfl->cfi_table[0x26] = 0x00;
707 /* Device size */
708 pfl->cfi_table[0x27] = ctz32(device_len); /* + 1; */
709 /* Flash device interface (8 & 16 bits) */
710 pfl->cfi_table[0x28] = 0x02;
711 pfl->cfi_table[0x29] = 0x00;
712 /* Max number of bytes in multi-bytes write */
713 if (pfl->bank_width == 1) {
714 pfl->cfi_table[0x2A] = 0x08;
715 } else {
716 pfl->cfi_table[0x2A] = 0x0B;
717 }
718 pfl->writeblock_size = 1 << pfl->cfi_table[0x2A];
719 if (!pfl->old_multiple_chip_handling && num_devices > 1) {
720 pfl->writeblock_size *= num_devices;
721 }
722
723 pfl->cfi_table[0x2B] = 0x00;
724 /* Number of erase block regions (uniform) */
725 pfl->cfi_table[0x2C] = 0x01;
726 /* Erase block region 1 */
727 pfl->cfi_table[0x2D] = blocks_per_device - 1;
728 pfl->cfi_table[0x2E] = (blocks_per_device - 1) >> 8;
729 pfl->cfi_table[0x2F] = sector_len_per_device >> 8;
730 pfl->cfi_table[0x30] = sector_len_per_device >> 16;
731
732 /* Extended */
733 pfl->cfi_table[0x31] = 'P';
734 pfl->cfi_table[0x32] = 'R';
735 pfl->cfi_table[0x33] = 'I';
736
737 pfl->cfi_table[0x34] = '1';
738 pfl->cfi_table[0x35] = '0';
739
740 pfl->cfi_table[0x36] = 0x00;
741 pfl->cfi_table[0x37] = 0x00;
742 pfl->cfi_table[0x38] = 0x00;
743 pfl->cfi_table[0x39] = 0x00;
744
745 pfl->cfi_table[0x3a] = 0x00;
746
747 pfl->cfi_table[0x3b] = 0x00;
748 pfl->cfi_table[0x3c] = 0x00;
749
750 pfl->cfi_table[0x3f] = 0x01; /* Number of protection fields */
751 }
752
753 static void pflash_cfi01_realize(DeviceState *dev, Error **errp)
754 {
755 ERRP_GUARD();
756 PFlashCFI01 *pfl = PFLASH_CFI01(dev);
757 uint64_t total_len;
758 int ret;
759
760 if (pfl->sector_len == 0) {
761 error_setg(errp, "attribute \"sector-length\" not specified or zero.");
762 return;
763 }
764 if (pfl->nb_blocs == 0) {
765 error_setg(errp, "attribute \"num-blocks\" not specified or zero.");
766 return;
767 }
768 if (pfl->name == NULL) {
769 error_setg(errp, "attribute \"name\" not specified.");
770 return;
771 }
772
773 total_len = pfl->sector_len * pfl->nb_blocs;
774
775 memory_region_init_rom_device(
776 &pfl->mem, OBJECT(dev),
777 &pflash_cfi01_ops,
778 pfl,
779 pfl->name, total_len, errp);
780 if (*errp) {
781 return;
782 }
783
784 pfl->storage = memory_region_get_ram_ptr(&pfl->mem);
785 sysbus_init_mmio(SYS_BUS_DEVICE(dev), &pfl->mem);
786
787 if (pfl->blk) {
788 uint64_t perm;
789 pfl->ro = !blk_supports_write_perm(pfl->blk);
790 perm = BLK_PERM_CONSISTENT_READ | (pfl->ro ? 0 : BLK_PERM_WRITE);
791 ret = blk_set_perm(pfl->blk, perm, BLK_PERM_ALL, errp);
792 if (ret < 0) {
793 return;
794 }
795 } else {
796 pfl->ro = false;
797 }
798
799 if (pfl->blk) {
800 if (!blk_check_size_and_read_all(pfl->blk, pfl->storage, total_len,
801 errp)) {
802 vmstate_unregister_ram(&pfl->mem, DEVICE(pfl));
803 return;
804 }
805 }
806
807 /*
808 * Default to devices being used at their maximum device width. This was
809 * assumed before the device_width support was added.
810 */
811 if (!pfl->max_device_width) {
812 pfl->max_device_width = pfl->device_width;
813 }
814
815 pfl->wcycle = 0;
816 /*
817 * The command 0x00 is not assigned by the CFI open standard,
818 * but QEMU historically uses it for the READ_ARRAY command (0xff).
819 */
820 pfl->cmd = 0x00;
821 pfl->status = 0x80; /* WSM ready */
822 pflash_cfi01_fill_cfi_table(pfl);
823 }
824
825 static void pflash_cfi01_system_reset(DeviceState *dev)
826 {
827 PFlashCFI01 *pfl = PFLASH_CFI01(dev);
828
829 trace_pflash_reset(pfl->name);
830 /*
831 * The command 0x00 is not assigned by the CFI open standard,
832 * but QEMU historically uses it for the READ_ARRAY command (0xff).
833 */
834 pfl->cmd = 0x00;
835 pfl->wcycle = 0;
836 memory_region_rom_device_set_romd(&pfl->mem, true);
837 /*
838 * The WSM ready timer occurs at most 150ns after system reset.
839 * This model deliberately ignores this delay.
840 */
841 pfl->status = 0x80;
842 }
843
844 static Property pflash_cfi01_properties[] = {
845 DEFINE_PROP_DRIVE("drive", PFlashCFI01, blk),
846 /* num-blocks is the number of blocks actually visible to the guest,
847 * ie the total size of the device divided by the sector length.
848 * If we're emulating flash devices wired in parallel the actual
849 * number of blocks per individual device will differ.
850 */
851 DEFINE_PROP_UINT32("num-blocks", PFlashCFI01, nb_blocs, 0),
852 DEFINE_PROP_UINT64("sector-length", PFlashCFI01, sector_len, 0),
853 /* width here is the overall width of this QEMU device in bytes.
854 * The QEMU device may be emulating a number of flash devices
855 * wired up in parallel; the width of each individual flash
856 * device should be specified via device-width. If the individual
857 * devices have a maximum width which is greater than the width
858 * they are being used for, this maximum width should be set via
859 * max-device-width (which otherwise defaults to device-width).
860 * So for instance a 32-bit wide QEMU flash device made from four
861 * 16-bit flash devices used in 8-bit wide mode would be configured
862 * with width = 4, device-width = 1, max-device-width = 2.
863 *
864 * If device-width is not specified we default to backwards
865 * compatible behaviour which is a bad emulation of two
866 * 16 bit devices making up a 32 bit wide QEMU device. This
867 * is deprecated for new uses of this device.
868 */
869 DEFINE_PROP_UINT8("width", PFlashCFI01, bank_width, 0),
870 DEFINE_PROP_UINT8("device-width", PFlashCFI01, device_width, 0),
871 DEFINE_PROP_UINT8("max-device-width", PFlashCFI01, max_device_width, 0),
872 DEFINE_PROP_BIT("big-endian", PFlashCFI01, features, PFLASH_BE, 0),
873 DEFINE_PROP_BIT("secure", PFlashCFI01, features, PFLASH_SECURE, 0),
874 DEFINE_PROP_UINT16("id0", PFlashCFI01, ident0, 0),
875 DEFINE_PROP_UINT16("id1", PFlashCFI01, ident1, 0),
876 DEFINE_PROP_UINT16("id2", PFlashCFI01, ident2, 0),
877 DEFINE_PROP_UINT16("id3", PFlashCFI01, ident3, 0),
878 DEFINE_PROP_STRING("name", PFlashCFI01, name),
879 DEFINE_PROP_BOOL("old-multiple-chip-handling", PFlashCFI01,
880 old_multiple_chip_handling, false),
881 DEFINE_PROP_END_OF_LIST(),
882 };
883
884 static void pflash_cfi01_class_init(ObjectClass *klass, void *data)
885 {
886 DeviceClass *dc = DEVICE_CLASS(klass);
887
888 dc->reset = pflash_cfi01_system_reset;
889 dc->realize = pflash_cfi01_realize;
890 device_class_set_props(dc, pflash_cfi01_properties);
891 dc->vmsd = &vmstate_pflash;
892 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
893 }
894
895
896 static const TypeInfo pflash_cfi01_info = {
897 .name = TYPE_PFLASH_CFI01,
898 .parent = TYPE_SYS_BUS_DEVICE,
899 .instance_size = sizeof(PFlashCFI01),
900 .class_init = pflash_cfi01_class_init,
901 };
902
903 static void pflash_cfi01_register_types(void)
904 {
905 type_register_static(&pflash_cfi01_info);
906 }
907
908 type_init(pflash_cfi01_register_types)
909
910 PFlashCFI01 *pflash_cfi01_register(hwaddr base,
911 const char *name,
912 hwaddr size,
913 BlockBackend *blk,
914 uint32_t sector_len,
915 int bank_width,
916 uint16_t id0, uint16_t id1,
917 uint16_t id2, uint16_t id3,
918 int be)
919 {
920 DeviceState *dev = qdev_new(TYPE_PFLASH_CFI01);
921
922 if (blk) {
923 qdev_prop_set_drive(dev, "drive", blk);
924 }
925 assert(QEMU_IS_ALIGNED(size, sector_len));
926 qdev_prop_set_uint32(dev, "num-blocks", size / sector_len);
927 qdev_prop_set_uint64(dev, "sector-length", sector_len);
928 qdev_prop_set_uint8(dev, "width", bank_width);
929 qdev_prop_set_bit(dev, "big-endian", !!be);
930 qdev_prop_set_uint16(dev, "id0", id0);
931 qdev_prop_set_uint16(dev, "id1", id1);
932 qdev_prop_set_uint16(dev, "id2", id2);
933 qdev_prop_set_uint16(dev, "id3", id3);
934 qdev_prop_set_string(dev, "name", name);
935 sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
936
937 sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, base);
938 return PFLASH_CFI01(dev);
939 }
940
941 BlockBackend *pflash_cfi01_get_blk(PFlashCFI01 *fl)
942 {
943 return fl->blk;
944 }
945
946 MemoryRegion *pflash_cfi01_get_memory(PFlashCFI01 *fl)
947 {
948 return &fl->mem;
949 }
950
951 /*
952 * Handle -drive if=pflash for machines that use properties.
953 * If @dinfo is null, do nothing.
954 * Else if @fl's property "drive" is already set, fatal error.
955 * Else set it to the BlockBackend with @dinfo.
956 */
957 void pflash_cfi01_legacy_drive(PFlashCFI01 *fl, DriveInfo *dinfo)
958 {
959 Location loc;
960
961 if (!dinfo) {
962 return;
963 }
964
965 loc_push_none(&loc);
966 qemu_opts_loc_restore(dinfo->opts);
967 if (fl->blk) {
968 error_report("clashes with -machine");
969 exit(1);
970 }
971 qdev_prop_set_drive_err(DEVICE(fl), "drive", blk_by_legacy_dinfo(dinfo),
972 &error_fatal);
973 loc_pop(&loc);
974 }
975
976 static void postload_update_cb(void *opaque, bool running, RunState state)
977 {
978 PFlashCFI01 *pfl = opaque;
979
980 /* This is called after bdrv_activate_all. */
981 qemu_del_vm_change_state_handler(pfl->vmstate);
982 pfl->vmstate = NULL;
983
984 trace_pflash_postload_cb(pfl->name);
985 pflash_update(pfl, 0, pfl->sector_len * pfl->nb_blocs);
986 }
987
988 static int pflash_post_load(void *opaque, int version_id)
989 {
990 PFlashCFI01 *pfl = opaque;
991
992 if (!pfl->ro) {
993 pfl->vmstate = qemu_add_vm_change_state_handler(postload_update_cb,
994 pfl);
995 }
996 return 0;
997 }