at91_nand.c 19.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 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 136 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 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 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 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635
/*
* Copyright (c) 2006-2022, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date           Author        Notes
* 2022-01-08     brightsally   first version
*/

#include <rtdevice.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <at91sam926x.h>
#include "at91_nand.h"

static struct nand_chip_id *chip;
static unsigned int bufsize = 528;
static unsigned char pages_per_block = 32;
static unsigned char eccsize = 6;
static struct rt_mtd_nand_device _partition[2];
/*****************************************************************************
nand_calculate_ecc function copy from uboot
*****************************************************************************/
#define u_char unsigned char
/* Define default oob placement schemes for large and small page devices */
static struct nand_ecclayout nand_oob_16 =
{

    .eccbytes = 6,
      .eccpos = {0, 1, 2, 3, 6, 7},
      .oobfree = {
            {
            .offset = 8,
            . length = 8
            }
    }
};

static struct nand_ecclayout nand_oob_64 =
{
    .eccbytes = 24,
      .eccpos = {
        40, 41, 42, 43, 44, 45, 46, 47,
        48, 49, 50, 51, 52, 53, 54, 55,
        56, 57, 58, 59, 60, 61, 62, 63
    },
    .oobfree = {
        {
        .offset = 2,
        .length = 38
        }
        }
};
/*
* Pre-calculated 256-way 1 byte column parity
*/

static const u_char nand_ecc_precalc_table[] =
{
    0x00, 0x55, 0x56, 0x03, 0x59, 0x0c, 0x0f, 0x5a, 0x5a, 0x0f, 0x0c, 0x59, 0x03, 0x56, 0x55, 0x00,
    0x65, 0x30, 0x33, 0x66, 0x3c, 0x69, 0x6a, 0x3f, 0x3f, 0x6a, 0x69, 0x3c, 0x66, 0x33, 0x30, 0x65,
    0x66, 0x33, 0x30, 0x65, 0x3f, 0x6a, 0x69, 0x3c, 0x3c, 0x69, 0x6a, 0x3f, 0x65, 0x30, 0x33, 0x66,
    0x03, 0x56, 0x55, 0x00, 0x5a, 0x0f, 0x0c, 0x59, 0x59, 0x0c, 0x0f, 0x5a, 0x00, 0x55, 0x56, 0x03,
    0x69, 0x3c, 0x3f, 0x6a, 0x30, 0x65, 0x66, 0x33, 0x33, 0x66, 0x65, 0x30, 0x6a, 0x3f, 0x3c, 0x69,
    0x0c, 0x59, 0x5a, 0x0f, 0x55, 0x00, 0x03, 0x56, 0x56, 0x03, 0x00, 0x55, 0x0f, 0x5a, 0x59, 0x0c,
    0x0f, 0x5a, 0x59, 0x0c, 0x56, 0x03, 0x00, 0x55, 0x55, 0x00, 0x03, 0x56, 0x0c, 0x59, 0x5a, 0x0f,
    0x6a, 0x3f, 0x3c, 0x69, 0x33, 0x66, 0x65, 0x30, 0x30, 0x65, 0x66, 0x33, 0x69, 0x3c, 0x3f, 0x6a,
    0x6a, 0x3f, 0x3c, 0x69, 0x33, 0x66, 0x65, 0x30, 0x30, 0x65, 0x66, 0x33, 0x69, 0x3c, 0x3f, 0x6a,
    0x0f, 0x5a, 0x59, 0x0c, 0x56, 0x03, 0x00, 0x55, 0x55, 0x00, 0x03, 0x56, 0x0c, 0x59, 0x5a, 0x0f,
    0x0c, 0x59, 0x5a, 0x0f, 0x55, 0x00, 0x03, 0x56, 0x56, 0x03, 0x00, 0x55, 0x0f, 0x5a, 0x59, 0x0c,
    0x69, 0x3c, 0x3f, 0x6a, 0x30, 0x65, 0x66, 0x33, 0x33, 0x66, 0x65, 0x30, 0x6a, 0x3f, 0x3c, 0x69,
    0x03, 0x56, 0x55, 0x00, 0x5a, 0x0f, 0x0c, 0x59, 0x59, 0x0c, 0x0f, 0x5a, 0x00, 0x55, 0x56, 0x03,
    0x66, 0x33, 0x30, 0x65, 0x3f, 0x6a, 0x69, 0x3c, 0x3c, 0x69, 0x6a, 0x3f, 0x65, 0x30, 0x33, 0x66,
    0x65, 0x30, 0x33, 0x66, 0x3c, 0x69, 0x6a, 0x3f, 0x3f, 0x6a, 0x69, 0x3c, 0x66, 0x33, 0x30, 0x65,
    0x00, 0x55, 0x56, 0x03, 0x59, 0x0c, 0x0f, 0x5a, 0x5a, 0x0f, 0x0c, 0x59, 0x03, 0x56, 0x55, 0x00
};

