nand.c 23.3 KB
Newer Older
1 2 3 4 5 6 7 8
/*
 * Flash NAND memory emulation.  Based on "16M x 8 Bit NAND Flash
 * Memory" datasheet for the KM29U128AT / K9F2808U0A chips from
 * Samsung Electronic.
 *
 * Copyright (c) 2006 Openedhand Ltd.
 * Written by Andrzej Zaborowski <balrog@zabor.org>
 *
9 10 11 12
 * Support for additional features based on "MT29F2G16ABCWP 2Gx16"
 * datasheet from Micron Technology and "NAND02G-B2C" datasheet
 * from ST Microelectronics.
 *
13
 * This code is licensed under the GNU GPL v2.
14 15 16
 *
 * Contributions after 2012-01-13 are licensed under the terms of the
 * GNU GPL, version 2 or (at your option) any later version.
17 18 19 20
 */

#ifndef NAND_IO

21
# include "hw/hw.h"
P
Paolo Bonzini 已提交
22
# include "hw/block/flash.h"
23
# include "sysemu/blockdev.h"
24
# include "hw/sysbus.h"
25
#include "qemu/error-report.h"
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48

# define NAND_CMD_READ0		0x00
# define NAND_CMD_READ1		0x01
# define NAND_CMD_READ2		0x50
# define NAND_CMD_LPREAD2	0x30
# define NAND_CMD_NOSERIALREAD2	0x35
# define NAND_CMD_RANDOMREAD1	0x05
# define NAND_CMD_RANDOMREAD2	0xe0
# define NAND_CMD_READID	0x90
# define NAND_CMD_RESET		0xff
# define NAND_CMD_PAGEPROGRAM1	0x80
# define NAND_CMD_PAGEPROGRAM2	0x10
# define NAND_CMD_CACHEPROGRAM2	0x15
# define NAND_CMD_BLOCKERASE1	0x60
# define NAND_CMD_BLOCKERASE2	0xd0
# define NAND_CMD_READSTATUS	0x70
# define NAND_CMD_COPYBACKPRG1	0x85

# define NAND_IOSTATUS_ERROR	(1 << 0)
# define NAND_IOSTATUS_PLANE0	(1 << 1)
# define NAND_IOSTATUS_PLANE1	(1 << 2)
# define NAND_IOSTATUS_PLANE2	(1 << 3)
# define NAND_IOSTATUS_PLANE3	(1 << 4)
49
# define NAND_IOSTATUS_READY    (1 << 6)
50 51 52 53 54
# define NAND_IOSTATUS_UNPROTCT	(1 << 7)

# define MAX_PAGE		0x800
# define MAX_OOB		0x40

J
Juha Riihimäki 已提交
55
typedef struct NANDFlashState NANDFlashState;
P
Paul Brook 已提交
56
struct NANDFlashState {
J
Juha Riihimäki 已提交
57
    SysBusDevice busdev;
58
    uint8_t manf_id, chip_id;
59
    uint8_t buswidth; /* in BYTES */
60 61 62 63 64 65
    int size, pages;
    int page_shift, oob_shift, erase_shift, addr_shift;
    uint8_t *storage;
    BlockDriverState *bdrv;
    int mem_oob;

J
Juan Quintela 已提交
66
    uint8_t cle, ale, ce, wp, gnd;
67 68 69 70 71

    uint8_t io[MAX_PAGE + MAX_OOB + 0x400];
    uint8_t *ioaddr;
    int iolen;

72 73
    uint32_t cmd;
    uint64_t addr;
74 75 76 77
    int addrlen;
    int status;
    int offset;

P
Paul Brook 已提交
78 79
    void (*blk_write)(NANDFlashState *s);
    void (*blk_erase)(NANDFlashState *s);
80
    void (*blk_load)(NANDFlashState *s, uint64_t addr, int offset);
J
Juan Quintela 已提交
81 82

    uint32_t ioaddr_vmstate;
83 84
};

85 86 87 88 89
#define TYPE_NAND "nand"

#define NAND(obj) \
    OBJECT_CHECK(NANDFlashState, (obj), TYPE_NAND)

90 91 92 93 94 95 96 97 98
static void mem_and(uint8_t *dest, const uint8_t *src, size_t n)
{
    /* Like memcpy() but we logical-AND the data into the destination */
    int i;
    for (i = 0; i < n; i++) {
        dest[i] &= src[i];
    }
}

