Skip to content
Snippets Groups Projects
nand_base.c 73.9 KiB
Newer Older
  • Learn to ignore specific revisions
  • 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000
    					int idx = oobsel->eccpos[i];
    					if (oobdata[idx] != oob_buf[oobofs + idx] ) {
    						DEBUG (MTD_DEBUG_LEVEL0,
    					       	"%s: Failed ECC write "
    						"verify, page 0x%08x, " "%6i bytes were succesful\n", __FUNCTION__, page, i);
    						goto out;
    					}
    				}
    			}	
    		}
    		oobofs += mtd->oobsize - hweccbytes * eccsteps;
    		page++;
    		numpages--;
    
    		/* Apply delay or wait for ready/busy pin 
    		 * Do this before the AUTOINCR check, so no problems
    		 * arise if a chip which does auto increment
    		 * is marked as NOAUTOINCR by the board driver.
    		 * Do this also before returning, so the chip is
    		 * ready for the next command.
    		*/
    		if (!this->dev_ready) 
    			udelay (this->chip_delay);
    		else
    			while (!this->dev_ready(mtd));	
    
    		/* All done, return happy */
    		if (!numpages)
    			return 0;
    		
    			
    		/* Check, if the chip supports auto page increment */ 
    		if (!NAND_CANAUTOINCR(this))
    			this->cmdfunc (mtd, NAND_CMD_READ0, 0x00, page);
    	}
    	/* 
    	 * Terminate the read command. We come here in case of an error
    	 * So we must issue a reset command.
    	 */
    out:	 
    	this->cmdfunc (mtd, NAND_CMD_RESET, -1, -1);
    	return res;
    }
    #endif
    
    /**
     * nand_read - [MTD Interface] MTD compability function for nand_read_ecc
     * @mtd:	MTD device structure
     * @from:	offset to read from
     * @len:	number of bytes to read
     * @retlen:	pointer to variable to store the number of read bytes
     * @buf:	the databuffer to put data
     *
     * This function simply calls nand_read_ecc with oob buffer and oobsel = NULL
    */
    static int nand_read (struct mtd_info *mtd, loff_t from, size_t len, size_t * retlen, u_char * buf)
    {
    	return nand_read_ecc (mtd, from, len, retlen, buf, NULL, NULL);
    }			   
    
    
    /**
     * nand_read_ecc - [MTD Interface] Read data with ECC
     * @mtd:	MTD device structure
     * @from:	offset to read from
     * @len:	number of bytes to read
     * @retlen:	pointer to variable to store the number of read bytes
     * @buf:	the databuffer to put data
     * @oob_buf:	filesystem supplied oob data buffer
     * @oobsel:	oob selection structure
     *
     * NAND read with ECC
     */
    static int nand_read_ecc (struct mtd_info *mtd, loff_t from, size_t len,
    			  size_t * retlen, u_char * buf, u_char * oob_buf, struct nand_oobinfo *oobsel)
    {
    	int i, j, col, realpage, page, end, ecc, chipnr, sndcmd = 1;
    	int read = 0, oob = 0, ecc_status = 0, ecc_failed = 0;
    	struct nand_chip *this = mtd->priv;
    	u_char *data_poi, *oob_data = oob_buf;
    	u_char ecc_calc[32];
    	u_char ecc_code[32];
            int eccmode, eccsteps;
    	int	*oob_config, datidx;
    	int	blockcheck = (1 << (this->phys_erase_shift - this->page_shift)) - 1;
    	int	eccbytes;
    	int	compareecc = 1;
    	int	oobreadlen;
    
    
    	DEBUG (MTD_DEBUG_LEVEL3, "nand_read_ecc: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len);
    
    	/* Do not allow reads past end of device */
    	if ((from + len) > mtd->size) {
    		DEBUG (MTD_DEBUG_LEVEL0, "nand_read_ecc: Attempt read beyond end of device\n");
    		*retlen = 0;
    		return -EINVAL;
    	}
    
    	/* Grab the lock and see if the device is available */
    	nand_get_device (this, mtd ,FL_READING);
    
    	/* use userspace supplied oobinfo, if zero */
    	if (oobsel == NULL)
    		oobsel = &mtd->oobinfo;
    	
    	/* Autoplace of oob data ? Use the default placement scheme */
    	if (oobsel->useecc == MTD_NANDECC_AUTOPLACE)
    		oobsel = this->autooob;
    		
    	eccmode = oobsel->useecc ? this->eccmode : NAND_ECC_NONE;
    	oob_config = oobsel->eccpos;
    
    	/* Select the NAND device */
    	chipnr = (int)(from >> this->chip_shift);
    	this->select_chip(mtd, chipnr);
    
    	/* First we calculate the starting page */
    	realpage = (int) (from >> this->page_shift);
    	page = realpage & this->pagemask;
    
    	/* Get raw starting column */
    	col = from & (mtd->oobblock - 1);
    
    	end = mtd->oobblock;
    	ecc = this->eccsize;
    	eccbytes = this->eccbytes;
    	
    	if ((eccmode == NAND_ECC_NONE) || (this->options & NAND_HWECC_SYNDROME))
    		compareecc = 0;
    
    	oobreadlen = mtd->oobsize;
    	if (this->options & NAND_HWECC_SYNDROME) 
    		oobreadlen -= oobsel->eccbytes;
    
    	/* Loop until all data read */
    	while (read < len) {
    		
    		int aligned = (!col && (len - read) >= end);
    		/* 
    		 * If the read is not page aligned, we have to read into data buffer
    		 * due to ecc, else we read into return buffer direct
    		 */
    		if (aligned)
    			data_poi = &buf[read];
    		else 
    			data_poi = this->data_buf;
    		
    		/* Check, if we have this page in the buffer 
    		 *
    		 * FIXME: Make it work when we must provide oob data too,
    		 * check the usage of data_buf oob field
    		 */
    		if (realpage == this->pagebuf && !oob_buf) {
    			/* aligned read ? */
    			if (aligned)
    				memcpy (data_poi, this->data_buf, end);
    			goto readdata;
    		}
    
    		/* Check, if we must send the read command */
    		if (sndcmd) {
    			this->cmdfunc (mtd, NAND_CMD_READ0, 0x00, page);
    			sndcmd = 0;
    		}	
    
    		/* get oob area, if we have no oob buffer from fs-driver */
    		if (!oob_buf || oobsel->useecc == MTD_NANDECC_AUTOPLACE ||
    			oobsel->useecc == MTD_NANDECC_AUTOPL_USR)
    			oob_data = &this->data_buf[end];
    
    		eccsteps = this->eccsteps;
    		
    		switch (eccmode) {
    		case NAND_ECC_NONE: {	/* No ECC, Read in a page */
    /* XXX U-BOOT XXX */
    #if 0
    			static unsigned long lastwhinge = 0;
    			if ((lastwhinge / HZ) != (jiffies / HZ)) {
    				printk (KERN_WARNING "Reading data from NAND FLASH without ECC is not recommended\n");
    				lastwhinge = jiffies;
    			}
    #else
    			puts("Reading data from NAND FLASH without ECC is not recommended\n");
    #endif
    			this->read_buf(mtd, data_poi, end);
    			break;
    		}
    			
    		case NAND_ECC_SOFT:	/* Software ECC 3/256: Read in a page + oob data */
    			this->read_buf(mtd, data_poi, end);
    			for (i = 0, datidx = 0; eccsteps; eccsteps--, i+=3, datidx += ecc) 
    				this->calculate_ecc(mtd, &data_poi[datidx], &ecc_calc[i]);
    			break;	
    
    		default:
    			for (i = 0, datidx = 0; eccsteps; eccsteps--, i+=eccbytes, datidx += ecc) {
    				this->enable_hwecc(mtd, NAND_ECC_READ);
    				this->read_buf(mtd, &data_poi[datidx], ecc);
    
    				/* HW ecc with syndrome calculation must read the
    				 * syndrome from flash immidiately after the data */
    				if (!compareecc) {
    					/* Some hw ecc generators need to know when the
    					 * syndrome is read from flash */
    					this->enable_hwecc(mtd, NAND_ECC_READSYN);
    					this->read_buf(mtd, &oob_data[i], eccbytes);
    					/* We calc error correction directly, it checks the hw
    					 * generator for an error, reads back the syndrome and
    					 * does the error correction on the fly */
    					if (this->correct_data(mtd, &data_poi[datidx], &oob_data[i], &ecc_code[i]) == -1) {
    						DEBUG (MTD_DEBUG_LEVEL0, "nand_read_ecc: " 
    							"Failed ECC read, page 0x%08x on chip %d\n", page, chipnr);
    						ecc_failed++;
    					}
    				} else {
    					this->calculate_ecc(mtd, &data_poi[datidx], &ecc_calc[i]);
    				}	
    			}
    			break;						
    		}
    
    		/* read oobdata */
    		this->read_buf(mtd, &oob_data[mtd->oobsize - oobreadlen], oobreadlen);
    
    		/* Skip ECC check, if not requested (ECC_NONE or HW_ECC with syndromes) */
    		if (!compareecc)
    			goto readoob;	
    		
    		/* Pick the ECC bytes out of the oob data */
    		for (j = 0; j < oobsel->eccbytes; j++)
    			ecc_code[j] = oob_data[oob_config[j]];
    
    		/* correct data, if neccecary */
    		for (i = 0, j = 0, datidx = 0; i < this->eccsteps; i++, datidx += ecc) {
    			ecc_status = this->correct_data(mtd, &data_poi[datidx], &ecc_code[j], &ecc_calc[j]);
    			
    			/* Get next chunk of ecc bytes */
    			j += eccbytes;
    			
    			/* Check, if we have a fs supplied oob-buffer, 
    			 * This is the legacy mode. Used by YAFFS1
    			 * Should go away some day
    			 */
    			if (oob_buf && oobsel->useecc == MTD_NANDECC_PLACE) { 
    				int *p = (int *)(&oob_data[mtd->oobsize]);
    				p[i] = ecc_status;
    			}
    			
    			if (ecc_status == -1) {	
    				DEBUG (MTD_DEBUG_LEVEL0, "nand_read_ecc: " "Failed ECC read, page 0x%08x\n", page);
    				ecc_failed++;
    			}
    		}		
    
    	readoob:
    		/* check, if we have a fs supplied oob-buffer */
    		if (oob_buf) {
    			/* without autoplace. Legacy mode used by YAFFS1 */
    			switch(oobsel->useecc) {
    			case MTD_NANDECC_AUTOPLACE:
    			case MTD_NANDECC_AUTOPL_USR:
    				/* Walk through the autoplace chunks */
    				for (i = 0, j = 0; j < mtd->oobavail; i++) {
    					int from = oobsel->oobfree[i][0];
    					int num = oobsel->oobfree[i][1];
    					memcpy(&oob_buf[oob], &oob_data[from], num);
    					j+= num;
    				}
    				oob += mtd->oobavail;
    				break;
    			case MTD_NANDECC_PLACE:
    				/* YAFFS1 legacy mode */
    				oob_data += this->eccsteps * sizeof (int);
    			default:
    				oob_data += mtd->oobsize;
    			}
    		}
    	readdata:
    		/* Partial page read, transfer data into fs buffer */
    		if (!aligned) { 
    			for (j = col; j < end && read < len; j++)
    				buf[read++] = data_poi[j];
    			this->pagebuf = realpage;	
    		} else		
    			read += mtd->oobblock;
    
    		/* Apply delay or wait for ready/busy pin 
    		 * Do this before the AUTOINCR check, so no problems
    		 * arise if a chip which does auto increment
    		 * is marked as NOAUTOINCR by the board driver.
    		*/
    		if (!this->dev_ready) 
    			udelay (this->chip_delay);
    		else
    			while (!this->dev_ready(mtd));	
    			
    		if (read == len)
    			break;	
    
    		/* For subsequent reads align to page boundary. */
    		col = 0;
    		/* Increment page address */
    		realpage++;
    
    		page = realpage & this->pagemask;
    		/* Check, if we cross a chip boundary */
    		if (!page) {
    			chipnr++;
    			this->select_chip(mtd, -1);
    			this->select_chip(mtd, chipnr);
    		}
    		/* Check, if the chip supports auto page increment 
    		 * or if we have hit a block boundary. 
    		*/ 
    		if (!NAND_CANAUTOINCR(this) || !(page & blockcheck))
    			sndcmd = 1;				
    	}
    
    	/* Deselect and wake up anyone waiting on the device */
    	nand_release_device(mtd);
    
    	/*
    	 * Return success, if no ECC failures, else -EBADMSG
    	 * fs driver will take care of that, because
    	 * retlen == desired len and result == -EBADMSG
    	 */
    	*retlen = read;
    	return ecc_failed ? -EBADMSG : 0;
    }
    
    /**
     * nand_read_oob - [MTD Interface] NAND read out-of-band
     * @mtd:	MTD device structure
     * @from:	offset to read from
     * @len:	number of bytes to read
     * @retlen:	pointer to variable to store the number of read bytes
     * @buf:	the databuffer to put data
     *
     * NAND read out-of-band data from the spare area
     */
    static int nand_read_oob (struct mtd_info *mtd, loff_t from, size_t len, size_t * retlen, u_char * buf)
    {
    	int i, col, page, chipnr;
    	struct nand_chip *this = mtd->priv;
    	int	blockcheck = (1 << (this->phys_erase_shift - this->page_shift)) - 1;
    
    	DEBUG (MTD_DEBUG_LEVEL3, "nand_read_oob: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len);
    
    	/* Shift to get page */
    	page = (int)(from >> this->page_shift);
    	chipnr = (int)(from >> this->chip_shift);
    	
    	/* Mask to get column */
    	col = from & (mtd->oobsize - 1);
    
    	/* Initialize return length value */
    	*retlen = 0;
    
    	/* Do not allow reads past end of device */
    	if ((from + len) > mtd->size) {
    		DEBUG (MTD_DEBUG_LEVEL0, "nand_read_oob: Attempt read beyond end of device\n");
    		*retlen = 0;
    		return -EINVAL;
    	}
    
    	/* 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);
    
    	/* Send the read command */
    	this->cmdfunc (mtd, NAND_CMD_READOOB, col, page & this->pagemask);
    	/* 
    	 * Read the data, if we read more than one page
    	 * oob data, let the device transfer the data !
    	 */
    	i = 0;
    	while (i < len) {
    		int thislen = mtd->oobsize - col;
    		thislen = min_t(int, thislen, len);
    		this->read_buf(mtd, &buf[i], thislen);
    		i += thislen;
    		
    		/* Apply delay or wait for ready/busy pin 
    		 * Do this before the AUTOINCR check, so no problems
    		 * arise if a chip which does auto increment
    		 * is marked as NOAUTOINCR by the board driver.
    		*/
    		if (!this->dev_ready) 
    			udelay (this->chip_delay);
    		else
    			while (!this->dev_ready(mtd));	
    
    		/* Read more ? */
    		if (i < len) {
    			page++;
    			col = 0;
    
    			/* Check, if we cross a chip boundary */
    			if (!(page & this->pagemask)) {
    				chipnr++;
    				this->select_chip(mtd, -1);
    				this->select_chip(mtd, chipnr);
    			}
    				
    			/* Check, if the chip supports auto page increment 
    			 * or if we have hit a block boundary. 
    			*/ 
    			if (!NAND_CANAUTOINCR(this) || !(page & blockcheck)) {
    				/* For subsequent page reads set offset to 0 */
    			        this->cmdfunc (mtd, NAND_CMD_READOOB, 0x0, page & this->pagemask);
    			}
    		}
    	}
    
    	/* Deselect and wake up anyone waiting on the device */
    	nand_release_device(mtd);
    
    	/* Return happy */
    	*retlen = len;
    	return 0;
    }
    
    /**
     * nand_read_raw - [GENERIC] Read raw data including oob into buffer
     * @mtd:	MTD device structure
     * @buf:	temporary buffer
     * @from:	offset to read from
     * @len:	number of bytes to read
     * @ooblen:	number of oob data bytes to read
     *
     * Read raw data including oob into buffer
     */
    int nand_read_raw (struct mtd_info *mtd, uint8_t *buf, loff_t from, size_t len, size_t ooblen)
    {
    	struct nand_chip *this = mtd->priv;
    	int page = (int) (from >> this->page_shift);
    	int chip = (int) (from >> this->chip_shift);
    	int sndcmd = 1;
    	int cnt = 0;
    	int pagesize = mtd->oobblock + mtd->oobsize;
    	int	blockcheck = (1 << (this->phys_erase_shift - this->page_shift)) - 1;
    
    	/* Do not allow reads past end of device */
    	if ((from + len) > mtd->size) {
    		DEBUG (MTD_DEBUG_LEVEL0, "nand_read_raw: Attempt read beyond end of device\n");
    		return -EINVAL;
    	}
    
    	/* Grab the lock and see if the device is available */
    	nand_get_device (this, mtd , FL_READING);
    
    	this->select_chip (mtd, chip);
    	
    	/* Add requested oob length */
    	len += ooblen;
    	
    	while (len) {
    		if (sndcmd)
    			this->cmdfunc (mtd, NAND_CMD_READ0, 0, page & this->pagemask);
    		sndcmd = 0;	
    
    		this->read_buf (mtd, &buf[cnt], pagesize);
    
    		len -= pagesize;
    		cnt += pagesize;
    		page++;
    		
    		if (!this->dev_ready) 
    			udelay (this->chip_delay);
    		else
    			while (!this->dev_ready(mtd));	
    			
    		/* Check, if the chip supports auto page increment */ 
    		if (!NAND_CANAUTOINCR(this) || !(page & blockcheck))
    			sndcmd = 1;
    	}
    
    	/* Deselect and wake up anyone waiting on the device */
    	nand_release_device(mtd);
    	return 0;
    }
    
    
    /** 
     * nand_prepare_oobbuf - [GENERIC] Prepare the out of band buffer 
     * @mtd:	MTD device structure
     * @fsbuf:	buffer given by fs driver
     * @oobsel:	out of band selection structre
     * @autoplace:	1 = place given buffer into the oob bytes
     * @numpages:	number of pages to prepare
     *
     * Return:
     * 1. Filesystem buffer available and autoplacement is off,
     *    return filesystem buffer
     * 2. No filesystem buffer or autoplace is off, return internal
     *    buffer
     * 3. Filesystem buffer is given and autoplace selected
     *    put data from fs buffer into internal buffer and
     *    retrun internal buffer
     *
     * Note: The internal buffer is filled with 0xff. This must
     * be done only once, when no autoplacement happens
     * Autoplacement sets the buffer dirty flag, which
     * forces the 0xff fill before using the buffer again.
     *
    */
    static u_char * nand_prepare_oobbuf (struct mtd_info *mtd, u_char *fsbuf, struct nand_oobinfo *oobsel,
    		int autoplace, int numpages)
    {
    	struct nand_chip *this = mtd->priv;
    	int i, len, ofs;
    
    	/* Zero copy fs supplied buffer */
    	if (fsbuf && !autoplace) 
    		return fsbuf;
    
    	/* Check, if the buffer must be filled with ff again */
    	if (this->oobdirty) {	
    		memset (this->oob_buf, 0xff, 
    			mtd->oobsize << (this->phys_erase_shift - this->page_shift));
    		this->oobdirty = 0;
    	}	
    	
    	/* If we have no autoplacement or no fs buffer use the internal one */
    	if (!autoplace || !fsbuf)
    		return this->oob_buf;
    	
    	/* Walk through the pages and place the data */
    	this->oobdirty = 1;
    	ofs = 0;
    	while (numpages--) {
    		for (i = 0, len = 0; len < mtd->oobavail; i++) {
    			int to = ofs + oobsel->oobfree[i][0];
    			int num = oobsel->oobfree[i][1];
    			memcpy (&this->oob_buf[to], fsbuf, num);
    			len += num;
    			fsbuf += num;
    		}
    		ofs += mtd->oobavail;
    	}
    	return this->oob_buf;
    }
    
    #define NOTALIGNED(x) (x & (mtd->oobblock-1)) != 0
    
    /**
     * nand_write - [MTD Interface] compability function for nand_write_ecc
     * @mtd:	MTD device structure
     * @to:		offset to write to
     * @len:	number of bytes to write
     * @retlen:	pointer to variable to store the number of written bytes
     * @buf:	the data to write
     *
     * This function simply calls nand_write_ecc with oob buffer and oobsel = NULL
     *
    */
    static int nand_write (struct mtd_info *mtd, loff_t to, size_t len, size_t * retlen, const u_char * buf)
    {
    	return (nand_write_ecc (mtd, to, len, retlen, buf, NULL, NULL));
    }
    			   
    /**
     * nand_write_ecc - [MTD Interface] NAND write with ECC
     * @mtd:	MTD device structure
     * @to:		offset to write to
     * @len:	number of bytes to write
     * @retlen:	pointer to variable to store the number of written bytes
     * @buf:	the data to write
     * @eccbuf:	filesystem supplied oob data buffer
     * @oobsel:	oob selection structure
     *
     * NAND write with ECC
     */
    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)
    {
    	int startpage, page, ret = -EIO, oob = 0, written = 0, chipnr;
    	int autoplace = 0, numpages, totalpages;
    	struct nand_chip *this = mtd->priv;
    	u_char *oobbuf, *bufstart;
    	int	ppblock = (1 << (this->phys_erase_shift - this->page_shift));
    
    	DEBUG (MTD_DEBUG_LEVEL3, "nand_write_ecc: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
    
    	/* Initialize retlen, in case of early exit */
    	*retlen = 0;
    
    	/* Do not allow write past end of device */
    	if ((to + len) > mtd->size) {
    		DEBUG (MTD_DEBUG_LEVEL0, "nand_write_ecc: Attempt to write past end of page\n");
    		return -EINVAL;
    	}
    
    	/* reject writes, which are not page aligned */	
    	if (NOTALIGNED (to) || NOTALIGNED(len)) {
    		printk (KERN_NOTICE "nand_write_ecc: Attempt to write not page aligned data\n");
    		return -EINVAL;
    	}
    
    	/* Grab the lock and see if the device is available */
    	nand_get_device (this, mtd, FL_WRITING);
    
    	/* Calculate chipnr */
    	chipnr = (int)(to >> this->chip_shift);
    	/* Select the NAND device */
    	this->select_chip(mtd, chipnr);
    
    	/* Check, if it is write protected */
    	if (nand_check_wp(mtd))
    		goto out;
    
    	/* if oobsel is NULL, use chip defaults */
    	if (oobsel == NULL) 
    		oobsel = &mtd->oobinfo;		
    		
    	/* Autoplace of oob data ? Use the default placement scheme */
    	if (oobsel->useecc == MTD_NANDECC_AUTOPLACE) {
    		oobsel = this->autooob;
    		autoplace = 1;
    	}	
    	if (oobsel->useecc == MTD_NANDECC_AUTOPL_USR)
    		autoplace = 1;
    
    	/* Setup variables and oob buffer */
    	totalpages = len >> this->page_shift;
    	page = (int) (to >> this->page_shift);
    	/* Invalidate the page cache, if we write to the cached page */
    	if (page <= this->pagebuf && this->pagebuf < (page + totalpages))  
    		this->pagebuf = -1;
    	
    	/* Set it relative to chip */
    	page &= this->pagemask;
    	startpage = page;
    	/* Calc number of pages we can write in one go */
    	numpages = min (ppblock - (startpage  & (ppblock - 1)), totalpages);
    	oobbuf = nand_prepare_oobbuf (mtd, eccbuf, oobsel, autoplace, numpages);
    	bufstart = (u_char *)buf;
    
    	/* Loop until all data is written */
    	while (written < len) {
    
    		this->data_poi = (u_char*) &buf[written];
    		/* Write one page. If this is the last page to write
    		 * or the last page in this block, then use the
    		 * real pageprogram command, else select cached programming
    		 * if supported by the chip.
    		 */
    		ret = nand_write_page (mtd, this, page, &oobbuf[oob], oobsel, (--numpages > 0));
    		if (ret) {
    			DEBUG (MTD_DEBUG_LEVEL0, "nand_write_ecc: write_page failed %d\n", ret);
    			goto out;
    		}	
    		/* Next oob page */
    		oob += mtd->oobsize;
    		/* Update written bytes count */
    		written += mtd->oobblock;
    		if (written == len) 
    			goto cmp;
    		
    		/* Increment page address */
    		page++;
    
    		/* Have we hit a block boundary ? Then we have to verify and
    		 * if verify is ok, we have to setup the oob buffer for
    		 * the next pages.
    		*/
    		if (!(page & (ppblock - 1))){
    			int ofs;
    			this->data_poi = bufstart;
    			ret = nand_verify_pages (mtd, this, startpage, 
    				page - startpage,
    				oobbuf, oobsel, chipnr, (eccbuf != NULL));
    			if (ret) {
    				DEBUG (MTD_DEBUG_LEVEL0, "nand_write_ecc: verify_pages failed %d\n", ret);
    				goto out;
    			}	
    			*retlen = written;
    
    			ofs = autoplace ? mtd->oobavail : mtd->oobsize;
    			if (eccbuf)
    				eccbuf += (page - startpage) * ofs;
    			totalpages -= page - startpage;
    			numpages = min (totalpages, ppblock);
    			page &= this->pagemask;
    			startpage = page;
    			oobbuf = nand_prepare_oobbuf (mtd, eccbuf, oobsel, 
    					autoplace, numpages);
    			/* Check, if we cross a chip boundary */
    			if (!page) {
    				chipnr++;
    				this->select_chip(mtd, -1);
    				this->select_chip(mtd, chipnr);
    			}
    		}
    	}
    	/* Verify the remaining pages */
    cmp:
    	this->data_poi = bufstart;
     	ret = nand_verify_pages (mtd, this, startpage, totalpages,
    		oobbuf, oobsel, chipnr, (eccbuf != NULL));
    	if (!ret)
    		*retlen = written;
    	else	
    		DEBUG (MTD_DEBUG_LEVEL0, "nand_write_ecc: verify_pages failed %d\n", ret);
    
    out:
    	/* Deselect and wake up anyone waiting on the device */
    	nand_release_device(mtd);
    
    	return ret;
    }
    
    
    /**
     * nand_write_oob - [MTD Interface] NAND write out-of-band
     * @mtd:	MTD device structure
     * @to:		offset to write to
     * @len:	number of bytes to write
     * @retlen:	pointer to variable to store the number of written bytes
     * @buf:	the data to write
     *
     * NAND write out-of-band
     */
    static int nand_write_oob (struct mtd_info *mtd, loff_t to, size_t len, size_t * retlen, const u_char * buf)
    {
    	int column, page, status, ret = -EIO, chipnr;
    	struct nand_chip *this = mtd->priv;
    
    	DEBUG (MTD_DEBUG_LEVEL3, "nand_write_oob: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
    
    	/* Shift to get page */
    	page = (int) (to >> this->page_shift);
    	chipnr = (int) (to >> this->chip_shift);
    
    	/* Mask to get column */
    	column = to & (mtd->oobsize - 1);
    
    	/* Initialize return length value */
    	*retlen = 0;
    
    	/* Do not allow write past end of page */
    	if ((column + len) > mtd->oobsize) {
    		DEBUG (MTD_DEBUG_LEVEL0, "nand_write_oob: Attempt to write past end of page\n");
    		return -EINVAL;
    	}
    
    	/* Grab the lock and see if the device is available */
    	nand_get_device (this, mtd, FL_WRITING);
    
    	/* Select the NAND device */
    	this->select_chip(mtd, chipnr);
    
    	/* Reset the chip. Some chips (like the Toshiba TC5832DC found
    	   in one of my DiskOnChip 2000 test units) will clear the whole
    	   data page too if we don't do this. I have no clue why, but
    	   I seem to have 'fixed' it in the doc2000 driver in
    	   August 1999.  dwmw2. */
    	this->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
    
    	/* Check, if it is write protected */
    	if (nand_check_wp(mtd))
    		goto out;
    	
    	/* Invalidate the page cache, if we write to the cached page */
    	if (page == this->pagebuf)
    		this->pagebuf = -1;
    
    	if (NAND_MUST_PAD(this)) {
    		/* Write out desired data */
    		this->cmdfunc (mtd, NAND_CMD_SEQIN, mtd->oobblock, page & this->pagemask);
    		/* prepad 0xff for partial programming */
    		this->write_buf(mtd, ffchars, column);
    		/* write data */
    		this->write_buf(mtd, buf, len);
    		/* postpad 0xff for partial programming */
    		this->write_buf(mtd, ffchars, mtd->oobsize - (len+column));
    	} else {
    		/* Write out desired data */
    		this->cmdfunc (mtd, NAND_CMD_SEQIN, mtd->oobblock + column, page & this->pagemask);
    		/* write data */
    		this->write_buf(mtd, buf, len);
    	}
    	/* Send command to program the OOB data */
    	this->cmdfunc (mtd, NAND_CMD_PAGEPROG, -1, -1);
    
    	status = this->waitfunc (mtd, this, FL_WRITING);
    
    	/* See if device thinks it succeeded */
    	if (status & 0x01) {
    		DEBUG (MTD_DEBUG_LEVEL0, "nand_write_oob: " "Failed write, page 0x%08x\n", page);
    		ret = -EIO;
    		goto out;
    	}
    	/* Return happy */
    	*retlen = len;
    
    #ifdef CONFIG_MTD_NAND_VERIFY_WRITE
    	/* Send command to read back the data */
    	this->cmdfunc (mtd, NAND_CMD_READOOB, column, page & this->pagemask);
    
    	if (this->verify_buf(mtd, buf, len)) {
    		DEBUG (MTD_DEBUG_LEVEL0, "nand_write_oob: " "Failed write verify, page 0x%08x\n", page);
    		ret = -EIO;
    		goto out;
    	}
    #endif
    	ret = 0;
    out:
    	/* Deselect and wake up anyone waiting on the device */
    	nand_release_device(mtd);
    
    	return ret;
    }
    
    /* XXX U-BOOT XXX */
    #if 0
    /**
     * nand_writev - [MTD Interface] compabilty function for nand_writev_ecc
     * @mtd:	MTD device structure
     * @vecs:	the iovectors to write
     * @count:	number of vectors
     * @to:		offset to write to
     * @retlen:	pointer to variable to store the number of written bytes
     *
     * NAND write with kvec. This just calls the ecc function
     */
    static int nand_writev (struct mtd_info *mtd, const struct kvec *vecs, unsigned long count, 
    		loff_t to, size_t * retlen)
    {
    	return (nand_writev_ecc (mtd, vecs, count, to, retlen, NULL, NULL));	
    }
    
    /**
     * nand_writev_ecc - [MTD Interface] write with iovec with ecc
     * @mtd:	MTD device structure
     * @vecs:	the iovectors to write
     * @count:	number of vectors
     * @to:		offset to write to
     * @retlen:	pointer to variable to store the number of written bytes
     * @eccbuf:	filesystem supplied oob data buffer
     * @oobsel:	oob selection structure
     *
     * NAND write with iovec with ecc
     */
    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)
    {
    	int i, page, len, total_len, ret = -EIO, written = 0, chipnr;
    	int oob, numpages, autoplace = 0, startpage;
    	struct nand_chip *this = mtd->priv;
    	int	ppblock = (1 << (this->phys_erase_shift - this->page_shift));
    	u_char *oobbuf, *bufstart;
    
    	/* Preset written len for early exit */
    	*retlen = 0;
    
    	/* Calculate total length of data */
    	total_len = 0;
    	for (i = 0; i < count; i++)
    		total_len += (int) vecs[i].iov_len;
    
    	DEBUG (MTD_DEBUG_LEVEL3,
    	       "nand_writev: to = 0x%08x, len = %i, count = %ld\n", (unsigned int) to, (unsigned int) total_len, count);
    
    	/* Do not allow write past end of page */
    	if ((to + total_len) > mtd->size) {
    		DEBUG (MTD_DEBUG_LEVEL0, "nand_writev: Attempted write past end of device\n");
    		return -EINVAL;
    	}
    
    	/* reject writes, which are not page aligned */	
    	if (NOTALIGNED (to) || NOTALIGNED(total_len)) {
    		printk (KERN_NOTICE "nand_write_ecc: Attempt to write not page aligned data\n");
    		return -EINVAL;
    	}
    
    	/* Grab the lock and see if the device is available */
    	nand_get_device (this, mtd, FL_WRITING);
    
    	/* Get the current chip-nr */
    	chipnr = (int) (to >> this->chip_shift);
    	/* Select the NAND device */
    	this->select_chip(mtd, chipnr);
    
    	/* Check, if it is write protected */
    	if (nand_check_wp(mtd))
    		goto out;
    
    	/* if oobsel is NULL, use chip defaults */
    	if (oobsel == NULL) 
    		oobsel = &mtd->oobinfo;		
    
    	/* Autoplace of oob data ? Use the default placement scheme */
    	if (oobsel->useecc == MTD_NANDECC_AUTOPLACE) {
    		oobsel = this->autooob;
    		autoplace = 1;
    	}	
    	if (oobsel->useecc == MTD_NANDECC_AUTOPL_USR)
    		autoplace = 1;
    
    	/* Setup start page */
    	page = (int) (to >> this->page_shift);
    	/* Invalidate the page cache, if we write to the cached page */
    	if (page <= this->pagebuf && this->pagebuf < ((to + total_len) >> this->page_shift))  
    		this->pagebuf = -1;
    
    	startpage = page & this->pagemask;
    
    	/* Loop until all kvec' data has been written */
    	len = 0;
    	while (count) {
    		/* If the given tuple is >= pagesize then
    		 * write it out from the iov
    		 */
    		if ((vecs->iov_len - len) >= mtd->oobblock) {
    			/* Calc number of pages we can write
    			 * out of this iov in one go */
    			numpages = (vecs->iov_len - len) >> this->page_shift;
    			/* Do not cross block boundaries */
    			numpages = min (ppblock - (startpage & (ppblock - 1)), numpages);
    			oobbuf = nand_prepare_oobbuf (mtd, NULL, oobsel, autoplace, numpages);
    			bufstart = (u_char *)vecs->iov_base;
    			bufstart += len;
    			this->data_poi = bufstart;
    			oob = 0;
    			for (i = 1; i <= numpages; i++) {
    				/* Write one page. If this is the last page to write
    				 * then use the real pageprogram command, else select 
    				 * cached programming if supported by the chip.
    				 */
    				ret = nand_write_page (mtd, this, page & this->pagemask, 
    					&oobbuf[oob], oobsel, i != numpages);
    				if (ret)
    					goto out;
    				this->data_poi += mtd->oobblock;
    				len += mtd->oobblock;
    				oob += mtd->oobsize;
    				page++;
    			}
    			/* Check, if we have to switch to the next tuple */
    			if (len >= (int) vecs->iov_len) {
    				vecs++;
    				len = 0;
    				count--;
    			}
    		} else {
    			/* We must use the internal buffer, read data out of each 
    			 * tuple until we have a full page to write
    			 */
    			int cnt = 0;
    			while (cnt < mtd->oobblock) {
    				if (vecs->iov_base != NULL && vecs->iov_len) 
    					this->data_buf[cnt++] = ((u_char *) vecs->iov_base)[len++];
    				/* Check, if we have to switch to the next tuple */
    				if (len >= (int) vecs->iov_len) {
    					vecs++;
    					len = 0;
    					count--;
    				}
    			}
    			this->pagebuf = page;	
    			this->data_poi = this->data_buf;	
    			bufstart = this->data_poi;
    			numpages = 1;		
    			oobbuf = nand_prepare_oobbuf (mtd, NULL, oobsel, autoplace, numpages);
    			ret = nand_write_page (mtd, this, page & this->pagemask,
    				oobbuf, oobsel, 0);
    			if (ret)
    				goto out;
    			page++;
    		}
    
    		this->data_poi = bufstart;
    		ret = nand_verify_pages (mtd, this, startpage, numpages, oobbuf, oobsel, chipnr, 0);
    		if (ret)
    			goto out;
    			
    		written += mtd->oobblock * numpages;
    		/* All done ? */
    		if (!count)
    			break;
    
    		startpage = page & this->pagemask;
    		/* Check, if we cross a chip boundary */
    		if (!startpage) {
    			chipnr++;
    			this->select_chip(mtd, -1);
    			this->select_chip(mtd, chipnr);
    		}
    	}
    	ret = 0;
    out:
    	/* Deselect and wake up anyone waiting on the device */
    	nand_release_device(mtd);
    
    	*retlen = written;
    	return ret;