/**
* nand_calculate_ecc - [NAND Interface] Calculate 3-byte ECC for 256-byte block
* @mtd:    MTD block structure
* @dat:    raw data
* @ecc_code:   buffer for ECC
*/
int nand_calculate_ecc(const u_char *dat, u_char *ecc_code)
{
    uint8_t idx, reg1, reg2, reg3, tmp1, tmp2;
    int i;

    /* Initialize variables */
    reg1 = reg2 = reg3 = 0;

    /* Build up column parity */
    for (i = 0; i < 256; i++)
    {
        /* Get CP0 - CP5 from table */
        idx = nand_ecc_precalc_table[*dat++];
        reg1 ^= (idx & 0x3f);

        /* All bit XOR = 1 ? */
        if (idx & 0x40)
        {
            reg3 ^= (uint8_t) i;
            reg2 ^= ~((uint8_t) i);
        }
    }

    /* Create non-inverted ECC code from line parity */
    tmp1  = (reg3 & 0x80) >> 0; /* B7 -> B7 */
    tmp1 |= (reg2 & 0x80) >> 1; /* B7 -> B6 */
    tmp1 |= (reg3 & 0x40) >> 1; /* B6 -> B5 */
    tmp1 |= (reg2 & 0x40) >> 2; /* B6 -> B4 */
    tmp1 |= (reg3 & 0x20) >> 2; /* B5 -> B3 */
    tmp1 |= (reg2 & 0x20) >> 3; /* B5 -> B2 */
    tmp1 |= (reg3 & 0x10) >> 3; /* B4 -> B1 */
    tmp1 |= (reg2 & 0x10) >> 4; /* B4 -> B0 */

    tmp2  = (reg3 & 0x08) << 4; /* B3 -> B7 */
    tmp2 |= (reg2 & 0x08) << 3; /* B3 -> B6 */
    tmp2 |= (reg3 & 0x04) << 3; /* B2 -> B5 */
    tmp2 |= (reg2 & 0x04) << 2; /* B2 -> B4 */
    tmp2 |= (reg3 & 0x02) << 2; /* B1 -> B3 */
    tmp2 |= (reg2 & 0x02) << 1; /* B1 -> B2 */
    tmp2 |= (reg3 & 0x01) << 1; /* B0 -> B1 */
    tmp2 |= (reg2 & 0x01) << 0; /* B7 -> B0 */

    /* Calculate final ECC code */
#ifdef CONFIG_MTD_NAND_ECC_SMC
    ecc_code[0] = ~tmp2;
    ecc_code[1] = ~tmp1;
#else
    ecc_code[0] = ~tmp1;
    ecc_code[1] = ~tmp2;
#endif
    ecc_code[2] = ((~reg1) << 2) | 0x03;

    return 0;
}
/*********nand_calculate_ecc function copy from uboot end*********************/
static void at91_nand_udelay(rt_uint32_t us)
{
    rt_uint32_t len;
    for (; us > 0; us --)
        for (len = 0; len < 10; len++);
}
static void wait_udelay()
{
    if (chip->pagesize == 2048)at91_nand_udelay(2000);
    else    at91_nand_udelay(1);
}
void nand_enable_cs(void)
{
    writel(0x00004000, 0xfffff834);
    readl(0xfffff838);
    //rt_kprintf("===i=0x%x\r\n",i);
    wait_udelay();
}
void nand_disable_cs(void)
{
    writeb(0xff, 0x40400000);
    writel(0x00004000, 0xfffff830);
    readl(0xfffff830);
    wait_udelay();
}

void nand_write_cmd(unsigned char cmd)
{
    writeb(cmd, CMD_REG);
    wait_udelay();
}
void nand_write_addr(unsigned char addr)
{
    writeb(addr, ADDR_REG);
    wait_udelay();
}
void nand_write_data_byte(unsigned char data)
{
    writeb(data, DATA_REG);
    wait_udelay();
}