99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
# define NAND_NO_AUTOINCR	0x00000001
# define NAND_BUSWIDTH_16	0x00000002
# define NAND_NO_PADDING	0x00000004
# define NAND_CACHEPRG		0x00000008
# define NAND_COPYBACK		0x00000010
# define NAND_IS_AND		0x00000020
# define NAND_4PAGE_ARRAY	0x00000040
# define NAND_NO_READRDY	0x00000100
# define NAND_SAMSUNG_LP	(NAND_NO_PADDING | NAND_COPYBACK)

# define NAND_IO

# define PAGE(addr)		((addr) >> ADDR_SHIFT)
# define PAGE_START(page)	(PAGE(page) * (PAGE_SIZE + OOB_SIZE))
# define PAGE_MASK		((1 << ADDR_SHIFT) - 1)
# define OOB_SHIFT		(PAGE_SHIFT - 5)
# define OOB_SIZE		(1 << OOB_SHIFT)
# define SECTOR(addr)		((addr) >> (9 + ADDR_SHIFT - PAGE_SHIFT))
# define SECTOR_OFFSET(addr)	((addr) & ((511 >> PAGE_SHIFT) << 8))

# define PAGE_SIZE		256
# define PAGE_SHIFT		8
# define PAGE_SECTORS		1
# define ADDR_SHIFT		8
# include "nand.c"
# define PAGE_SIZE		512
# define PAGE_SHIFT		9
# define PAGE_SECTORS		1
# define ADDR_SHIFT		8
# include "nand.c"
# define PAGE_SIZE		2048
# define PAGE_SHIFT		11
# define PAGE_SECTORS		4
# define ADDR_SHIFT		16
# include "nand.c"

/* Information based on Linux drivers/mtd/nand/nand_ids.c */
P
Paul Brook 已提交
136
static const struct {
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
    int size;
    int width;
    int page_shift;
    int erase_shift;
    uint32_t options;
} nand_flash_ids[0x100] = {
    [0 ... 0xff] = { 0 },

    [0x6e] = { 1,	8,	8, 4, 0 },
    [0x64] = { 2,	8,	8, 4, 0 },
    [0x6b] = { 4,	8,	9, 4, 0 },
    [0xe8] = { 1,	8,	8, 4, 0 },
    [0xec] = { 1,	8,	8, 4, 0 },
    [0xea] = { 2,	8,	8, 4, 0 },
    [0xd5] = { 4,	8,	9, 4, 0 },
    [0xe3] = { 4,	8,	9, 4, 0 },
    [0xe5] = { 4,	8,	9, 4, 0 },
    [0xd6] = { 8,	8,	9, 4, 0 },

    [0x39] = { 8,	8,	9, 4, 0 },
    [0xe6] = { 8,	8,	9, 4, 0 },
    [0x49] = { 8,	16,	9, 4, NAND_BUSWIDTH_16 },
    [0x59] = { 8,	16,	9, 4, NAND_BUSWIDTH_16 },

    [0x33] = { 16,	8,	9, 5, 0 },
    [0x73] = { 16,	8,	9, 5, 0 },
    [0x43] = { 16,	16,	9, 5, NAND_BUSWIDTH_16 },
    [0x53] = { 16,	16,	9, 5, NAND_BUSWIDTH_16 },

    [0x35] = { 32,	8,	9, 5, 0 },
    [0x75] = { 32,	8,	9, 5, 0 },
    [0x45] = { 32,	16,	9, 5, NAND_BUSWIDTH_16 },
    [0x55] = { 32,	16,	9, 5, NAND_BUSWIDTH_16 },

    [0x36] = { 64,	8,	9, 5, 0 },
    [0x76] = { 64,	8,	9, 5, 0 },
    [0x46] = { 64,	16,	9, 5, NAND_BUSWIDTH_16 },
    [0x56] = { 64,	16,	9, 5, NAND_BUSWIDTH_16 },

    [0x78] = { 128,	8,	9, 5, 0 },
    [0x39] = { 128,	8,	9, 5, 0 },
    [0x79] = { 128,	8,	9, 5, 0 },
    [0x72] = { 128,	16,	9, 5, NAND_BUSWIDTH_16 },
    [0x49] = { 128,	16,	9, 5, NAND_BUSWIDTH_16 },
    [0x74] = { 128,	16,	9, 5, NAND_BUSWIDTH_16 },
    [0x59] = { 128,	16,	9, 5, NAND_BUSWIDTH_16 },

    [0x71] = { 256,	8,	9, 5, 0 },

    /*
     * These are the new chips with large page size. The pagesize and the
     * erasesize is determined from the extended id bytes
     */
# define LP_OPTIONS	(NAND_SAMSUNG_LP | NAND_NO_READRDY | NAND_NO_AUTOINCR)
# define LP_OPTIONS16	(LP_OPTIONS | NAND_BUSWIDTH_16)

    /* 512 Megabit */
    [0xa2] = { 64,	8,	0, 0, LP_OPTIONS },
    [0xf2] = { 64,	8,	0, 0, LP_OPTIONS },
    [0xb2] = { 64,	16,	0, 0, LP_OPTIONS16 },
    [0xc2] = { 64,	16,	0, 0, LP_OPTIONS16 },

    /* 1 Gigabit */
    [0xa1] = { 128,	8,	0, 0, LP_OPTIONS },
    [0xf1] = { 128,	8,	0, 0, LP_OPTIONS },
    [0xb1] = { 128,	16,	0, 0, LP_OPTIONS16 },
    [0xc1] = { 128,	16,	0, 0, LP_OPTIONS16 },

    /* 2 Gigabit */
    [0xaa] = { 256,	8,	0, 0, LP_OPTIONS },
    [0xda] = { 256,	8,	0, 0, LP_OPTIONS },
    [0xba] = { 256,	16,	0, 0, LP_OPTIONS16 },
    [0xca] = { 256,	16,	0, 0, LP_OPTIONS16 },

    /* 4 Gigabit */
    [0xac] = { 512,	8,	0, 0, LP_OPTIONS },
    [0xdc] = { 512,	8,	0, 0, LP_OPTIONS },
    [0xbc] = { 512,	16,	0, 0, LP_OPTIONS16 },
    [0xcc] = { 512,	16,	0, 0, LP_OPTIONS16 },

    /* 8 Gigabit */
    [0xa3] = { 1024,	8,	0, 0, LP_OPTIONS },
    [0xd3] = { 1024,	8,	0, 0, LP_OPTIONS },
    [0xb3] = { 1024,	16,	0, 0, LP_OPTIONS16 },
    [0xc3] = { 1024,	16,	0, 0, LP_OPTIONS16 },

    /* 16 Gigabit */
    [0xa5] = { 2048,	8,	0, 0, LP_OPTIONS },
    [0xd5] = { 2048,	8,	0, 0, LP_OPTIONS },
    [0xb5] = { 2048,	16,	0, 0, LP_OPTIONS16 },
    [0xc5] = { 2048,	16,	0, 0, LP_OPTIONS16 },
};

