]> git.proxmox.com Git - qemu.git/blame - hw/pflash_cfi02.c
Emulate address wrap in CFI02 chips mapping (Jan Kiszka).
[qemu.git] / hw / 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
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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 * - chip erase
29 * - unlock bypass command
30 * - CFI queries
31 *
32 * It does not support flash interleaving.
33 * It does not implement boot blocs with reduced size
34 * It does not implement software data protection as found in many real chips
35 * It does not implement erase suspend/resume commands
36 * It does not implement multiple sectors erase
37 */
38
87ecb68b
PB
39#include "hw.h"
40#include "flash.h"
41#include "qemu-timer.h"
42#include "block.h"
29133e9a
FB
43
44//#define PFLASH_DEBUG
45#ifdef PFLASH_DEBUG
46#define DPRINTF(fmt, args...) \
47do { \
29133e9a
FB
48 printf("PFLASH: " fmt , ##args); \
49} while (0)
50#else
51#define DPRINTF(fmt, args...) do { } while (0)
52#endif
53
54struct pflash_t {
55 BlockDriverState *bs;
71db710f
BS
56 target_phys_addr_t base;
57 uint32_t sector_len;
4fbd24ba
AZ
58 uint32_t chip_len;
59 int mappings;
29133e9a
FB
60 int width;
61 int wcycle; /* if 0, the flash is read normally */
62 int bypass;
63 int ro;
64 uint8_t cmd;
65 uint8_t status;
66 uint16_t ident[4];
6725070d 67 uint16_t unlock_addr[2];
29133e9a
FB
68 uint8_t cfi_len;
69 uint8_t cfi_table[0x52];
70 QEMUTimer *timer;
71 ram_addr_t off;
72 int fl_mem;
73 void *storage;
74};
75
4fbd24ba
AZ
76static void pflash_register_memory(pflash_t *pfl, int rom_mode)
77{
78 unsigned long phys_offset = pfl->fl_mem;
79 int i;
80
81 if (rom_mode)
82 phys_offset |= pfl->off | IO_MEM_ROMD;
83
84 for (i = 0; i < pfl->mappings; i++)
85 cpu_register_physical_memory(pfl->base + i * pfl->chip_len,
86 pfl->chip_len, phys_offset);
87}
88
29133e9a
FB
89static void pflash_timer (void *opaque)
90{
91 pflash_t *pfl = opaque;
92
93 DPRINTF("%s: command %02x done\n", __func__, pfl->cmd);
94 /* Reset flash */
95 pfl->status ^= 0x80;
96 if (pfl->bypass) {
97 pfl->wcycle = 2;
98 } else {
4fbd24ba 99 pflash_register_memory(pfl, 1);
29133e9a
FB
100 pfl->wcycle = 0;
101 }
102 pfl->cmd = 0;
103}
104
71db710f 105static uint32_t pflash_read (pflash_t *pfl, uint32_t offset, int width)
29133e9a 106{
71db710f 107 uint32_t boff;
29133e9a
FB
108 uint32_t ret;
109 uint8_t *p;
110
e96efcfc 111 DPRINTF("%s: offset " TARGET_FMT_lx "\n", __func__, offset);
29133e9a
FB
112 ret = -1;
113 offset -= pfl->base;
4fbd24ba 114 offset &= pfl->chip_len - 1;
29133e9a
FB
115 boff = offset & 0xFF;
116 if (pfl->width == 2)
117 boff = boff >> 1;
118 else if (pfl->width == 4)
119 boff = boff >> 2;
120 switch (pfl->cmd) {
121 default:
122 /* This should never happen : reset state & treat it as a read*/
123 DPRINTF("%s: unknown command state: %x\n", __func__, pfl->cmd);
124 pfl->wcycle = 0;
125 pfl->cmd = 0;
126 case 0x80:
127 /* We accept reads during second unlock sequence... */
128 case 0x00:
129 flash_read:
130 /* Flash area read */
131 p = pfl->storage;
132 switch (width) {
133 case 1:
134 ret = p[offset];
135// DPRINTF("%s: data offset %08x %02x\n", __func__, offset, ret);
136 break;
137 case 2:
138#if defined(TARGET_WORDS_BIGENDIAN)
139 ret = p[offset] << 8;
140 ret |= p[offset + 1];
141#else
142 ret = p[offset];
143 ret |= p[offset + 1] << 8;
144#endif
145// DPRINTF("%s: data offset %08x %04x\n", __func__, offset, ret);
146 break;
147 case 4:
148#if defined(TARGET_WORDS_BIGENDIAN)
149 ret = p[offset] << 24;
150 ret |= p[offset + 1] << 16;
151 ret |= p[offset + 2] << 8;
152 ret |= p[offset + 3];
153#else
154 ret = p[offset];
155 ret |= p[offset + 1] << 8;
29133e9a
FB
156 ret |= p[offset + 2] << 16;
157 ret |= p[offset + 3] << 24;
158#endif
159// DPRINTF("%s: data offset %08x %08x\n", __func__, offset, ret);
160 break;
161 }
162 break;
163 case 0x90:
164 /* flash ID read */
165 switch (boff) {
166 case 0x00:
167 case 0x01:
168 ret = pfl->ident[boff & 0x01];
169 break;
170 case 0x02:
171 ret = 0x00; /* Pretend all sectors are unprotected */
172 break;
173 case 0x0E:
174 case 0x0F:
175 if (pfl->ident[2 + (boff & 0x01)] == (uint8_t)-1)
176 goto flash_read;
177 ret = pfl->ident[2 + (boff & 0x01)];
178 break;
179 default:
180 goto flash_read;
181 }
e96efcfc 182 DPRINTF("%s: ID " TARGET_FMT_ld " %x\n", __func__, boff, ret);
29133e9a
FB
183 break;
184 case 0xA0:
185 case 0x10:
186 case 0x30:
187 /* Status register read */
188 ret = pfl->status;
189 DPRINTF("%s: status %x\n", __func__, ret);
190 /* Toggle bit 6 */
191 pfl->status ^= 0x40;
192 break;
193 case 0x98:
194 /* CFI query mode */
195 if (boff > pfl->cfi_len)
196 ret = 0;
197 else
198 ret = pfl->cfi_table[boff];
199 break;
200 }
201
202 return ret;
203}
204
205/* update flash content on disk */
5fafdf24 206static void pflash_update(pflash_t *pfl, int offset,
29133e9a
FB
207 int size)
208{
209 int offset_end;
210 if (pfl->bs) {
211 offset_end = offset + size;
212 /* round to sectors */
213 offset = offset >> 9;
214 offset_end = (offset_end + 511) >> 9;
5fafdf24 215 bdrv_write(pfl->bs, offset, pfl->storage + (offset << 9),
29133e9a
FB
216 offset_end - offset);
217 }
218}
219
71db710f 220static void pflash_write (pflash_t *pfl, uint32_t offset, uint32_t value,
29133e9a
FB
221 int width)
222{
71db710f 223 uint32_t boff;
29133e9a
FB
224 uint8_t *p;
225 uint8_t cmd;
226
227 /* WARNING: when the memory area is in ROMD mode, the offset is a
228 ram offset, not a physical address */
95d1f3ed
JM
229 cmd = value;
230 if (pfl->cmd != 0xA0 && cmd == 0xF0) {
231#if 0
232 DPRINTF("%s: flash reset asked (%02x %02x)\n",
233 __func__, pfl->cmd, cmd);
234#endif
235 goto reset_flash;
236 }
237 DPRINTF("%s: offset " TARGET_FMT_lx " %08x %d %d\n", __func__,
238 offset, value, width, pfl->wcycle);
29133e9a 239 if (pfl->wcycle == 0)
71db710f 240 offset -= (uint32_t)(long)pfl->storage;
29133e9a
FB
241 else
242 offset -= pfl->base;
4fbd24ba 243 offset &= pfl->chip_len - 1;
3b46e624 244
e96efcfc
JM
245 DPRINTF("%s: offset " TARGET_FMT_lx " %08x %d\n", __func__,
246 offset, value, width);
29133e9a
FB
247 boff = offset & (pfl->sector_len - 1);
248 if (pfl->width == 2)
249 boff = boff >> 1;
250 else if (pfl->width == 4)
251 boff = boff >> 2;
252 switch (pfl->wcycle) {
253 case 0:
4fbd24ba
AZ
254 /* Set the device in I/O access mode */
255 pflash_register_memory(pfl, 0);
29133e9a
FB
256 /* We're in read mode */
257 check_unlock0:
258 if (boff == 0x55 && cmd == 0x98) {
259 enter_CFI_mode:
260 /* Enter CFI query mode */
261 pfl->wcycle = 7;
262 pfl->cmd = 0x98;
263 return;
264 }
6725070d 265 if (boff != pfl->unlock_addr[0] || cmd != 0xAA) {
e96efcfc 266 DPRINTF("%s: unlock0 failed " TARGET_FMT_lx " %02x %04x\n",
6725070d 267 __func__, boff, cmd, pfl->unlock_addr[0]);
29133e9a
FB
268 goto reset_flash;
269 }
270 DPRINTF("%s: unlock sequence started\n", __func__);
271 break;
272 case 1:
273 /* We started an unlock sequence */
274 check_unlock1:
6725070d 275 if (boff != pfl->unlock_addr[1] || cmd != 0x55) {
e96efcfc
JM
276 DPRINTF("%s: unlock1 failed " TARGET_FMT_lx " %02x\n", __func__,
277 boff, cmd);
29133e9a
FB
278 goto reset_flash;
279 }
280 DPRINTF("%s: unlock sequence done\n", __func__);
281 break;
282 case 2:
283 /* We finished an unlock sequence */
6725070d 284 if (!pfl->bypass && boff != pfl->unlock_addr[0]) {
e96efcfc
JM
285 DPRINTF("%s: command failed " TARGET_FMT_lx " %02x\n", __func__,
286 boff, cmd);
29133e9a
FB
287 goto reset_flash;
288 }
289 switch (cmd) {
290 case 0x20:
291 pfl->bypass = 1;
292 goto do_bypass;
293 case 0x80:
294 case 0x90:
295 case 0xA0:
296 pfl->cmd = cmd;
297 DPRINTF("%s: starting command %02x\n", __func__, cmd);
298 break;
299 default:
300 DPRINTF("%s: unknown command %02x\n", __func__, cmd);
301 goto reset_flash;
302 }
303 break;
304 case 3:
305 switch (pfl->cmd) {
306 case 0x80:
307 /* We need another unlock sequence */
308 goto check_unlock0;
309 case 0xA0:
e96efcfc 310 DPRINTF("%s: write data offset " TARGET_FMT_lx " %08x %d\n",
29133e9a
FB
311 __func__, offset, value, width);
312 p = pfl->storage;
313 switch (width) {
314 case 1:
315 p[offset] &= value;
316 pflash_update(pfl, offset, 1);
317 break;
318 case 2:
319#if defined(TARGET_WORDS_BIGENDIAN)
320 p[offset] &= value >> 8;
321 p[offset + 1] &= value;
322#else
323 p[offset] &= value;
324 p[offset + 1] &= value >> 8;
325#endif
326 pflash_update(pfl, offset, 2);
327 break;
328 case 4:
329#if defined(TARGET_WORDS_BIGENDIAN)
330 p[offset] &= value >> 24;
331 p[offset + 1] &= value >> 16;
332 p[offset + 2] &= value >> 8;
333 p[offset + 3] &= value;
334#else
335 p[offset] &= value;
336 p[offset + 1] &= value >> 8;
337 p[offset + 2] &= value >> 16;
338 p[offset + 3] &= value >> 24;
339#endif
340 pflash_update(pfl, offset, 4);
341 break;
342 }
343 pfl->status = 0x00 | ~(value & 0x80);
344 /* Let's pretend write is immediate */
345 if (pfl->bypass)
346 goto do_bypass;
347 goto reset_flash;
348 case 0x90:
349 if (pfl->bypass && cmd == 0x00) {
350 /* Unlock bypass reset */
351 goto reset_flash;
352 }
353 /* We can enter CFI query mode from autoselect mode */
354 if (boff == 0x55 && cmd == 0x98)
355 goto enter_CFI_mode;
356 /* No break here */
357 default:
358 DPRINTF("%s: invalid write for command %02x\n",
359 __func__, pfl->cmd);
360 goto reset_flash;
361 }
362 case 4:
363 switch (pfl->cmd) {
364 case 0xA0:
365 /* Ignore writes while flash data write is occuring */
366 /* As we suppose write is immediate, this should never happen */
367 return;
368 case 0x80:
369 goto check_unlock1;
370 default:
371 /* Should never happen */
372 DPRINTF("%s: invalid command state %02x (wc 4)\n",
373 __func__, pfl->cmd);
374 goto reset_flash;
375 }
376 break;
377 case 5:
378 switch (cmd) {
379 case 0x10:
6725070d 380 if (boff != pfl->unlock_addr[0]) {
e96efcfc 381 DPRINTF("%s: chip erase: invalid address " TARGET_FMT_lx "\n",
29133e9a
FB
382 __func__, offset);
383 goto reset_flash;
384 }
385 /* Chip erase */
386 DPRINTF("%s: start chip erase\n", __func__);
4fbd24ba 387 memset(pfl->storage, 0xFF, pfl->chip_len);
29133e9a 388 pfl->status = 0x00;
4fbd24ba 389 pflash_update(pfl, 0, pfl->chip_len);
29133e9a 390 /* Let's wait 5 seconds before chip erase is done */
5fafdf24 391 qemu_mod_timer(pfl->timer,
29133e9a
FB
392 qemu_get_clock(vm_clock) + (ticks_per_sec * 5));
393 break;
394 case 0x30:
395 /* Sector erase */
396 p = pfl->storage;
397 offset &= ~(pfl->sector_len - 1);
e96efcfc
JM
398 DPRINTF("%s: start sector erase at " TARGET_FMT_lx "\n", __func__,
399 offset);
29133e9a
FB
400 memset(p + offset, 0xFF, pfl->sector_len);
401 pflash_update(pfl, offset, pfl->sector_len);
402 pfl->status = 0x00;
403 /* Let's wait 1/2 second before sector erase is done */
5fafdf24 404 qemu_mod_timer(pfl->timer,
29133e9a
FB
405 qemu_get_clock(vm_clock) + (ticks_per_sec / 2));
406 break;
407 default:
408 DPRINTF("%s: invalid command %02x (wc 5)\n", __func__, cmd);
409 goto reset_flash;
410 }
411 pfl->cmd = cmd;
412 break;
413 case 6:
414 switch (pfl->cmd) {
415 case 0x10:
416 /* Ignore writes during chip erase */
417 return;
418 case 0x30:
419 /* Ignore writes during sector erase */
420 return;
421 default:
422 /* Should never happen */
423 DPRINTF("%s: invalid command state %02x (wc 6)\n",
424 __func__, pfl->cmd);
425 goto reset_flash;
426 }
427 break;
428 case 7: /* Special value for CFI queries */
429 DPRINTF("%s: invalid write in CFI query mode\n", __func__);
430 goto reset_flash;
431 default:
432 /* Should never happen */
433 DPRINTF("%s: invalid write state (wc 7)\n", __func__);
434 goto reset_flash;
435 }
436 pfl->wcycle++;
437
438 return;
439
440 /* Reset flash */
441 reset_flash:
4fbd24ba 442 pflash_register_memory(pfl, 1);
29133e9a
FB
443 pfl->bypass = 0;
444 pfl->wcycle = 0;
445 pfl->cmd = 0;
446 return;
447
448 do_bypass:
449 pfl->wcycle = 2;
450 pfl->cmd = 0;
451 return;
452}
453
454
455static uint32_t pflash_readb (void *opaque, target_phys_addr_t addr)
456{
457 return pflash_read(opaque, addr, 1);
458}
459
460static uint32_t pflash_readw (void *opaque, target_phys_addr_t addr)
461{
462 pflash_t *pfl = opaque;
463
464 return pflash_read(pfl, addr, 2);
465}
466
467static uint32_t pflash_readl (void *opaque, target_phys_addr_t addr)
468{
469 pflash_t *pfl = opaque;
470
471 return pflash_read(pfl, addr, 4);
472}
473
474static void pflash_writeb (void *opaque, target_phys_addr_t addr,
475 uint32_t value)
476{
477 pflash_write(opaque, addr, value, 1);
478}
479
480static void pflash_writew (void *opaque, target_phys_addr_t addr,
481 uint32_t value)
482{
483 pflash_t *pfl = opaque;
484
485 pflash_write(pfl, addr, value, 2);
486}
487
488static void pflash_writel (void *opaque, target_phys_addr_t addr,
489 uint32_t value)
490{
491 pflash_t *pfl = opaque;
492
493 pflash_write(pfl, addr, value, 4);
494}
495
496static CPUWriteMemoryFunc *pflash_write_ops[] = {
497 &pflash_writeb,
498 &pflash_writew,
499 &pflash_writel,
500};
501
502static CPUReadMemoryFunc *pflash_read_ops[] = {
503 &pflash_readb,
504 &pflash_readw,
505 &pflash_readl,
506};
507
508/* Count trailing zeroes of a 32 bits quantity */
509static int ctz32 (uint32_t n)
510{
511 int ret;
512
513 ret = 0;
514 if (!(n & 0xFFFF)) {
515 ret += 16;
516 n = n >> 16;
517 }
518 if (!(n & 0xFF)) {
519 ret += 8;
520 n = n >> 8;
521 }
522 if (!(n & 0xF)) {
523 ret += 4;
524 n = n >> 4;
525 }
526 if (!(n & 0x3)) {
527 ret += 2;
528 n = n >> 2;
529 }
530 if (!(n & 0x1)) {
531 ret++;
532 n = n >> 1;
533 }
534#if 0 /* This is not necessary as n is never 0 */
535 if (!n)
536 ret++;
537#endif
538
539 return ret;
540}
541
88eeee0a 542pflash_t *pflash_cfi02_register(target_phys_addr_t base, ram_addr_t off,
cf6d9118 543 BlockDriverState *bs, uint32_t sector_len,
4fbd24ba 544 int nb_blocs, int nb_mappings, int width,
88eeee0a 545 uint16_t id0, uint16_t id1,
6725070d
AZ
546 uint16_t id2, uint16_t id3,
547 uint16_t unlock_addr0, uint16_t unlock_addr1)
29133e9a
FB
548{
549 pflash_t *pfl;
4fbd24ba 550 int32_t chip_len;
29133e9a 551
4fbd24ba 552 chip_len = sector_len * nb_blocs;
29133e9a 553 /* XXX: to be fixed */
95d1f3ed 554#if 0
29133e9a
FB
555 if (total_len != (8 * 1024 * 1024) && total_len != (16 * 1024 * 1024) &&
556 total_len != (32 * 1024 * 1024) && total_len != (64 * 1024 * 1024))
557 return NULL;
95d1f3ed 558#endif
29133e9a
FB
559 pfl = qemu_mallocz(sizeof(pflash_t));
560 if (pfl == NULL)
561 return NULL;
562 pfl->storage = phys_ram_base + off;
95d1f3ed
JM
563 pfl->fl_mem = cpu_register_io_memory(0, pflash_read_ops, pflash_write_ops,
564 pfl);
29133e9a 565 pfl->off = off;
4fbd24ba
AZ
566 pfl->base = base;
567 pfl->chip_len = chip_len;
568 pfl->mappings = nb_mappings;
569 pflash_register_memory(pfl, 1);
29133e9a
FB
570 pfl->bs = bs;
571 if (pfl->bs) {
572 /* read the initial flash content */
4fbd24ba 573 bdrv_read(pfl->bs, 0, pfl->storage, chip_len >> 9);
29133e9a
FB
574 }
575#if 0 /* XXX: there should be a bit to set up read-only,
576 * the same way the hardware does (with WP pin).
577 */
578 pfl->ro = 1;
579#else
580 pfl->ro = 0;
581#endif
582 pfl->timer = qemu_new_timer(vm_clock, pflash_timer, pfl);
29133e9a 583 pfl->sector_len = sector_len;
29133e9a
FB
584 pfl->width = width;
585 pfl->wcycle = 0;
586 pfl->cmd = 0;
587 pfl->status = 0;
588 pfl->ident[0] = id0;
589 pfl->ident[1] = id1;
590 pfl->ident[2] = id2;
591 pfl->ident[3] = id3;
6725070d
AZ
592 pfl->unlock_addr[0] = unlock_addr0;
593 pfl->unlock_addr[1] = unlock_addr1;
29133e9a
FB
594 /* Hardcoded CFI table (mostly from SG29 Spansion flash) */
595 pfl->cfi_len = 0x52;
596 /* Standard "QRY" string */
597 pfl->cfi_table[0x10] = 'Q';
598 pfl->cfi_table[0x11] = 'R';
599 pfl->cfi_table[0x12] = 'Y';
600 /* Command set (AMD/Fujitsu) */
601 pfl->cfi_table[0x13] = 0x02;
602 pfl->cfi_table[0x14] = 0x00;
603 /* Primary extended table address (none) */
604 pfl->cfi_table[0x15] = 0x00;
605 pfl->cfi_table[0x16] = 0x00;
606 /* Alternate command set (none) */
607 pfl->cfi_table[0x17] = 0x00;
608 pfl->cfi_table[0x18] = 0x00;
609 /* Alternate extended table (none) */
610 pfl->cfi_table[0x19] = 0x00;
611 pfl->cfi_table[0x1A] = 0x00;
612 /* Vcc min */
613 pfl->cfi_table[0x1B] = 0x27;
614 /* Vcc max */
615 pfl->cfi_table[0x1C] = 0x36;
616 /* Vpp min (no Vpp pin) */
617 pfl->cfi_table[0x1D] = 0x00;
618 /* Vpp max (no Vpp pin) */
619 pfl->cfi_table[0x1E] = 0x00;
620 /* Reserved */
621 pfl->cfi_table[0x1F] = 0x07;
622