unsigned char nand_read_data_byte(void)
{
    return readb(DATA_REG);
}

static int  nand_wait_ready(void)
{
    unsigned int timeout = 10000;

    nand_write_cmd(CMD_STATUS);
    while ((!(nand_read_data_byte() & STATUS_READY)) && timeout--);
    if (!timeout)return -1;
    return 0;
}

//----------------------------------------------------------------
/* read chip id */
static rt_err_t at9260_nand_read_id(struct rt_mtd_nand_device *device)
{
    int manf_id, dev_id;
    unsigned int chipid;
    unsigned int i;

    nand_enable_cs();

    nand_write_cmd(CMD_READID);
    nand_write_addr(CMD_READ_1);
    manf_id  = nand_read_data_byte();
    dev_id   = nand_read_data_byte();
    nand_disable_cs();

    chipid = (manf_id << 8) | dev_id;

    for (i = 0; i < ARRAY_SIZE(nand_ids); i++)
    {
        if (chipid == nand_ids[i].chip_id)
            break;
    }

    if (i == ARRAY_SIZE(nand_ids))
    {
        rt_kprintf("NAND: Not found Manufacturer ID: %x," \
            "Chip ID: 0x%x\n", manf_id, dev_id);
        return -1;
    }

    //find nand chip
    rt_kprintf("NAND: Manufacturer ID: %x Chip ID: %x Total Block:%d\n", manf_id, dev_id, nand_ids[i].numblocks);

    chip->numblocks = nand_ids[i].numblocks;
    chip->pagesize  = nand_ids[i].pagesize;
    chip->blocksize = nand_ids[i].blocksize;
    chip->oobsize   = nand_ids[i].oobsize;
    chip->buswidth  = nand_ids[i].buswidth;
    chip->numblocks = nand_ids[i].numblocks;

    bufsize = chip->pagesize + chip->oobsize;
    eccsize = (chip->pagesize) * 3 / 256;

    if (chip->pagesize == 512)
    {
        pages_per_block = 32;
        rt_kprintf("===small block pages===== \n");
    }
    else if (chip->pagesize == 2048)
    {
        pages_per_block = 64;
        rt_kprintf("===big   block pages===== \n");
    }

    return i;
}

int nand_read_page9260(unsigned int block, unsigned int page, unsigned char *data, rt_uint32_t data_len, unsigned char *spare, rt_uint32_t spare_len)
{
    int i = 0;
    unsigned int blockpage;
    unsigned char buff1[bufsize];

    blockpage = block * (pages_per_block) + page;
    memset(buff1, 0xff, bufsize);

    nand_enable_cs();
    nand_write_cmd(CMD_READ_A0);
    nand_write_addr(0);
    if (bufsize == 2112)nand_write_addr(0);
    nand_write_addr(blockpage & 0xff);
    nand_write_addr((blockpage >> 8) & 0xff);
    nand_write_addr((blockpage >> 16) & 0xff);

    at91_nand_udelay(2000);

    if (bufsize == 528)nand_write_cmd(CMD_READ_1);
    else if (bufsize == 2112)nand_write_cmd(CMD_READ_2);

    for (i = 0; i < chip->pagesize; i++)buff1[i] = nand_read_data_byte();
    for (i = 0; i < chip->oobsize; i++)buff1[i + chip->pagesize] = nand_read_data_byte();

    nand_wait_ready();
    nand_disable_cs();

    if (data != RT_NULL)
        rt_memcpy(data, buff1, data_len);
    if (spare != RT_NULL)
        rt_memcpy(spare, &buff1[chip->pagesize], spare_len);

    return 0x00;
}
//============================================================================
static rt_err_t at9260_nand_read_page(struct rt_mtd_nand_device *device,
                                      rt_off_t page,
                                      rt_uint8_t *data, rt_uint32_t data_len,
                                      rt_uint8_t *spare, rt_uint32_t spare_len)

{
    return nand_read_page9260(page / pages_per_block, page % pages_per_block, data, data_len, spare, spare_len);
}