J
Juha Riihimäki 已提交
230
static void nand_reset(DeviceState *dev)
231
{
232
    NANDFlashState *s = NAND(dev);
233 234 235 236 237 238
    s->cmd = NAND_CMD_READ0;
    s->addr = 0;
    s->addrlen = 0;
    s->iolen = 0;
    s->offset = 0;
    s->status &= NAND_IOSTATUS_UNPROTCT;
239
    s->status |= NAND_IOSTATUS_READY;
240 241
}

242 243 244 245 246 247 248 249
static inline void nand_pushio_byte(NANDFlashState *s, uint8_t value)
{
    s->ioaddr[s->iolen++] = value;
    for (value = s->buswidth; --value;) {
        s->ioaddr[s->iolen++] = 0;
    }
}

P
Paul Brook 已提交
250
static void nand_command(NANDFlashState *s)
251
{
252
    unsigned int offset;
253 254 255 256 257 258 259
    switch (s->cmd) {
    case NAND_CMD_READ0:
        s->iolen = 0;
        break;

    case NAND_CMD_READID:
        s->ioaddr = s->io;
260 261 262 263 264 265 266 267 268 269 270 271
        s->iolen = 0;
        nand_pushio_byte(s, s->manf_id);
        nand_pushio_byte(s, s->chip_id);
        nand_pushio_byte(s, 'Q'); /* Don't-care byte (often 0xa5) */
        if (nand_flash_ids[s->chip_id].options & NAND_SAMSUNG_LP) {
            /* Page Size, Block Size, Spare Size; bit 6 indicates
             * 8 vs 16 bit width NAND.
             */
            nand_pushio_byte(s, (s->buswidth == 2) ? 0x55 : 0x15);
        } else {
            nand_pushio_byte(s, 0xc0); /* Multi-plane */
        }
272 273 274 275 276 277
        break;

    case NAND_CMD_RANDOMREAD2:
    case NAND_CMD_NOSERIALREAD2:
        if (!(nand_flash_ids[s->chip_id].options & NAND_SAMSUNG_LP))
            break;
278 279 280 281 282 283
        offset = s->addr & ((1 << s->addr_shift) - 1);
        s->blk_load(s, s->addr, offset);
        if (s->gnd)
            s->iolen = (1 << s->page_shift) - offset;
        else
            s->iolen = (1 << s->page_shift) + (1 << s->oob_shift) - offset;
284 285 286
        break;

    case NAND_CMD_RESET:
287
        nand_reset(DEVICE(s));
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
        break;

    case NAND_CMD_PAGEPROGRAM1:
        s->ioaddr = s->io;
        s->iolen = 0;
        break;

    case NAND_CMD_PAGEPROGRAM2:
        if (s->wp) {
            s->blk_write(s);
        }
        break;

    case NAND_CMD_BLOCKERASE1:
        break;

    case NAND_CMD_BLOCKERASE2:
305
        s->addr &= (1ull << s->addrlen * 8) - 1;
P
Peter Crosthwaite 已提交
306 307
        s->addr <<= nand_flash_ids[s->chip_id].options & NAND_SAMSUNG_LP ?
                                                                    16 : 8;
308 309 310 311 312 313 314 315

        if (s->wp) {
            s->blk_erase(s);
        }
        break;

    case NAND_CMD_READSTATUS:
        s->ioaddr = s->io;
316 317
        s->iolen = 0;
        nand_pushio_byte(s, s->status);
318 319 320 321 322 323 324
        break;

    default:
        printf("%s: Unknown NAND command 0x%02x\n", __FUNCTION__, s->cmd);
    }
}

