]> git.proxmox.com Git - mirror_qemu.git/blob - hw/block/pflash_cfi01.c
hw/pflash: refactor pflash_data_write()
[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 switch (width) {
229 case 1:
230 ret = p[offset];
231 break;
232 case 2:
233 if (be) {
234 ret = p[offset] << 8;
235 ret |= p[offset + 1];
236 } else {
237 ret = p[offset];
238 ret |= p[offset + 1] << 8;
239 }
240 break;
241 case 4:
242 if (be) {
243 ret = p[offset] << 24;
244 ret |= p[offset + 1] << 16;
245 ret |= p[offset + 2] << 8;
246 ret |= p[offset + 3];
247 } else {
248 ret = p[offset];
249 ret |= p[offset + 1] << 8;
250 ret |= p[offset + 2] << 16;
251 ret |= p[offset + 3] << 24;
252 }
253 break;
254 default:
255 abort();
256 }
257 trace_pflash_data_read(pfl->name, offset, width, ret);
258 return ret;
259 }
260
261 static uint32_t pflash_read(PFlashCFI01 *pfl, hwaddr offset,
262 int width, int be)
263 {
264 hwaddr boff;
265 uint32_t ret;
266
267 ret = -1;
268 switch (pfl->cmd) {
269 default:
270 /* This should never happen : reset state & treat it as a read */
271 trace_pflash_read_unknown_state(pfl->name, pfl->cmd);
272 pfl->wcycle = 0;
273 /*
274 * The command 0x00 is not assigned by the CFI open standard,
275 * but QEMU historically uses it for the READ_ARRAY command (0xff).
276 */
277 pfl->cmd = 0x00;
278 /* fall through to read code */
279 case 0x00: /* This model reset value for READ_ARRAY (not CFI compliant) */
280 /* Flash area read */
281 ret = pflash_data_read(pfl, offset, width, be);
282 break;
283 case 0x10: /* Single byte program */
284 case 0x20: /* Block erase */
285 case 0x28: /* Block erase */
286 case 0x40: /* single byte program */
287 case 0x50: /* Clear status register */
288 case 0x60: /* Block /un)lock */
289 case 0x70: /* Status Register */
290 case 0xe8: /* Write block */
291 /*
292 * Status register read. Return status from each device in
293 * bank.
294 */
295 ret = pfl->status;
296 if (pfl->device_width && width > pfl->device_width) {
297 int shift = pfl->device_width * 8;
298 while (shift + pfl->device_width * 8 <= width * 8) {
299 ret |= pfl->status << shift;
300 shift += pfl->device_width * 8;
301 }
302 } else if (!pfl->device_width && width > 2) {
303 /*
304 * Handle 32 bit flash cases where device width is not
305 * set. (Existing behavior before device width added.)
306 */
307 ret |= pfl->status << 16;
308 }
309 trace_pflash_read_status(pfl->name, ret);
310 break;
311 case 0x90:
312 if (!pfl->device_width) {
313 /* Preserve old behavior if device width not specified */
314 boff = offset & 0xFF;
315 if (pfl->bank_width == 2) {
316 boff = boff >> 1;
317 } else if (pfl->bank_width == 4) {
318 boff = boff >> 2;
319 }
320
321 switch (boff) {
322 case 0:
323 ret = pfl->ident0 << 8 | pfl->ident1;
324 trace_pflash_manufacturer_id(pfl->name, ret);
325 break;
326 case 1:
327 ret = pfl->ident2 << 8 | pfl->ident3;
328 trace_pflash_device_id(pfl->name, ret);
329 break;
330 default:
331 trace_pflash_device_info(pfl->name, boff);
332 ret = 0;
333 break;
334 }
335 } else {
336 /*
337 * If we have a read larger than the bank_width, combine multiple
338 * manufacturer/device ID queries into a single response.
339 */
340 int i;
341 for (i = 0; i < width; i += pfl->bank_width) {
342 ret = deposit32(ret, i * 8, pfl->bank_width * 8,
343 pflash_devid_query(pfl,
344 offset + i * pfl->bank_width));
345 }
346 }
347 break;
348 case 0x98: /* Query mode */
349 if (!pfl->device_width) {
350 /* Preserve old behavior if device width not specified */
351 boff = offset & 0xFF;
352 if (pfl->bank_width == 2) {
353 boff = boff >> 1;
354 } else if (pfl->bank_width == 4) {
355 boff = boff >> 2;
356 }
357
358 if (boff < sizeof(pfl->cfi_table)) {
359 ret = pfl->cfi_table[boff];
360 } else {
361 ret = 0;
362 }
363 } else {
364 /*
365 * If we have a read larger than the bank_width, combine multiple
366 * CFI queries into a single response.
367 */
368 int i;
369 for (i = 0; i < width; i += pfl->bank_width) {
370 ret = deposit32(ret, i * 8, pfl->bank_width * 8,
371 pflash_cfi_query(pfl,
372 offset + i * pfl->bank_width));
373 }
374 }
375
376 break;
377 }
378 trace_pflash_io_read(pfl->name, offset, width, ret, pfl->cmd, pfl->wcycle);
379
380 return ret;
381 }
382
383 /* update flash content on disk */
384 static void pflash_update(PFlashCFI01 *pfl, int offset,
385 int size)
386 {
387 int offset_end;
388 int ret;
389 if (pfl->blk) {
390 offset_end = offset + size;
391 /* widen to sector boundaries */
392 offset = QEMU_ALIGN_DOWN(offset, BDRV_SECTOR_SIZE);
393 offset_end = QEMU_ALIGN_UP(offset_end, BDRV_SECTOR_SIZE);
394 ret = blk_pwrite(pfl->blk, offset, offset_end - offset,
395 pfl->storage + offset, 0);
396 if (ret < 0) {
397 /* TODO set error bit in status */
398 error_report("Could not update PFLASH: %s", strerror(-ret));
399 }
400 }
401 }
402
403 static inline void pflash_data_write(PFlashCFI01 *pfl, hwaddr offset,
404 uint32_t value, int width, int be)
405 {
406 uint8_t *p;
407
408 trace_pflash_data_write(pfl->name, offset, width, value, pfl->counter);
409 p = pfl->storage + offset;
410
411 switch (width) {
412 case 1:
413 p[0] = value;
414 break;
415 case 2:
416 if (be) {
417 p[0] = value >> 8;
418 p[1] = value;
419 } else {
420 p[0] = value;
421 p[1] = value >> 8;
422 }
423 break;
424 case 4:
425 if (be) {
426 p[0] = value >> 24;
427 p[1] = value >> 16;
428 p[2] = value >> 8;
429 p[3] = value;
430 } else {
431 p[0] = value;
432 p[1] = value >> 8;
433 p[2] = value >> 16;
434 p[3] = value >> 24;
435 }
436 break;
437 }
438
439 }
440
441 static void pflash_write(PFlashCFI01 *pfl, hwaddr offset,
442 uint32_t value, int width, int be)
443 {
444 uint8_t *p;
445 uint8_t cmd;
446
447 cmd = value;
448
449 trace_pflash_io_write(pfl->name, offset, width, value, pfl->wcycle);
450 if (!pfl->wcycle) {
451 /* Set the device in I/O access mode */
452 memory_region_rom_device_set_romd(&pfl->mem, false);
453 }
454
455 switch (pfl->wcycle) {
456 case 0:
457 /* read mode */
458 switch (cmd) {
459 case 0x00: /* This model reset value for READ_ARRAY (not CFI) */
460 goto mode_read_array;
461 case 0x10: /* Single Byte Program */
462 case 0x40: /* Single Byte Program */
463 trace_pflash_write(pfl->name, "single byte program (0)");
464 break;
465 case 0x20: /* Block erase */
466 p = pfl->storage;
467 offset &= ~(pfl->sector_len - 1);
468
469 trace_pflash_write_block_erase(pfl->name, offset, pfl->sector_len);
470
471 if (!pfl->ro) {
472 memset(p + offset, 0xff, pfl->sector_len);
473 pflash_update(pfl, offset, pfl->sector_len);
474 } else {
475 pfl->status |= 0x20; /* Block erase error */
476 }
477 pfl->status |= 0x80; /* Ready! */
478 break;
479 case 0x50: /* Clear status bits */
480 trace_pflash_write(pfl->name, "clear status bits");
481 pfl->status = 0x0;
482 goto mode_read_array;
483 case 0x60: /* Block (un)lock */
484 trace_pflash_write(pfl->name, "block unlock");
485 break;
486 case 0x70: /* Status Register */
487 trace_pflash_write(pfl->name, "read status register");
488 pfl->cmd = cmd;
489 return;
490 case 0x90: /* Read Device ID */
491 trace_pflash_write(pfl->name, "read device information");
492 pfl->cmd = cmd;
493 return;
494 case 0x98: /* CFI query */
495 trace_pflash_write(pfl->name, "CFI query");
496 break;
497 case 0xe8: /* Write to buffer */
498 trace_pflash_write(pfl->name, "write to buffer");
499 /* FIXME should save @offset, @width for case 1+ */
500 qemu_log_mask(LOG_UNIMP,
501 "%s: Write to buffer emulation is flawed\n",
502 __func__);
503 pfl->status |= 0x80; /* Ready! */
504 break;
505 case 0xf0: /* Probe for AMD flash */
506 trace_pflash_write(pfl->name, "probe for AMD flash");
507 goto mode_read_array;
508 case 0xff: /* Read Array */
509 trace_pflash_write(pfl->name, "read array mode");
510 goto mode_read_array;
511 default:
512 goto error_flash;
513 }
514 pfl->wcycle++;
515 pfl->cmd = cmd;
516 break;
517 case 1:
518 switch (pfl->cmd) {
519 case 0x10: /* Single Byte Program */
520 case 0x40: /* Single Byte Program */
521 trace_pflash_write(pfl->name, "single byte program (1)");
522 if (!pfl->ro) {
523 pflash_data_write(pfl, offset, value, width, be);
524 pflash_update(pfl, offset, width);
525 } else {
526 pfl->status |= 0x10; /* Programming error */
527 }
528 pfl->status |= 0x80; /* Ready! */
529 pfl->wcycle = 0;
530 break;
531 case 0x20: /* Block erase */
532 case 0x28:
533 if (cmd == 0xd0) { /* confirm */
534 pfl->wcycle = 0;
535 pfl->status |= 0x80;
536 } else if (cmd == 0xff) { /* Read Array */
537 goto mode_read_array;
538 } else
539 goto error_flash;
540
541 break;
542 case 0xe8:
543 /*
544 * Mask writeblock size based on device width, or bank width if
545 * device width not specified.
546 */
547 /* FIXME check @offset, @width */
548 if (pfl->device_width) {
549 value = extract32(value, 0, pfl->device_width * 8);
550 } else {
551 value = extract32(value, 0, pfl->bank_width * 8);
552 }
553 trace_pflash_write_block(pfl->name, value);
554 pfl->counter = value;
555 pfl->wcycle++;
556 break;
557 case 0x60:
558 if (cmd == 0xd0) {
559 pfl->wcycle = 0;
560 pfl->status |= 0x80;
561 } else if (cmd == 0x01) {
562 pfl->wcycle = 0;
563 pfl->status |= 0x80;
564 } else if (cmd == 0xff) { /* Read Array */
565 goto mode_read_array;
566 } else {
567 trace_pflash_write(pfl->name, "unknown (un)locking command");
568 goto mode_read_array;
569 }
570 break;
571 case 0x98:
572 if (cmd == 0xff) { /* Read Array */
573 goto mode_read_array;
574 } else {
575 trace_pflash_write(pfl->name, "leaving query mode");
576 }
577 break;
578 default:
579 goto error_flash;
580 }
581 break;
582 case 2:
583 switch (pfl->cmd) {
584 case 0xe8: /* Block write */
585 /* FIXME check @offset, @width */
586 if (!pfl->ro) {
587 /*
588 * FIXME writing straight to memory is *wrong*. We
589 * should write to a buffer, and flush it to memory
590 * only on confirm command (see below).
591 */
592 pflash_data_write(pfl, offset, value, width, be);
593 } else {
594 pfl->status |= 0x10; /* Programming error */
595 }
596
597 pfl->status |= 0x80;
598
599 if (!pfl->counter) {
600 hwaddr mask = pfl->writeblock_size - 1;
601 mask = ~mask;
602
603 trace_pflash_write(pfl->name, "block write finished");
604 pfl->wcycle++;
605 if (!pfl->ro) {
606 /* Flush the entire write buffer onto backing storage. */
607 /* FIXME premature! */
608 pflash_update(pfl, offset & mask, pfl->writeblock_size);
609 } else {
610 pfl->status |= 0x10; /* Programming error */
611 }
612 }
613
614 pfl->counter--;
615 break;
616 default:
617 goto error_flash;
618 }
619 break;
620 case 3: /* Confirm mode */
621 switch (pfl->cmd) {
622 case 0xe8: /* Block write */
623 if (cmd == 0xd0) {
624 /* FIXME this is where we should write out the buffer */
625 pfl->wcycle = 0;
626 pfl->status |= 0x80;
627 } else {
628 qemu_log_mask(LOG_UNIMP,
629 "%s: Aborting write to buffer not implemented,"
630 " the data is already written to storage!\n"
631 "Flash device reset into READ mode.\n",
632 __func__);
633 goto mode_read_array;
634 }
635 break;
636 default:
637 goto error_flash;
638 }
639 break;
640 default:
641 /* Should never happen */
642 trace_pflash_write(pfl->name, "invalid write state");
643 goto mode_read_array;
644 }
645 return;
646
647 error_flash:
648 qemu_log_mask(LOG_UNIMP, "%s: Unimplemented flash cmd sequence "
649 "(offset " HWADDR_FMT_plx ", wcycle 0x%x cmd 0x%x value 0x%x)"
650 "\n", __func__, offset, pfl->wcycle, pfl->cmd, value);
651
652 mode_read_array:
653 trace_pflash_mode_read_array(pfl->name);
654 memory_region_rom_device_set_romd(&pfl->mem, true);
655 pfl->wcycle = 0;
656 pfl->cmd = 0x00; /* This model reset value for READ_ARRAY (not CFI) */
657 }
658
659
660 static MemTxResult pflash_mem_read_with_attrs(void *opaque, hwaddr addr, uint64_t *value,
661 unsigned len, MemTxAttrs attrs)
662 {
663 PFlashCFI01 *pfl = opaque;
664 bool be = !!(pfl->features & (1 << PFLASH_BE));
665
666 if ((pfl->features & (1 << PFLASH_SECURE)) && !attrs.secure) {
667 *value = pflash_data_read(opaque, addr, len, be);
668 } else {
669 *value = pflash_read(opaque, addr, len, be);
670 }
671 return MEMTX_OK;
672 }
673
674 static MemTxResult pflash_mem_write_with_attrs(void *opaque, hwaddr addr, uint64_t value,
675 unsigned len, MemTxAttrs attrs)
676 {
677 PFlashCFI01 *pfl = opaque;
678 bool be = !!(pfl->features & (1 << PFLASH_BE));
679
680 if ((pfl->features & (1 << PFLASH_SECURE)) && !attrs.secure) {
681 return MEMTX_ERROR;
682 } else {
683 pflash_write(opaque, addr, value, len, be);
684 return MEMTX_OK;
685 }
686 }
687
688 static const MemoryRegionOps pflash_cfi01_ops = {
689 .read_with_attrs = pflash_mem_read_with_attrs,
690 .write_with_attrs = pflash_mem_write_with_attrs,
691 .endianness = DEVICE_NATIVE_ENDIAN,
692 };
693
694 static void pflash_cfi01_fill_cfi_table(PFlashCFI01 *pfl)
695 {
696 uint64_t blocks_per_device, sector_len_per_device, device_len;
697 int num_devices;
698
699 /*
700 * These are only used to expose the parameters of each device
701 * in the cfi_table[].
702 */
703 num_devices = pfl->device_width ? (pfl->bank_width / pfl->device_width) : 1;
704 if (pfl->old_multiple_chip_handling) {
705 blocks_per_device = pfl->nb_blocs / num_devices;
706 sector_len_per_device = pfl->sector_len;
707 } else {
708 blocks_per_device = pfl->nb_blocs;
709 sector_len_per_device = pfl->sector_len / num_devices;
710 }
711 device_len = sector_len_per_device * blocks_per_device;
712
713 /* Hardcoded CFI table */
714 /* Standard "QRY" string */
715 pfl->cfi_table[0x10] = 'Q';
716 pfl->cfi_table[0x11] = 'R';
717 pfl->cfi_table[0x12] = 'Y';
718 /* Command set (Intel) */
719 pfl->cfi_table[0x13] = 0x01;
720 pfl->cfi_table[0x14] = 0x00;
721 /* Primary extended table address (none) */
722 pfl->cfi_table[0x15] = 0x31;
723 pfl->cfi_table[0x16] = 0x00;
724 /* Alternate command set (none) */
725 pfl->cfi_table[0x17] = 0x00;
726 pfl->cfi_table[0x18] = 0x00;
727 /* Alternate extended table (none) */
728 pfl->cfi_table[0x19] = 0x00;
729 pfl->cfi_table[0x1A] = 0x00;
730 /* Vcc min */
731 pfl->cfi_table[0x1B] = 0x45;
732 /* Vcc max */
733 pfl->cfi_table[0x1C] = 0x55;
734 /* Vpp min (no Vpp pin) */
735 pfl->cfi_table[0x1D] = 0x00;
736 /* Vpp max (no Vpp pin) */
737 pfl->cfi_table[0x1E] = 0x00;
738 /* Reserved */
739 pfl->cfi_table[0x1F] = 0x07;
740 /* Timeout for min size buffer write */
741 pfl->cfi_table[0x20] = 0x07;
742 /* Typical timeout for block erase */
743 pfl->cfi_table[0x21] = 0x0a;
744 /* Typical timeout for full chip erase (4096 ms) */
745 pfl->cfi_table[0x22] = 0x00;
746 /* Reserved */
747 pfl->cfi_table[0x23] = 0x04;
748 /* Max timeout for buffer write */
749 pfl->cfi_table[0x24] = 0x04;
750 /* Max timeout for block erase */
751 pfl->cfi_table[0x25] = 0x04;
752 /* Max timeout for chip erase */
753 pfl->cfi_table[0x26] = 0x00;
754 /* Device size */
755 pfl->cfi_table[0x27] = ctz32(device_len); /* + 1; */
756 /* Flash device interface (8 & 16 bits) */
757 pfl->cfi_table[0x28] = 0x02;
758 pfl->cfi_table[0x29] = 0x00;
759 /* Max number of bytes in multi-bytes write */
760 if (pfl->bank_width == 1) {
761 pfl->cfi_table[0x2A] = 0x08;
762 } else {
763 pfl->cfi_table[0x2A] = 0x0B;
764 }
765 pfl->writeblock_size = 1 << pfl->cfi_table[0x2A];
766 if (!pfl->old_multiple_chip_handling && num_devices > 1) {
767 pfl->writeblock_size *= num_devices;
768 }
769
770 pfl->cfi_table[0x2B] = 0x00;
771 /* Number of erase block regions (uniform) */
772 pfl->cfi_table[0x2C] = 0x01;
773 /* Erase block region 1 */
774 pfl->cfi_table[0x2D] = blocks_per_device - 1;
775 pfl->cfi_table[0x2E] = (blocks_per_device - 1) >> 8;
776 pfl->cfi_table[0x2F] = sector_len_per_device >> 8;
777 pfl->cfi_table[0x30] = sector_len_per_device >> 16;
778
779 /* Extended */
780 pfl->cfi_table[0x31] = 'P';
781 pfl->cfi_table[0x32] = 'R';
782 pfl->cfi_table[0x33] = 'I';
783
784 pfl->cfi_table[0x34] = '1';
785 pfl->cfi_table[0x35] = '0';
786
787 pfl->cfi_table[0x36] = 0x00;
788 pfl->cfi_table[0x37] = 0x00;
789 pfl->cfi_table[0x38] = 0x00;
790 pfl->cfi_table[0x39] = 0x00;
791
792 pfl->cfi_table[0x3a] = 0x00;
793
794 pfl->cfi_table[0x3b] = 0x00;
795 pfl->cfi_table[0x3c] = 0x00;
796
797 pfl->cfi_table[0x3f] = 0x01; /* Number of protection fields */
798 }
799
800 static void pflash_cfi01_realize(DeviceState *dev, Error **errp)
801 {
802 ERRP_GUARD();
803 PFlashCFI01 *pfl = PFLASH_CFI01(dev);
804 uint64_t total_len;
805 int ret;
806
807 if (pfl->sector_len == 0) {
808 error_setg(errp, "attribute \"sector-length\" not specified or zero.");
809 return;
810 }
811 if (pfl->nb_blocs == 0) {
812 error_setg(errp, "attribute \"num-blocks\" not specified or zero.");
813 return;
814 }
815 if (pfl->name == NULL) {
816 error_setg(errp, "attribute \"name\" not specified.");
817 return;
818 }
819
820 total_len = pfl->sector_len * pfl->nb_blocs;
821
822 memory_region_init_rom_device(
823 &pfl->mem, OBJECT(dev),
824 &pflash_cfi01_ops,
825 pfl,
826 pfl->name, total_len, errp);
827 if (*errp) {
828 return;
829 }
830
831 pfl->storage = memory_region_get_ram_ptr(&pfl->mem);
832 sysbus_init_mmio(SYS_BUS_DEVICE(dev), &pfl->mem);
833
834 if (pfl->blk) {
835 uint64_t perm;
836 pfl->ro = !blk_supports_write_perm(pfl->blk);
837 perm = BLK_PERM_CONSISTENT_READ | (pfl->ro ? 0 : BLK_PERM_WRITE);
838 ret = blk_set_perm(pfl->blk, perm, BLK_PERM_ALL, errp);
839 if (ret < 0) {
840 return;
841 }
842 } else {
843 pfl->ro = false;
844 }
845
846 if (pfl->blk) {
847 if (!blk_check_size_and_read_all(pfl->blk, pfl->storage, total_len,
848 errp)) {
849 vmstate_unregister_ram(&pfl->mem, DEVICE(pfl));
850 return;
851 }
852 }
853
854 /*
855 * Default to devices being used at their maximum device width. This was
856 * assumed before the device_width support was added.
857 */
858 if (!pfl->max_device_width) {
859 pfl->max_device_width = pfl->device_width;
860 }
861
862 pfl->wcycle = 0;
863 /*
864 * The command 0x00 is not assigned by the CFI open standard,
865 * but QEMU historically uses it for the READ_ARRAY command (0xff).
866 */
867 pfl->cmd = 0x00;
868 pfl->status = 0x80; /* WSM ready */
869 pflash_cfi01_fill_cfi_table(pfl);
870 }
871
872 static void pflash_cfi01_system_reset(DeviceState *dev)
873 {
874 PFlashCFI01 *pfl = PFLASH_CFI01(dev);
875
876 trace_pflash_reset(pfl->name);
877 /*
878 * The command 0x00 is not assigned by the CFI open standard,
879 * but QEMU historically uses it for the READ_ARRAY command (0xff).
880 */
881 pfl->cmd = 0x00;
882 pfl->wcycle = 0;
883 memory_region_rom_device_set_romd(&pfl->mem, true);
884 /*
885 * The WSM ready timer occurs at most 150ns after system reset.
886 * This model deliberately ignores this delay.
887 */
888 pfl->status = 0x80;
889 }
890
891 static Property pflash_cfi01_properties[] = {
892 DEFINE_PROP_DRIVE("drive", PFlashCFI01, blk),
893 /* num-blocks is the number of blocks actually visible to the guest,
894 * ie the total size of the device divided by the sector length.
895 * If we're emulating flash devices wired in parallel the actual
896 * number of blocks per individual device will differ.
897 */
898 DEFINE_PROP_UINT32("num-blocks", PFlashCFI01, nb_blocs, 0),
899 DEFINE_PROP_UINT64("sector-length", PFlashCFI01, sector_len, 0),
900 /* width here is the overall width of this QEMU device in bytes.
901 * The QEMU device may be emulating a number of flash devices
902 * wired up in parallel; the width of each individual flash
903 * device should be specified via device-width. If the individual
904 * devices have a maximum width which is greater than the width
905 * they are being used for, this maximum width should be set via
906 * max-device-width (which otherwise defaults to device-width).
907 * So for instance a 32-bit wide QEMU flash device made from four
908 * 16-bit flash devices used in 8-bit wide mode would be configured
909 * with width = 4, device-width = 1, max-device-width = 2.
910 *
911 * If device-width is not specified we default to backwards
912 * compatible behaviour which is a bad emulation of two
913 * 16 bit devices making up a 32 bit wide QEMU device. This
914 * is deprecated for new uses of this device.
915 */
916 DEFINE_PROP_UINT8("width", PFlashCFI01, bank_width, 0),
917 DEFINE_PROP_UINT8("device-width", PFlashCFI01, device_width, 0),
918 DEFINE_PROP_UINT8("max-device-width", PFlashCFI01, max_device_width, 0),
919 DEFINE_PROP_BIT("big-endian", PFlashCFI01, features, PFLASH_BE, 0),
920 DEFINE_PROP_BIT("secure", PFlashCFI01, features, PFLASH_SECURE, 0),
921 DEFINE_PROP_UINT16("id0", PFlashCFI01, ident0, 0),
922 DEFINE_PROP_UINT16("id1", PFlashCFI01, ident1, 0),
923 DEFINE_PROP_UINT16("id2", PFlashCFI01, ident2, 0),
924 DEFINE_PROP_UINT16("id3", PFlashCFI01, ident3, 0),
925 DEFINE_PROP_STRING("name", PFlashCFI01, name),
926 DEFINE_PROP_BOOL("old-multiple-chip-handling", PFlashCFI01,
927 old_multiple_chip_handling, false),
928 DEFINE_PROP_END_OF_LIST(),
929 };
930
931 static void pflash_cfi01_class_init(ObjectClass *klass, void *data)
932 {
933 DeviceClass *dc = DEVICE_CLASS(klass);
934
935 dc->reset = pflash_cfi01_system_reset;
936 dc->realize = pflash_cfi01_realize;
937 device_class_set_props(dc, pflash_cfi01_properties);
938 dc->vmsd = &vmstate_pflash;
939 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
940 }
941
942
943 static const TypeInfo pflash_cfi01_info = {
944 .name = TYPE_PFLASH_CFI01,
945 .parent = TYPE_SYS_BUS_DEVICE,
946 .instance_size = sizeof(PFlashCFI01),
947 .class_init = pflash_cfi01_class_init,
948 };
949
950 static void pflash_cfi01_register_types(void)
951 {
952 type_register_static(&pflash_cfi01_info);
953 }
954
955 type_init(pflash_cfi01_register_types)
956
957 PFlashCFI01 *pflash_cfi01_register(hwaddr base,
958 const char *name,
959 hwaddr size,
960 BlockBackend *blk,
961 uint32_t sector_len,
962 int bank_width,
963 uint16_t id0, uint16_t id1,
964 uint16_t id2, uint16_t id3,
965 int be)
966 {
967 DeviceState *dev = qdev_new(TYPE_PFLASH_CFI01);
968
969 if (blk) {
970 qdev_prop_set_drive(dev, "drive", blk);
971 }
972 assert(QEMU_IS_ALIGNED(size, sector_len));
973 qdev_prop_set_uint32(dev, "num-blocks", size / sector_len);
974 qdev_prop_set_uint64(dev, "sector-length", sector_len);
975 qdev_prop_set_uint8(dev, "width", bank_width);
976 qdev_prop_set_bit(dev, "big-endian", !!be);
977 qdev_prop_set_uint16(dev, "id0", id0);
978 qdev_prop_set_uint16(dev, "id1", id1);
979 qdev_prop_set_uint16(dev, "id2", id2);
980 qdev_prop_set_uint16(dev, "id3", id3);
981 qdev_prop_set_string(dev, "name", name);
982 sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
983
984 sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, base);
985 return PFLASH_CFI01(dev);
986 }
987
988 BlockBackend *pflash_cfi01_get_blk(PFlashCFI01 *fl)
989 {
990 return fl->blk;
991 }
992
993 MemoryRegion *pflash_cfi01_get_memory(PFlashCFI01 *fl)
994 {
995 return &fl->mem;
996 }
997
998 /*
999 * Handle -drive if=pflash for machines that use properties.
1000 * If @dinfo is null, do nothing.
1001 * Else if @fl's property "drive" is already set, fatal error.
1002 * Else set it to the BlockBackend with @dinfo.
1003 */
1004 void pflash_cfi01_legacy_drive(PFlashCFI01 *fl, DriveInfo *dinfo)
1005 {
1006 Location loc;
1007
1008 if (!dinfo) {
1009 return;
1010 }
1011
1012 loc_push_none(&loc);
1013 qemu_opts_loc_restore(dinfo->opts);
1014 if (fl->blk) {
1015 error_report("clashes with -machine");
1016 exit(1);
1017 }
1018 qdev_prop_set_drive_err(DEVICE(fl), "drive", blk_by_legacy_dinfo(dinfo),
1019 &error_fatal);
1020 loc_pop(&loc);
1021 }
1022
1023 static void postload_update_cb(void *opaque, bool running, RunState state)
1024 {
1025 PFlashCFI01 *pfl = opaque;
1026
1027 /* This is called after bdrv_activate_all. */
1028 qemu_del_vm_change_state_handler(pfl->vmstate);
1029 pfl->vmstate = NULL;
1030
1031 trace_pflash_postload_cb(pfl->name);
1032 pflash_update(pfl, 0, pfl->sector_len * pfl->nb_blocs);
1033 }
1034
1035 static int pflash_post_load(void *opaque, int version_id)
1036 {
1037 PFlashCFI01 *pfl = opaque;
1038
1039 if (!pfl->ro) {
1040 pfl->vmstate = qemu_add_vm_change_state_handler(postload_update_cb,
1041 pfl);
1042 }
1043 return 0;
1044 }