int nand_write_page9260(unsigned int block, unsigned int page, unsigned char *data, rt_uint32_t data_len, unsigned char *spare, rt_uint32_t spare_len)
{
    unsigned int blockpage;
    unsigned char buff2[chip->pagesize];
    unsigned char se[chip->oobsize];
    unsigned char ecc_code[eccsize];
    int i = 0;

    blockpage = block * (pages_per_block) + page;
    memset(buff2, 0xff, chip->pagesize);
    memset(se, 0xff, chip->oobsize);
    memset(ecc_code, 0xff, eccsize);

    nand_enable_cs();
    nand_write_cmd(CMD_WRITE_1);

    nand_write_addr(0);
    if (bufsize == 2112)nand_write_addr(0);
    nand_write_addr(blockpage & 0xff);
    nand_write_addr((blockpage >> 8) & 0xff);
    nand_write_addr((blockpage >> 16) & 0xff);

    at91_nand_udelay(2000);

    for (i = 0; i < data_len; i++)buff2[i] = *(data + i);
    for (i = 0; i < chip->pagesize; i++)
    {
        nand_write_data_byte(buff2[i]);
    }

#ifndef RT_USING_DFS_UFFS
    if (bufsize == 528)
    {
        //caclu ECC
        nand_calculate_ecc(buff2, ecc_code);
        nand_calculate_ecc(&buff2[256], &ecc_code[3]);
        //use uboot MTD ECC layout
        for (i = 0; i < 6; i++)
        {
            se[nand_oob_16.eccpos[i]] = ecc_code[i];
        }
    }
    else if (bufsize == 2112)
    {
        //caclu ECC
        for (i = 0; i < 8; i++)
        {
            nand_calculate_ecc(&buff2[256 * i], &(ecc_code[3 * i]));
        }
        //use uboot MTD ECC layout
        for (i = 0; i < 24; i++)
        {
            se[nand_oob_64.eccpos[i]] = ecc_code[i];
        }
    }
#else
    //UFFS do ECC
    for (i = 0; i < chip->oobsize; i++)
    {
        se[i] = *(spare + i);
    }
#endif

    for (i = 0; i < chip->oobsize; i++)
    {
        nand_write_data_byte(se[i]);
    }

    nand_write_cmd(CMD_WRITE_2);
    nand_wait_ready();
    nand_disable_cs();

    return RT_EOK;
}
//===========================================================================================
static rt_err_t at9260_nand_write_page(struct rt_mtd_nand_device *device,
                                       rt_off_t page,
                                       rt_uint8_t *data, rt_uint32_t data_len,
                                       rt_uint8_t *oob, rt_uint32_t spare_len)
{

    return nand_write_page9260(page / pages_per_block, page % pages_per_block, data, data_len, oob, spare_len);
}
//===========================================================================================
static rt_err_t at9260_nand_move_page(struct rt_mtd_nand_device *device, rt_off_t src_page, rt_off_t dst_page)
{
    return RT_EOK;
}
//===========================================================================================
static long at9260_nand_isbad(struct rt_mtd_nand_device *nand, uint32_t blk)
{
    int i = 0;
    unsigned char buff2[bufsize];
    unsigned int blockpage = blk * (chip->blocksize / chip->pagesize) + 0;

    memset(buff2, 0xff, bufsize);

    //read blk page 0
    nand_enable_cs();
    nand_write_cmd(CMD_READ_A0);
    nand_write_addr(0);
    if (bufsize == 2112)nand_write_addr(0);
    nand_write_addr(blockpage & 0xff);
    nand_write_addr((blockpage >> 8) & 0xff);
    nand_write_addr((blockpage >> 16) & 0xff);

    at91_nand_udelay(2000);

    if (bufsize == 528)nand_write_cmd(CMD_READ_1);
    else if (bufsize == 2112)nand_write_cmd(CMD_READ_2);

    for (i = 0; i < bufsize; i++)
    {
        buff2[i] = nand_read_data_byte();
    }
    nand_disable_cs();

    if (bufsize == 528)
    {
        if (buff2[5 + 512] != 0xff)
        {
            rt_kprintf("\r\n Bad Block=0x%x: Cannot read page #0 of block #%d,addr=0x%x \n\r", buff2[5 + 512], blk, blk * 512 * 32);
            return buff2[5 + 512];
        }
    }
    else if (bufsize == 2112)
    {
        if ((buff2[2048 + 0] != 0xff) && (buff2[2048 + 1] != 0xff))
        {
            rt_kprintf("\r\n Bad Block=0x%x: Cannot read page #0 of block #%d,addr=0x%x \n\r", buff2[0 + 2048], blk, blk * 2048 * 64);
            return buff2[0 + 2048] << 8 | buff2[0 + 2048];
        }
    }

    //read blk page 1
    blockpage = blockpage + 1;
    memset(buff2, 0xff, bufsize);
    nand_enable_cs();
    nand_write_cmd(CMD_READ_A0);
    nand_write_addr(0);
    if (bufsize == 2112)nand_write_addr(0);
    nand_write_addr(blockpage & 0xff);
    nand_write_addr((blockpage >> 8) & 0xff);
    nand_write_addr((blockpage >> 16) & 0xff);
    at91_nand_udelay(2000);

    if (bufsize == 528)nand_write_cmd(CMD_READ_1);
    else if (bufsize == 2112)nand_write_cmd(CMD_READ_2);


    for (i = 0; i < bufsize; i++)
    {
        buff2[i] = nand_read_data_byte();
    }

    nand_disable_cs();

    if (bufsize == 528)
    {
        if (buff2[5 + 512] != 0xff)
        {
            rt_kprintf("\r\n Bad Block=0x%x: Cannot read page #1 of block #%d,addr=0x%x \n\r", buff2[5 + 512], blk, blk * 512 * 32);
            return buff2[5 + 512];
        }
    }
    else if (bufsize == 2112)
    {
        if ((buff2[2048 + 0] != 0xff) && (buff2[2048 + 1] != 0xff))
        {
            rt_kprintf("\r\n Bad Block=0x%x: Cannot read page #1 of block #%d,addr=0x%x \n\r", buff2[0 + 2048], blk, blk * 2048 * 64);
            return buff2[0 + 2048] << 8 | buff2[0 + 2048];
        }
    }

    return GOODBLOCK;
}