J
Juan Quintela 已提交
325
static void nand_pre_save(void *opaque)
326
{
327
    NANDFlashState *s = NAND(opaque);
J
Juan Quintela 已提交
328 329

    s->ioaddr_vmstate = s->ioaddr - s->io;
330 331
}

J
Juan Quintela 已提交
332
static int nand_post_load(void *opaque, int version_id)
333
{
334
    NANDFlashState *s = NAND(opaque);
J
Juan Quintela 已提交
335 336

    if (s->ioaddr_vmstate > sizeof(s->io)) {
337
        return -EINVAL;
J
Juan Quintela 已提交
338 339
    }
    s->ioaddr = s->io + s->ioaddr_vmstate;
340 341 342 343

    return 0;
}

J
Juan Quintela 已提交
344 345
static const VMStateDescription vmstate_nand = {
    .name = "nand",
346 347 348
    .version_id = 1,
    .minimum_version_id = 1,
    .minimum_version_id_old = 1,
J
Juan Quintela 已提交
349 350 351 352 353 354 355 356 357 358 359 360
    .pre_save = nand_pre_save,
    .post_load = nand_post_load,
    .fields      = (VMStateField[]) {
        VMSTATE_UINT8(cle, NANDFlashState),
        VMSTATE_UINT8(ale, NANDFlashState),
        VMSTATE_UINT8(ce, NANDFlashState),
        VMSTATE_UINT8(wp, NANDFlashState),
        VMSTATE_UINT8(gnd, NANDFlashState),
        VMSTATE_BUFFER(io, NANDFlashState),
        VMSTATE_UINT32(ioaddr_vmstate, NANDFlashState),
        VMSTATE_INT32(iolen, NANDFlashState),
        VMSTATE_UINT32(cmd, NANDFlashState),
361
        VMSTATE_UINT64(addr, NANDFlashState),
J
Juan Quintela 已提交
362 363 364 365 366 367 368 369
        VMSTATE_INT32(addrlen, NANDFlashState),
        VMSTATE_INT32(status, NANDFlashState),
        VMSTATE_INT32(offset, NANDFlashState),
        /* XXX: do we want to save s->storage too? */
        VMSTATE_END_OF_LIST()
    }
};

J
Juha Riihimäki 已提交
370 371 372
static int nand_device_init(SysBusDevice *dev)
{
    int pagesize;
373
    NANDFlashState *s = NAND(dev);
J
Juha Riihimäki 已提交
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395

    s->buswidth = nand_flash_ids[s->chip_id].width >> 3;
    s->size = nand_flash_ids[s->chip_id].size << 20;
    if (nand_flash_ids[s->chip_id].options & NAND_SAMSUNG_LP) {
        s->page_shift = 11;
        s->erase_shift = 6;
    } else {
        s->page_shift = nand_flash_ids[s->chip_id].page_shift;
        s->erase_shift = nand_flash_ids[s->chip_id].erase_shift;
    }

    switch (1 << s->page_shift) {
    case 256:
        nand_init_256(s);
        break;
    case 512:
        nand_init_512(s);
        break;
    case 2048:
        nand_init_2048(s);
        break;
    default:
J
Juha Riihimäki 已提交
396 397
        error_report("Unsupported NAND block size");
        return -1;
J
Juha Riihimäki 已提交
398 399 400 401
    }

    pagesize = 1 << s->oob_shift;
    s->mem_oob = 1;
J
Juha Riihimäki 已提交
402 403 404 405 406 407 408 409 410 411 412
    if (s->bdrv) {
        if (bdrv_is_read_only(s->bdrv)) {
            error_report("Can't use a read-only drive");
            return -1;
        }
        if (bdrv_getlength(s->bdrv) >=
                (s->pages << s->page_shift) + (s->pages << s->oob_shift)) {
            pagesize = 0;
            s->mem_oob = 0;
        }
    } else {
J
Juha Riihimäki 已提交
413 414 415
        pagesize += 1 << s->page_shift;
    }
    if (pagesize) {
416
        s->storage = (uint8_t *) memset(g_malloc(s->pages * pagesize),
J
Juha Riihimäki 已提交
417 418 419 420 421 422 423 424
                        0xff, s->pages * pagesize);
    }
    /* Give s->ioaddr a sane value in case we save state before it is used. */
    s->ioaddr = s->io;

    return 0;
}

