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