static long at9260_nand_markbad(struct rt_mtd_nand_device *nand, uint32_t blk)
{
    unsigned int i = 0;
    unsigned int blockpage = blk * (chip->blocksize / chip->pagesize) + 0;
    unsigned char bad_flag = 0xff;
    long ret_bad;

    ret_bad = at9260_nand_isbad(nand, blk);

    if (bufsize == 528)bad_flag = ret_bad & 0xff;
    else if (bufsize == 2112)
    {
        if (bad_flag == 0xff)bad_flag = (ret_bad >> 8) & 0xff;
    }

    if (bad_flag != 0xff)
    {
        nand_enable_cs();
        nand_write_cmd(CMD_WRITE_1);
        nand_write_addr(0);
        if (bufsize == 2112)nand_write_addr(0);
        nand_write_addr(blockpage & 0xff);
        nand_write_addr((blockpage >> 8) & 0xff);
        nand_write_addr((blockpage >> 16) & 0xff);
        at91_nand_udelay(2000);

        for (i = 0; i < bufsize; i++)
        {
            nand_write_data_byte(0x00);
        }

        nand_write_cmd(CMD_WRITE_2);
        nand_wait_ready();
        nand_disable_cs();
    }
    return RT_EOK;
}
//---------------------------------------------------------------------------------------------
/* erase block */
static rt_err_t at9260_nand_erase_block(struct rt_mtd_nand_device *device, rt_uint32_t block)
{
    unsigned int row_address;


    //Calculate address used for erase
    row_address = (block) * (chip->blocksize / chip->pagesize);

    nand_enable_cs();
    nand_write_cmd(CMD_ERASE_1);
    nand_write_addr(row_address & 0xff);
    nand_write_addr((row_address >> 8) & 0xff);
    nand_write_addr((row_address >> 16) & 0xff);
    nand_write_cmd(CMD_ERASE_2);

    at91_nand_udelay(2000);
    nand_wait_ready();
    nand_disable_cs();
    return RT_EOK;
}

const static struct rt_mtd_nand_driver_ops _ops =
{
    at9260_nand_read_id,
    at9260_nand_read_page,
    at9260_nand_write_page,
    at9260_nand_move_page,
    at9260_nand_erase_block,
#ifndef RT_USING_DFS_UFFS
    at9260_nand_isbad,
    at9260_nand_markbad,
#else
    RT_NULL,
    RT_NULL,
#endif
};

void nand_eraseall(void);