425 426 427 428 429 430 431 432 433
static Property nand_properties[] = {
    DEFINE_PROP_UINT8("manufacturer_id", NANDFlashState, manf_id, 0),
    DEFINE_PROP_UINT8("chip_id", NANDFlashState, chip_id, 0),
    DEFINE_PROP_DRIVE("drive", NANDFlashState, bdrv),
    DEFINE_PROP_END_OF_LIST(),
};

static void nand_class_init(ObjectClass *klass, void *data)
{
434
    DeviceClass *dc = DEVICE_CLASS(klass);
435 436 437
    SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);

    k->init = nand_device_init;
438 439 440
    dc->reset = nand_reset;
    dc->vmsd = &vmstate_nand;
    dc->props = nand_properties;
441 442
}

443
static const TypeInfo nand_info = {
444
    .name          = TYPE_NAND,
445 446 447
    .parent        = TYPE_SYS_BUS_DEVICE,
    .instance_size = sizeof(NANDFlashState),
    .class_init    = nand_class_init,
J
Juha Riihimäki 已提交
448 449
};

A
Andreas Färber 已提交
450
static void nand_register_types(void)
J
Juha Riihimäki 已提交
451
{
452
    type_register_static(&nand_info);
J
Juha Riihimäki 已提交
453 454
}

455 456 457 458 459 460
/*
 * Chip inputs are CLE, ALE, CE, WP, GND and eight I/O pins.  Chip
 * outputs are R/B and eight I/O pins.
 *
 * CE, WP and R/B are active low.
 */
J
Juha Riihimäki 已提交
461
void nand_setpins(DeviceState *dev, uint8_t cle, uint8_t ale,
J
Juan Quintela 已提交
462
                  uint8_t ce, uint8_t wp, uint8_t gnd)
463
{
464 465
    NANDFlashState *s = NAND(dev);

466 467 468 469 470
    s->cle = cle;
    s->ale = ale;
    s->ce = ce;
    s->wp = wp;
    s->gnd = gnd;
P
Peter Crosthwaite 已提交
471
    if (wp) {
472
        s->status |= NAND_IOSTATUS_UNPROTCT;
P
Peter Crosthwaite 已提交
473
    } else {
474
        s->status &= ~NAND_IOSTATUS_UNPROTCT;
P
Peter Crosthwaite 已提交
475
    }
476 477
}

J
Juha Riihimäki 已提交
478
void nand_getpins(DeviceState *dev, int *rb)
479 480 481 482
{
    *rb = 1;
}

