Skip to content
Snippets Groups Projects
nand_base.c 73.9 KiB
Newer Older
  • Learn to ignore specific revisions
  • 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 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000
    /*
     *  drivers/mtd/nand.c
     *
     *  Overview:
     *   This is the generic MTD driver for NAND flash devices. It should be
     *   capable of working with almost all NAND chips currently available.
     *   Basic support for AG-AND chips is provided.
     *   
     *	Additional technical information is available on
     *	http://www.linux-mtd.infradead.org/tech/nand.html
     *	
     *  Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com)
     * 		  2002 Thomas Gleixner (tglx@linutronix.de)
     *
     *  02-08-2004  tglx: support for strange chips, which cannot auto increment 
     *		pages on read / read_oob
     *
     *  03-17-2004  tglx: Check ready before auto increment check. Simon Bayes
     *		pointed this out, as he marked an auto increment capable chip
     *		as NOAUTOINCR in the board driver.
     *		Make reads over block boundaries work too
     *
     *  04-14-2004	tglx: first working version for 2k page size chips
     *  
     *  05-19-2004  tglx: Basic support for Renesas AG-AND chips
     *
     *  09-24-2004  tglx: add support for hardware controllers (e.g. ECC) shared
     *		among multiple independend devices. Suggestions and initial patch
     *		from Ben Dooks <ben-mtd@fluff.org>
     *
     * Credits:
     *	David Woodhouse for adding multichip support  
     *	
     *	Aleph One Ltd. and Toby Churchill Ltd. for supporting the
     *	rework for 2K page size chips
     *
     * TODO:
     *	Enable cached programming for 2k page size chips
     *	Check, if mtd->ecctype should be set to MTD_ECC_HW
     *	if we have HW ecc support.
     *	The AG-AND chips have nice features for speed improvement,
     *	which are not supported yet. Read / program 4 pages in one go.
     *
     * $Id: nand_base.c,v 1.126 2004/12/13 11:22:25 lavinen Exp $
     *
     * This program is free software; you can redistribute it and/or modify
     * it under the terms of the GNU General Public License version 2 as
     * published by the Free Software Foundation.
     *
     */
    
    /* XXX U-BOOT XXX */
    #if 0
    #include <linux/delay.h>
    #include <linux/errno.h>
    #include <linux/sched.h>
    #include <linux/slab.h>
    #include <linux/types.h>
    #include <linux/mtd/mtd.h>
    #include <linux/mtd/nand.h>
    #include <linux/mtd/nand_ecc.h>
    #include <linux/mtd/compatmac.h>
    #include <linux/interrupt.h>
    #include <linux/bitops.h>
    #include <asm/io.h>
    
    #ifdef CONFIG_MTD_PARTITIONS
    #include <linux/mtd/partitions.h>
    #endif
    
    #else
    
    #include <common.h>
    
    #if (CONFIG_COMMANDS & CFG_CMD_NAND)
    
    #include <malloc.h>
    #include <watchdog.h>
    #include <linux/mtd/compat.h>
    #include <linux/mtd/mtd.h>
    #include <linux/mtd/nand.h>
    #include <linux/mtd/nand_ecc.h>
    
    #include <asm/io.h>
    #include <asm/errno.h>
    
    #ifdef CONFIG_JFFS2_NAND
    #include <jffs2/jffs2.h>
    #endif
    
    #endif
    
    /* Define default oob placement schemes for large and small page devices */
    static struct nand_oobinfo nand_oob_8 = {
    	.useecc = MTD_NANDECC_AUTOPLACE,
    	.eccbytes = 3,
    	.eccpos = {0, 1, 2},
    	.oobfree = { {3, 2}, {6, 2} }
    };
    
    static struct nand_oobinfo nand_oob_16 = {
    	.useecc = MTD_NANDECC_AUTOPLACE,
    	.eccbytes = 6,
    	.eccpos = {0, 1, 2, 3, 6, 7},
    	.oobfree = { {8, 8} }
    };
    
    static struct nand_oobinfo nand_oob_64 = {
    	.useecc = MTD_NANDECC_AUTOPLACE,
    	.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 = { {2, 38} }
    };
    
    /* This is used for padding purposes in nand_write_oob */
    static u_char ffchars[] = {
    	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    };
    
    /*
     * NAND low-level MTD interface functions
     */
    static void nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len);
    static void nand_read_buf(struct mtd_info *mtd, u_char *buf, int len);
    static int nand_verify_buf(struct mtd_info *mtd, const u_char *buf, int len);
    
    static int nand_read (struct mtd_info *mtd, loff_t from, size_t len, size_t * retlen, u_char * buf);
    static int nand_read_ecc (struct mtd_info *mtd, loff_t from, size_t len,
    			  size_t * retlen, u_char * buf, u_char * eccbuf, struct nand_oobinfo *oobsel);
    static int nand_read_oob (struct mtd_info *mtd, loff_t from, size_t len, size_t * retlen, u_char * buf);
    static int nand_write (struct mtd_info *mtd, loff_t to, size_t len, size_t * retlen, const u_char * buf);
    static int nand_write_ecc (struct mtd_info *mtd, loff_t to, size_t len,
    			   size_t * retlen, const u_char * buf, u_char * eccbuf, struct nand_oobinfo *oobsel);
    static int nand_write_oob (struct mtd_info *mtd, loff_t to, size_t len, size_t * retlen, const u_char *buf);
    /* XXX U-BOOT XXX */
    #if 0
    static int nand_writev (struct mtd_info *mtd, const struct kvec *vecs,
    			unsigned long count, loff_t to, size_t * retlen);
    static int nand_writev_ecc (struct mtd_info *mtd, const struct kvec *vecs,
    			unsigned long count, loff_t to, size_t * retlen, u_char *eccbuf, struct nand_oobinfo *oobsel);
    #endif
    static int nand_erase (struct mtd_info *mtd, struct erase_info *instr);
    static void nand_sync (struct mtd_info *mtd);
    
    /* Some internal functions */
    static int nand_write_page (struct mtd_info *mtd, struct nand_chip *this, int page, u_char *oob_buf,
    		struct nand_oobinfo *oobsel, int mode);
    #ifdef CONFIG_MTD_NAND_VERIFY_WRITE
    static int nand_verify_pages (struct mtd_info *mtd, struct nand_chip *this, int page, int numpages, 
    	u_char *oob_buf, struct nand_oobinfo *oobsel, int chipnr, int oobmode);
    #else
    #define nand_verify_pages(...) (0)
    #endif
    		
    static void nand_get_device (struct nand_chip *this, struct mtd_info *mtd, int new_state);
    
    /**
     * nand_release_device - [GENERIC] release chip
     * @mtd:	MTD device structure
     * 
     * Deselect, release chip lock and wake up anyone waiting on the device 
     */
    /* XXX U-BOOT XXX */
    #if 0
    static void nand_release_device (struct mtd_info *mtd)
    {
    	struct nand_chip *this = mtd->priv;
    
    	/* De-select the NAND device */
    	this->select_chip(mtd, -1);
    	/* Do we have a hardware controller ? */
    	if (this->controller) {
    		spin_lock(&this->controller->lock);
    		this->controller->active = NULL;
    		spin_unlock(&this->controller->lock);
    	}
    	/* Release the chip */
    	spin_lock (&this->chip_lock);
    	this->state = FL_READY;
    	wake_up (&this->wq);
    	spin_unlock (&this->chip_lock);
    }
    #else
    #define nand_release_device(mtd)	do {} while(0)
    #endif
    
    /**
     * nand_read_byte - [DEFAULT] read one byte from the chip
     * @mtd:	MTD device structure
     *
     * Default read function for 8bit buswith
     */
    static u_char nand_read_byte(struct mtd_info *mtd)
    {
    	struct nand_chip *this = mtd->priv;
    	return readb(this->IO_ADDR_R);
    }
    
    /**
     * nand_write_byte - [DEFAULT] write one byte to the chip
     * @mtd:	MTD device structure
     * @byte:	pointer to data byte to write
     *
     * Default write function for 8it buswith
     */
    static void nand_write_byte(struct mtd_info *mtd, u_char byte)
    {
    	struct nand_chip *this = mtd->priv;
    	writeb(byte, this->IO_ADDR_W);
    }
    
    /**
     * nand_read_byte16 - [DEFAULT] read one byte endianess aware from the chip
     * @mtd:	MTD device structure
     *
     * Default read function for 16bit buswith with 
     * endianess conversion
     */
    static u_char nand_read_byte16(struct mtd_info *mtd)
    {
    	struct nand_chip *this = mtd->priv;
    	return (u_char) cpu_to_le16(readw(this->IO_ADDR_R));
    }
    
    /**
     * nand_write_byte16 - [DEFAULT] write one byte endianess aware to the chip
     * @mtd:	MTD device structure
     * @byte:	pointer to data byte to write
     *
     * Default write function for 16bit buswith with
     * endianess conversion
     */
    static void nand_write_byte16(struct mtd_info *mtd, u_char byte)
    {
    	struct nand_chip *this = mtd->priv;
    	writew(le16_to_cpu((u16) byte), this->IO_ADDR_W);
    }
    
    /**
     * nand_read_word - [DEFAULT] read one word from the chip
     * @mtd:	MTD device structure
     *
     * Default read function for 16bit buswith without 
     * endianess conversion
     */
    static u16 nand_read_word(struct mtd_info *mtd)
    {
    	struct nand_chip *this = mtd->priv;
    	return readw(this->IO_ADDR_R);
    }
    
    /**
     * nand_write_word - [DEFAULT] write one word to the chip
     * @mtd:	MTD device structure
     * @word:	data word to write
     *
     * Default write function for 16bit buswith without 
     * endianess conversion
     */
    static void nand_write_word(struct mtd_info *mtd, u16 word)
    {
    	struct nand_chip *this = mtd->priv;
    	writew(word, this->IO_ADDR_W);
    }
    
    /**
     * nand_select_chip - [DEFAULT] control CE line
     * @mtd:	MTD device structure
     * @chip:	chipnumber to select, -1 for deselect
     *
     * Default select function for 1 chip devices.
     */
    static void nand_select_chip(struct mtd_info *mtd, int chip)
    {
    	struct nand_chip *this = mtd->priv;
    	switch(chip) {
    	case -1:
    		this->hwcontrol(mtd, NAND_CTL_CLRNCE);	
    		break;
    	case 0:
    		this->hwcontrol(mtd, NAND_CTL_SETNCE);
    		break;
    
    	default:
    		BUG();
    	}
    }
    
    /**
     * nand_write_buf - [DEFAULT] write buffer to chip
     * @mtd:	MTD device structure
     * @buf:	data buffer
     * @len:	number of bytes to write
     *
     * Default write function for 8bit buswith
     */
    static void nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
    {
    	int i;
    	struct nand_chip *this = mtd->priv;
    
    	for (i=0; i<len; i++)
    		writeb(buf[i], this->IO_ADDR_W);
    }
    
    /**
     * nand_read_buf - [DEFAULT] read chip data into buffer 
     * @mtd:	MTD device structure
     * @buf:	buffer to store date
     * @len:	number of bytes to read
     *
     * Default read function for 8bit buswith
     */
    static void nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
    {
    	int i;
    	struct nand_chip *this = mtd->priv;
    
    	for (i=0; i<len; i++)
    		buf[i] = readb(this->IO_ADDR_R);
    }
    
    /**
     * nand_verify_buf - [DEFAULT] Verify chip data against buffer 
     * @mtd:	MTD device structure
     * @buf:	buffer containing the data to compare
     * @len:	number of bytes to compare
     *
     * Default verify function for 8bit buswith
     */
    static int nand_verify_buf(struct mtd_info *mtd, const u_char *buf, int len)
    {
    	int i;
    	struct nand_chip *this = mtd->priv;
    
    	for (i=0; i<len; i++)
    		if (buf[i] != readb(this->IO_ADDR_R))
    			return -EFAULT;
    
    	return 0;
    }
    
    /**
     * nand_write_buf16 - [DEFAULT] write buffer to chip
     * @mtd:	MTD device structure
     * @buf:	data buffer
     * @len:	number of bytes to write
     *
     * Default write function for 16bit buswith
     */
    static void nand_write_buf16(struct mtd_info *mtd, const u_char *buf, int len)
    {
    	int i;
    	struct nand_chip *this = mtd->priv;
    	u16 *p = (u16 *) buf;
    	len >>= 1;
    	
    	for (i=0; i<len; i++)
    		writew(p[i], this->IO_ADDR_W);
    		
    }
    
    /**
     * nand_read_buf16 - [DEFAULT] read chip data into buffer 
     * @mtd:	MTD device structure
     * @buf:	buffer to store date
     * @len:	number of bytes to read
     *
     * Default read function for 16bit buswith
     */
    static void nand_read_buf16(struct mtd_info *mtd, u_char *buf, int len)
    {
    	int i;
    	struct nand_chip *this = mtd->priv;
    	u16 *p = (u16 *) buf;
    	len >>= 1;
    
    	for (i=0; i<len; i++)
    		p[i] = readw(this->IO_ADDR_R);
    }
    
    /**
     * nand_verify_buf16 - [DEFAULT] Verify chip data against buffer 
     * @mtd:	MTD device structure
     * @buf:	buffer containing the data to compare
     * @len:	number of bytes to compare
     *
     * Default verify function for 16bit buswith
     */
    static int nand_verify_buf16(struct mtd_info *mtd, const u_char *buf, int len)
    {
    	int i;
    	struct nand_chip *this = mtd->priv;
    	u16 *p = (u16 *) buf;
    	len >>= 1;
    
    	for (i=0; i<len; i++)
    		if (p[i] != readw(this->IO_ADDR_R))
    			return -EFAULT;
    
    	return 0;
    }
    
    /**
     * nand_block_bad - [DEFAULT] Read bad block marker from the chip
     * @mtd:	MTD device structure
     * @ofs:	offset from device start
     * @getchip:	0, if the chip is already selected
     *
     * Check, if the block is bad. 
     */
    static int nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
    {
    	int page, chipnr, res = 0;
    	struct nand_chip *this = mtd->priv;
    	u16 bad;
    
    	if (getchip) {
    		page = (int)(ofs >> this->page_shift);
    		chipnr = (int)(ofs >> this->chip_shift);
    
    		/* Grab the lock and see if the device is available */
    		nand_get_device (this, mtd, FL_READING);
    
    		/* Select the NAND device */
    		this->select_chip(mtd, chipnr);
    	} else 
    		page = (int) ofs;	
    
    	if (this->options & NAND_BUSWIDTH_16) {
    		this->cmdfunc (mtd, NAND_CMD_READOOB, this->badblockpos & 0xFE, page & this->pagemask);
    		bad = cpu_to_le16(this->read_word(mtd));
    		if (this->badblockpos & 0x1)
    			bad >>= 1;
    		if ((bad & 0xFF) != 0xff)
    			res = 1;
    	} else {
    		this->cmdfunc (mtd, NAND_CMD_READOOB, this->badblockpos, page & this->pagemask);
    		if (this->read_byte(mtd) != 0xff)
    			res = 1;
    	}
    		
    	if (getchip) {
    		/* Deselect and wake up anyone waiting on the device */
    		nand_release_device(mtd);
    	}	
    	
    	return res;
    }
    
    /**
     * nand_default_block_markbad - [DEFAULT] mark a block bad
     * @mtd:	MTD device structure
     * @ofs:	offset from device start
     *
     * This is the default implementation, which can be overridden by
     * a hardware specific driver.
    */
    static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
    {
    	struct nand_chip *this = mtd->priv;
    	u_char buf[2] = {0, 0};
    	size_t	retlen;
    	int block;
    	
    	/* Get block number */
    	block = ((int) ofs) >> this->bbt_erase_shift;
    	this->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
    
    	/* Do we have a flash based bad block table ? */
    	if (this->options & NAND_USE_FLASH_BBT)
    		return nand_update_bbt (mtd, ofs);
    		
    	/* We write two bytes, so we dont have to mess with 16 bit access */
    	ofs += mtd->oobsize + (this->badblockpos & ~0x01);
    	return nand_write_oob (mtd, ofs , 2, &retlen, buf);
    }
    
    /** 
     * nand_check_wp - [GENERIC] check if the chip is write protected
     * @mtd:	MTD device structure
     * Check, if the device is write protected 
     *
     * The function expects, that the device is already selected 
     */
    static int nand_check_wp (struct mtd_info *mtd)
    {
    	struct nand_chip *this = mtd->priv;
    	/* Check the WP bit */
    	this->cmdfunc (mtd, NAND_CMD_STATUS, -1, -1);
    	return (this->read_byte(mtd) & 0x80) ? 0 : 1; 
    }
    
    /**
     * nand_block_checkbad - [GENERIC] Check if a block is marked bad
     * @mtd:	MTD device structure
     * @ofs:	offset from device start
     * @getchip:	0, if the chip is already selected
     * @allowbbt:	1, if its allowed to access the bbt area
     *
     * Check, if the block is bad. Either by reading the bad block table or
     * calling of the scan function.
     */
    static int nand_block_checkbad (struct mtd_info *mtd, loff_t ofs, int getchip, int allowbbt)
    {
    	struct nand_chip *this = mtd->priv;
    	
    	if (!this->bbt)
    		return this->block_bad(mtd, ofs, getchip);
    	
    	/* Return info from the table */
    	return nand_isbad_bbt (mtd, ofs, allowbbt);
    }
    
    /**
     * nand_command - [DEFAULT] Send command to NAND device
     * @mtd:	MTD device structure
     * @command:	the command to be sent
     * @column:	the column address for this command, -1 if none
     * @page_addr:	the page address for this command, -1 if none
     *
     * Send command to NAND device. This function is used for small page
     * devices (256/512 Bytes per page)
     */
    static void nand_command (struct mtd_info *mtd, unsigned command, int column, int page_addr)
    {
    	register struct nand_chip *this = mtd->priv;
    
    	/* Begin command latch cycle */
    	this->hwcontrol(mtd, NAND_CTL_SETCLE);
    	/*
    	 * Write out the command to the device.
    	 */
    	if (command == NAND_CMD_SEQIN) {
    		int readcmd;
    
    		if (column >= mtd->oobblock) {
    			/* OOB area */
    			column -= mtd->oobblock;
    			readcmd = NAND_CMD_READOOB;
    		} else if (column < 256) {
    			/* First 256 bytes --> READ0 */
    			readcmd = NAND_CMD_READ0;
    		} else {
    			column -= 256;
    			readcmd = NAND_CMD_READ1;
    		}
    		this->write_byte(mtd, readcmd);
    	}
    	this->write_byte(mtd, command);
    
    	/* Set ALE and clear CLE to start address cycle */
    	this->hwcontrol(mtd, NAND_CTL_CLRCLE);
    
    	if (column != -1 || page_addr != -1) {
    		this->hwcontrol(mtd, NAND_CTL_SETALE);
    
    		/* Serially input address */
    		if (column != -1) {
    			/* Adjust columns for 16 bit buswidth */
    			if (this->options & NAND_BUSWIDTH_16)
    				column >>= 1;
    			this->write_byte(mtd, column);
    		}
    		if (page_addr != -1) {
    			this->write_byte(mtd, (unsigned char) (page_addr & 0xff));
    			this->write_byte(mtd, (unsigned char) ((page_addr >> 8) & 0xff));
    			/* One more address cycle for devices > 32MiB */
    			if (this->chipsize > (32 << 20))
    				this->write_byte(mtd, (unsigned char) ((page_addr >> 16) & 0x0f));
    		}
    		/* Latch in address */
    		this->hwcontrol(mtd, NAND_CTL_CLRALE);
    	}
    	
    	/* 
    	 * program and erase have their own busy handlers 
    	 * status and sequential in needs no delay
    	*/
    	switch (command) {
    			
    	case NAND_CMD_PAGEPROG:
    	case NAND_CMD_ERASE1:
    	case NAND_CMD_ERASE2:
    	case NAND_CMD_SEQIN:
    	case NAND_CMD_STATUS:
    		return;
    
    	case NAND_CMD_RESET:
    		if (this->dev_ready)	
    			break;
    		udelay(this->chip_delay);
    		this->hwcontrol(mtd, NAND_CTL_SETCLE);
    		this->write_byte(mtd, NAND_CMD_STATUS);
    		this->hwcontrol(mtd, NAND_CTL_CLRCLE);
    		while ( !(this->read_byte(mtd) & 0x40));
    		return;
    
    	/* This applies to read commands */	
    	default:
    		/* 
    		 * If we don't have access to the busy pin, we apply the given
    		 * command delay
    		*/
    		if (!this->dev_ready) {
    			udelay (this->chip_delay);
    			return;
    		}	
    	}
    	
    	/* Apply this short delay always to ensure that we do wait tWB in
    	 * any case on any machine. */
    	ndelay (100);
    	/* wait until command is processed */
    	while (!this->dev_ready(mtd));
    }
    
    /**
     * nand_command_lp - [DEFAULT] Send command to NAND large page device
     * @mtd:	MTD device structure
     * @command:	the command to be sent
     * @column:	the column address for this command, -1 if none
     * @page_addr:	the page address for this command, -1 if none
     *
     * Send command to NAND device. This is the version for the new large page devices
     * We dont have the seperate regions as we have in the small page devices.
     * We must emulate NAND_CMD_READOOB to keep the code compatible.
     *
     */
    static void nand_command_lp (struct mtd_info *mtd, unsigned command, int column, int page_addr)
    {
    	register struct nand_chip *this = mtd->priv;
    
    	/* Emulate NAND_CMD_READOOB */
    	if (command == NAND_CMD_READOOB) {
    		column += mtd->oobblock;
    		command = NAND_CMD_READ0;
    	}
    	
    		
    	/* Begin command latch cycle */
    	this->hwcontrol(mtd, NAND_CTL_SETCLE);
    	/* Write out the command to the device. */
    	this->write_byte(mtd, command);
    	/* End command latch cycle */
    	this->hwcontrol(mtd, NAND_CTL_CLRCLE);
    
    	if (column != -1 || page_addr != -1) {
    		this->hwcontrol(mtd, NAND_CTL_SETALE);
    
    		/* Serially input address */
    		if (column != -1) {
    			/* Adjust columns for 16 bit buswidth */
    			if (this->options & NAND_BUSWIDTH_16)
    				column >>= 1;
    			this->write_byte(mtd, column & 0xff);
    			this->write_byte(mtd, column >> 8);
    		}	
    		if (page_addr != -1) {
    			this->write_byte(mtd, (unsigned char) (page_addr & 0xff));
    			this->write_byte(mtd, (unsigned char) ((page_addr >> 8) & 0xff));
    			/* One more address cycle for devices > 128MiB */
    			if (this->chipsize > (128 << 20))
    				this->write_byte(mtd, (unsigned char) ((page_addr >> 16) & 0xff));
    		}
    		/* Latch in address */
    		this->hwcontrol(mtd, NAND_CTL_CLRALE);
    	}
    	
    	/* 
    	 * program and erase have their own busy handlers 
    	 * status and sequential in needs no delay
    	*/
    	switch (command) {
    			
    	case NAND_CMD_CACHEDPROG:
    	case NAND_CMD_PAGEPROG:
    	case NAND_CMD_ERASE1:
    	case NAND_CMD_ERASE2:
    	case NAND_CMD_SEQIN:
    	case NAND_CMD_STATUS:
    		return;
    
    
    	case NAND_CMD_RESET:
    		if (this->dev_ready)	
    			break;
    		udelay(this->chip_delay);
    		this->hwcontrol(mtd, NAND_CTL_SETCLE);
    		this->write_byte(mtd, NAND_CMD_STATUS);
    		this->hwcontrol(mtd, NAND_CTL_CLRCLE);
    		while ( !(this->read_byte(mtd) & 0x40));
    		return;
    
    	case NAND_CMD_READ0:
    		/* Begin command latch cycle */
    		this->hwcontrol(mtd, NAND_CTL_SETCLE);
    		/* Write out the start read command */
    		this->write_byte(mtd, NAND_CMD_READSTART);
    		/* End command latch cycle */
    		this->hwcontrol(mtd, NAND_CTL_CLRCLE);
    		/* Fall through into ready check */
    		
    	/* This applies to read commands */	
    	default:
    		/* 
    		 * If we don't have access to the busy pin, we apply the given
    		 * command delay
    		*/
    		if (!this->dev_ready) {
    			udelay (this->chip_delay);
    			return;
    		}	
    	}
    	
    	/* Apply this short delay always to ensure that we do wait tWB in
    	 * any case on any machine. */
    	ndelay (100);
    	/* wait until command is processed */
    	while (!this->dev_ready(mtd));
    }
    
    /**
     * nand_get_device - [GENERIC] Get chip for selected access
     * @this:	the nand chip descriptor
     * @mtd:	MTD device structure
     * @new_state:	the state which is requested 
     *
     * Get the device and lock it for exclusive access
     */
    /* XXX U-BOOT XXX */
    #if 0
    static void nand_get_device (struct nand_chip *this, struct mtd_info *mtd, int new_state)
    {
    	struct nand_chip *active = this;
    
    	DECLARE_WAITQUEUE (wait, current);
    
    	/* 
    	 * Grab the lock and see if the device is available 
    	*/
    retry:
    	/* Hardware controller shared among independend devices */
    	if (this->controller) {
    		spin_lock (&this->controller->lock);
    		if (this->controller->active)
    			active = this->controller->active;
    		else
    			this->controller->active = this;
    		spin_unlock (&this->controller->lock);
    	}
    	
    	if (active == this) {
    		spin_lock (&this->chip_lock);
    		if (this->state == FL_READY) {
    			this->state = new_state;
    			spin_unlock (&this->chip_lock);
    			return;
    		}
    	}	
    	set_current_state (TASK_UNINTERRUPTIBLE);
    	add_wait_queue (&active->wq, &wait);
    	spin_unlock (&active->chip_lock);
    	schedule ();
    	remove_wait_queue (&active->wq, &wait);
    	goto retry;
    }
    #else
    static void nand_get_device (struct nand_chip *this, struct mtd_info *mtd, int new_state) {}
    #endif
    
    /**
     * nand_wait - [DEFAULT]  wait until the command is done
     * @mtd:	MTD device structure
     * @this:	NAND chip structure
     * @state:	state to select the max. timeout value
     *
     * Wait for command done. This applies to erase and program only
     * Erase can take up to 400ms and program up to 20ms according to 
     * general NAND and SmartMedia specs
     *
    */
    /* XXX U-BOOT XXX */
    #if 0
    static int nand_wait(struct mtd_info *mtd, struct nand_chip *this, int state)
    {
    	unsigned long	timeo = jiffies;
    	int	status;
    	
    	if (state == FL_ERASING)
    		 timeo += (HZ * 400) / 1000;
    	else
    		 timeo += (HZ * 20) / 1000;
    
    	/* Apply this short delay always to ensure that we do wait tWB in
    	 * any case on any machine. */
    	ndelay (100);
    
    	if ((state == FL_ERASING) && (this->options & NAND_IS_AND))
    		this->cmdfunc (mtd, NAND_CMD_STATUS_MULTI, -1, -1);
    	else	
    		this->cmdfunc (mtd, NAND_CMD_STATUS, -1, -1);
    
    	while (time_before(jiffies, timeo)) {		
    		/* Check, if we were interrupted */
    		if (this->state != state)
    			return 0;
    
    		if (this->dev_ready) {
    			if (this->dev_ready(mtd))
    				break;	
    		} else {
    			if (this->read_byte(mtd) & NAND_STATUS_READY)
    				break;
    		}
    		yield ();
    	}
    	status = (int) this->read_byte(mtd);
    	return status;
    
    	return 0;
    }
    #else
    static int nand_wait(struct mtd_info *mtd, struct nand_chip *this, int state)
    {
    	/* TODO */
    	return 0;
    }
    #endif
    
    /**
     * nand_write_page - [GENERIC] write one page
     * @mtd:	MTD device structure
     * @this:	NAND chip structure
     * @page: 	startpage inside the chip, must be called with (page & this->pagemask)
     * @oob_buf:	out of band data buffer
     * @oobsel:	out of band selecttion structre
     * @cached:	1 = enable cached programming if supported by chip
     *
     * Nand_page_program function is used for write and writev !
     * This function will always program a full page of data
     * If you call it with a non page aligned buffer, you're lost :)
     *
     * Cached programming is not supported yet.
     */
    static int nand_write_page (struct mtd_info *mtd, struct nand_chip *this, int page, 
    	u_char *oob_buf,  struct nand_oobinfo *oobsel, int cached)
    {
    	int 	i, status;
    	u_char	ecc_code[32];
    	int	eccmode = oobsel->useecc ? this->eccmode : NAND_ECC_NONE;
    	int  	*oob_config = oobsel->eccpos;
    	int	datidx = 0, eccidx = 0, eccsteps = this->eccsteps;
    	int	eccbytes = 0;
    	
    	/* FIXME: Enable cached programming */
    	cached = 0;
    	
    	/* Send command to begin auto page programming */
    	this->cmdfunc (mtd, NAND_CMD_SEQIN, 0x00, page);
    
    	/* Write out complete page of data, take care of eccmode */
    	switch (eccmode) {
    	/* No ecc, write all */
    	case NAND_ECC_NONE:
    		printk (KERN_WARNING "Writing data without ECC to NAND-FLASH is not recommended\n");
    		this->write_buf(mtd, this->data_poi, mtd->oobblock);
    		break;
    		
    	/* Software ecc 3/256, write all */
    	case NAND_ECC_SOFT:
    		for (; eccsteps; eccsteps--) {
    			this->calculate_ecc(mtd, &this->data_poi[datidx], ecc_code);
    			for (i = 0; i < 3; i++, eccidx++)
    				oob_buf[oob_config[eccidx]] = ecc_code[i];
    			datidx += this->eccsize;
    		}
    		this->write_buf(mtd, this->data_poi, mtd->oobblock);
    		break;
    	default:
    		eccbytes = this->eccbytes;
    		for (; eccsteps; eccsteps--) {
    			/* enable hardware ecc logic for write */
    			this->enable_hwecc(mtd, NAND_ECC_WRITE);
    			this->write_buf(mtd, &this->data_poi[datidx], this->eccsize);
    			this->calculate_ecc(mtd, &this->data_poi[datidx], ecc_code);
    			for (i = 0; i < eccbytes; i++, eccidx++)
    				oob_buf[oob_config[eccidx]] = ecc_code[i];
    			/* If the hardware ecc provides syndromes then
    			 * the ecc code must be written immidiately after
    			 * the data bytes (words) */
    			if (this->options & NAND_HWECC_SYNDROME)
    				this->write_buf(mtd, ecc_code, eccbytes);
    			datidx += this->eccsize;
    		}
    		break;
    	}
    										
    	/* Write out OOB data */
    	if (this->options & NAND_HWECC_SYNDROME)
    		this->write_buf(mtd, &oob_buf[oobsel->eccbytes], mtd->oobsize - oobsel->eccbytes);
    	else 
    		this->write_buf(mtd, oob_buf, mtd->oobsize);
    
    	/* Send command to actually program the data */
    	this->cmdfunc (mtd, cached ? NAND_CMD_CACHEDPROG : NAND_CMD_PAGEPROG, -1, -1);
    
    	if (!cached) {
    		/* call wait ready function */
    		status = this->waitfunc (mtd, this, FL_WRITING);
    		/* See if device thinks it succeeded */
    		if (status & 0x01) {
    			DEBUG (MTD_DEBUG_LEVEL0, "%s: " "Failed write, page 0x%08x, ", __FUNCTION__, page);
    			return -EIO;
    		}
    	} else {
    		/* FIXME: Implement cached programming ! */
    		/* wait until cache is ready*/
    		// status = this->waitfunc (mtd, this, FL_CACHEDRPG);
    	}
    	return 0;	
    }
    
    #ifdef CONFIG_MTD_NAND_VERIFY_WRITE
    /**
     * nand_verify_pages - [GENERIC] verify the chip contents after a write
     * @mtd:	MTD device structure
     * @this:	NAND chip structure
     * @page: 	startpage inside the chip, must be called with (page & this->pagemask)
     * @numpages:	number of pages to verify
     * @oob_buf:	out of band data buffer
     * @oobsel:	out of band selecttion structre
     * @chipnr:	number of the current chip
     * @oobmode:	1 = full buffer verify, 0 = ecc only
     *
     * The NAND device assumes that it is always writing to a cleanly erased page.
     * Hence, it performs its internal write verification only on bits that 
     * transitioned from 1 to 0. The device does NOT verify the whole page on a
     * byte by byte basis. It is possible that the page was not completely erased 
     * or the page is becoming unusable due to wear. The read with ECC would catch 
     * the error later when the ECC page check fails, but we would rather catch 
     * it early in the page write stage. Better to write no data than invalid data.
     */
    static int nand_verify_pages (struct mtd_info *mtd, struct nand_chip *this, int page, int numpages, 
    	u_char *oob_buf, struct nand_oobinfo *oobsel, int chipnr, int oobmode)
    {
    	int 	i, j, datidx = 0, oobofs = 0, res = -EIO;
    	int	eccsteps = this->eccsteps;
    	int	hweccbytes; 
    	u_char 	oobdata[64];
    
    	hweccbytes = (this->options & NAND_HWECC_SYNDROME) ? (oobsel->eccbytes / eccsteps) : 0;
    
    	/* Send command to read back the first page */
    	this->cmdfunc (mtd, NAND_CMD_READ0, 0, page);
    
    	for(;;) {
    		for (j = 0; j < eccsteps; j++) {
    			/* Loop through and verify the data */
    			if (this->verify_buf(mtd, &this->data_poi[datidx], mtd->eccsize)) {
    				DEBUG (MTD_DEBUG_LEVEL0, "%s: " "Failed write verify, page 0x%08x ", __FUNCTION__, page);
    				goto out;
    			}
    			datidx += mtd->eccsize;
    			/* Have we a hw generator layout ? */
    			if (!hweccbytes)
    				continue;
    			if (this->verify_buf(mtd, &this->oob_buf[oobofs], hweccbytes)) {
    				DEBUG (MTD_DEBUG_LEVEL0, "%s: " "Failed write verify, page 0x%08x ", __FUNCTION__, page);
    				goto out;
    			}
    			oobofs += hweccbytes;
    		}
    
    		/* check, if we must compare all data or if we just have to
    		 * compare the ecc bytes
    		 */
    		if (oobmode) {
    			if (this->verify_buf(mtd, &oob_buf[oobofs], mtd->oobsize - hweccbytes * eccsteps)) {
    				DEBUG (MTD_DEBUG_LEVEL0, "%s: " "Failed write verify, page 0x%08x ", __FUNCTION__, page);
    				goto out;
    			}
    		} else {
    			/* Read always, else autoincrement fails */
    			this->read_buf(mtd, oobdata, mtd->oobsize - hweccbytes * eccsteps);
    
    			if (oobsel->useecc != MTD_NANDECC_OFF && !hweccbytes) {
    				int ecccnt = oobsel->eccbytes;
    		
    				for (i = 0; i < ecccnt; i++) {