]> Git Repo - qemu.git/blob - hw/block/pflash_cfi01.c
pflash_cfi01: change to new-style MMIO accessors
[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 "hw/hw.h"
40 #include "hw/block/flash.h"
41 #include "sysemu/block-backend.h"
42 #include "qemu/timer.h"
43 #include "qemu/bitops.h"
44 #include "exec/address-spaces.h"
45 #include "qemu/host-utils.h"
46 #include "hw/sysbus.h"
47
48 #define PFLASH_BUG(fmt, ...) \
49 do { \
50     fprintf(stderr, "PFLASH: Possible BUG - " fmt, ## __VA_ARGS__); \
51     exit(1); \
52 } while(0)
53
54 /* #define PFLASH_DEBUG */
55 #ifdef PFLASH_DEBUG
56 #define DPRINTF(fmt, ...)                                   \
57 do {                                                        \
58     fprintf(stderr, "PFLASH: " fmt , ## __VA_ARGS__);       \
59 } while (0)
60 #else
61 #define DPRINTF(fmt, ...) do { } while (0)
62 #endif
63
64 #define TYPE_CFI_PFLASH01 "cfi.pflash01"
65 #define CFI_PFLASH01(obj) OBJECT_CHECK(pflash_t, (obj), TYPE_CFI_PFLASH01)
66
67 #define PFLASH_BE          0
68
69 struct pflash_t {
70     /*< private >*/
71     SysBusDevice parent_obj;
72     /*< public >*/
73
74     BlockBackend *blk;
75     uint32_t nb_blocs;
76     uint64_t sector_len;
77     uint8_t bank_width;
78     uint8_t device_width; /* If 0, device width not specified. */
79     uint8_t max_device_width;  /* max device width in bytes */
80     uint32_t features;
81     uint8_t wcycle; /* if 0, the flash is read normally */
82     int ro;
83     uint8_t cmd;
84     uint8_t status;
85     uint16_t ident0;
86     uint16_t ident1;
87     uint16_t ident2;
88     uint16_t ident3;
89     uint8_t cfi_len;
90     uint8_t cfi_table[0x52];
91     uint64_t counter;
92     unsigned int writeblock_size;
93     QEMUTimer *timer;
94     MemoryRegion mem;
95     char *name;
96     void *storage;
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, pflash_t),
108         VMSTATE_UINT8(cmd, pflash_t),
109         VMSTATE_UINT8(status, pflash_t),
110         VMSTATE_UINT64(counter, pflash_t),
111         VMSTATE_END_OF_LIST()
112     }
113 };
114
115 static void pflash_timer (void *opaque)
116 {
117     pflash_t *pfl = opaque;
118
119     DPRINTF("%s: command %02x done\n", __func__, 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(pflash_t *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 > pfl->cfi_len) {
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(pflash_t *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         DPRINTF("%s: Manufacturer Code %04x\n", __func__, resp);
218         break;
219     case 1:
220         resp = pfl->ident1;
221         DPRINTF("%s: Device ID Code %04x\n", __func__, resp);
222         break;
223     default:
224         DPRINTF("%s: Read Device Information offset=%x\n", __func__,
225                 (unsigned)offset);
226         return 0;
227         break;
228     }
229     /* Replicate responses for each device in bank. */
230     if (pfl->device_width < pfl->bank_width) {
231         for (i = pfl->device_width;
232               i < pfl->bank_width; i += pfl->device_width) {
233             resp = deposit32(resp, 8 * i, 8 * pfl->device_width, resp);
234         }
235     }
236
237     return resp;
238 }
239
240 static uint32_t pflash_read (pflash_t *pfl, hwaddr offset,
241                              int width, int be)
242 {
243     hwaddr boff;
244     uint32_t ret;
245     uint8_t *p;
246
247     ret = -1;
248
249 #if 0
250     DPRINTF("%s: reading offset " TARGET_FMT_plx " under cmd %02x width %d\n",
251             __func__, offset, pfl->cmd, width);
252 #endif
253     switch (pfl->cmd) {
254     default:
255         /* This should never happen : reset state & treat it as a read */
256         DPRINTF("%s: unknown command state: %x\n", __func__, pfl->cmd);
257         pfl->wcycle = 0;
258         pfl->cmd = 0;
259         /* fall through to read code */
260     case 0x00:
261         /* Flash area read */
262         p = pfl->storage;
263         switch (width) {
264         case 1:
265             ret = p[offset];
266             DPRINTF("%s: data offset " TARGET_FMT_plx " %02x\n",
267                     __func__, offset, ret);
268             break;
269         case 2:
270             if (be) {
271                 ret = p[offset] << 8;
272                 ret |= p[offset + 1];
273             } else {
274                 ret = p[offset];
275                 ret |= p[offset + 1] << 8;
276             }
277             DPRINTF("%s: data offset " TARGET_FMT_plx " %04x\n",
278                     __func__, offset, ret);
279             break;
280         case 4:
281             if (be) {
282                 ret = p[offset] << 24;
283                 ret |= p[offset + 1] << 16;
284                 ret |= p[offset + 2] << 8;
285                 ret |= p[offset + 3];
286             } else {
287                 ret = p[offset];
288                 ret |= p[offset + 1] << 8;
289                 ret |= p[offset + 2] << 16;
290                 ret |= p[offset + 3] << 24;
291             }
292             DPRINTF("%s: data offset " TARGET_FMT_plx " %08x\n",
293                     __func__, offset, ret);
294             break;
295         default:
296             DPRINTF("BUG in %s\n", __func__);
297         }
298
299         break;
300     case 0x10: /* Single byte program */
301     case 0x20: /* Block erase */
302     case 0x28: /* Block erase */
303     case 0x40: /* single byte program */
304     case 0x50: /* Clear status register */
305     case 0x60: /* Block /un)lock */
306     case 0x70: /* Status Register */
307     case 0xe8: /* Write block */
308         /* Status register read.  Return status from each device in
309          * bank.
310          */
311         ret = pfl->status;
312         if (pfl->device_width && width > pfl->device_width) {
313             int shift = pfl->device_width * 8;
314             while (shift + pfl->device_width * 8 <= width * 8) {
315                 ret |= pfl->status << shift;
316                 shift += pfl->device_width * 8;
317             }
318         } else if (!pfl->device_width && width > 2) {
319             /* Handle 32 bit flash cases where device width is not
320              * set. (Existing behavior before device width added.)
321              */
322             ret |= pfl->status << 16;
323         }
324         DPRINTF("%s: status %x\n", __func__, ret);
325         break;
326     case 0x90:
327         if (!pfl->device_width) {
328             /* Preserve old behavior if device width not specified */
329             boff = offset & 0xFF;
330             if (pfl->bank_width == 2) {
331                 boff = boff >> 1;
332             } else if (pfl->bank_width == 4) {
333                 boff = boff >> 2;
334             }
335
336             switch (boff) {
337             case 0:
338                 ret = pfl->ident0 << 8 | pfl->ident1;
339                 DPRINTF("%s: Manufacturer Code %04x\n", __func__, ret);
340                 break;
341             case 1:
342                 ret = pfl->ident2 << 8 | pfl->ident3;
343                 DPRINTF("%s: Device ID Code %04x\n", __func__, ret);
344                 break;
345             default:
346                 DPRINTF("%s: Read Device Information boff=%x\n", __func__,
347                         (unsigned)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 > pfl->cfi_len) {
374                 ret = 0;
375             } else {
376                 ret = pfl->cfi_table[boff];
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(pflash_t *pfl, int offset,
397                           int size)
398 {
399     int offset_end;
400     if (pfl->blk) {
401         offset_end = offset + size;
402         /* round to sectors */
403         offset = offset >> 9;
404         offset_end = (offset_end + 511) >> 9;
405         blk_write(pfl->blk, offset, pfl->storage + (offset << 9),
406                   offset_end - offset);
407     }
408 }
409
410 static inline void pflash_data_write(pflash_t *pfl, hwaddr offset,
411                                      uint32_t value, int width, int be)
412 {
413     uint8_t *p = pfl->storage;
414
415     DPRINTF("%s: block write offset " TARGET_FMT_plx
416             " value %x counter %016" PRIx64 "\n",
417             __func__, offset, value, pfl->counter);
418     switch (width) {
419     case 1:
420         p[offset] = value;
421         break;
422     case 2:
423         if (be) {
424             p[offset] = value >> 8;
425             p[offset + 1] = value;
426         } else {
427             p[offset] = value;
428             p[offset + 1] = value >> 8;
429         }
430         break;
431     case 4:
432         if (be) {
433             p[offset] = value >> 24;
434             p[offset + 1] = value >> 16;
435             p[offset + 2] = value >> 8;
436             p[offset + 3] = value;
437         } else {
438             p[offset] = value;
439             p[offset + 1] = value >> 8;
440             p[offset + 2] = value >> 16;
441             p[offset + 3] = value >> 24;
442         }
443         break;
444     }
445
446 }
447
448 static void pflash_write(pflash_t *pfl, hwaddr offset,
449                          uint32_t value, int width, int be)
450 {
451     uint8_t *p;
452     uint8_t cmd;
453
454     cmd = value;
455
456     DPRINTF("%s: writing offset " TARGET_FMT_plx " value %08x width %d wcycle 0x%x\n",
457             __func__, offset, value, width, pfl->wcycle);
458
459     if (!pfl->wcycle) {
460         /* Set the device in I/O access mode */
461         memory_region_rom_device_set_romd(&pfl->mem, false);
462     }
463
464     switch (pfl->wcycle) {
465     case 0:
466         /* read mode */
467         switch (cmd) {
468         case 0x00: /* ??? */
469             goto reset_flash;
470         case 0x10: /* Single Byte Program */
471         case 0x40: /* Single Byte Program */
472             DPRINTF("%s: Single Byte Program\n", __func__);
473             break;
474         case 0x20: /* Block erase */
475             p = pfl->storage;
476             offset &= ~(pfl->sector_len - 1);
477
478             DPRINTF("%s: block erase at " TARGET_FMT_plx " bytes %x\n",
479                     __func__, offset, (unsigned)pfl->sector_len);
480
481             if (!pfl->ro) {
482                 memset(p + offset, 0xff, pfl->sector_len);
483                 pflash_update(pfl, offset, pfl->sector_len);
484             } else {
485                 pfl->status |= 0x20; /* Block erase error */
486             }
487             pfl->status |= 0x80; /* Ready! */
488             break;
489         case 0x50: /* Clear status bits */
490             DPRINTF("%s: Clear status bits\n", __func__);
491             pfl->status = 0x0;
492             goto reset_flash;
493         case 0x60: /* Block (un)lock */
494             DPRINTF("%s: Block unlock\n", __func__);
495             break;
496         case 0x70: /* Status Register */
497             DPRINTF("%s: Read status register\n", __func__);
498             pfl->cmd = cmd;
499             return;
500         case 0x90: /* Read Device ID */
501             DPRINTF("%s: Read Device information\n", __func__);
502             pfl->cmd = cmd;
503             return;
504         case 0x98: /* CFI query */
505             DPRINTF("%s: CFI query\n", __func__);
506             break;
507         case 0xe8: /* Write to buffer */
508             DPRINTF("%s: Write to buffer\n", __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             if (pfl->device_width) {
553                 value = extract32(value, 0, pfl->device_width * 8);
554             } else {
555                 value = extract32(value, 0, pfl->bank_width * 8);
556             }
557             DPRINTF("%s: block write of %x bytes\n", __func__, value);
558             pfl->counter = value;
559             pfl->wcycle++;
560             break;
561         case 0x60:
562             if (cmd == 0xd0) {
563                 pfl->wcycle = 0;
564                 pfl->status |= 0x80;
565             } else if (cmd == 0x01) {
566                 pfl->wcycle = 0;
567                 pfl->status |= 0x80;
568             } else if (cmd == 0xff) {
569                 goto reset_flash;
570             } else {
571                 DPRINTF("%s: Unknown (un)locking command\n", __func__);
572                 goto reset_flash;
573             }
574             break;
575         case 0x98:
576             if (cmd == 0xff) {
577                 goto reset_flash;
578             } else {
579                 DPRINTF("%s: leaving query mode\n", __func__);
580             }
581             break;
582         default:
583             goto error_flash;
584         }
585         break;
586     case 2:
587         switch (pfl->cmd) {
588         case 0xe8: /* Block write */
589             if (!pfl->ro) {
590                 pflash_data_write(pfl, offset, value, width, be);
591             } else {
592                 pfl->status |= 0x10; /* Programming error */
593             }
594
595             pfl->status |= 0x80;
596
597             if (!pfl->counter) {
598                 hwaddr mask = pfl->writeblock_size - 1;
599                 mask = ~mask;
600
601                 DPRINTF("%s: block write finished\n", __func__);
602                 pfl->wcycle++;
603                 if (!pfl->ro) {
604                     /* Flush the entire write buffer onto backing storage.  */
605                     pflash_update(pfl, offset & mask, pfl->writeblock_size);
606                 } else {
607                     pfl->status |= 0x10; /* Programming error */
608                 }
609             }
610
611             pfl->counter--;
612             break;
613         default:
614             goto error_flash;
615         }
616         break;
617     case 3: /* Confirm mode */
618         switch (pfl->cmd) {
619         case 0xe8: /* Block write */
620             if (cmd == 0xd0) {
621                 pfl->wcycle = 0;
622                 pfl->status |= 0x80;
623             } else {
624                 DPRINTF("%s: unknown command for \"write block\"\n", __func__);
625                 PFLASH_BUG("Write block confirm");
626                 goto reset_flash;
627             }
628             break;
629         default:
630             goto error_flash;
631         }
632         break;
633     default:
634         /* Should never happen */
635         DPRINTF("%s: invalid write state\n",  __func__);
636         goto reset_flash;
637     }
638     return;
639
640  error_flash:
641     qemu_log_mask(LOG_UNIMP, "%s: Unimplemented flash cmd sequence "
642                   "(offset " TARGET_FMT_plx ", wcycle 0x%x cmd 0x%x value 0x%x)"
643                   "\n", __func__, offset, pfl->wcycle, pfl->cmd, value);
644
645  reset_flash:
646     memory_region_rom_device_set_romd(&pfl->mem, true);
647
648     pfl->wcycle = 0;
649     pfl->cmd = 0;
650 }
651
652
653 static uint64_t pflash_mem_read(void *opaque, hwaddr addr, unsigned len)
654 {
655     pflash_t *pfl = opaque;
656     bool be = !!(pfl->features & (1 << PFLASH_BE));
657
658     return pflash_read(pfl, addr, len, be);
659 }
660
661 static void pflash_mem_write(void *opaque, hwaddr addr, uint64_t value, unsigned len)
662 {
663     pflash_t *pfl = opaque;
664     bool be = !!(pfl->features & (1 << PFLASH_BE));
665
666     pflash_write(pfl, addr, value, len, be);
667 }
668
669 static const MemoryRegionOps pflash_cfi01_ops = {
670     .read = pflash_mem_read,
671     .write = pflash_mem_write,
672     .endianness = DEVICE_NATIVE_ENDIAN,
673 };
674
675 static void pflash_cfi01_realize(DeviceState *dev, Error **errp)
676 {
677     pflash_t *pfl = CFI_PFLASH01(dev);
678     uint64_t total_len;
679     int ret;
680     uint64_t blocks_per_device, device_len;
681     int num_devices;
682     Error *local_err = NULL;
683
684     total_len = pfl->sector_len * pfl->nb_blocs;
685
686     /* These are only used to expose the parameters of each device
687      * in the cfi_table[].
688      */
689     num_devices = pfl->device_width ? (pfl->bank_width / pfl->device_width) : 1;
690     blocks_per_device = pfl->nb_blocs / num_devices;
691     device_len = pfl->sector_len * blocks_per_device;
692
693     /* XXX: to be fixed */
694 #if 0
695     if (total_len != (8 * 1024 * 1024) && total_len != (16 * 1024 * 1024) &&
696         total_len != (32 * 1024 * 1024) && total_len != (64 * 1024 * 1024))
697         return NULL;
698 #endif
699
700     memory_region_init_rom_device(
701         &pfl->mem, OBJECT(dev),
702         &pflash_cfi01_ops,
703         pfl,
704         pfl->name, total_len, &local_err);
705     if (local_err) {
706         error_propagate(errp, local_err);
707         return;
708     }
709
710     vmstate_register_ram(&pfl->mem, DEVICE(pfl));
711     pfl->storage = memory_region_get_ram_ptr(&pfl->mem);
712     sysbus_init_mmio(SYS_BUS_DEVICE(dev), &pfl->mem);
713
714     if (pfl->blk) {
715         /* read the initial flash content */
716         ret = blk_read(pfl->blk, 0, pfl->storage, total_len >> 9);
717
718         if (ret < 0) {
719             vmstate_unregister_ram(&pfl->mem, DEVICE(pfl));
720             error_setg(errp, "failed to read the initial flash content");
721             return;
722         }
723     }
724
725     if (pfl->blk) {
726         pfl->ro = blk_is_read_only(pfl->blk);
727     } else {
728         pfl->ro = 0;
729     }
730
731     /* Default to devices being used at their maximum device width. This was
732      * assumed before the device_width support was added.
733      */
734     if (!pfl->max_device_width) {
735         pfl->max_device_width = pfl->device_width;
736     }
737
738     pfl->timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, pflash_timer, pfl);
739     pfl->wcycle = 0;
740     pfl->cmd = 0;
741     pfl->status = 0;
742     /* Hardcoded CFI table */
743     pfl->cfi_len = 0x52;
744     /* Standard "QRY" string */
745     pfl->cfi_table[0x10] = 'Q';
746     pfl->cfi_table[0x11] = 'R';
747     pfl->cfi_table[0x12] = 'Y';
748     /* Command set (Intel) */
749     pfl->cfi_table[0x13] = 0x01;
750     pfl->cfi_table[0x14] = 0x00;
751     /* Primary extended table address (none) */
752     pfl->cfi_table[0x15] = 0x31;
753     pfl->cfi_table[0x16] = 0x00;
754     /* Alternate command set (none) */
755     pfl->cfi_table[0x17] = 0x00;
756     pfl->cfi_table[0x18] = 0x00;
757     /* Alternate extended table (none) */
758     pfl->cfi_table[0x19] = 0x00;
759     pfl->cfi_table[0x1A] = 0x00;
760     /* Vcc min */
761     pfl->cfi_table[0x1B] = 0x45;
762     /* Vcc max */
763     pfl->cfi_table[0x1C] = 0x55;
764     /* Vpp min (no Vpp pin) */
765     pfl->cfi_table[0x1D] = 0x00;
766     /* Vpp max (no Vpp pin) */
767     pfl->cfi_table[0x1E] = 0x00;
768     /* Reserved */
769     pfl->cfi_table[0x1F] = 0x07;
770     /* Timeout for min size buffer write */
771     pfl->cfi_table[0x20] = 0x07;
772     /* Typical timeout for block erase */
773     pfl->cfi_table[0x21] = 0x0a;
774     /* Typical timeout for full chip erase (4096 ms) */
775     pfl->cfi_table[0x22] = 0x00;
776     /* Reserved */
777     pfl->cfi_table[0x23] = 0x04;
778     /* Max timeout for buffer write */
779     pfl->cfi_table[0x24] = 0x04;
780     /* Max timeout for block erase */
781     pfl->cfi_table[0x25] = 0x04;
782     /* Max timeout for chip erase */
783     pfl->cfi_table[0x26] = 0x00;
784     /* Device size */
785     pfl->cfi_table[0x27] = ctz32(device_len); /* + 1; */
786     /* Flash device interface (8 & 16 bits) */
787     pfl->cfi_table[0x28] = 0x02;
788     pfl->cfi_table[0x29] = 0x00;
789     /* Max number of bytes in multi-bytes write */
790     if (pfl->bank_width == 1) {
791         pfl->cfi_table[0x2A] = 0x08;
792     } else {
793         pfl->cfi_table[0x2A] = 0x0B;
794     }
795     pfl->writeblock_size = 1 << pfl->cfi_table[0x2A];
796
797     pfl->cfi_table[0x2B] = 0x00;
798     /* Number of erase block regions (uniform) */
799     pfl->cfi_table[0x2C] = 0x01;
800     /* Erase block region 1 */
801     pfl->cfi_table[0x2D] = blocks_per_device - 1;
802     pfl->cfi_table[0x2E] = (blocks_per_device - 1) >> 8;
803     pfl->cfi_table[0x2F] = pfl->sector_len >> 8;
804     pfl->cfi_table[0x30] = pfl->sector_len >> 16;
805
806     /* Extended */
807     pfl->cfi_table[0x31] = 'P';
808     pfl->cfi_table[0x32] = 'R';
809     pfl->cfi_table[0x33] = 'I';
810
811     pfl->cfi_table[0x34] = '1';
812     pfl->cfi_table[0x35] = '0';
813
814     pfl->cfi_table[0x36] = 0x00;
815     pfl->cfi_table[0x37] = 0x00;
816     pfl->cfi_table[0x38] = 0x00;
817     pfl->cfi_table[0x39] = 0x00;
818
819     pfl->cfi_table[0x3a] = 0x00;
820
821     pfl->cfi_table[0x3b] = 0x00;
822     pfl->cfi_table[0x3c] = 0x00;
823
824     pfl->cfi_table[0x3f] = 0x01; /* Number of protection fields */
825 }
826
827 static Property pflash_cfi01_properties[] = {
828     DEFINE_PROP_DRIVE("drive", struct pflash_t, blk),
829     /* num-blocks is the number of blocks actually visible to the guest,
830      * ie the total size of the device divided by the sector length.
831      * If we're emulating flash devices wired in parallel the actual
832      * number of blocks per indvidual device will differ.
833      */
834     DEFINE_PROP_UINT32("num-blocks", struct pflash_t, nb_blocs, 0),
835     DEFINE_PROP_UINT64("sector-length", struct pflash_t, sector_len, 0),
836     /* width here is the overall width of this QEMU device in bytes.
837      * The QEMU device may be emulating a number of flash devices
838      * wired up in parallel; the width of each individual flash
839      * device should be specified via device-width. If the individual
840      * devices have a maximum width which is greater than the width
841      * they are being used for, this maximum width should be set via
842      * max-device-width (which otherwise defaults to device-width).
843      * So for instance a 32-bit wide QEMU flash device made from four
844      * 16-bit flash devices used in 8-bit wide mode would be configured
845      * with width = 4, device-width = 1, max-device-width = 2.
846      *
847      * If device-width is not specified we default to backwards
848      * compatible behaviour which is a bad emulation of two
849      * 16 bit devices making up a 32 bit wide QEMU device. This
850      * is deprecated for new uses of this device.
851      */
852     DEFINE_PROP_UINT8("width", struct pflash_t, bank_width, 0),
853     DEFINE_PROP_UINT8("device-width", struct pflash_t, device_width, 0),
854     DEFINE_PROP_UINT8("max-device-width", struct pflash_t, max_device_width, 0),
855     DEFINE_PROP_BIT("big-endian", struct pflash_t, features, PFLASH_BE, 0),
856     DEFINE_PROP_UINT16("id0", struct pflash_t, ident0, 0),
857     DEFINE_PROP_UINT16("id1", struct pflash_t, ident1, 0),
858     DEFINE_PROP_UINT16("id2", struct pflash_t, ident2, 0),
859     DEFINE_PROP_UINT16("id3", struct pflash_t, ident3, 0),
860     DEFINE_PROP_STRING("name", struct pflash_t, name),
861     DEFINE_PROP_END_OF_LIST(),
862 };
863
864 static void pflash_cfi01_class_init(ObjectClass *klass, void *data)
865 {
866     DeviceClass *dc = DEVICE_CLASS(klass);
867
868     dc->realize = pflash_cfi01_realize;
869     dc->props = pflash_cfi01_properties;
870     dc->vmsd = &vmstate_pflash;
871     set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
872 }
873
874
875 static const TypeInfo pflash_cfi01_info = {
876     .name           = TYPE_CFI_PFLASH01,
877     .parent         = TYPE_SYS_BUS_DEVICE,
878     .instance_size  = sizeof(struct pflash_t),
879     .class_init     = pflash_cfi01_class_init,
880 };
881
882 static void pflash_cfi01_register_types(void)
883 {
884     type_register_static(&pflash_cfi01_info);
885 }
886
887 type_init(pflash_cfi01_register_types)
888
889 pflash_t *pflash_cfi01_register(hwaddr base,
890                                 DeviceState *qdev, const char *name,
891                                 hwaddr size,
892                                 BlockBackend *blk,
893                                 uint32_t sector_len, int nb_blocs,
894                                 int bank_width, uint16_t id0, uint16_t id1,
895                                 uint16_t id2, uint16_t id3, int be)
896 {
897     DeviceState *dev = qdev_create(NULL, TYPE_CFI_PFLASH01);
898
899     if (blk) {
900         qdev_prop_set_drive(dev, "drive", blk, &error_abort);
901     }
902     qdev_prop_set_uint32(dev, "num-blocks", nb_blocs);
903     qdev_prop_set_uint64(dev, "sector-length", sector_len);
904     qdev_prop_set_uint8(dev, "width", bank_width);
905     qdev_prop_set_bit(dev, "big-endian", !!be);
906     qdev_prop_set_uint16(dev, "id0", id0);
907     qdev_prop_set_uint16(dev, "id1", id1);
908     qdev_prop_set_uint16(dev, "id2", id2);
909     qdev_prop_set_uint16(dev, "id3", id3);
910     qdev_prop_set_string(dev, "name", name);
911     qdev_init_nofail(dev);
912
913     sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, base);
914     return CFI_PFLASH01(dev);
915 }
916
917 MemoryRegion *pflash_cfi01_get_memory(pflash_t *fl)
918 {
919     return &fl->mem;
920 }
921
922 static int pflash_post_load(void *opaque, int version_id)
923 {
924     pflash_t *pfl = opaque;
925
926     if (!pfl->ro) {
927         DPRINTF("%s: updating bdrv for %s\n", __func__, pfl->name);
928         pflash_update(pfl, 0, pfl->sector_len * pfl->nb_blocs);
929     }
930     return 0;
931 }
This page took 0.072938 seconds and 4 git commands to generate.