J
Juha Riihimäki 已提交
483
void nand_setio(DeviceState *dev, uint32_t value)
484
{
485
    int i;
486 487
    NANDFlashState *s = NAND(dev);

488 489 490 491 492 493 494 495 496 497
    if (!s->ce && s->cle) {
        if (nand_flash_ids[s->chip_id].options & NAND_SAMSUNG_LP) {
            if (s->cmd == NAND_CMD_READ0 && value == NAND_CMD_LPREAD2)
                return;
            if (value == NAND_CMD_RANDOMREAD1) {
                s->addr &= ~((1 << s->addr_shift) - 1);
                s->addrlen = 0;
                return;
            }
        }
P
Peter Crosthwaite 已提交
498
        if (value == NAND_CMD_READ0) {
499
            s->offset = 0;
P
Peter Crosthwaite 已提交
500
        } else if (value == NAND_CMD_READ1) {
501 502
            s->offset = 0x100;
            value = NAND_CMD_READ0;
P
Peter Crosthwaite 已提交
503
        } else if (value == NAND_CMD_READ2) {
504 505 506 507 508 509 510 511 512 513 514 515
            s->offset = 1 << s->page_shift;
            value = NAND_CMD_READ0;
        }

        s->cmd = value;

        if (s->cmd == NAND_CMD_READSTATUS ||
                s->cmd == NAND_CMD_PAGEPROGRAM2 ||
                s->cmd == NAND_CMD_BLOCKERASE1 ||
                s->cmd == NAND_CMD_BLOCKERASE2 ||
                s->cmd == NAND_CMD_NOSERIALREAD2 ||
                s->cmd == NAND_CMD_RANDOMREAD2 ||
P
Peter Crosthwaite 已提交
516
                s->cmd == NAND_CMD_RESET) {
517
            nand_command(s);
P
Peter Crosthwaite 已提交
518
        }
519 520 521 522 523 524 525

        if (s->cmd != NAND_CMD_RANDOMREAD2) {
            s->addrlen = 0;
        }
    }

    if (s->ale) {
526 527 528 529 530
        unsigned int shift = s->addrlen * 8;
        unsigned int mask = ~(0xff << shift);
        unsigned int v = value << shift;

        s->addr = (s->addr & mask) | v;
531 532
        s->addrlen ++;

533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567
        switch (s->addrlen) {
        case 1:
            if (s->cmd == NAND_CMD_READID) {
                nand_command(s);
            }
            break;
        case 2: /* fix cache address as a byte address */
            s->addr <<= (s->buswidth - 1);
            break;
        case 3:
            if (!(nand_flash_ids[s->chip_id].options & NAND_SAMSUNG_LP) &&
                    (s->cmd == NAND_CMD_READ0 ||
                     s->cmd == NAND_CMD_PAGEPROGRAM1)) {
                nand_command(s);
            }
            break;
        case 4:
            if ((nand_flash_ids[s->chip_id].options & NAND_SAMSUNG_LP) &&
                    nand_flash_ids[s->chip_id].size < 256 && /* 1Gb or less */
                    (s->cmd == NAND_CMD_READ0 ||
                     s->cmd == NAND_CMD_PAGEPROGRAM1)) {
                nand_command(s);
            }
            break;
        case 5:
            if ((nand_flash_ids[s->chip_id].options & NAND_SAMSUNG_LP) &&
                    nand_flash_ids[s->chip_id].size >= 256 && /* 2Gb or more */
                    (s->cmd == NAND_CMD_READ0 ||
                     s->cmd == NAND_CMD_PAGEPROGRAM1)) {
                nand_command(s);
            }
            break;
        default:
            break;
        }
568 569 570
    }

    if (!s->cle && !s->ale && s->cmd == NAND_CMD_PAGEPROGRAM1) {
571 572 573 574 575
        if (s->iolen < (1 << s->page_shift) + (1 << s->oob_shift)) {
            for (i = s->buswidth; i--; value >>= 8) {
                s->io[s->iolen ++] = (uint8_t) (value & 0xff);
            }
        }
576 577 578
    } else if (!s->cle && !s->ale && s->cmd == NAND_CMD_COPYBACKPRG1) {
        if ((s->addr & ((1 << s->addr_shift) - 1)) <
                (1 << s->page_shift) + (1 << s->oob_shift)) {
579 580 581 582
            for (i = s->buswidth; i--; s->addr++, value >>= 8) {
                s->io[s->iolen + (s->addr & ((1 << s->addr_shift) - 1))] =
                    (uint8_t) (value & 0xff);
            }
583 584 585 586
        }
    }
}

J
Juha Riihimäki 已提交
587
uint32_t nand_getio(DeviceState *dev)
588 589
{
    int offset;
590
    uint32_t x = 0;
591
    NANDFlashState *s = NAND(dev);
592

593 594
    /* Allow sequential reading */
    if (!s->iolen && s->cmd == NAND_CMD_READ0) {
595
        offset = (int) (s->addr & ((1 << s->addr_shift) - 1)) + s->offset;
596 597 598 599 600 601 602 603 604
        s->offset = 0;

        s->blk_load(s, s->addr, offset);
        if (s->gnd)
            s->iolen = (1 << s->page_shift) - offset;
        else
            s->iolen = (1 << s->page_shift) + (1 << s->oob_shift) - offset;
    }

P
Peter Crosthwaite 已提交
605
    if (s->ce || s->iolen <= 0) {
606
        return 0;
P
Peter Crosthwaite 已提交
607
    }
608

609 610 611
    for (offset = s->buswidth; offset--;) {
        x |= s->ioaddr[offset] << (offset << 3);
    }
612 613 614 615 616 617 618 619
    /* after receiving READ STATUS command all subsequent reads will
     * return the status register value until another command is issued
     */
    if (s->cmd != NAND_CMD_READSTATUS) {
        s->addr   += s->buswidth;
        s->ioaddr += s->buswidth;
        s->iolen  -= s->buswidth;
    }
620 621 622
    return x;
}

