Merge tag 'v9.0.0-rc3'
[qemu/ar7.git] / hw / block / nand.c
blobe2433c25bdc4913ffa51a07dbbb84d53071f459d
1 /*
2 * Flash NAND memory emulation. Based on "16M x 8 Bit NAND Flash
3 * Memory" datasheet for the KM29U128AT / K9F2808U0A chips from
4 * Samsung Electronic.
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.
19 #ifndef NAND_IO
21 #include "qemu/osdep.h"
22 #include "hw/hw.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
59 # define MAX_OOB 0x40
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 */
67 int size, pages;
68 int page_shift, oob_shift, erase_shift, addr_shift;
69 uint8_t *storage;
70 BlockBackend *blk;
71 int mem_oob;
73 uint8_t cle, ale, ce, wp, gnd;
75 uint8_t io[MAX_PAGE + MAX_OOB + 0x400];
76 uint8_t *ioaddr;
77 int iolen;
79 uint32_t cmd;
80 uint64_t addr;
81 int addrlen;
82 int status;
83 int offset;
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 */
103 int i;
104 for (i = 0; i < n; i++) {
105 dest[i] &= src[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)
119 # define NAND_IO
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
133 # include "nand.c"
134 # define NAND_PAGE_SIZE 512
135 # define PAGE_SHIFT 9
136 # define PAGE_SECTORS 1
137 # define ADDR_SHIFT 8
138 # include "nand.c"
139 # define NAND_PAGE_SIZE 2048
140 # define PAGE_SHIFT 11
141 # define PAGE_SECTORS 4
142 # define ADDR_SHIFT 16
143 # include "nand.c"
145 /* Information based on Linux drivers/mtd/nand/raw/nand_ids.c */
146 static const struct {
147 int size;
148 int width;
149 int page_shift;
150 int erase_shift;
151 uint32_t options;
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)
193 /* 512 Megabit */
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 },
199 /* 1 Gigabit */
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 },
205 /* 2 Gigabit */
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 },
211 /* 4 Gigabit */
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 },
217 /* 8 Gigabit */
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 },
223 /* 16 Gigabit */
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;
234 s->addr = 0;
235 s->addrlen = 0;
236 s->iolen = 0;
237 s->offset = 0;
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)
256 unsigned iolen;
258 if (!s->blk_load(s, s->addr, offset)) {
259 return 0;
262 iolen = (1 << s->page_shift);
263 if (s->gnd) {
264 iolen += 1 << s->oob_shift;
266 assert(offset <= iolen);
267 iolen -= offset;
269 return iolen;
272 static void nand_command(NANDFlashState *s)
274 switch (s->cmd) {
275 case NAND_CMD_READ0:
276 s->iolen = 0;
277 break;
279 case NAND_CMD_READID:
280 s->ioaddr = s->io;
281 s->iolen = 0;
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);
290 } else {
291 nand_pushio_byte(s, 0xc0); /* Multi-plane */
293 break;
295 case NAND_CMD_RANDOMREAD2:
296 case NAND_CMD_NOSERIALREAD2:
297 if (!(nand_flash_ids[s->chip_id].options & NAND_SAMSUNG_LP))
298 break;
299 s->iolen = nand_load_block(s, s->addr & ((1 << s->addr_shift) - 1));
300 break;
302 case NAND_CMD_RESET:
303 nand_reset(DEVICE(s));
304 break;
306 case NAND_CMD_PAGEPROGRAM1:
307 s->ioaddr = s->io;
308 s->iolen = 0;
309 break;
311 case NAND_CMD_PAGEPROGRAM2:
312 if (s->wp) {
313 s->blk_write(s);
315 break;
317 case NAND_CMD_BLOCKERASE1:
318 break;
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 ?
323 16 : 8;
325 if (s->wp) {
326 s->blk_erase(s);
328 break;
330 case NAND_CMD_READSTATUS:
331 s->ioaddr = s->io;
332 s->iolen = 0;
333 nand_pushio_byte(s, s->status);
334 break;
336 default:
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;
347 return 0;
350 static int nand_post_load(void *opaque, int version_id)
352 NANDFlashState *s = NAND(opaque);
354 if (s->ioaddr_vmstate > sizeof(s->io)) {
355 return -EINVAL;
357 s->ioaddr = s->io + s->ioaddr_vmstate;
359 return 0;
362 static const VMStateDescription vmstate_nand = {
363 .name = "nand",
364 .version_id = 1,
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)
389 int pagesize;
390 NANDFlashState *s = NAND(dev);
391 int ret;
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) {
397 s->page_shift = 11;
398 s->erase_shift = 6;
399 } else {
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) {
405 case 256:
406 nand_init_256(s);
407 break;
408 case 512:
409 nand_init_512(s);
410 break;
411 case 2048:
412 nand_init_2048(s);
413 break;
414 default:
415 error_setg(errp, "Unsupported NAND block size %#x",
416 1 << s->page_shift);
417 return;
420 pagesize = 1 << s->oob_shift;
421 s->mem_oob = 1;
422 if (s->blk) {
423 if (!blk_supports_write_perm(s->blk)) {
424 error_setg(errp, "Can't use a read-only drive");
425 return;
427 ret = blk_set_perm(s->blk, BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE,
428 BLK_PERM_ALL, errp);
429 if (ret < 0) {
430 return;
432 if (blk_getlength(s->blk) >=
433 (s->pages << s->page_shift) + (s->pages << s->oob_shift)) {
434 pagesize = 0;
435 s->mem_oob = 0;
437 } else {
438 pagesize += 1 << s->page_shift;
440 if (pagesize) {
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. */
445 s->ioaddr = s->io;
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 = {
467 .name = TYPE_NAND,
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);
489 s->cle = cle;
490 s->ale = ale;
491 s->ce = ce;
492 s->wp = wp;
493 s->gnd = gnd;
494 if (wp) {
495 s->status |= NAND_IOSTATUS_UNPROTCT;
496 } else {
497 s->status &= ~NAND_IOSTATUS_UNPROTCT;
501 void nand_getpins(DeviceState *dev, int *rb)
503 *rb = 1;
506 void nand_setio(DeviceState *dev, uint32_t value)
508 int i;
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)
514 return;
515 if (value == NAND_CMD_RANDOMREAD1) {
516 s->addr &= ~((1 << s->addr_shift) - 1);
517 s->addrlen = 0;
518 return;
521 if (value == NAND_CMD_READ0) {
522 s->offset = 0;
523 } else if (value == NAND_CMD_READ1) {
524 s->offset = 0x100;
525 value = NAND_CMD_READ0;
526 } else if (value == NAND_CMD_READ2) {
527 s->offset = 1 << s->page_shift;
528 value = NAND_CMD_READ0;
531 s->cmd = value;
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) {
540 nand_command(s);
543 if (s->cmd != NAND_CMD_RANDOMREAD2) {
544 s->addrlen = 0;
548 if (s->ale) {
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;
554 s->addrlen ++;
556 switch (s->addrlen) {
557 case 1:
558 if (s->cmd == NAND_CMD_READID) {
559 nand_command(s);
561 break;
562 case 2: /* fix cache address as a byte address */
563 s->addr <<= (s->buswidth - 1);
564 break;
565 case 3:
566 if (!(nand_flash_ids[s->chip_id].options & NAND_SAMSUNG_LP) &&
567 (s->cmd == NAND_CMD_READ0 ||
568 s->cmd == NAND_CMD_PAGEPROGRAM1)) {
569 nand_command(s);
571 break;
572 case 4:
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)) {
577 nand_command(s);
579 break;
580 case 5:
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)) {
585 nand_command(s);
587 break;
588 default:
589 break;
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)
612 int offset;
613 uint32_t x = 0;
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;
619 s->offset = 0;
620 s->iolen = nand_load_block(s, offset);
623 if (s->ce || s->iolen <= 0) {
624 return 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;
638 return x;
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)
649 DeviceState *dev;
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);
657 if (blk) {
658 qdev_prop_set_drive_err(dev, "drive", blk, &error_fatal);
661 qdev_realize(dev, NULL, &error_fatal);
662 return dev;
665 type_init(nand_register_types)
667 #else
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)
675 return;
677 if (!s->blk) {
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);
687 return;
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);
701 } else {
702 off = PAGE_START(s->addr) + (s->addr & PAGE_MASK) + s->offset;
703 sector = off >> 9;
704 soff = off & 0x1ff;
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);
708 return;
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);
718 s->offset = 0;
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) {
729 return;
732 if (!s->blk) {
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);
738 i = SECTOR(addr);
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);
745 } else {
746 addr = PAGE_START(addr);
747 page = addr >> 9;
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",
764 __func__, i >> 9);
768 page = i >> 9;
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) {
785 return false;
788 if (offset > NAND_PAGE_SIZE + OOB_SIZE) {
789 return false;
792 if (s->blk) {
793 if (s->mem_oob) {
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),
801 OOB_SIZE);
802 s->ioaddr = s->io + SECTOR_OFFSET(s->addr) + offset;
803 } else {
804 if (blk_pread(s->blk, PAGE_START(addr),
805 (PAGE_SECTORS + 2) << BDRV_SECTOR_BITS, s->io, 0)
806 < 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;
812 } else {
813 memcpy(s->io, s->storage + PAGE_START(s->addr) +
814 offset, NAND_PAGE_SIZE + OOB_SIZE - offset);
815 s->ioaddr = s->io;
818 return true;
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
833 # undef PAGE_SHIFT
834 # undef PAGE_SECTORS
835 # undef ADDR_SHIFT
836 #endif /* NAND_IO */