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.
21 #include "qemu/osdep.h"
23 #include "hw/qdev-properties.h"
24 #include "hw/qdev-properties-system.h"
25 #include "hw/block/flash.h"
26 #include "sysemu/block-backend.h"
27 #include "migration/vmstate.h"
28 #include "qapi/error.h"
29 #include "qemu/error-report.h"
30 #include "qemu/module.h"
31 #include "qom/object.h"
33 # define NAND_CMD_READ0 0x00
34 # define NAND_CMD_READ1 0x01
35 # define NAND_CMD_READ2 0x50
36 # define NAND_CMD_LPREAD2 0x30
37 # define NAND_CMD_NOSERIALREAD2 0x35
38 # define NAND_CMD_RANDOMREAD1 0x05
39 # define NAND_CMD_RANDOMREAD2 0xe0
40 # define NAND_CMD_READID 0x90
41 # define NAND_CMD_RESET 0xff
42 # define NAND_CMD_PAGEPROGRAM1 0x80
43 # define NAND_CMD_PAGEPROGRAM2 0x10
44 # define NAND_CMD_CACHEPROGRAM2 0x15
45 # define NAND_CMD_BLOCKERASE1 0x60
46 # define NAND_CMD_BLOCKERASE2 0xd0
47 # define NAND_CMD_READSTATUS 0x70
48 # define NAND_CMD_COPYBACKPRG1 0x85
50 # define NAND_IOSTATUS_ERROR (1 << 0)
51 # define NAND_IOSTATUS_PLANE0 (1 << 1)
52 # define NAND_IOSTATUS_PLANE1 (1 << 2)
53 # define NAND_IOSTATUS_PLANE2 (1 << 3)
54 # define NAND_IOSTATUS_PLANE3 (1 << 4)
55 # define NAND_IOSTATUS_READY (1 << 6)
56 # define NAND_IOSTATUS_UNPROTCT (1 << 7)
58 # define MAX_PAGE 0x800
61 typedef struct NANDFlashState NANDFlashState
;
62 struct NANDFlashState
{
63 DeviceState parent_obj
;
65 uint8_t manf_id
, chip_id
;
66 uint8_t buswidth
; /* in BYTES */
68 int page_shift
, oob_shift
, erase_shift
, addr_shift
;
73 uint8_t cle
, ale
, ce
, wp
, gnd
;
75 uint8_t io
[MAX_PAGE
+ MAX_OOB
+ 0x400];
85 void (*blk_write
)(NANDFlashState
*s
);
86 void (*blk_erase
)(NANDFlashState
*s
);
88 * Returns %true when block containing (@addr + @offset) is
89 * successfully loaded, otherwise %false.
91 bool (*blk_load
)(NANDFlashState
*s
, uint64_t addr
, unsigned offset
);
93 uint32_t ioaddr_vmstate
;
96 #define TYPE_NAND "nand"
98 OBJECT_DECLARE_SIMPLE_TYPE(NANDFlashState
, NAND
)
100 static void mem_and(uint8_t *dest
, const uint8_t *src
, size_t n
)
102 /* Like memcpy() but we logical-AND the data into the destination */
104 for (i
= 0; i
< n
; i
++) {
109 # define NAND_NO_AUTOINCR 0x00000001
110 # define NAND_BUSWIDTH_16 0x00000002
111 # define NAND_NO_PADDING 0x00000004
112 # define NAND_CACHEPRG 0x00000008
113 # define NAND_COPYBACK 0x00000010
114 # define NAND_IS_AND 0x00000020
115 # define NAND_4PAGE_ARRAY 0x00000040
116 # define NAND_NO_READRDY 0x00000100
117 # define NAND_SAMSUNG_LP (NAND_NO_PADDING | NAND_COPYBACK)
121 # define PAGE(addr) ((addr) >> ADDR_SHIFT)
122 # define PAGE_START(page) (PAGE(page) * (NAND_PAGE_SIZE + OOB_SIZE))
123 # define PAGE_MASK ((1 << ADDR_SHIFT) - 1)
124 # define OOB_SHIFT (PAGE_SHIFT - 5)
125 # define OOB_SIZE (1 << OOB_SHIFT)
126 # define SECTOR(addr) ((addr) >> (9 + ADDR_SHIFT - PAGE_SHIFT))
127 # define SECTOR_OFFSET(addr) ((addr) & ((511 >> PAGE_SHIFT) << 8))
129 # define NAND_PAGE_SIZE 256
130 # define PAGE_SHIFT 8
131 # define PAGE_SECTORS 1
132 # define ADDR_SHIFT 8
134 # define NAND_PAGE_SIZE 512
135 # define PAGE_SHIFT 9
136 # define PAGE_SECTORS 1
137 # define ADDR_SHIFT 8
139 # define NAND_PAGE_SIZE 2048
140 # define PAGE_SHIFT 11
141 # define PAGE_SECTORS 4
142 # define ADDR_SHIFT 16
145 /* Information based on Linux drivers/mtd/nand/raw/nand_ids.c */
146 static const struct {
152 } nand_flash_ids
[0x100] = {
153 [0 ... 0xff] = { 0 },
155 [0x6b] = { 4, 8, 9, 4, 0 },
156 [0xe3] = { 4, 8, 9, 4, 0 },
157 [0xe5] = { 4, 8, 9, 4, 0 },
158 [0xd6] = { 8, 8, 9, 4, 0 },
159 [0xe6] = { 8, 8, 9, 4, 0 },
161 [0x33] = { 16, 8, 9, 5, 0 },
162 [0x73] = { 16, 8, 9, 5, 0 },
163 [0x43] = { 16, 16, 9, 5, NAND_BUSWIDTH_16
},
164 [0x53] = { 16, 16, 9, 5, NAND_BUSWIDTH_16
},
166 [0x35] = { 32, 8, 9, 5, 0 },
167 [0x75] = { 32, 8, 9, 5, 0 },
168 [0x45] = { 32, 16, 9, 5, NAND_BUSWIDTH_16
},
169 [0x55] = { 32, 16, 9, 5, NAND_BUSWIDTH_16
},
171 [0x36] = { 64, 8, 9, 5, 0 },
172 [0x76] = { 64, 8, 9, 5, 0 },
173 [0x46] = { 64, 16, 9, 5, NAND_BUSWIDTH_16
},
174 [0x56] = { 64, 16, 9, 5, NAND_BUSWIDTH_16
},
176 [0x78] = { 128, 8, 9, 5, 0 },
177 [0x39] = { 128, 8, 9, 5, 0 },
178 [0x79] = { 128, 8, 9, 5, 0 },
179 [0x72] = { 128, 16, 9, 5, NAND_BUSWIDTH_16
},
180 [0x49] = { 128, 16, 9, 5, NAND_BUSWIDTH_16
},
181 [0x74] = { 128, 16, 9, 5, NAND_BUSWIDTH_16
},
182 [0x59] = { 128, 16, 9, 5, NAND_BUSWIDTH_16
},
184 [0x71] = { 256, 8, 9, 5, 0 },
187 * These are the new chips with large page size. The pagesize and the
188 * erasesize is determined from the extended id bytes
190 # define LP_OPTIONS (NAND_SAMSUNG_LP | NAND_NO_READRDY | NAND_NO_AUTOINCR)
191 # define LP_OPTIONS16 (LP_OPTIONS | NAND_BUSWIDTH_16)
194 [0xa2] = { 64, 8, 0, 0, LP_OPTIONS
},
195 [0xf2] = { 64, 8, 0, 0, LP_OPTIONS
},
196 [0xb2] = { 64, 16, 0, 0, LP_OPTIONS16
},
197 [0xc2] = { 64, 16, 0, 0, LP_OPTIONS16
},
200 [0xa1] = { 128, 8, 0, 0, LP_OPTIONS
},
201 [0xf1] = { 128, 8, 0, 0, LP_OPTIONS
},
202 [0xb1] = { 128, 16, 0, 0, LP_OPTIONS16
},
203 [0xc1] = { 128, 16, 0, 0, LP_OPTIONS16
},
206 [0xaa] = { 256, 8, 0, 0, LP_OPTIONS
},
207 [0xda] = { 256, 8, 0, 0, LP_OPTIONS
},
208 [0xba] = { 256, 16, 0, 0, LP_OPTIONS16
},
209 [0xca] = { 256, 16, 0, 0, LP_OPTIONS16
},
212 [0xac] = { 512, 8, 0, 0, LP_OPTIONS
},
213 [0xdc] = { 512, 8, 0, 0, LP_OPTIONS
},
214 [0xbc] = { 512, 16, 0, 0, LP_OPTIONS16
},
215 [0xcc] = { 512, 16, 0, 0, LP_OPTIONS16
},
218 [0xa3] = { 1024, 8, 0, 0, LP_OPTIONS
},
219 [0xd3] = { 1024, 8, 0, 0, LP_OPTIONS
},
220 [0xb3] = { 1024, 16, 0, 0, LP_OPTIONS16
},
221 [0xc3] = { 1024, 16, 0, 0, LP_OPTIONS16
},
224 [0xa5] = { 2048, 8, 0, 0, LP_OPTIONS
},
225 [0xd5] = { 2048, 8, 0, 0, LP_OPTIONS
},
226 [0xb5] = { 2048, 16, 0, 0, LP_OPTIONS16
},
227 [0xc5] = { 2048, 16, 0, 0, LP_OPTIONS16
},
230 static void nand_reset(DeviceState
*dev
)
232 NANDFlashState
*s
= NAND(dev
);
233 s
->cmd
= NAND_CMD_READ0
;
238 s
->status
&= NAND_IOSTATUS_UNPROTCT
;
239 s
->status
|= NAND_IOSTATUS_READY
;
242 static inline void nand_pushio_byte(NANDFlashState
*s
, uint8_t value
)
244 s
->ioaddr
[s
->iolen
++] = value
;
245 for (value
= s
->buswidth
; --value
;) {
246 s
->ioaddr
[s
->iolen
++] = 0;
251 * nand_load_block: Load block containing (s->addr + @offset).
252 * Returns length of data available at @offset in this block.
254 static unsigned nand_load_block(NANDFlashState
*s
, unsigned offset
)
258 if (!s
->blk_load(s
, s
->addr
, offset
)) {
262 iolen
= (1 << s
->page_shift
);
264 iolen
+= 1 << s
->oob_shift
;
266 assert(offset
<= iolen
);
272 static void nand_command(NANDFlashState
*s
)
279 case NAND_CMD_READID
:
282 nand_pushio_byte(s
, s
->manf_id
);
283 nand_pushio_byte(s
, s
->chip_id
);
284 nand_pushio_byte(s
, 'Q'); /* Don't-care byte (often 0xa5) */
285 if (nand_flash_ids
[s
->chip_id
].options
& NAND_SAMSUNG_LP
) {
286 /* Page Size, Block Size, Spare Size; bit 6 indicates
287 * 8 vs 16 bit width NAND.
289 nand_pushio_byte(s
, (s
->buswidth
== 2) ? 0x55 : 0x15);
291 nand_pushio_byte(s
, 0xc0); /* Multi-plane */
295 case NAND_CMD_RANDOMREAD2
:
296 case NAND_CMD_NOSERIALREAD2
:
297 if (!(nand_flash_ids
[s
->chip_id
].options
& NAND_SAMSUNG_LP
))
299 s
->iolen
= nand_load_block(s
, s
->addr
& ((1 << s
->addr_shift
) - 1));
303 nand_reset(DEVICE(s
));
306 case NAND_CMD_PAGEPROGRAM1
:
311 case NAND_CMD_PAGEPROGRAM2
:
317 case NAND_CMD_BLOCKERASE1
:
320 case NAND_CMD_BLOCKERASE2
:
321 s
->addr
&= (1ull << s
->addrlen
* 8) - 1;
322 s
->addr
<<= nand_flash_ids
[s
->chip_id
].options
& NAND_SAMSUNG_LP
?
330 case NAND_CMD_READSTATUS
:
333 nand_pushio_byte(s
, s
->status
);
337 printf("%s: Unknown NAND command 0x%02x\n", __func__
, s
->cmd
);
341 static int nand_pre_save(void *opaque
)
343 NANDFlashState
*s
= NAND(opaque
);
345 s
->ioaddr_vmstate
= s
->ioaddr
- s
->io
;
350 static int nand_post_load(void *opaque
, int version_id
)
352 NANDFlashState
*s
= NAND(opaque
);
354 if (s
->ioaddr_vmstate
> sizeof(s
->io
)) {
357 s
->ioaddr
= s
->io
+ s
->ioaddr_vmstate
;
362 static const VMStateDescription vmstate_nand
= {
365 .minimum_version_id
= 1,
366 .pre_save
= nand_pre_save
,
367 .post_load
= nand_post_load
,
368 .fields
= (const VMStateField
[]) {
369 VMSTATE_UINT8(cle
, NANDFlashState
),
370 VMSTATE_UINT8(ale
, NANDFlashState
),
371 VMSTATE_UINT8(ce
, NANDFlashState
),
372 VMSTATE_UINT8(wp
, NANDFlashState
),
373 VMSTATE_UINT8(gnd
, NANDFlashState
),
374 VMSTATE_BUFFER(io
, NANDFlashState
),
375 VMSTATE_UINT32(ioaddr_vmstate
, NANDFlashState
),
376 VMSTATE_INT32(iolen
, NANDFlashState
),
377 VMSTATE_UINT32(cmd
, NANDFlashState
),
378 VMSTATE_UINT64(addr
, NANDFlashState
),
379 VMSTATE_INT32(addrlen
, NANDFlashState
),
380 VMSTATE_INT32(status
, NANDFlashState
),
381 VMSTATE_INT32(offset
, NANDFlashState
),
382 /* XXX: do we want to save s->storage too? */
383 VMSTATE_END_OF_LIST()
387 static void nand_realize(DeviceState
*dev
, Error
**errp
)
390 NANDFlashState
*s
= NAND(dev
);
394 s
->buswidth
= nand_flash_ids
[s
->chip_id
].width
>> 3;
395 s
->size
= nand_flash_ids
[s
->chip_id
].size
<< 20;
396 if (nand_flash_ids
[s
->chip_id
].options
& NAND_SAMSUNG_LP
) {
400 s
->page_shift
= nand_flash_ids
[s
->chip_id
].page_shift
;
401 s
->erase_shift
= nand_flash_ids
[s
->chip_id
].erase_shift
;
404 switch (1 << s
->page_shift
) {
415 error_setg(errp
, "Unsupported NAND block size %#x",
420 pagesize
= 1 << s
->oob_shift
;
423 if (!blk_supports_write_perm(s
->blk
)) {
424 error_setg(errp
, "Can't use a read-only drive");
427 ret
= blk_set_perm(s
->blk
, BLK_PERM_CONSISTENT_READ
| BLK_PERM_WRITE
,
432 if (blk_getlength(s
->blk
) >=
433 (s
->pages
<< s
->page_shift
) + (s
->pages
<< s
->oob_shift
)) {
438 pagesize
+= 1 << s
->page_shift
;
441 s
->storage
= (uint8_t *) memset(g_malloc(s
->pages
* pagesize
),
442 0xff, s
->pages
* pagesize
);
444 /* Give s->ioaddr a sane value in case we save state before it is used. */
448 static Property nand_properties
[] = {
449 DEFINE_PROP_UINT8("manufacturer_id", NANDFlashState
, manf_id
, 0),
450 DEFINE_PROP_UINT8("chip_id", NANDFlashState
, chip_id
, 0),
451 DEFINE_PROP_DRIVE("drive", NANDFlashState
, blk
),
452 DEFINE_PROP_END_OF_LIST(),
455 static void nand_class_init(ObjectClass
*klass
, void *data
)
457 DeviceClass
*dc
= DEVICE_CLASS(klass
);
459 dc
->realize
= nand_realize
;
460 dc
->reset
= nand_reset
;
461 dc
->vmsd
= &vmstate_nand
;
462 device_class_set_props(dc
, nand_properties
);
463 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
466 static const TypeInfo nand_info
= {
468 .parent
= TYPE_DEVICE
,
469 .instance_size
= sizeof(NANDFlashState
),
470 .class_init
= nand_class_init
,
473 static void nand_register_types(void)
475 type_register_static(&nand_info
);
479 * Chip inputs are CLE, ALE, CE, WP, GND and eight I/O pins. Chip
480 * outputs are R/B and eight I/O pins.
482 * CE, WP and R/B are active low.
484 void nand_setpins(DeviceState
*dev
, uint8_t cle
, uint8_t ale
,
485 uint8_t ce
, uint8_t wp
, uint8_t gnd
)
487 NANDFlashState
*s
= NAND(dev
);
495 s
->status
|= NAND_IOSTATUS_UNPROTCT
;
497 s
->status
&= ~NAND_IOSTATUS_UNPROTCT
;
501 void nand_getpins(DeviceState
*dev
, int *rb
)
506 void nand_setio(DeviceState
*dev
, uint32_t value
)
509 NANDFlashState
*s
= NAND(dev
);
511 if (!s
->ce
&& s
->cle
) {
512 if (nand_flash_ids
[s
->chip_id
].options
& NAND_SAMSUNG_LP
) {
513 if (s
->cmd
== NAND_CMD_READ0
&& value
== NAND_CMD_LPREAD2
)
515 if (value
== NAND_CMD_RANDOMREAD1
) {
516 s
->addr
&= ~((1 << s
->addr_shift
) - 1);
521 if (value
== NAND_CMD_READ0
) {
523 } else if (value
== NAND_CMD_READ1
) {
525 value
= NAND_CMD_READ0
;
526 } else if (value
== NAND_CMD_READ2
) {
527 s
->offset
= 1 << s
->page_shift
;
528 value
= NAND_CMD_READ0
;
533 if (s
->cmd
== NAND_CMD_READSTATUS
||
534 s
->cmd
== NAND_CMD_PAGEPROGRAM2
||
535 s
->cmd
== NAND_CMD_BLOCKERASE1
||
536 s
->cmd
== NAND_CMD_BLOCKERASE2
||
537 s
->cmd
== NAND_CMD_NOSERIALREAD2
||
538 s
->cmd
== NAND_CMD_RANDOMREAD2
||
539 s
->cmd
== NAND_CMD_RESET
) {
543 if (s
->cmd
!= NAND_CMD_RANDOMREAD2
) {
549 unsigned int shift
= s
->addrlen
* 8;
550 uint64_t mask
= ~(0xffull
<< shift
);
551 uint64_t v
= (uint64_t)value
<< shift
;
553 s
->addr
= (s
->addr
& mask
) | v
;
556 switch (s
->addrlen
) {
558 if (s
->cmd
== NAND_CMD_READID
) {
562 case 2: /* fix cache address as a byte address */
563 s
->addr
<<= (s
->buswidth
- 1);
566 if (!(nand_flash_ids
[s
->chip_id
].options
& NAND_SAMSUNG_LP
) &&
567 (s
->cmd
== NAND_CMD_READ0
||
568 s
->cmd
== NAND_CMD_PAGEPROGRAM1
)) {
573 if ((nand_flash_ids
[s
->chip_id
].options
& NAND_SAMSUNG_LP
) &&
574 nand_flash_ids
[s
->chip_id
].size
< 256 && /* 1Gb or less */
575 (s
->cmd
== NAND_CMD_READ0
||
576 s
->cmd
== NAND_CMD_PAGEPROGRAM1
)) {
581 if ((nand_flash_ids
[s
->chip_id
].options
& NAND_SAMSUNG_LP
) &&
582 nand_flash_ids
[s
->chip_id
].size
>= 256 && /* 2Gb or more */
583 (s
->cmd
== NAND_CMD_READ0
||
584 s
->cmd
== NAND_CMD_PAGEPROGRAM1
)) {
593 if (!s
->cle
&& !s
->ale
&& s
->cmd
== NAND_CMD_PAGEPROGRAM1
) {
594 if (s
->iolen
< (1 << s
->page_shift
) + (1 << s
->oob_shift
)) {
595 for (i
= s
->buswidth
; i
--; value
>>= 8) {
596 s
->io
[s
->iolen
++] = (uint8_t) (value
& 0xff);
599 } else if (!s
->cle
&& !s
->ale
&& s
->cmd
== NAND_CMD_COPYBACKPRG1
) {
600 if ((s
->addr
& ((1 << s
->addr_shift
) - 1)) <
601 (1 << s
->page_shift
) + (1 << s
->oob_shift
)) {
602 for (i
= s
->buswidth
; i
--; s
->addr
++, value
>>= 8) {
603 s
->io
[s
->iolen
+ (s
->addr
& ((1 << s
->addr_shift
) - 1))] =
604 (uint8_t) (value
& 0xff);
610 uint32_t nand_getio(DeviceState
*dev
)
614 NANDFlashState
*s
= NAND(dev
);
616 /* Allow sequential reading */
617 if (!s
->iolen
&& s
->cmd
== NAND_CMD_READ0
) {
618 offset
= (int) (s
->addr
& ((1 << s
->addr_shift
) - 1)) + s
->offset
;
620 s
->iolen
= nand_load_block(s
, offset
);
623 if (s
->ce
|| s
->iolen
<= 0) {
627 for (offset
= s
->buswidth
; offset
--;) {
628 x
|= s
->ioaddr
[offset
] << (offset
<< 3);
630 /* after receiving READ STATUS command all subsequent reads will
631 * return the status register value until another command is issued
633 if (s
->cmd
!= NAND_CMD_READSTATUS
) {
634 s
->addr
+= s
->buswidth
;
635 s
->ioaddr
+= s
->buswidth
;
636 s
->iolen
-= s
->buswidth
;
641 uint32_t nand_getbuswidth(DeviceState
*dev
)
643 NANDFlashState
*s
= (NANDFlashState
*) dev
;
644 return s
->buswidth
<< 3;
647 DeviceState
*nand_init(BlockBackend
*blk
, int manf_id
, int chip_id
)
651 if (nand_flash_ids
[chip_id
].size
== 0) {
652 hw_error("%s: Unsupported NAND chip ID.\n", __func__
);
654 dev
= qdev_new(TYPE_NAND
);
655 qdev_prop_set_uint8(dev
, "manufacturer_id", manf_id
);
656 qdev_prop_set_uint8(dev
, "chip_id", chip_id
);
658 qdev_prop_set_drive_err(dev
, "drive", blk
, &error_fatal
);
661 qdev_realize(dev
, NULL
, &error_fatal
);
665 type_init(nand_register_types
)
669 /* Program a single page */
670 static void glue(nand_blk_write_
, NAND_PAGE_SIZE
)(NANDFlashState
*s
)
672 uint64_t off
, page
, sector
, soff
;
673 uint8_t iobuf
[(PAGE_SECTORS
+ 2) * 0x200];
674 if (PAGE(s
->addr
) >= s
->pages
)
678 mem_and(s
->storage
+ PAGE_START(s
->addr
) + (s
->addr
& PAGE_MASK
) +
679 s
->offset
, s
->io
, s
->iolen
);
680 } else if (s
->mem_oob
) {
681 sector
= SECTOR(s
->addr
);
682 off
= (s
->addr
& PAGE_MASK
) + s
->offset
;
683 soff
= SECTOR_OFFSET(s
->addr
);
684 if (blk_pread(s
->blk
, sector
<< BDRV_SECTOR_BITS
,
685 PAGE_SECTORS
<< BDRV_SECTOR_BITS
, iobuf
, 0) < 0) {
686 printf("%s: read error in sector %" PRIu64
"\n", __func__
, sector
);
690 mem_and(iobuf
+ (soff
| off
), s
->io
, MIN(s
->iolen
, NAND_PAGE_SIZE
- off
));
691 if (off
+ s
->iolen
> NAND_PAGE_SIZE
) {
692 page
= PAGE(s
->addr
);
693 mem_and(s
->storage
+ (page
<< OOB_SHIFT
), s
->io
+ NAND_PAGE_SIZE
- off
,
694 MIN(OOB_SIZE
, off
+ s
->iolen
- NAND_PAGE_SIZE
));
697 if (blk_pwrite(s
->blk
, sector
<< BDRV_SECTOR_BITS
,
698 PAGE_SECTORS
<< BDRV_SECTOR_BITS
, iobuf
, 0) < 0) {
699 printf("%s: write error in sector %" PRIu64
"\n", __func__
, sector
);
702 off
= PAGE_START(s
->addr
) + (s
->addr
& PAGE_MASK
) + s
->offset
;
705 if (blk_pread(s
->blk
, sector
<< BDRV_SECTOR_BITS
,
706 (PAGE_SECTORS
+ 2) << BDRV_SECTOR_BITS
, iobuf
, 0) < 0) {
707 printf("%s: read error in sector %" PRIu64
"\n", __func__
, sector
);
711 mem_and(iobuf
+ soff
, s
->io
, s
->iolen
);
713 if (blk_pwrite(s
->blk
, sector
<< BDRV_SECTOR_BITS
,
714 (PAGE_SECTORS
+ 2) << BDRV_SECTOR_BITS
, iobuf
, 0) < 0) {
715 printf("%s: write error in sector %" PRIu64
"\n", __func__
, sector
);
721 /* Erase a single block */
722 static void glue(nand_blk_erase_
, NAND_PAGE_SIZE
)(NANDFlashState
*s
)
724 uint64_t i
, page
, addr
;
725 uint8_t iobuf
[0x200] = { [0 ... 0x1ff] = 0xff, };
726 addr
= s
->addr
& ~((1 << (ADDR_SHIFT
+ s
->erase_shift
)) - 1);
728 if (PAGE(addr
) >= s
->pages
) {
733 memset(s
->storage
+ PAGE_START(addr
),
734 0xff, (NAND_PAGE_SIZE
+ OOB_SIZE
) << s
->erase_shift
);
735 } else if (s
->mem_oob
) {
736 memset(s
->storage
+ (PAGE(addr
) << OOB_SHIFT
),
737 0xff, OOB_SIZE
<< s
->erase_shift
);
739 page
= SECTOR(addr
+ (1 << (ADDR_SHIFT
+ s
->erase_shift
)));
740 for (; i
< page
; i
++)
741 if (blk_pwrite(s
->blk
, i
<< BDRV_SECTOR_BITS
,
742 BDRV_SECTOR_SIZE
, iobuf
, 0) < 0) {
743 printf("%s: write error in sector %" PRIu64
"\n", __func__
, i
);
746 addr
= PAGE_START(addr
);
748 if (blk_pread(s
->blk
, page
<< BDRV_SECTOR_BITS
,
749 BDRV_SECTOR_SIZE
, iobuf
, 0) < 0) {
750 printf("%s: read error in sector %" PRIu64
"\n", __func__
, page
);
752 memset(iobuf
+ (addr
& 0x1ff), 0xff, (~addr
& 0x1ff) + 1);
753 if (blk_pwrite(s
->blk
, page
<< BDRV_SECTOR_BITS
,
754 BDRV_SECTOR_SIZE
, iobuf
, 0) < 0) {
755 printf("%s: write error in sector %" PRIu64
"\n", __func__
, page
);
758 memset(iobuf
, 0xff, 0x200);
759 i
= (addr
& ~0x1ff) + 0x200;
760 for (addr
+= ((NAND_PAGE_SIZE
+ OOB_SIZE
) << s
->erase_shift
) - 0x200;
761 i
< addr
; i
+= 0x200) {
762 if (blk_pwrite(s
->blk
, i
, BDRV_SECTOR_SIZE
, iobuf
, 0) < 0) {
763 printf("%s: write error in sector %" PRIu64
"\n",
769 if (blk_pread(s
->blk
, page
<< BDRV_SECTOR_BITS
,
770 BDRV_SECTOR_SIZE
, iobuf
, 0) < 0) {
771 printf("%s: read error in sector %" PRIu64
"\n", __func__
, page
);
773 memset(iobuf
, 0xff, ((addr
- 1) & 0x1ff) + 1);
774 if (blk_pwrite(s
->blk
, page
<< BDRV_SECTOR_BITS
,
775 BDRV_SECTOR_SIZE
, iobuf
, 0) < 0) {
776 printf("%s: write error in sector %" PRIu64
"\n", __func__
, page
);
781 static bool glue(nand_blk_load_
, NAND_PAGE_SIZE
)(NANDFlashState
*s
,
782 uint64_t addr
, unsigned offset
)
784 if (PAGE(addr
) >= s
->pages
) {
788 if (offset
> NAND_PAGE_SIZE
+ OOB_SIZE
) {
794 if (blk_pread(s
->blk
, SECTOR(addr
) << BDRV_SECTOR_BITS
,
795 PAGE_SECTORS
<< BDRV_SECTOR_BITS
, s
->io
, 0) < 0) {
796 printf("%s: read error in sector %" PRIu64
"\n",
797 __func__
, SECTOR(addr
));
799 memcpy(s
->io
+ SECTOR_OFFSET(s
->addr
) + NAND_PAGE_SIZE
,
800 s
->storage
+ (PAGE(s
->addr
) << OOB_SHIFT
),
802 s
->ioaddr
= s
->io
+ SECTOR_OFFSET(s
->addr
) + offset
;
804 if (blk_pread(s
->blk
, PAGE_START(addr
),
805 (PAGE_SECTORS
+ 2) << BDRV_SECTOR_BITS
, s
->io
, 0)
807 printf("%s: read error in sector %" PRIu64
"\n",
808 __func__
, PAGE_START(addr
) >> 9);
810 s
->ioaddr
= s
->io
+ (PAGE_START(addr
) & 0x1ff) + offset
;
813 memcpy(s
->io
, s
->storage
+ PAGE_START(s
->addr
) +
814 offset
, NAND_PAGE_SIZE
+ OOB_SIZE
- offset
);
821 static void glue(nand_init_
, NAND_PAGE_SIZE
)(NANDFlashState
*s
)
823 s
->oob_shift
= PAGE_SHIFT
- 5;
824 s
->pages
= s
->size
>> PAGE_SHIFT
;
825 s
->addr_shift
= ADDR_SHIFT
;
827 s
->blk_erase
= glue(nand_blk_erase_
, NAND_PAGE_SIZE
);
828 s
->blk_write
= glue(nand_blk_write_
, NAND_PAGE_SIZE
);
829 s
->blk_load
= glue(nand_blk_load_
, NAND_PAGE_SIZE
);
832 # undef NAND_PAGE_SIZE