J
Juha Riihimäki 已提交
623
uint32_t nand_getbuswidth(DeviceState *dev)
624
{
J
Juha Riihimäki 已提交
625
    NANDFlashState *s = (NANDFlashState *) dev;
626
    return s->buswidth << 3;
627 628
}

J
Juha Riihimäki 已提交
629
DeviceState *nand_init(BlockDriverState *bdrv, int manf_id, int chip_id)
630
{
J
Juha Riihimäki 已提交
631
    DeviceState *dev;
632 633

    if (nand_flash_ids[chip_id].size == 0) {
P
Paul Brook 已提交
634
        hw_error("%s: Unsupported NAND chip ID.\n", __FUNCTION__);
635
    }
J
Juha Riihimäki 已提交
636 637 638 639 640
    dev = qdev_create(NULL, "nand");
    qdev_prop_set_uint8(dev, "manufacturer_id", manf_id);
    qdev_prop_set_uint8(dev, "chip_id", chip_id);
    if (bdrv) {
        qdev_prop_set_drive_nofail(dev, "drive", bdrv);
641 642
    }

J
Juha Riihimäki 已提交
643 644
    qdev_init_nofail(dev);
    return dev;
645 646
}

A
Andreas Färber 已提交
647
type_init(nand_register_types)
648 649 650 651

#else

/* Program a single page */
P
Paul Brook 已提交
652
static void glue(nand_blk_write_, PAGE_SIZE)(NANDFlashState *s)
653
{
654
    uint64_t off, page, sector, soff;
655 656 657 658 659
    uint8_t iobuf[(PAGE_SECTORS + 2) * 0x200];
    if (PAGE(s->addr) >= s->pages)
        return;

    if (!s->bdrv) {
660
        mem_and(s->storage + PAGE_START(s->addr) + (s->addr & PAGE_MASK) +
661 662 663 664 665
                        s->offset, s->io, s->iolen);
    } else if (s->mem_oob) {
        sector = SECTOR(s->addr);
        off = (s->addr & PAGE_MASK) + s->offset;
        soff = SECTOR_OFFSET(s->addr);
666
        if (bdrv_read(s->bdrv, sector, iobuf, PAGE_SECTORS) < 0) {
667
            printf("%s: read error in sector %" PRIu64 "\n", __func__, sector);
668 669 670
            return;
        }

671
        mem_and(iobuf + (soff | off), s->io, MIN(s->iolen, PAGE_SIZE - off));
672 673
        if (off + s->iolen > PAGE_SIZE) {
            page = PAGE(s->addr);
674
            mem_and(s->storage + (page << OOB_SHIFT), s->io + PAGE_SIZE - off,
675 676 677
                            MIN(OOB_SIZE, off + s->iolen - PAGE_SIZE));
        }

678
        if (bdrv_write(s->bdrv, sector, iobuf, PAGE_SECTORS) < 0) {
679
            printf("%s: write error in sector %" PRIu64 "\n", __func__, sector);
680
        }
681 682 683 684
    } else {
        off = PAGE_START(s->addr) + (s->addr & PAGE_MASK) + s->offset;
        sector = off >> 9;
        soff = off & 0x1ff;
685
        if (bdrv_read(s->bdrv, sector, iobuf, PAGE_SECTORS + 2) < 0) {
686
            printf("%s: read error in sector %" PRIu64 "\n", __func__, sector);
687 688 689
            return;
        }

690
        mem_and(iobuf + soff, s->io, s->iolen);
691

692
        if (bdrv_write(s->bdrv, sector, iobuf, PAGE_SECTORS + 2) < 0) {
693
            printf("%s: write error in sector %" PRIu64 "\n", __func__, sector);
694
        }
695 696 697 698 699
    }
    s->offset = 0;
}