int rt_hw_mtd_nand_init(void)
{
    unsigned int i, reg, index;

    /* Setup Smart Media, first enable the address range of CS3 in HMATRIX user interface  */
    reg = readl(AT91C_BASE_CCFG + CCFG_EBICSA);
    reg |= AT91C_EBI_CS3A_SM;
    writel(reg, AT91C_BASE_CCFG + CCFG_EBICSA);
    /* Configure SMC CS3 */
    writel((AT91C_SMC_NWESETUP_(1) | AT91C_SMC_NCS_WRSETUP_(0)   | AT91C_SMC_NRDSETUP_(1) | AT91C_SMC_NCS_RDSETUP_(0)), AT91C_BASE_SMC + SMC_SETUP3);
    writel((AT91C_SMC_NWEPULSE_(3) | AT91C_SMC_NCS_WRPULSE_(3)   | AT91C_SMC_NRDPULSE_(3) | AT91C_SMC_NCS_RDPULSE_(3)), AT91C_BASE_SMC + SMC_PULSE3);
    writel((AT91C_SMC_NWECYCLE_(5) | AT91C_SMC_NRDCYCLE_(5)), AT91C_BASE_SMC + SMC_CYCLE3);
    writel((AT91C_SMC_READMODE  | AT91C_SMC_WRITEMODE   | (0x0 << 5) | AT91C_SMC_DBW_WIDTH_BITS_8 | AT91_SMC_TDF_(2)),    AT91C_BASE_SMC + SMC_CTRL3);
    /* {"NANDCS", AT91C_PIN_PC(14), 1, PIO_PULLUP, PIO_OUTPUT} */
    writel((0x01 << 14), 0xfffff800 + 0x0044);
    writel((0x01 << 14), 0xfffff800 + 0x0060);
    writel((0x01 << 14), 0xfffff800 + 0x0030);
    writel((0x01 << 14), 0xfffff800 + 0x0010);
    writel((0x01 << 14), 0xfffff800 + 0x0000);
    /* enable PIOC clock  */
    writel(0x01 << 4, 0x10 + AT91C_BASE_PMC);
    i = at9260_nand_read_id(RT_NULL);
    index = i;

    chip->pagesize  = nand_ids[index].pagesize;
    chip->blocksize = nand_ids[index].blocksize;
    chip->oobsize   = nand_ids[index].oobsize;
    chip->buswidth  = nand_ids[index].buswidth;
    chip->numblocks = nand_ids[index].numblocks;

    _partition[0].page_size = chip->pagesize;
    _partition[1].page_size = chip->pagesize;
    _partition[0].pages_per_block = chip->blocksize / chip->pagesize;
      _partition[1].pages_per_block = chip->blocksize / chip->pagesize;
    _partition[0].oob_size = _partition[1].oob_size = chip->oobsize;
    _partition[0].oob_free = _partition[1].oob_free = chip->oobsize - (chip->pagesize / 256 * 3); //oob_free = oob_size - ecc_size


    _partition[0].block_total  = DATA_PART_ADDR / (chip->blocksize);
    _partition[0].block_start  = 0;
    _partition[0].block_end    = DATA_PART_ADDR / (chip->blocksize) - 1;

    _partition[1].block_total  = chip->numblocks - _partition[0].block_total;
    _partition[1].block_start  = _partition[0].block_end + 1;
    _partition[1].block_end    = chip->numblocks - 1;

    _partition[0].ops = &_ops;
      _partition[1].ops = &_ops;

    rt_mtd_nand_register_device("nand0", &_partition[0]);
    rt_mtd_nand_register_device("nand1", &_partition[1]);

    return RT_EOK;
}
INIT_DEVICE_EXPORT(rt_hw_mtd_nand_init);

#if defined(RT_USING_FINSH)
#include <finsh.h>
void nand_eraseall()
{
    int tmp=0;
    int index=0;


    if (chip->pagesize == 512)
        tmp = (DATA_PART_ADDR / 0x4000);            //0X4000=512*32=PAGER_SIZE*PAGES_PER_BLOCK
    else if (chip->pagesize == 2048)
        tmp = (DATA_PART_ADDR / 0x20000);                     //0X20000=2048*64

    for (index=tmp; index < chip->numblocks; index ++)
    {
        at9260_nand_erase_block(RT_NULL, index);
    }
}
FINSH_FUNCTION_EXPORT(nand_eraseall, erase all of block in the nand flash);

#endif //RT_USING_FINSH