2 * Flash NAND memory emulation. Based on "16M x 8 Bit NAND Flash
3 * Memory" datasheet for the KM29U128AT / K9F2808U0A chips from
6 * Copyright (c) 2006 Openedhand Ltd.
7 * Written by Andrzej Zaborowski <balrog@zabor.org>
9 * Support for additional features based on "MT29F2G16ABCWP 2Gx16"
10 * datasheet from Micron Technology and "NAND02G-B2C" datasheet
11 * from ST Microelectronics.
13 * This code is licensed under the GNU GPL v2.
15 * Contributions after 2012-01-13 are licensed under the terms of the
16 * GNU GPL, version 2 or (at your option) any later version.
23 # include "blockdev.h"
25 #include "qemu-error.h"
27 # define NAND_CMD_READ0 0x00
28 # define NAND_CMD_READ1 0x01
29 # define NAND_CMD_READ2 0x50
30 # define NAND_CMD_LPREAD2 0x30
31 # define NAND_CMD_NOSERIALREAD2 0x35
32 # define NAND_CMD_RANDOMREAD1 0x05
33 # define NAND_CMD_RANDOMREAD2 0xe0
34 # define NAND_CMD_READID 0x90
35 # define NAND_CMD_RESET 0xff
36 # define NAND_CMD_PAGEPROGRAM1 0x80
37 # define NAND_CMD_PAGEPROGRAM2 0x10
38 # define NAND_CMD_CACHEPROGRAM2 0x15
39 # define NAND_CMD_BLOCKERASE1 0x60
40 # define NAND_CMD_BLOCKERASE2 0xd0
41 # define NAND_CMD_READSTATUS 0x70
42 # define NAND_CMD_COPYBACKPRG1 0x85
44 # define NAND_IOSTATUS_ERROR (1 << 0)
45 # define NAND_IOSTATUS_PLANE0 (1 << 1)
46 # define NAND_IOSTATUS_PLANE1 (1 << 2)
47 # define NAND_IOSTATUS_PLANE2 (1 << 3)
48 # define NAND_IOSTATUS_PLANE3 (1 << 4)
49 # define NAND_IOSTATUS_BUSY (1 << 6)
50 # define NAND_IOSTATUS_UNPROTCT (1 << 7)
52 # define MAX_PAGE 0x800
55 typedef struct NANDFlashState NANDFlashState
;
56 struct NANDFlashState
{
58 uint8_t manf_id
, chip_id
;
59 uint8_t buswidth
; /* in BYTES */
61 int page_shift
, oob_shift
, erase_shift
, addr_shift
;
63 BlockDriverState
*bdrv
;
66 uint8_t cle
, ale
, ce
, wp
, gnd
;
68 uint8_t io
[MAX_PAGE
+ MAX_OOB
+ 0x400];
78 void (*blk_write
)(NANDFlashState
*s
);
79 void (*blk_erase
)(NANDFlashState
*s
);
80 void (*blk_load
)(NANDFlashState
*s
, uint64_t addr
, int offset
);
82 uint32_t ioaddr_vmstate
;
85 static void mem_and(uint8_t *dest
, const uint8_t *src
, size_t n
)
87 /* Like memcpy() but we logical-AND the data into the destination */
89 for (i
= 0; i
< n
; i
++) {
94 # define NAND_NO_AUTOINCR 0x00000001
95 # define NAND_BUSWIDTH_16 0x00000002
96 # define NAND_NO_PADDING 0x00000004
97 # define NAND_CACHEPRG 0x00000008
98 # define NAND_COPYBACK 0x00000010
99 # define NAND_IS_AND 0x00000020
100 # define NAND_4PAGE_ARRAY 0x00000040
101 # define NAND_NO_READRDY 0x00000100
102 # define NAND_SAMSUNG_LP (NAND_NO_PADDING | NAND_COPYBACK)
106 # define PAGE(addr) ((addr) >> ADDR_SHIFT)
107 # define PAGE_START(page) (PAGE(page) * (PAGE_SIZE + OOB_SIZE))
108 # define PAGE_MASK ((1 << ADDR_SHIFT) - 1)
109 # define OOB_SHIFT (PAGE_SHIFT - 5)
110 # define OOB_SIZE (1 << OOB_SHIFT)
111 # define SECTOR(addr) ((addr) >> (9 + ADDR_SHIFT - PAGE_SHIFT))
112 # define SECTOR_OFFSET(addr) ((addr) & ((511 >> PAGE_SHIFT) << 8))
114 # define PAGE_SIZE 256
115 # define PAGE_SHIFT 8
116 # define PAGE_SECTORS 1
117 # define ADDR_SHIFT 8
119 # define PAGE_SIZE 512
120 # define PAGE_SHIFT 9
121 # define PAGE_SECTORS 1
122 # define ADDR_SHIFT 8
124 # define PAGE_SIZE 2048
125 # define PAGE_SHIFT 11
126 # define PAGE_SECTORS 4
127 # define ADDR_SHIFT 16
130 /* Information based on Linux drivers/mtd/nand/nand_ids.c */
131 static const struct {
137 } nand_flash_ids
[0x100] = {
138 [0 ... 0xff] = { 0 },
140 [0x6e] = { 1, 8, 8, 4, 0 },
141 [0x64] = { 2, 8, 8, 4, 0 },
142 [0x6b] = { 4, 8, 9, 4, 0 },
143 [0xe8] = { 1, 8, 8, 4, 0 },
144 [0xec] = { 1, 8, 8, 4, 0 },
145 [0xea] = { 2, 8, 8, 4, 0 },
146 [0xd5] = { 4, 8, 9, 4, 0 },
147 [0xe3] = { 4, 8, 9, 4, 0 },
148 [0xe5] = { 4, 8, 9, 4, 0 },
149 [0xd6] = { 8, 8, 9, 4, 0 },
151 [0x39] = { 8, 8, 9, 4, 0 },
152 [0xe6] = { 8, 8, 9, 4, 0 },
153 [0x49] = { 8, 16, 9, 4, NAND_BUSWIDTH_16
},
154 [0x59] = { 8, 16, 9, 4, NAND_BUSWIDTH_16
},
156 [0x33] = { 16, 8, 9, 5, 0 },
157 [0x73] = { 16, 8, 9, 5, 0 },
158 [0x43] = { 16, 16, 9, 5, NAND_BUSWIDTH_16
},
159 [0x53] = { 16, 16, 9, 5, NAND_BUSWIDTH_16
},
161 [0x35] = { 32, 8, 9, 5, 0 },
162 [0x75] = { 32, 8, 9, 5, 0 },
163 [0x45] = { 32, 16, 9, 5, NAND_BUSWIDTH_16
},
164 [0x55] = { 32, 16, 9, 5, NAND_BUSWIDTH_16
},
166 [0x36] = { 64, 8, 9, 5, 0 },
167 [0x76] = { 64, 8, 9, 5, 0 },
168 [0x46] = { 64, 16, 9, 5, NAND_BUSWIDTH_16
},
169 [0x56] = { 64, 16, 9, 5, NAND_BUSWIDTH_16
},
171 [0x78] = { 128, 8, 9, 5, 0 },
172 [0x39] = { 128, 8, 9, 5, 0 },
173 [0x79] = { 128, 8, 9, 5, 0 },
174 [0x72] = { 128, 16, 9, 5, NAND_BUSWIDTH_16
},
175 [0x49] = { 128, 16, 9, 5, NAND_BUSWIDTH_16
},
176 [0x74] = { 128, 16, 9, 5, NAND_BUSWIDTH_16
},
177 [0x59] = { 128, 16, 9, 5, NAND_BUSWIDTH_16
},
179 [0x71] = { 256, 8, 9, 5, 0 },
182 * These are the new chips with large page size. The pagesize and the
183 * erasesize is determined from the extended id bytes
185 # define LP_OPTIONS (NAND_SAMSUNG_LP | NAND_NO_READRDY | NAND_NO_AUTOINCR)
186 # define LP_OPTIONS16 (LP_OPTIONS | NAND_BUSWIDTH_16)
189 [0xa2] = { 64, 8, 0, 0, LP_OPTIONS
},
190 [0xf2] = { 64, 8, 0, 0, LP_OPTIONS
},
191 [0xb2] = { 64, 16, 0, 0, LP_OPTIONS16
},
192 [0xc2] = { 64, 16, 0, 0, LP_OPTIONS16
},
195 [0xa1] = { 128, 8, 0, 0, LP_OPTIONS
},
196 [0xf1] = { 128, 8, 0, 0, LP_OPTIONS
},
197 [0xb1] = { 128, 16, 0, 0, LP_OPTIONS16
},
198 [0xc1] = { 128, 16, 0, 0, LP_OPTIONS16
},
201 [0xaa] = { 256, 8, 0, 0, LP_OPTIONS
},
202 [0xda] = { 256, 8, 0, 0, LP_OPTIONS
},
203 [0xba] = { 256, 16, 0, 0, LP_OPTIONS16
},
204 [0xca] = { 256, 16, 0, 0, LP_OPTIONS16
},
207 [0xac] = { 512, 8, 0, 0, LP_OPTIONS
},
208 [0xdc] = { 512, 8, 0, 0, LP_OPTIONS
},
209 [0xbc] = { 512, 16, 0, 0, LP_OPTIONS16
},
210 [0xcc] = { 512, 16, 0, 0, LP_OPTIONS16
},
213 [0xa3] = { 1024, 8, 0, 0, LP_OPTIONS
},
214 [0xd3] = { 1024, 8, 0, 0, LP_OPTIONS
},
215 [0xb3] = { 1024, 16, 0, 0, LP_OPTIONS16
},
216 [0xc3] = { 1024, 16, 0, 0, LP_OPTIONS16
},
219 [0xa5] = { 2048, 8, 0, 0, LP_OPTIONS
},
220 [0xd5] = { 2048, 8, 0, 0, LP_OPTIONS
},
221 [0xb5] = { 2048, 16, 0, 0, LP_OPTIONS16
},
222 [0xc5] = { 2048, 16, 0, 0, LP_OPTIONS16
},
225 static void nand_reset(DeviceState
*dev
)
227 NANDFlashState
*s
= FROM_SYSBUS(NANDFlashState
, sysbus_from_qdev(dev
));
228 s
->cmd
= NAND_CMD_READ0
;
233 s
->status
&= NAND_IOSTATUS_UNPROTCT
;
236 static inline void nand_pushio_byte(NANDFlashState
*s
, uint8_t value
)
238 s
->ioaddr
[s
->iolen
++] = value
;
239 for (value
= s
->buswidth
; --value
;) {
240 s
->ioaddr
[s
->iolen
++] = 0;
244 static void nand_command(NANDFlashState
*s
)
252 case NAND_CMD_READID
:
255 nand_pushio_byte(s
, s
->manf_id
);
256 nand_pushio_byte(s
, s
->chip_id
);
257 nand_pushio_byte(s
, 'Q'); /* Don't-care byte (often 0xa5) */
258 if (nand_flash_ids
[s
->chip_id
].options
& NAND_SAMSUNG_LP
) {
259 /* Page Size, Block Size, Spare Size; bit 6 indicates
260 * 8 vs 16 bit width NAND.
262 nand_pushio_byte(s
, (s
->buswidth
== 2) ? 0x55 : 0x15);
264 nand_pushio_byte(s
, 0xc0); /* Multi-plane */
268 case NAND_CMD_RANDOMREAD2
:
269 case NAND_CMD_NOSERIALREAD2
:
270 if (!(nand_flash_ids
[s
->chip_id
].options
& NAND_SAMSUNG_LP
))
272 offset
= s
->addr
& ((1 << s
->addr_shift
) - 1);
273 s
->blk_load(s
, s
->addr
, offset
);
275 s
->iolen
= (1 << s
->page_shift
) - offset
;
277 s
->iolen
= (1 << s
->page_shift
) + (1 << s
->oob_shift
) - offset
;
281 nand_reset(&s
->busdev
.qdev
);
284 case NAND_CMD_PAGEPROGRAM1
:
289 case NAND_CMD_PAGEPROGRAM2
:
295 case NAND_CMD_BLOCKERASE1
:
298 case NAND_CMD_BLOCKERASE2
:
299 if (nand_flash_ids
[s
->chip_id
].options
& NAND_SAMSUNG_LP
)
309 case NAND_CMD_READSTATUS
:
312 nand_pushio_byte(s
, s
->status
);
316 printf("%s: Unknown NAND command 0x%02x\n", __FUNCTION__
, s
->cmd
);
320 static void nand_pre_save(void *opaque
)
322 NANDFlashState
*s
= opaque
;
324 s
->ioaddr_vmstate
= s
->ioaddr
- s
->io
;
327 static int nand_post_load(void *opaque
, int version_id
)
329 NANDFlashState
*s
= opaque
;
331 if (s
->ioaddr_vmstate
> sizeof(s
->io
)) {
334 s
->ioaddr
= s
->io
+ s
->ioaddr_vmstate
;
339 static const VMStateDescription vmstate_nand
= {
342 .minimum_version_id
= 1,
343 .minimum_version_id_old
= 1,
344 .pre_save
= nand_pre_save
,
345 .post_load
= nand_post_load
,
346 .fields
= (VMStateField
[]) {
347 VMSTATE_UINT8(cle
, NANDFlashState
),
348 VMSTATE_UINT8(ale
, NANDFlashState
),
349 VMSTATE_UINT8(ce
, NANDFlashState
),
350 VMSTATE_UINT8(wp
, NANDFlashState
),
351 VMSTATE_UINT8(gnd
, NANDFlashState
),
352 VMSTATE_BUFFER(io
, NANDFlashState
),
353 VMSTATE_UINT32(ioaddr_vmstate
, NANDFlashState
),
354 VMSTATE_INT32(iolen
, NANDFlashState
),
355 VMSTATE_UINT32(cmd
, NANDFlashState
),
356 VMSTATE_UINT64(addr
, NANDFlashState
),
357 VMSTATE_INT32(addrlen
, NANDFlashState
),
358 VMSTATE_INT32(status
, NANDFlashState
),
359 VMSTATE_INT32(offset
, NANDFlashState
),
360 /* XXX: do we want to save s->storage too? */
361 VMSTATE_END_OF_LIST()
365 static int nand_device_init(SysBusDevice
*dev
)
368 NANDFlashState
*s
= FROM_SYSBUS(NANDFlashState
, dev
);
370 s
->buswidth
= nand_flash_ids
[s
->chip_id
].width
>> 3;
371 s
->size
= nand_flash_ids
[s
->chip_id
].size
<< 20;
372 if (nand_flash_ids
[s
->chip_id
].options
& NAND_SAMSUNG_LP
) {
376 s
->page_shift
= nand_flash_ids
[s
->chip_id
].page_shift
;
377 s
->erase_shift
= nand_flash_ids
[s
->chip_id
].erase_shift
;
380 switch (1 << s
->page_shift
) {
391 error_report("Unsupported NAND block size");
395 pagesize
= 1 << s
->oob_shift
;
398 if (bdrv_is_read_only(s
->bdrv
)) {
399 error_report("Can't use a read-only drive");
402 if (bdrv_getlength(s
->bdrv
) >=
403 (s
->pages
<< s
->page_shift
) + (s
->pages
<< s
->oob_shift
)) {
408 pagesize
+= 1 << s
->page_shift
;
411 s
->storage
= (uint8_t *) memset(g_malloc(s
->pages
* pagesize
),
412 0xff, s
->pages
* pagesize
);
414 /* Give s->ioaddr a sane value in case we save state before it is used. */
420 static Property nand_properties
[] = {
421 DEFINE_PROP_UINT8("manufacturer_id", NANDFlashState
, manf_id
, 0),
422 DEFINE_PROP_UINT8("chip_id", NANDFlashState
, chip_id
, 0),
423 DEFINE_PROP_DRIVE("drive", NANDFlashState
, bdrv
),
424 DEFINE_PROP_END_OF_LIST(),
427 static void nand_class_init(ObjectClass
*klass
, void *data
)
429 DeviceClass
*dc
= DEVICE_CLASS(klass
);
430 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
432 k
->init
= nand_device_init
;
433 dc
->reset
= nand_reset
;
434 dc
->vmsd
= &vmstate_nand
;
435 dc
->props
= nand_properties
;
438 static TypeInfo nand_info
= {
440 .parent
= TYPE_SYS_BUS_DEVICE
,
441 .instance_size
= sizeof(NANDFlashState
),
442 .class_init
= nand_class_init
,
445 static void nand_register_types(void)
447 type_register_static(&nand_info
);
451 * Chip inputs are CLE, ALE, CE, WP, GND and eight I/O pins. Chip
452 * outputs are R/B and eight I/O pins.
454 * CE, WP and R/B are active low.
456 void nand_setpins(DeviceState
*dev
, uint8_t cle
, uint8_t ale
,
457 uint8_t ce
, uint8_t wp
, uint8_t gnd
)
459 NANDFlashState
*s
= (NANDFlashState
*) dev
;
466 s
->status
|= NAND_IOSTATUS_UNPROTCT
;
468 s
->status
&= ~NAND_IOSTATUS_UNPROTCT
;
471 void nand_getpins(DeviceState
*dev
, int *rb
)
476 void nand_setio(DeviceState
*dev
, uint32_t value
)
479 NANDFlashState
*s
= (NANDFlashState
*) dev
;
480 if (!s
->ce
&& s
->cle
) {
481 if (nand_flash_ids
[s
->chip_id
].options
& NAND_SAMSUNG_LP
) {
482 if (s
->cmd
== NAND_CMD_READ0
&& value
== NAND_CMD_LPREAD2
)
484 if (value
== NAND_CMD_RANDOMREAD1
) {
485 s
->addr
&= ~((1 << s
->addr_shift
) - 1);
490 if (value
== NAND_CMD_READ0
)
492 else if (value
== NAND_CMD_READ1
) {
494 value
= NAND_CMD_READ0
;
496 else if (value
== NAND_CMD_READ2
) {
497 s
->offset
= 1 << s
->page_shift
;
498 value
= NAND_CMD_READ0
;
503 if (s
->cmd
== NAND_CMD_READSTATUS
||
504 s
->cmd
== NAND_CMD_PAGEPROGRAM2
||
505 s
->cmd
== NAND_CMD_BLOCKERASE1
||
506 s
->cmd
== NAND_CMD_BLOCKERASE2
||
507 s
->cmd
== NAND_CMD_NOSERIALREAD2
||
508 s
->cmd
== NAND_CMD_RANDOMREAD2
||
509 s
->cmd
== NAND_CMD_RESET
)
512 if (s
->cmd
!= NAND_CMD_RANDOMREAD2
) {
518 unsigned int shift
= s
->addrlen
* 8;
519 unsigned int mask
= ~(0xff << shift
);
520 unsigned int v
= value
<< shift
;
522 s
->addr
= (s
->addr
& mask
) | v
;
525 switch (s
->addrlen
) {
527 if (s
->cmd
== NAND_CMD_READID
) {
531 case 2: /* fix cache address as a byte address */
532 s
->addr
<<= (s
->buswidth
- 1);
535 if (!(nand_flash_ids
[s
->chip_id
].options
& NAND_SAMSUNG_LP
) &&
536 (s
->cmd
== NAND_CMD_READ0
||
537 s
->cmd
== NAND_CMD_PAGEPROGRAM1
)) {
542 if ((nand_flash_ids
[s
->chip_id
].options
& NAND_SAMSUNG_LP
) &&
543 nand_flash_ids
[s
->chip_id
].size
< 256 && /* 1Gb or less */
544 (s
->cmd
== NAND_CMD_READ0
||
545 s
->cmd
== NAND_CMD_PAGEPROGRAM1
)) {
550 if ((nand_flash_ids
[s
->chip_id
].options
& NAND_SAMSUNG_LP
) &&
551 nand_flash_ids
[s
->chip_id
].size
>= 256 && /* 2Gb or more */
552 (s
->cmd
== NAND_CMD_READ0
||
553 s
->cmd
== NAND_CMD_PAGEPROGRAM1
)) {
562 if (!s
->cle
&& !s
->ale
&& s
->cmd
== NAND_CMD_PAGEPROGRAM1
) {
563 if (s
->iolen
< (1 << s
->page_shift
) + (1 << s
->oob_shift
)) {
564 for (i
= s
->buswidth
; i
--; value
>>= 8) {
565 s
->io
[s
->iolen
++] = (uint8_t) (value
& 0xff);
568 } else if (!s
->cle
&& !s
->ale
&& s
->cmd
== NAND_CMD_COPYBACKPRG1
) {
569 if ((s
->addr
& ((1 << s
->addr_shift
) - 1)) <
570 (1 << s
->page_shift
) + (1 << s
->oob_shift
)) {
571 for (i
= s
->buswidth
; i
--; s
->addr
++, value
>>= 8) {
572 s
->io
[s
->iolen
+ (s
->addr
& ((1 << s
->addr_shift
) - 1))] =
573 (uint8_t) (value
& 0xff);
579 uint32_t nand_getio(DeviceState
*dev
)
583 NANDFlashState
*s
= (NANDFlashState
*) dev
;
585 /* Allow sequential reading */
586 if (!s
->iolen
&& s
->cmd
== NAND_CMD_READ0
) {
587 offset
= (int) (s
->addr
& ((1 << s
->addr_shift
) - 1)) + s
->offset
;
590 s
->blk_load(s
, s
->addr
, offset
);
592 s
->iolen
= (1 << s
->page_shift
) - offset
;
594 s
->iolen
= (1 << s
->page_shift
) + (1 << s
->oob_shift
) - offset
;
597 if (s
->ce
|| s
->iolen
<= 0)
600 for (offset
= s
->buswidth
; offset
--;) {
601 x
|= s
->ioaddr
[offset
] << (offset
<< 3);
603 /* after receiving READ STATUS command all subsequent reads will
604 * return the status register value until another command is issued
606 if (s
->cmd
!= NAND_CMD_READSTATUS
) {
607 s
->addr
+= s
->buswidth
;
608 s
->ioaddr
+= s
->buswidth
;
609 s
->iolen
-= s
->buswidth
;
614 uint32_t nand_getbuswidth(DeviceState
*dev
)
616 NANDFlashState
*s
= (NANDFlashState
*) dev
;
617 return s
->buswidth
<< 3;
620 DeviceState
*nand_init(BlockDriverState
*bdrv
, int manf_id
, int chip_id
)
624 if (nand_flash_ids
[chip_id
].size
== 0) {
625 hw_error("%s: Unsupported NAND chip ID.\n", __FUNCTION__
);
627 dev
= qdev_create(NULL
, "nand");
628 qdev_prop_set_uint8(dev
, "manufacturer_id", manf_id
);
629 qdev_prop_set_uint8(dev
, "chip_id", chip_id
);
631 qdev_prop_set_drive_nofail(dev
, "drive", bdrv
);
634 qdev_init_nofail(dev
);
638 type_init(nand_register_types
)
642 /* Program a single page */
643 static void glue(nand_blk_write_
, PAGE_SIZE
)(NANDFlashState
*s
)
645 uint64_t off
, page
, sector
, soff
;
646 uint8_t iobuf
[(PAGE_SECTORS
+ 2) * 0x200];
647 if (PAGE(s
->addr
) >= s
->pages
)
651 mem_and(s
->storage
+ PAGE_START(s
->addr
) + (s
->addr
& PAGE_MASK
) +
652 s
->offset
, s
->io
, s
->iolen
);
653 } else if (s
->mem_oob
) {
654 sector
= SECTOR(s
->addr
);
655 off
= (s
->addr
& PAGE_MASK
) + s
->offset
;
656 soff
= SECTOR_OFFSET(s
->addr
);
657 if (bdrv_read(s
->bdrv
, sector
, iobuf
, PAGE_SECTORS
) < 0) {
658 printf("%s: read error in sector %" PRIu64
"\n", __func__
, sector
);
662 mem_and(iobuf
+ (soff
| off
), s
->io
, MIN(s
->iolen
, PAGE_SIZE
- off
));
663 if (off
+ s
->iolen
> PAGE_SIZE
) {
664 page
= PAGE(s
->addr
);
665 mem_and(s
->storage
+ (page
<< OOB_SHIFT
), s
->io
+ PAGE_SIZE
- off
,
666 MIN(OOB_SIZE
, off
+ s
->iolen
- PAGE_SIZE
));
669 if (bdrv_write(s
->bdrv
, sector
, iobuf
, PAGE_SECTORS
) < 0) {
670 printf("%s: write error in sector %" PRIu64
"\n", __func__
, sector
);
673 off
= PAGE_START(s
->addr
) + (s
->addr
& PAGE_MASK
) + s
->offset
;
676 if (bdrv_read(s
->bdrv
, sector
, iobuf
, PAGE_SECTORS
+ 2) < 0) {
677 printf("%s: read error in sector %" PRIu64
"\n", __func__
, sector
);
681 mem_and(iobuf
+ soff
, s
->io
, s
->iolen
);
683 if (bdrv_write(s
->bdrv
, sector
, iobuf
, PAGE_SECTORS
+ 2) < 0) {
684 printf("%s: write error in sector %" PRIu64
"\n", __func__
, sector
);
690 /* Erase a single block */
691 static void glue(nand_blk_erase_
, PAGE_SIZE
)(NANDFlashState
*s
)
693 uint64_t i
, page
, addr
;
694 uint8_t iobuf
[0x200] = { [0 ... 0x1ff] = 0xff, };
695 addr
= s
->addr
& ~((1 << (ADDR_SHIFT
+ s
->erase_shift
)) - 1);
697 if (PAGE(addr
) >= s
->pages
)
701 memset(s
->storage
+ PAGE_START(addr
),
702 0xff, (PAGE_SIZE
+ OOB_SIZE
) << s
->erase_shift
);
703 } else if (s
->mem_oob
) {
704 memset(s
->storage
+ (PAGE(addr
) << OOB_SHIFT
),
705 0xff, OOB_SIZE
<< s
->erase_shift
);
707 page
= SECTOR(addr
+ (ADDR_SHIFT
+ s
->erase_shift
));
708 for (; i
< page
; i
++)
709 if (bdrv_write(s
->bdrv
, i
, iobuf
, 1) < 0) {
710 printf("%s: write error in sector %" PRIu64
"\n", __func__
, i
);
713 addr
= PAGE_START(addr
);
715 if (bdrv_read(s
->bdrv
, page
, iobuf
, 1) < 0) {
716 printf("%s: read error in sector %" PRIu64
"\n", __func__
, page
);
718 memset(iobuf
+ (addr
& 0x1ff), 0xff, (~addr
& 0x1ff) + 1);
719 if (bdrv_write(s
->bdrv
, page
, iobuf
, 1) < 0) {
720 printf("%s: write error in sector %" PRIu64
"\n", __func__
, page
);
723 memset(iobuf
, 0xff, 0x200);
724 i
= (addr
& ~0x1ff) + 0x200;
725 for (addr
+= ((PAGE_SIZE
+ OOB_SIZE
) << s
->erase_shift
) - 0x200;
726 i
< addr
; i
+= 0x200)
727 if (bdrv_write(s
->bdrv
, i
>> 9, iobuf
, 1) < 0) {
728 printf("%s: write error in sector %" PRIu64
"\n",
733 if (bdrv_read(s
->bdrv
, page
, iobuf
, 1) < 0) {
734 printf("%s: read error in sector %" PRIu64
"\n", __func__
, page
);
736 memset(iobuf
, 0xff, ((addr
- 1) & 0x1ff) + 1);
737 if (bdrv_write(s
->bdrv
, page
, iobuf
, 1) < 0) {
738 printf("%s: write error in sector %" PRIu64
"\n", __func__
, page
);
743 static void glue(nand_blk_load_
, PAGE_SIZE
)(NANDFlashState
*s
,
744 uint64_t addr
, int offset
)
746 if (PAGE(addr
) >= s
->pages
)
751 if (bdrv_read(s
->bdrv
, SECTOR(addr
), s
->io
, PAGE_SECTORS
) < 0) {
752 printf("%s: read error in sector %" PRIu64
"\n",
753 __func__
, SECTOR(addr
));
755 memcpy(s
->io
+ SECTOR_OFFSET(s
->addr
) + PAGE_SIZE
,
756 s
->storage
+ (PAGE(s
->addr
) << OOB_SHIFT
),
758 s
->ioaddr
= s
->io
+ SECTOR_OFFSET(s
->addr
) + offset
;
760 if (bdrv_read(s
->bdrv
, PAGE_START(addr
) >> 9,
761 s
->io
, (PAGE_SECTORS
+ 2)) < 0) {
762 printf("%s: read error in sector %" PRIu64
"\n",
763 __func__
, PAGE_START(addr
) >> 9);
765 s
->ioaddr
= s
->io
+ (PAGE_START(addr
) & 0x1ff) + offset
;
768 memcpy(s
->io
, s
->storage
+ PAGE_START(s
->addr
) +
769 offset
, PAGE_SIZE
+ OOB_SIZE
- offset
);
774 static void glue(nand_init_
, PAGE_SIZE
)(NANDFlashState
*s
)
776 s
->oob_shift
= PAGE_SHIFT
- 5;
777 s
->pages
= s
->size
>> PAGE_SHIFT
;
778 s
->addr_shift
= ADDR_SHIFT
;
780 s
->blk_erase
= glue(nand_blk_erase_
, PAGE_SIZE
);
781 s
->blk_write
= glue(nand_blk_write_
, PAGE_SIZE
);
782 s
->blk_load
= glue(nand_blk_load_
, PAGE_SIZE
);