]> Git Repo - qemu.git/blame - hw/block/pflash_cfi02.c
hw/block/pflash_cfi02: Use the ldst API in pflash_read()
[qemu.git] / hw / block / pflash_cfi02.c
CommitLineData
29133e9a
FB
1/*
2 * CFI parallel flash with AMD command set emulation
5fafdf24 3 *
29133e9a
FB
4 * Copyright (c) 2005 Jocelyn Mayer
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
8167ee88 17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
29133e9a
FB
18 */
19
20/*
21 * For now, this code can emulate flashes of 1, 2 or 4 bytes width.
22 * Supported commands/modes are:
23 * - flash read
24 * - flash write
25 * - flash ID read
26 * - sector erase
27 * - chip erase
28 * - unlock bypass command
29 * - CFI queries
30 *
31 * It does not support flash interleaving.
32 * It does not implement boot blocs with reduced size
33 * It does not implement software data protection as found in many real chips
34 * It does not implement erase suspend/resume commands
35 * It does not implement multiple sectors erase
36 */
37
80c71a24 38#include "qemu/osdep.h"
83c9f4ca 39#include "hw/hw.h"
06f15217 40#include "hw/block/block.h"
0d09e41a 41#include "hw/block/flash.h"
da34e65c 42#include "qapi/error.h"
1de7afc9 43#include "qemu/timer.h"
4be74634 44#include "sysemu/block-backend.h"
1de7afc9 45#include "qemu/host-utils.h"
0b8fa32f 46#include "qemu/module.h"
83c9f4ca 47#include "hw/sysbus.h"
13019f1f 48#include "trace.h"
29133e9a 49
6536987f 50#define PFLASH_DEBUG false
ec9ea489
PC
51#define DPRINTF(fmt, ...) \
52do { \
6536987f
PMD
53 if (PFLASH_DEBUG) { \
54 fprintf(stderr, "PFLASH: " fmt, ## __VA_ARGS__); \
55 } \
29133e9a 56} while (0)
29133e9a 57
661bfc80
JK
58#define PFLASH_LAZY_ROMD_THRESHOLD 42
59
aeaf6c20
PMD
60/* Special write cycles for CFI queries. */
61enum {
62 WCYCLE_CFI = 7,
63};
64
16434065 65struct PFlashCFI02 {
3509c396
HT
66 /*< private >*/
67 SysBusDevice parent_obj;
68 /*< public >*/
69
4be74634 70 BlockBackend *blk;
71db710f 71 uint32_t sector_len;
368a354f 72 uint32_t nb_blocs;
4fbd24ba 73 uint32_t chip_len;
368a354f
PC
74 uint8_t mappings;
75 uint8_t width;
76 uint8_t be;
29133e9a
FB
77 int wcycle; /* if 0, the flash is read normally */
78 int bypass;
79 int ro;
80 uint8_t cmd;
81 uint8_t status;
368a354f
PC
82 /* FIXME: implement array device properties */
83 uint16_t ident0;
84 uint16_t ident1;
85 uint16_t ident2;
86 uint16_t ident3;
87 uint16_t unlock_addr0;
88 uint16_t unlock_addr1;
29133e9a 89 uint8_t cfi_table[0x52];
d80cf1eb 90 QEMUTimer timer;
cfe5f011
AK
91 /* The device replicates the flash memory across its memory space. Emulate
92 * that by having a container (.mem) filled with an array of aliases
93 * (.mem_mappings) pointing to the flash memory (.orig_mem).
94 */
95 MemoryRegion mem;
96 MemoryRegion *mem_mappings; /* array; one per mapping */
97 MemoryRegion orig_mem;
9c9bb6c8 98 int rom_mode;
661bfc80 99 int read_counter; /* used for lazy switch-back to rom mode */
368a354f 100 char *name;
29133e9a
FB
101 void *storage;
102};
103
1d311e73
PMD
104/*
105 * Toggle status bit DQ7.
106 */
107static inline void toggle_dq7(PFlashCFI02 *pfl)
108{
109 pfl->status ^= 0x80;
110}
111
112/*
113 * Set status bit DQ7 to bit 7 of value.
114 */
115static inline void set_dq7(PFlashCFI02 *pfl, uint8_t value)
116{
117 pfl->status &= 0x7F;
118 pfl->status |= value & 0x80;
119}
120
121/*
122 * Toggle status bit DQ6.
123 */
124static inline void toggle_dq6(PFlashCFI02 *pfl)
125{
126 pfl->status ^= 0x40;
127}
128
cfe5f011
AK
129/*
130 * Set up replicated mappings of the same region.
131 */
16434065 132static void pflash_setup_mappings(PFlashCFI02 *pfl)
c8a50e59 133{
cfe5f011 134 unsigned i;
a8170e5e 135 hwaddr size = memory_region_size(&pfl->orig_mem);
cfe5f011 136
2d256e6f 137 memory_region_init(&pfl->mem, OBJECT(pfl), "pflash", pfl->mappings * size);
cfe5f011
AK
138 pfl->mem_mappings = g_new(MemoryRegion, pfl->mappings);
139 for (i = 0; i < pfl->mappings; ++i) {
2d256e6f
PB
140 memory_region_init_alias(&pfl->mem_mappings[i], OBJECT(pfl),
141 "pflash-alias", &pfl->orig_mem, 0, size);
cfe5f011
AK
142 memory_region_add_subregion(&pfl->mem, i * size, &pfl->mem_mappings[i]);
143 }
144}
01e0451a 145
16434065 146static void pflash_register_memory(PFlashCFI02 *pfl, int rom_mode)
cfe5f011 147{
5f9a5ea1 148 memory_region_rom_device_set_romd(&pfl->orig_mem, rom_mode);
bda254da 149 pfl->rom_mode = rom_mode;
4fbd24ba
AZ
150}
151
29133e9a
FB
152static void pflash_timer (void *opaque)
153{
16434065 154 PFlashCFI02 *pfl = opaque;
29133e9a 155
13019f1f 156 trace_pflash_timer_expired(pfl->cmd);
29133e9a 157 /* Reset flash */
1d311e73 158 toggle_dq7(pfl);
29133e9a
FB
159 if (pfl->bypass) {
160 pfl->wcycle = 2;
161 } else {
4fbd24ba 162 pflash_register_memory(pfl, 1);
29133e9a
FB
163 pfl->wcycle = 0;
164 }
165 pfl->cmd = 0;
166}
167
16434065
MA
168static uint32_t pflash_read(PFlashCFI02 *pfl, hwaddr offset,
169 int width, int be)
29133e9a 170{
a8170e5e 171 hwaddr boff;
29133e9a
FB
172 uint32_t ret;
173 uint8_t *p;
174
29133e9a 175 ret = -1;
661bfc80
JK
176 /* Lazy reset to ROMD mode after a certain amount of read accesses */
177 if (!pfl->rom_mode && pfl->wcycle == 0 &&
178 ++pfl->read_counter > PFLASH_LAZY_ROMD_THRESHOLD) {
179 pflash_register_memory(pfl, 1);
0f459d16 180 }
4fbd24ba 181 offset &= pfl->chip_len - 1;
29133e9a
FB
182 boff = offset & 0xFF;
183 if (pfl->width == 2)
184 boff = boff >> 1;
185 else if (pfl->width == 4)
186 boff = boff >> 2;
187 switch (pfl->cmd) {
188 default:
189 /* This should never happen : reset state & treat it as a read*/
190 DPRINTF("%s: unknown command state: %x\n", __func__, pfl->cmd);
191 pfl->wcycle = 0;
192 pfl->cmd = 0;
30954850 193 /* fall through to the read code */
29133e9a
FB
194 case 0x80:
195 /* We accept reads during second unlock sequence... */
196 case 0x00:
197 flash_read:
198 /* Flash area read */
3e4bcf89
PMD
199 p = (uint8_t *)pfl->storage + offset;
200 if (pfl->be) {
201 ret = ldn_be_p(p, width);
202 } else {
203 ret = ldn_le_p(p, width);
29133e9a 204 }
c1474acd 205 trace_pflash_data_read(offset, width << 1, ret);
29133e9a
FB
206 break;
207 case 0x90:
208 /* flash ID read */
209 switch (boff) {
210 case 0x00:
211 case 0x01:
368a354f 212 ret = boff & 0x01 ? pfl->ident1 : pfl->ident0;
29133e9a
FB
213 break;
214 case 0x02:
215 ret = 0x00; /* Pretend all sectors are unprotected */
216 break;
217 case 0x0E:
218 case 0x0F:
368a354f 219 ret = boff & 0x01 ? pfl->ident3 : pfl->ident2;
7f7bdcaf
PMD
220 if (ret != (uint8_t)-1) {
221 break;
368a354f 222 }
7f7bdcaf 223 /* Fall through to data read. */
29133e9a
FB
224 default:
225 goto flash_read;
226 }
6536987f 227 DPRINTF("%s: ID " TARGET_FMT_plx " %" PRIx32 "\n", __func__, boff, ret);
29133e9a
FB
228 break;
229 case 0xA0:
230 case 0x10:
231 case 0x30:
232 /* Status register read */
233 ret = pfl->status;
6536987f 234 DPRINTF("%s: status %" PRIx32 "\n", __func__, ret);
1d311e73 235 toggle_dq6(pfl);
29133e9a
FB
236 break;
237 case 0x98:
238 /* CFI query mode */
07c13a71 239 if (boff < sizeof(pfl->cfi_table)) {
29133e9a 240 ret = pfl->cfi_table[boff];
07c13a71
PMD
241 } else {
242 ret = 0;
243 }
29133e9a
FB
244 break;
245 }
e8aa2d95 246 trace_pflash_io_read(offset, width, width << 1, ret, pfl->cmd, pfl->wcycle);
29133e9a
FB
247
248 return ret;
249}
250
251/* update flash content on disk */
16434065 252static void pflash_update(PFlashCFI02 *pfl, int offset,
29133e9a
FB
253 int size)
254{
255 int offset_end;
4be74634 256 if (pfl->blk) {
29133e9a 257 offset_end = offset + size;
098e732d
EB
258 /* widen to sector boundaries */
259 offset = QEMU_ALIGN_DOWN(offset, BDRV_SECTOR_SIZE);
260 offset_end = QEMU_ALIGN_UP(offset_end, BDRV_SECTOR_SIZE);
261 blk_pwrite(pfl->blk, offset, pfl->storage + offset,
262 offset_end - offset, 0);
29133e9a
FB
263 }
264}
265
16434065
MA
266static void pflash_write(PFlashCFI02 *pfl, hwaddr offset,
267 uint32_t value, int width, int be)
29133e9a 268{
a8170e5e 269 hwaddr boff;
29133e9a
FB
270 uint8_t *p;
271 uint8_t cmd;
272
e8aa2d95 273 trace_pflash_io_write(offset, width, width << 1, value, pfl->wcycle);
95d1f3ed
JM
274 cmd = value;
275 if (pfl->cmd != 0xA0 && cmd == 0xF0) {
95d1f3ed
JM
276 goto reset_flash;
277 }
4fbd24ba 278 offset &= pfl->chip_len - 1;
3b46e624 279
29133e9a
FB
280 boff = offset & (pfl->sector_len - 1);
281 if (pfl->width == 2)
282 boff = boff >> 1;
283 else if (pfl->width == 4)
284 boff = boff >> 2;
285 switch (pfl->wcycle) {
286 case 0:
9c9bb6c8
AZ
287 /* Set the device in I/O access mode if required */
288 if (pfl->rom_mode)
289 pflash_register_memory(pfl, 0);
661bfc80 290 pfl->read_counter = 0;
29133e9a
FB
291 /* We're in read mode */
292 check_unlock0:
293 if (boff == 0x55 && cmd == 0x98) {
294 enter_CFI_mode:
295 /* Enter CFI query mode */
aeaf6c20 296 pfl->wcycle = WCYCLE_CFI;
29133e9a
FB
297 pfl->cmd = 0x98;
298 return;
299 }
368a354f 300 if (boff != pfl->unlock_addr0 || cmd != 0xAA) {
f8be67ee 301 DPRINTF("%s: unlock0 failed " TARGET_FMT_plx " %02x %04x\n",
368a354f 302 __func__, boff, cmd, pfl->unlock_addr0);
29133e9a
FB
303 goto reset_flash;
304 }
305 DPRINTF("%s: unlock sequence started\n", __func__);
306 break;
307 case 1:
308 /* We started an unlock sequence */
309 check_unlock1:
368a354f 310 if (boff != pfl->unlock_addr1 || cmd != 0x55) {
f8be67ee 311 DPRINTF("%s: unlock1 failed " TARGET_FMT_plx " %02x\n", __func__,
e96efcfc 312 boff, cmd);
29133e9a
FB
313 goto reset_flash;
314 }
315 DPRINTF("%s: unlock sequence done\n", __func__);
316 break;
317 case 2:
318 /* We finished an unlock sequence */
368a354f 319 if (!pfl->bypass && boff != pfl->unlock_addr0) {
f8be67ee 320 DPRINTF("%s: command failed " TARGET_FMT_plx " %02x\n", __func__,
e96efcfc 321 boff, cmd);
29133e9a
FB
322 goto reset_flash;
323 }
324 switch (cmd) {
325 case 0x20:
326 pfl->bypass = 1;
327 goto do_bypass;
328 case 0x80:
329 case 0x90:
330 case 0xA0:
331 pfl->cmd = cmd;
332 DPRINTF("%s: starting command %02x\n", __func__, cmd);
333 break;
334 default:
335 DPRINTF("%s: unknown command %02x\n", __func__, cmd);
336 goto reset_flash;
337 }
338 break;
339 case 3:
340 switch (pfl->cmd) {
341 case 0x80:
342 /* We need another unlock sequence */
343 goto check_unlock0;
344 case 0xA0:
c1474acd 345 trace_pflash_data_write(offset, width << 1, value, 0);
de8efe8f 346 if (!pfl->ro) {
c3d25271
PMD
347 p = (uint8_t *)pfl->storage + offset;
348 if (pfl->be) {
349 uint64_t current = ldn_be_p(p, width);
350 stn_be_p(p, width, current & value);
351 } else {
352 uint64_t current = ldn_le_p(p, width);
353 stn_le_p(p, width, current & value);
5f9fc5ad 354 }
c3d25271 355 pflash_update(pfl, offset, width);
29133e9a 356 }
1d311e73
PMD
357 /*
358 * While programming, status bit DQ7 should hold the opposite
359 * value from how it was programmed.
360 */
361 set_dq7(pfl, ~value);
29133e9a
FB
362 /* Let's pretend write is immediate */
363 if (pfl->bypass)
364 goto do_bypass;
365 goto reset_flash;
366 case 0x90:
367 if (pfl->bypass && cmd == 0x00) {
368 /* Unlock bypass reset */
369 goto reset_flash;
370 }
371 /* We can enter CFI query mode from autoselect mode */
372 if (boff == 0x55 && cmd == 0x98)
373 goto enter_CFI_mode;
374 /* No break here */
375 default:
376 DPRINTF("%s: invalid write for command %02x\n",
377 __func__, pfl->cmd);
378 goto reset_flash;
379 }
380 case 4:
381 switch (pfl->cmd) {
382 case 0xA0:
a1c7273b 383 /* Ignore writes while flash data write is occurring */
29133e9a
FB
384 /* As we suppose write is immediate, this should never happen */
385 return;
386 case 0x80:
387 goto check_unlock1;
388 default:
389 /* Should never happen */
390 DPRINTF("%s: invalid command state %02x (wc 4)\n",
391 __func__, pfl->cmd);
392 goto reset_flash;
393 }
394 break;
395 case 5:
396 switch (cmd) {
397 case 0x10:
368a354f 398 if (boff != pfl->unlock_addr0) {
f8be67ee 399 DPRINTF("%s: chip erase: invalid address " TARGET_FMT_plx "\n",
29133e9a
FB
400 __func__, offset);
401 goto reset_flash;
402 }
403 /* Chip erase */
404 DPRINTF("%s: start chip erase\n", __func__);
de8efe8f
JJ
405 if (!pfl->ro) {
406 memset(pfl->storage, 0xFF, pfl->chip_len);
407 pflash_update(pfl, 0, pfl->chip_len);
408 }
1d311e73 409 set_dq7(pfl, 0x00);
29133e9a 410 /* Let's wait 5 seconds before chip erase is done */
d80cf1eb 411 timer_mod(&pfl->timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
73bcb24d 412 (NANOSECONDS_PER_SECOND * 5));
29133e9a
FB
413 break;
414 case 0x30:
415 /* Sector erase */
416 p = pfl->storage;
417 offset &= ~(pfl->sector_len - 1);
f8be67ee 418 DPRINTF("%s: start sector erase at " TARGET_FMT_plx "\n", __func__,
e96efcfc 419 offset);
de8efe8f
JJ
420 if (!pfl->ro) {
421 memset(p + offset, 0xFF, pfl->sector_len);
422 pflash_update(pfl, offset, pfl->sector_len);
423 }
1d311e73 424 set_dq7(pfl, 0x00);
29133e9a 425 /* Let's wait 1/2 second before sector erase is done */
d80cf1eb 426 timer_mod(&pfl->timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
73bcb24d 427 (NANOSECONDS_PER_SECOND / 2));
29133e9a
FB
428 break;
429 default:
430 DPRINTF("%s: invalid command %02x (wc 5)\n", __func__, cmd);
431 goto reset_flash;
432 }
433 pfl->cmd = cmd;
434 break;
435 case 6:
436 switch (pfl->cmd) {
437 case 0x10:
438 /* Ignore writes during chip erase */
439 return;
440 case 0x30:
441 /* Ignore writes during sector erase */
442 return;
443 default:
444 /* Should never happen */
445 DPRINTF("%s: invalid command state %02x (wc 6)\n",
446 __func__, pfl->cmd);
447 goto reset_flash;
448 }
449 break;
aeaf6c20
PMD
450 /* Special values for CFI queries */
451 case WCYCLE_CFI:
29133e9a
FB
452 DPRINTF("%s: invalid write in CFI query mode\n", __func__);
453 goto reset_flash;
454 default:
455 /* Should never happen */
456 DPRINTF("%s: invalid write state (wc 7)\n", __func__);
457 goto reset_flash;
458 }
459 pfl->wcycle++;
460
461 return;
462
463 /* Reset flash */
464 reset_flash:
13019f1f 465 trace_pflash_reset();
29133e9a
FB
466 pfl->bypass = 0;
467 pfl->wcycle = 0;
468 pfl->cmd = 0;
469 return;
470
471 do_bypass:
472 pfl->wcycle = 2;
473 pfl->cmd = 0;
29133e9a
FB
474}
475
a4afb28d 476static uint64_t pflash_be_readfn(void *opaque, hwaddr addr, unsigned size)
5f9fc5ad 477{
a4afb28d 478 return pflash_read(opaque, addr, size, 1);
5f9fc5ad
BS
479}
480
a4afb28d
PM
481static void pflash_be_writefn(void *opaque, hwaddr addr,
482 uint64_t value, unsigned size)
5f9fc5ad 483{
a4afb28d 484 pflash_write(opaque, addr, value, size, 1);
5f9fc5ad
BS
485}
486
a4afb28d 487static uint64_t pflash_le_readfn(void *opaque, hwaddr addr, unsigned size)
5f9fc5ad 488{
a4afb28d 489 return pflash_read(opaque, addr, size, 0);
29133e9a
FB
490}
491
a4afb28d
PM
492static void pflash_le_writefn(void *opaque, hwaddr addr,
493 uint64_t value, unsigned size)
5f9fc5ad 494{
a4afb28d 495 pflash_write(opaque, addr, value, size, 0);
29133e9a
FB
496}
497
cfe5f011 498static const MemoryRegionOps pflash_cfi02_ops_be = {
a4afb28d
PM
499 .read = pflash_be_readfn,
500 .write = pflash_be_writefn,
501 .valid.min_access_size = 1,
502 .valid.max_access_size = 4,
cfe5f011 503 .endianness = DEVICE_NATIVE_ENDIAN,
5f9fc5ad
BS
504};
505
cfe5f011 506static const MemoryRegionOps pflash_cfi02_ops_le = {
a4afb28d
PM
507 .read = pflash_le_readfn,
508 .write = pflash_le_writefn,
509 .valid.min_access_size = 1,
510 .valid.max_access_size = 4,
cfe5f011 511 .endianness = DEVICE_NATIVE_ENDIAN,
29133e9a
FB
512};
513
da3bd642 514static void pflash_cfi02_realize(DeviceState *dev, Error **errp)
29133e9a 515{
e7b62741 516 PFlashCFI02 *pfl = PFLASH_CFI02(dev);
368a354f 517 uint32_t chip_len;
d0e7605e 518 int ret;
33e0eb52 519 Error *local_err = NULL;
29133e9a 520
8929fc3a
ZY
521 if (pfl->sector_len == 0) {
522 error_setg(errp, "attribute \"sector-length\" not specified or zero.");
523 return;
524 }
525 if (pfl->nb_blocs == 0) {
526 error_setg(errp, "attribute \"num-blocks\" not specified or zero.");
527 return;
528 }
529 if (pfl->name == NULL) {
530 error_setg(errp, "attribute \"name\" not specified.");
531 return;
532 }
533
368a354f 534 chip_len = pfl->sector_len * pfl->nb_blocs;
368a354f 535
bba3ddf7 536 memory_region_init_rom_device(&pfl->orig_mem, OBJECT(pfl), pfl->be ?
368a354f 537 &pflash_cfi02_ops_be : &pflash_cfi02_ops_le,
33e0eb52
HT
538 pfl, pfl->name, chip_len, &local_err);
539 if (local_err) {
540 error_propagate(errp, local_err);
541 return;
542 }
543
cfe5f011 544 pfl->storage = memory_region_get_ram_ptr(&pfl->orig_mem);
4fbd24ba 545 pfl->chip_len = chip_len;
a17c17a2
KW
546
547 if (pfl->blk) {
548 uint64_t perm;
549 pfl->ro = blk_is_read_only(pfl->blk);
550 perm = BLK_PERM_CONSISTENT_READ | (pfl->ro ? 0 : BLK_PERM_WRITE);
551 ret = blk_set_perm(pfl->blk, perm, BLK_PERM_ALL, errp);
552 if (ret < 0) {
553 return;
554 }
555 } else {
556 pfl->ro = 0;
557 }
558
4be74634 559 if (pfl->blk) {
06f15217
MA
560 if (!blk_check_size_and_read_all(pfl->blk, pfl->storage, chip_len,
561 errp)) {
da3bd642 562 vmstate_unregister_ram(&pfl->orig_mem, DEVICE(pfl));
da3bd642 563 return;
d0e7605e 564 }
29133e9a 565 }
de8efe8f 566
cfe5f011
AK
567 pflash_setup_mappings(pfl);
568 pfl->rom_mode = 1;
da3bd642 569 sysbus_init_mmio(SYS_BUS_DEVICE(dev), &pfl->mem);
de8efe8f 570
d80cf1eb 571 timer_init_ns(&pfl->timer, QEMU_CLOCK_VIRTUAL, pflash_timer, pfl);
29133e9a
FB
572 pfl->wcycle = 0;
573 pfl->cmd = 0;
574 pfl->status = 0;
29133e9a 575 /* Hardcoded CFI table (mostly from SG29 Spansion flash) */
29133e9a
FB
576 /* Standard "QRY" string */
577 pfl->cfi_table[0x10] = 'Q';
578 pfl->cfi_table[0x11] = 'R';
579 pfl->cfi_table[0x12] = 'Y';
580 /* Command set (AMD/Fujitsu) */
581 pfl->cfi_table[0x13] = 0x02;
582 pfl->cfi_table[0x14] = 0x00;
78556820
EI
583 /* Primary extended table address */
584 pfl->cfi_table[0x15] = 0x31;
29133e9a
FB
585 pfl->cfi_table[0x16] = 0x00;
586 /* Alternate command set (none) */
587 pfl->cfi_table[0x17] = 0x00;
588 pfl->cfi_table[0x18] = 0x00;
589 /* Alternate extended table (none) */
590 pfl->cfi_table[0x19] = 0x00;
591 pfl->cfi_table[0x1A] = 0x00;
592 /* Vcc min */
593 pfl->cfi_table[0x1B] = 0x27;
594 /* Vcc max */
595 pfl->cfi_table[0x1C] = 0x36;
596 /* Vpp min (no Vpp pin) */
597 pfl->cfi_table[0x1D] = 0x00;
598 /* Vpp max (no Vpp pin) */
599 pfl->cfi_table[0x1E] = 0x00;
600 /* Reserved */
601 pfl->cfi_table[0x1F] = 0x07;
78556820
EI
602 /* Timeout for min size buffer write (NA) */
603 pfl->cfi_table[0x20] = 0x00;
29133e9a
FB
604 /* Typical timeout for block erase (512 ms) */
605 pfl->cfi_table[0x21] = 0x09;
606 /* Typical timeout for full chip erase (4096 ms) */
607 pfl->cfi_table[0x22] = 0x0C;
608 /* Reserved */
609 pfl->cfi_table[0x23] = 0x01;
78556820
EI
610 /* Max timeout for buffer write (NA) */
611 pfl->cfi_table[0x24] = 0x00;
29133e9a
FB
612 /* Max timeout for block erase */
613 pfl->cfi_table[0x25] = 0x0A;
614 /* Max timeout for chip erase */
615 pfl->cfi_table[0x26] = 0x0D;
616 /* Device size */
78556820 617 pfl->cfi_table[0x27] = ctz32(chip_len);
29133e9a
FB
618 /* Flash device interface (8 & 16 bits) */
619 pfl->cfi_table[0x28] = 0x02;
620 pfl->cfi_table[0x29] = 0x00;
621 /* Max number of bytes in multi-bytes write */
95d1f3ed
JM
622 /* XXX: disable buffered write as it's not supported */
623 // pfl->cfi_table[0x2A] = 0x05;
624 pfl->cfi_table[0x2A] = 0x00;
29133e9a
FB
625 pfl->cfi_table[0x2B] = 0x00;
626 /* Number of erase block regions (uniform) */
627 pfl->cfi_table[0x2C] = 0x01;
628 /* Erase block region 1 */
368a354f
PC
629 pfl->cfi_table[0x2D] = pfl->nb_blocs - 1;
630 pfl->cfi_table[0x2E] = (pfl->nb_blocs - 1) >> 8;
631 pfl->cfi_table[0x2F] = pfl->sector_len >> 8;
632 pfl->cfi_table[0x30] = pfl->sector_len >> 16;
29133e9a 633
78556820
EI
634 /* Extended */
635 pfl->cfi_table[0x31] = 'P';
636 pfl->cfi_table[0x32] = 'R';
637 pfl->cfi_table[0x33] = 'I';
638
639 pfl->cfi_table[0x34] = '1';
640 pfl->cfi_table[0x35] = '0';
641
642 pfl->cfi_table[0x36] = 0x00;
643 pfl->cfi_table[0x37] = 0x00;
644 pfl->cfi_table[0x38] = 0x00;
645 pfl->cfi_table[0x39] = 0x00;
646
647 pfl->cfi_table[0x3a] = 0x00;
648
649 pfl->cfi_table[0x3b] = 0x00;
650 pfl->cfi_table[0x3c] = 0x00;
368a354f
PC
651}
652
653static Property pflash_cfi02_properties[] = {
16434065
MA
654 DEFINE_PROP_DRIVE("drive", PFlashCFI02, blk),
655 DEFINE_PROP_UINT32("num-blocks", PFlashCFI02, nb_blocs, 0),
656 DEFINE_PROP_UINT32("sector-length", PFlashCFI02, sector_len, 0),
657 DEFINE_PROP_UINT8("width", PFlashCFI02, width, 0),
658 DEFINE_PROP_UINT8("mappings", PFlashCFI02, mappings, 0),
659 DEFINE_PROP_UINT8("big-endian", PFlashCFI02, be, 0),
660 DEFINE_PROP_UINT16("id0", PFlashCFI02, ident0, 0),
661 DEFINE_PROP_UINT16("id1", PFlashCFI02, ident1, 0),
662 DEFINE_PROP_UINT16("id2", PFlashCFI02, ident2, 0),
663 DEFINE_PROP_UINT16("id3", PFlashCFI02, ident3, 0),
664 DEFINE_PROP_UINT16("unlock-addr0", PFlashCFI02, unlock_addr0, 0),
665 DEFINE_PROP_UINT16("unlock-addr1", PFlashCFI02, unlock_addr1, 0),
666 DEFINE_PROP_STRING("name", PFlashCFI02, name),
368a354f
PC
667 DEFINE_PROP_END_OF_LIST(),
668};
669
d80cf1eb
SC
670static void pflash_cfi02_unrealize(DeviceState *dev, Error **errp)
671{
e7b62741 672 PFlashCFI02 *pfl = PFLASH_CFI02(dev);
d80cf1eb
SC
673 timer_del(&pfl->timer);
674}
675
368a354f
PC
676static void pflash_cfi02_class_init(ObjectClass *klass, void *data)
677{
678 DeviceClass *dc = DEVICE_CLASS(klass);
368a354f 679
da3bd642 680 dc->realize = pflash_cfi02_realize;
d80cf1eb 681 dc->unrealize = pflash_cfi02_unrealize;
368a354f 682 dc->props = pflash_cfi02_properties;
df6f9318 683 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
368a354f
PC
684}
685
686static const TypeInfo pflash_cfi02_info = {
e7b62741 687 .name = TYPE_PFLASH_CFI02,
368a354f 688 .parent = TYPE_SYS_BUS_DEVICE,
16434065 689 .instance_size = sizeof(PFlashCFI02),
368a354f
PC
690 .class_init = pflash_cfi02_class_init,
691};
692
693static void pflash_cfi02_register_types(void)
694{
695 type_register_static(&pflash_cfi02_info);
696}
697
698type_init(pflash_cfi02_register_types)
699
16434065 700PFlashCFI02 *pflash_cfi02_register(hwaddr base,
940d5b13 701 const char *name,
16434065
MA
702 hwaddr size,
703 BlockBackend *blk,
ce14710f 704 uint32_t sector_len,
16434065
MA
705 int nb_mappings, int width,
706 uint16_t id0, uint16_t id1,
707 uint16_t id2, uint16_t id3,
708 uint16_t unlock_addr0,
709 uint16_t unlock_addr1,
710 int be)
368a354f 711{
e7b62741 712 DeviceState *dev = qdev_create(NULL, TYPE_PFLASH_CFI02);
368a354f 713
9b3d111a
MA
714 if (blk) {
715 qdev_prop_set_drive(dev, "drive", blk, &error_abort);
368a354f 716 }
ce14710f
MA
717 assert(size % sector_len == 0);
718 qdev_prop_set_uint32(dev, "num-blocks", size / sector_len);
368a354f
PC
719 qdev_prop_set_uint32(dev, "sector-length", sector_len);
720 qdev_prop_set_uint8(dev, "width", width);
721 qdev_prop_set_uint8(dev, "mappings", nb_mappings);
722 qdev_prop_set_uint8(dev, "big-endian", !!be);
723 qdev_prop_set_uint16(dev, "id0", id0);
724 qdev_prop_set_uint16(dev, "id1", id1);
725 qdev_prop_set_uint16(dev, "id2", id2);
726 qdev_prop_set_uint16(dev, "id3", id3);
727 qdev_prop_set_uint16(dev, "unlock-addr0", unlock_addr0);
728 qdev_prop_set_uint16(dev, "unlock-addr1", unlock_addr1);
729 qdev_prop_set_string(dev, "name", name);
730 qdev_init_nofail(dev);
731
3509c396 732 sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, base);
e7b62741 733 return PFLASH_CFI02(dev);
29133e9a 734}
This page took 1.010797 seconds and 4 git commands to generate.