/* Erase a single block */
P
Paul Brook 已提交
700
static void glue(nand_blk_erase_, PAGE_SIZE)(NANDFlashState *s)
701
{
702
    uint64_t i, page, addr;
703 704 705
    uint8_t iobuf[0x200] = { [0 ... 0x1ff] = 0xff, };
    addr = s->addr & ~((1 << (ADDR_SHIFT + s->erase_shift)) - 1);

P
Peter Crosthwaite 已提交
706
    if (PAGE(addr) >= s->pages) {
707
        return;
P
Peter Crosthwaite 已提交
708
    }
709 710 711 712 713 714 715 716 717 718

    if (!s->bdrv) {
        memset(s->storage + PAGE_START(addr),
                        0xff, (PAGE_SIZE + OOB_SIZE) << s->erase_shift);
    } else if (s->mem_oob) {
        memset(s->storage + (PAGE(addr) << OOB_SHIFT),
                        0xff, OOB_SIZE << s->erase_shift);
        i = SECTOR(addr);
        page = SECTOR(addr + (ADDR_SHIFT + s->erase_shift));
        for (; i < page; i ++)
719
            if (bdrv_write(s->bdrv, i, iobuf, 1) < 0) {
720
                printf("%s: write error in sector %" PRIu64 "\n", __func__, i);
721
            }
722 723 724
    } else {
        addr = PAGE_START(addr);
        page = addr >> 9;
725
        if (bdrv_read(s->bdrv, page, iobuf, 1) < 0) {
726
            printf("%s: read error in sector %" PRIu64 "\n", __func__, page);
727
        }
728
        memset(iobuf + (addr & 0x1ff), 0xff, (~addr & 0x1ff) + 1);
729
        if (bdrv_write(s->bdrv, page, iobuf, 1) < 0) {
730
            printf("%s: write error in sector %" PRIu64 "\n", __func__, page);
731
        }
732 733 734 735

        memset(iobuf, 0xff, 0x200);
        i = (addr & ~0x1ff) + 0x200;
        for (addr += ((PAGE_SIZE + OOB_SIZE) << s->erase_shift) - 0x200;
P
Peter Crosthwaite 已提交
736
                        i < addr; i += 0x200) {
737
            if (bdrv_write(s->bdrv, i >> 9, iobuf, 1) < 0) {
738 739
                printf("%s: write error in sector %" PRIu64 "\n",
                       __func__, i >> 9);
740
            }
P
Peter Crosthwaite 已提交
741
        }
742 743

        page = i >> 9;
744
        if (bdrv_read(s->bdrv, page, iobuf, 1) < 0) {
745
            printf("%s: read error in sector %" PRIu64 "\n", __func__, page);
746
        }
747
        memset(iobuf, 0xff, ((addr - 1) & 0x1ff) + 1);
748
        if (bdrv_write(s->bdrv, page, iobuf, 1) < 0) {
749
            printf("%s: write error in sector %" PRIu64 "\n", __func__, page);
750
        }
751 752 753
    }
}

P
Paul Brook 已提交
754
static void glue(nand_blk_load_, PAGE_SIZE)(NANDFlashState *s,
755
                uint64_t addr, int offset)
756
{
P
Peter Crosthwaite 已提交
757
    if (PAGE(addr) >= s->pages) {
758
        return;
P
Peter Crosthwaite 已提交
759
    }
760 761 762

    if (s->bdrv) {
        if (s->mem_oob) {
763
            if (bdrv_read(s->bdrv, SECTOR(addr), s->io, PAGE_SECTORS) < 0) {
764 765
                printf("%s: read error in sector %" PRIu64 "\n",
                                __func__, SECTOR(addr));
766
            }
767 768 769 770 771 772
            memcpy(s->io + SECTOR_OFFSET(s->addr) + PAGE_SIZE,
                            s->storage + (PAGE(s->addr) << OOB_SHIFT),
                            OOB_SIZE);
            s->ioaddr = s->io + SECTOR_OFFSET(s->addr) + offset;
        } else {
            if (bdrv_read(s->bdrv, PAGE_START(addr) >> 9,
773
                                    s->io, (PAGE_SECTORS + 2)) < 0) {
774 775
                printf("%s: read error in sector %" PRIu64 "\n",
                                __func__, PAGE_START(addr) >> 9);
776
            }
777 778 779 780 781 782 783 784 785
            s->ioaddr = s->io + (PAGE_START(addr) & 0x1ff) + offset;
        }
    } else {
        memcpy(s->io, s->storage + PAGE_START(s->addr) +
                        offset, PAGE_SIZE + OOB_SIZE - offset);
        s->ioaddr = s->io;
    }
}

P
Paul Brook 已提交
786
static void glue(nand_init_, PAGE_SIZE)(NANDFlashState *s)
787 788 789 790 791 792 793 794 795 796 797 798 799 800 801
{
    s->oob_shift = PAGE_SHIFT - 5;
    s->pages = s->size >> PAGE_SHIFT;
    s->addr_shift = ADDR_SHIFT;

    s->blk_erase = glue(nand_blk_erase_, PAGE_SIZE);
    s->blk_write = glue(nand_blk_write_, PAGE_SIZE);
    s->blk_load = glue(nand_blk_load_, PAGE_SIZE);
}

# undef PAGE_SIZE
# undef PAGE_SHIFT
# undef PAGE_SECTORS
# undef ADDR_SHIFT
#endif	/* NAND_IO */