EDK2 doxygen online documents - Firmware Encoding Index 1
EDK2 doxygen online documents - Firmware Encoding Index

S3C24xxPkg/vivi/drivers/mtd/nand/smc_core.c

Go to the documentation of this file.
00001 /*
00002  * vivi/drivers/mtd/nand/smc_core.c: 
00003  *
00004  * Based on linux/drivers/mtd/nand/smc.c
00005  *
00006  * $Id: smc_core.c,v 1.1.1.1 2004/02/04 06:22:25 laputa Exp $
00007  *
00008  *
00009  */
00010 
00011 #include "config.h"
00012 #include "mtd/mtd.h"
00013 #include "mtd/nand.h"
00014 #include "mtd/nand_ids.h"
00015 #include "mtd/nand_ecc.h"
00016 #include "time.h"
00017 #include "printk.h"
00018 #include <types.h>
00019 #include <errno.h>
00020 
00021 /*
00022  * Macros for low-level register control
00023  */
00024 #define nand_select()   this->hwcontrol(NAND_CTL_SETNCE); \
00025                         nand_command(mtd, NAND_CMD_RESET, -1, -1); \
00026                         udelay(10);
00027 #define nand_deselect() this->hwcontrol(NAND_CTL_CLRNCE);
00028 
00029 static inline void 
00030 sm_swap(u_char *x, u_char *y) {
00031         u_char tmp;
00032         tmp = *x;
00033         *x = *y;
00034         *y = tmp;
00035 }
00036 
00037 /* define if you'll be using < 2M SMC device */
00038 #undef USE_256BYTE_NAND_FLASH
00039 
00040 /*
00041  * Send command to NAND device
00042  */
00043 static void
00044 nand_command(struct mtd_info *mtd, unsigned command, int column, int page_addr)
00045 {
00046         register struct nand_chip *this = mtd->priv;
00047 
00048         /* Begin command latch cycle */
00049         this->hwcontrol(NAND_CTL_SETCLE);
00050 
00051         this->hwcontrol(NAND_CTL_DAT_OUT);
00052 
00053         /*
00054          * Write out the command to the device.
00055          */
00056         if (command != NAND_CMD_SEQIN)
00057                 this->write_cmd(command);
00058         else {
00059                 if (mtd->oobblock == 256 && column >= 256) {
00060                         column -= 256;
00061                         this->write_cmd(NAND_CMD_RESET);
00062                         this->write_cmd(NAND_CMD_READOOB);
00063                         this->write_cmd(NAND_CMD_SEQIN);
00064                 } else if (mtd->oobblock == 512 && column >= 256) {
00065                         if (column < 512) {
00066                                 column -= 256;
00067                                 this->write_cmd(NAND_CMD_READ1);
00068                                 this->write_cmd(NAND_CMD_SEQIN);
00069                         } else {
00070                                 column -= 512;
00071                                 this->write_cmd(NAND_CMD_READOOB);
00072                                 this->write_cmd(NAND_CMD_SEQIN);
00073                         }
00074                 } else {
00075                         this->write_cmd(NAND_CMD_READ0);
00076                         this->write_cmd(NAND_CMD_SEQIN);
00077                 }
00078         }
00079 
00080         /* Set ALE and clear CLE to start address cycle */
00081         this->hwcontrol(NAND_CTL_CLRCLE);
00082         this->hwcontrol(NAND_CTL_SETALE);
00083 
00084         /* Serially input address */
00085         if (column != -1)
00086                 this->write_addr(column);
00087         if (page_addr != -1) {
00088                 this->write_addr((u_char)(page_addr & 0xff));
00089                 this->write_addr((u_char)((page_addr >> 8) & 0xff));
00090                 /* One more address cycle for higher density devices */
00091                 if (mtd->size & 0x0c000000) {
00092                         this->write_addr((u_char)((page_addr >> 16) & 0xff));
00093                 }
00094         }
00095 
00096         /* Latch in address */
00097         this->hwcontrol(NAND_CTL_CLRALE);
00098         
00099         this->hwcontrol(NAND_CTL_DAT_IN);
00100         /* Pause for 15us */
00101         udelay(15);
00102  }
00103 
00104 /*
00105  * NAND read
00106  */
00107 static int
00108 nand_read(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
00109 {
00110         int j, col, page, state;
00111         struct nand_chip *this = mtd->priv;
00112 
00113         DEBUG(MTD_DEBUG_LEVEL3,
00114                 __FUNCTION__"(): from = 0x%08lx, len = %i\n",
00115                 (unsigned int)from, (int)len);
00116 
00117         /* Do not allow reads past end of device */
00118         if ((from + len) > mtd->size) {
00119                 DEBUG(MTD_DEBUG_LEVEL0,
00120                         __FUNCTION__"(): Attempt read beyond end of device\n");
00121                 *retlen = 0;
00122                 return -EINVAL;
00123         }
00124 
00125         /* First we calculate the starting page */
00126         page = from >> this->page_shift;
00127 
00128         /* Get raw starting column */
00129         col = from & (mtd->oobblock - 1);
00130 
00131         /* State machine for devices having pages larger than 256 bytes */
00132         state = (col < mtd->eccsize) ? 0 : 1;
00133 
00134         /* Calculate column address within ECC block context */
00135         col = (col >= mtd->eccsize) ? (col - mtd->eccsize) : col;
00136 
00137         /* Initialize return value */
00138         *retlen = 0;
00139 
00140         /* Select the NAND device */
00141         nand_select();
00142 
00143         /* Loop until all data read */
00144         while (*retlen < len) {
00145                 /* Send the read command */
00146                 if (!state)
00147                         nand_command(mtd, NAND_CMD_READ0, col, page);
00148                 else
00149                         nand_command(mtd, NAND_CMD_READ1, col, page);
00150 
00151                 this->wait_for_ready();
00152 
00153                 /* Read the data directly into the return buffer */
00154                 if ((*retlen + (mtd->eccsize - col)) >= len) {
00155                         while (*retlen < len)
00156                                 buf[(*retlen)++] = this->read_data();
00157                         /* We're done */
00158                         continue;
00159                 } else {
00160                         for (j = col; j < mtd->eccsize; j++)
00161                                 buf[(*retlen)++] = this->read_data();
00162                 }
00163 
00164                 /*
00165                  * If the amount of data to be read is greater than
00166                  * (256 - col), then all subsequent reads will take
00167                  * place on page or half-page (in the case of 512 byte
00168                  * page devices) aligned boundaries and the column
00169                  * address will be zero. Setting the column address to
00170                  * zero after the first read allows us to simplify
00171                  * the reading of data and the if/else statements above.
00172                  */
00173                 if (col)
00174                         col = 0x00;
00175 
00176                 /* Increment page address */
00177                 if ((mtd->oobblock == 256) || state)
00178                         page++;
00179 
00180                 /* Toggle state machine */
00181                 if (mtd->oobblock == 512)
00182                         state = state ? 0 : 1;
00183         }
00184 
00185         /* De-select the NAND device */
00186         nand_deselect();
00187 
00188         /* Return happy */
00189         return 0;       
00190 }
00191 
00192 /*
00193  * if mtd->oobblock == 512
00194  */
00195 inline int
00196 smc_read_ecc_512(struct mtd_info *mtd, u_char *ecc_code)
00197 {
00198         struct nand_chip *this = mtd->priv;
00199         u_char ecc_calc[3];
00200         int j, ret;
00201 
00202         /* Read in a block big enough for ECC */
00203         for (j = 0; j < (mtd->oobblock + mtd->oobsize); j++)
00204                 this->data_buf[j] = this->read_data();
00205 
00206         for (j = 0; j < mtd->oobsize; j++)
00207                 ecc_code[j] = this->data_buf[(mtd->oobblock + j)];
00208 
00209         nand_calculate_ecc(&this->data_buf[0], &ecc_calc[0]);
00210         sm_swap(&ecc_calc[0], &ecc_calc[1]);
00211         DEBUG(MTD_DEBUG_LEVEL3,
00212                 __FUNCTION__"(): ECC [%02x%02x%02x : %02x%02x%02x]\n",
00213                 ecc_code[SMC_OOB_ECC1], ecc_code[SMC_OOB_ECC1+1],
00214                 ecc_code[SMC_OOB_ECC1+2], ecc_calc[0], ecc_calc[1], ecc_calc[2]);
00215         ret = nand_correct_data(&this->data_buf[0],
00216                                 &(ecc_code[SMC_OOB_ECC1]), &ecc_calc[0]);
00217         if (ret == -1)
00218                 return ret;
00219 
00220         nand_calculate_ecc(&this->data_buf[mtd->eccsize], &ecc_calc[0]);
00221         sm_swap(&ecc_calc[0], &ecc_calc[1]);
00222         DEBUG(MTD_DEBUG_LEVEL3,
00223                 __FUNCTION__"(): ECC [%02x%02x%02x : %02x%02x%02x]\n",
00224                 ecc_code[SMC_OOB_ECC2], ecc_code[SMC_OOB_ECC2+1],
00225                 ecc_code[SMC_OOB_ECC2+2], ecc_calc[0], ecc_calc[1], ecc_calc[2]);
00226         ret = nand_correct_data(&this->data_buf[mtd->eccsize],
00227                                 &(ecc_code[SMC_OOB_ECC2]), &ecc_calc[0]);
00228         if (ret == -1)
00229                 return ret;
00230 
00231         return 0;
00232 }
00233 
00234 /*
00235  * NAND read with ECC
00236  */
00237 static int
00238 nand_read_ecc(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen,
00239               u_char *buf, u_char *ecc_code)
00240 {
00241         int j, offset, page;
00242         struct nand_chip *this = mtd->priv;
00243         int ret;
00244 
00245         DEBUG(MTD_DEBUG_LEVEL3,
00246                 __FUNCTION__ "(): from = 0x%08x, len = %i\n", (unsigned int)from,
00247                 (int)len);
00248 
00249         /* Do not allow reads past end of device */
00250         if ((from + len) > mtd->size) {
00251                 DEBUG(MTD_DEBUG_LEVEL0,
00252                         __FUNCTION__ "(): Attempt read beyond end of device\n");
00253                 *retlen = 0;
00254                 return -EINVAL;
00255         }
00256 
00257         /* Select the NAND device */
00258         nand_select();
00259 
00260         /* Initialize return value */
00261         *retlen = 0;
00262 
00263 #ifdef USE_256BYTE_NAND_FLASH
00264         /* First we calculate the starting page */
00265         page = from >> this->page_shift;
00266 
00267         /* Get raw starting column */
00268         offset = from & (mtd->oobblock - 1);
00269 
00270         /* if the length of Page is 256bytes,
00271          * 2 Pages must be taken for 1 Sector and as a result,
00272          * higher level 8 bytes of information
00273          * among the above 16 byte information must coincide with
00274          * Spare(or OOB) of Even-Page while lowel level 8 bytes
00275          * coincide with Spar(or OOB) of Odd-page.
00276          *   i.e, [0 block][oob][1 block][oob]
00277          *        [2 block][oob][3 block][oob]...
00278          */
00279         if (mtd->oobblock == 256) {
00280                 u_char ecc_calc[3];
00281                 int oob_offset;
00282 
00283                 /* Loop until all data read */
00284                 while (*retlen < len) {
00285                         nand_command(mtd, NAND_CMD_READ0, 0x00, page);
00286 
00287                         this->wait_for_ready();
00288 
00289                         /* Read in a block big enough for ECC */
00290                         for (j = 0; j < mtd->eccsize; j++)
00291                                 this->data_buf[j] = this->read_data();
00292 
00293                         if (!(page & 0x1)) {    /* page is odd! */
00294                                 nand_command(mtd, NAND_CMD_READOOB,
00295                                              SMC_OOB256_ECC1, page + 1);
00296                                 oob_offset = SMC_OOB_ECC1;
00297                         } else {
00298                                 nand_command(mtd, NAND_CMD_READOOB,
00299                                              SMC_OOB256_ECC2, page);
00300                                 oob_offset = SMC_OOB_ECC2;
00301                         }
00302 
00303                         this->wait_for_ready();
00304 
00305                         for (j = 0; j < 3; j++)
00306                                 ecc_code[oob_offset + j] = this->read_data();
00307                         nand_calculate_ecc(&this->data_buf[0], &ecc_calc[0]);
00308                         sm_swap(&ecc_calc[0], &ecc_calc[1]);
00309                         ret = nand_correct_data(&this->data_buf[0], 
00310                                                 &(ecc_code[oob_offset]), 
00311                                                 &ecc_calc[0]);
00312                         if (ret == -1)
00313                                 goto nand_read_ecc_err;
00314 
00315                         /* Read the data from ECC data buffer into return buffer */
00316                         if ((*retlen + (mtd->eccsize - offset)) > = len) {
00317                                 while (*retlen < len)
00318                                         buf[(*retlen)++] = this->data_buf[offset++];
00319                                 /* We're done */
00320                                 continue;
00321                         } else {
00322                                 for (j = offset; j < mtd->eccsize; j++)
00323                                         buf[(*retlen)++] = this->data_buf[j];
00324                         }
00325 
00326                         /*
00327                          * If the amount of data to be read is greater than
00328                          * (256 - offset), then all subsequent reads will take
00329                          * place on page or half-page (in the case of 512 byte
00330                          * page devices) aligned boundaries and the column
00331                          * address will be zero. Setting the column address to
00332                          * to zero after the first read allows us to simplify
00333                          * the reading of data and the if/else statements above.
00334                          */
00335                         if (offset)
00336                                 offset = 0x00;
00337 
00338                         /* Increment page address */
00339                         page++;
00340                 }
00341         } 
00342         else 
00343 #endif  /* USE_256BYTE_NAND_FLASH */
00344         { /* mtd->oobblock == 512 */
00345                 size_t last, next, len2;
00346 
00347                 last = from + len;
00348                 for (next = from; from < last; ) {
00349                         page = from >> this->page_shift;
00350                         offset = from & (mtd->oobblock - 1);
00351                         len2 = mtd->oobblock - offset;
00352                         next += len2;
00353                         nand_command(mtd, NAND_CMD_READ0, 0x00, page);
00354 
00355                         this->wait_for_ready();
00356 
00357                         ret = smc_read_ecc_512(mtd, ecc_code);
00358                         if (ret == -1)
00359                                 goto nand_read_ecc_err;
00360 
00361                         if (next >= last)
00362                                 if ((last & (mtd->oobblock - 1)) != 0)
00363                                         len2 = (last & (mtd->oobblock - 1)) - offset;
00364 
00365                         for (j = 0; j < len2; j++)
00366                                 buf[(*retlen) + j] = this->data_buf[offset + j];
00367                         *retlen += len2;
00368                         from = next;
00369                 }
00370         }
00371 
00372         ret = 0;
00373 
00374         /* De-select the NAND device */
00375         nand_deselect();
00376 
00377         return ret;
00378 
00379 nand_read_ecc_err:
00380         DEBUG(MTD_DEBUG_LEVEL0,
00381                 __FUNCTION__"(): Failed ECC read, page 0x%08lx\n", page);
00382         return -EIO;
00383 }
00384 
00385 /*
00386  * NAND read out-of-band
00387  */
00388 static int
00389 nand_read_oob(struct mtd_info *mtd, loff_t from, size_t len, 
00390               size_t *retlen, u_char *buf)
00391 {
00392         int i, offset, page;
00393         struct nand_chip *this = mtd->priv;
00394 
00395         DEBUG(MTD_DEBUG_LEVEL3,
00396                 __FUNCTION__ "(): from = 0x%08x, len = %i\n", (unsigned int)from,
00397                 (int)len);
00398 
00399         /* Shift to get page */
00400         page = ((int)from) >> this->page_shift;
00401 
00402         /* Mask to get column */
00403         offset = from & 0x0f;
00404 
00405         /* Initialize return length value */
00406         *retlen = 0;
00407 
00408         /* Do not allow read past end of page */
00409         if ((offset + len) > mtd->oobsize) {
00410                 DEBUG(MTD_DEBUG_LEVEL0,
00411                         __FUNCTION__"(): Attempt read past end of page " \
00412                         "0x%08lx, column %i, length %i\n", page, offset, len);
00413                 return -EINVAL;
00414         }
00415 
00416         /* Select the NAND device */
00417         nand_select();
00418 
00419         /* Send the read command */
00420         nand_command(mtd, NAND_CMD_READOOB, offset, page);
00421 
00422         this->wait_for_ready();
00423 
00424         /* Read the data */
00425         for (i = 0; i < len; i++)
00426                 buf[i] = this->read_data();
00427 
00428         /* De-select the NAND device */
00429         nand_deselect();
00430 
00431         /* Return happy */
00432         *retlen = len;
00433         return 0;
00434 }
00435 
00436 /*
00437  * NAND write
00438  */
00439 static int
00440 nand_write(struct mtd_info *mtd, loff_t to, size_t len, 
00441            size_t *retlen, const u_char *buf)
00442 {
00443         int i, page, col, cnt, status;
00444         struct nand_chip *this = mtd->priv;
00445 
00446         DEBUG(MTD_DEBUG_LEVEL3,
00447                 __FUNCTION__"(): to = 0x%08x, len = %i\n", (unsigned int)to,
00448                 (int) len);
00449 
00450         /* Do not allow write past end of page */
00451         if ((to + len) > mtd->size) {
00452                 DEBUG(MTD_DEBUG_LEVEL0,
00453                         __FUNCTION__"(): Attempted write past end of device\n");
00454                 return -EINVAL;
00455         }
00456 
00457         /* Shift to get page */
00458         page = ((int)to) >> this->page_shift;
00459 
00460         /* Get the starting column */
00461         col = to & (mtd->oobblock - 1);
00462 
00463         /* Initialize return length value */
00464         *retlen = 0;
00465 
00466         /* Select the NAND device */
00467         nand_select();
00468 
00469         /* Check the WP bit */
00470         nand_command(mtd, NAND_CMD_STATUS, -1, -1);
00471 
00472         this->wait_for_ready();
00473 
00474         if (!(this->read_data() & SMC_STAT_NOT_WP)) {
00475                 DEBUG(MTD_DEBUG_LEVEL0,
00476                         __FUNCTION__"(): Device is write protected!!!\n");
00477                 i = -EPERM;
00478                 goto nand_write_exit;
00479         }
00480 
00481         /* Loop until all data is written */
00482         while (*retlen < len) {
00483                 /* Write data into buffer */
00484                 if ((col + len) >= mtd->oobblock)
00485                         for (i = col, cnt = 0; i < mtd->oobblock; i++, cnt++)
00486                                 this->data_buf[i] = buf[(*retlen + cnt)];
00487                 else
00488                         for (i = col, cnt = 0; cnt < (len - *retlen); i++, cnt++)
00489                                 this->data_buf[i] = buf[(*retlen + cnt)];
00490                 /* Write ones for partial page programming */
00491                 for (i = mtd->oobblock; i < (mtd->oobblock + mtd->oobsize); i++)
00492                         this->data_buf[i] = 0xff;
00493                 
00494                 /* Write pre-padding bytes into buffer */
00495                 for (i = 0; i < col; i++)
00496                         this->data_buf[i] = 0xff;
00497 
00498                 /* Write post-padding bytes into buffer */
00499                 if ((col + (len - *retlen)) < mtd->oobblock) {
00500                         for (i = (col + cnt); i < mtd->oobblock; i++)
00501                                 this->data_buf[i] = 0xff;
00502                 }
00503 
00504                 /* Send command to begin auto page programming */
00505                 nand_command(mtd, NAND_CMD_SEQIN, 0x00, page);
00506 
00507                 /* Write out complete page of data */
00508                 this->hwcontrol(NAND_CTL_DAT_OUT);
00509                 for (i = 0; i < (mtd->oobblock + mtd->oobsize); i++)
00510                         this->write_data(this->data_buf[i]);
00511                 this->hwcontrol(NAND_CTL_DAT_IN);
00512 
00513                 /* Send command to actually program the data */
00514                 nand_command(mtd, NAND_CMD_PAGEPROG, -1, -1);
00515 
00516                 this->wait_for_ready();
00517 
00518                 /*
00519                  * Wait for program operation to complete. This could
00520                  * take up to 3000us (3ms) on some devices. so we try
00521                  * and exit as quickly as possible.
00522                  */
00523                 status = 0;
00524                 for (i = 0; i < 24; i++) {
00525                         /* Delay for 125us */
00526                         udelay(125);
00527 
00528                         /* Check for status */
00529                         nand_command(mtd, NAND_CMD_STATUS, -1, -1);
00530                         status = (int)this->read_data();
00531                         if (status & SMC_STAT_READY)
00532                                 break;
00533                 }
00534 
00535                 /* See if device thinks it succeeded */
00536                 if (status & SMC_STAT_WRITE_ERR) {
00537                         DEBUG(MTD_DEBUG_LEVEL0,
00538                                 __FUNCTION__"(): Failed write, page 0x%08x, " \
00539                                 "%6i bytes were sucesful\n", page, *retlen);
00540                         i = -EIO;
00541                         goto nand_write_exit;
00542                 }
00543 
00544 #ifdef CONFIG_MTD_NAND_VERIFY_WRITE
00545                 /*
00546                  * The NAND device assumes that it is always writing to
00547                  * a cleanly erased page. Hence, it performs its internal
00548                  * write verification only on bits that transitioned from
00549                  * 1 to 0. The device does NOT verify the whole page on
00550                  * a byte by byte basis. It is possible that the page was
00551                  * not completely erased or the page is becoming unusable
00552                  * due to wear. The read with ECC would catch the error
00553                  * later when the ECC page check fails, but we would rather
00554                  * catch it early in the page write stage. Better to write
00555                  * no data than invalid data.
00556                  */
00557 
00558                 /* Send command to read back the page */
00559                 if (col < mtd->eccsize)
00560                         nand_command(mtd, NAND_CMD_READ0, col, page);
00561                 else
00562                         nand_command(mtd, NAND_CMD_READ1, col - 256, page);
00563 
00564                 this->wait_for_ready();
00565 
00566                 /* Loop through and verify the data */
00567                 for (i = col; i < cnt; i++) {
00568                         if (this->data_buf[i] != this->read_data()) {
00569                                 DEBUG(MTD_DEBUG_LEVEL0,
00570                                         __FUNCTION__"(): Failed write verify, " \
00571                                         "page 0x%08x, %6i bytes were succesful\n",
00572                                         page, *retlen);
00573                                 i = -EIO;
00574                                 goto nand_write_exit;
00575                         }
00576                 }
00577 #endif
00578 
00579                 /* 
00580                  * If we are writing a large amount of data and/or it
00581                  * crosses page or half-page boundaries, we set the
00582                  * the column to zero. It simplifies the program logic.
00583                  */
00584                 if (col)
00585                         col = 0x00;
00586 
00587                 /* Update written bytes count */
00588                 *retlen += cnt;
00589 
00590                 /* Increment page address */
00591                 page++;
00592                 
00593         }
00594 
00595         /* Return happy */
00596         *retlen = len;
00597         i = 0;
00598 
00599 nand_write_exit:
00600         /* De-select the NAND device */
00601         nand_deselect();
00602 
00603         return i;
00604 }
00605 
00606 /*
00607  * NAND write witdh ECC, but only 1 sector!
00608  */
00609 static int
00610 nand_write_ecc(struct mtd_info *mtd, loff_t to, size_t len,
00611                size_t *retlen, const u_char *buf, u_char *ecc_code)
00612 {
00613         int i, page, cnt, status, ret;
00614         struct nand_chip *this = mtd->priv;
00615         unsigned int sector_size, page_size, oob_size;
00616 
00617         DEBUG(MTD_DEBUG_LEVEL3,
00618                 __FUNCTION__"(): to = 0x%08x, len = %i\n", (unsigned int)to,
00619                 (int)len);
00620 
00621         sector_size = this->dev->szS;
00622         page_size = mtd->oobblock;
00623         oob_size = mtd->oobsize;
00624         if (to & (sector_size - 1)) {
00625                 DEBUG(MTD_DEBUG_LEVEL0,
00626                         __FUNCTION__"(): Not Sector aligned\n");
00627                 return -EINVAL;
00628         }
00629         if (len != sector_size) {
00630                 DEBUG(MTD_DEBUG_LEVEL0,
00631                         __FUNCTION__"(): Only 1 Sector\n");
00632                 return -EINVAL;
00633         }
00634         /* Do not allow write past end of page */
00635         if ((to + len) > mtd->size) {
00636                 DEBUG(MTD_DEBUG_LEVEL0,
00637                         __FUNCTION__"(): Attempted write past end of device\n");
00638                 return -EINVAL;
00639         }
00640 
00641         /* Shift to get page */
00642         page = ((int) to) >> this->page_shift;
00643 
00644         /* Initialize return length value */
00645         *retlen = 0;
00646 
00647         /* Select the NAND device */
00648         nand_select();
00649 
00650         /* Check the WP bit */
00651         nand_command(mtd, NAND_CMD_STATUS, -1, -1);
00652 
00653         this->wait_for_ready();
00654 
00655         if (!(this->read_data() & SMC_STAT_NOT_WP)) {
00656                 DEBUG(MTD_DEBUG_LEVEL0,
00657                         __FUNCTION__"(): Device is write protected!!!\n");
00658                 ret = -EPERM;
00659                 goto nand_write_err;
00660         }
00661 
00662         /* Loop until all data is written */
00663         while (*retlen < len) {
00664                 /* Send command to begin auto page programming */
00665                 nand_command(mtd, NAND_CMD_SEQIN, 0x00, page);
00666 
00667                 this->hwcontrol(NAND_CTL_DAT_OUT);
00668 
00669                 /* Write out complete page of data */
00670                 for (i = 0, cnt = 0; i < page_size; i++, cnt++)
00671                         this->write_data(buf[(*retlen) + cnt]);
00672 
00673                 /* Write ones for partial page programming */
00674                 for (i = 0; i < oob_size; i++) {
00675 #ifdef USE_256BYTE_NAND_FLASH
00676                         if (*retlen & (sector_size -1))
00677                                 this->write_data(ecc_code[SMC_OOB256_SIZE + i]);
00678                         else
00679 #endif
00680                                 this->write_data(ecc_code[i]);
00681                 }
00682 
00683                 this->hwcontrol(NAND_CTL_DAT_IN);
00684 
00685                 /* Send command to actually program the data */
00686                 nand_command(mtd, NAND_CMD_PAGEPROG, -1, -1);
00687 
00688                 this->wait_for_ready();
00689 
00690                 /*
00691                  * Wait for program operation to complete. This could
00692                  * take up to 3000us (3ms) on some devices, so we try
00693                  * and exit as quickly as possible
00694                  */
00695                 status = 0;
00696                 for (i = 0; i < 25; i++) {
00697                         /* Delay for 125us */
00698                         udelay(125);                
00699 
00700                         /* Check the status */
00701                         nand_command(mtd, NAND_CMD_STATUS, -1, -1);
00702                         status = (int)this->read_data();
00703                         if (status & SMC_STAT_READY)
00704                                 break;
00705                 }
00706 
00707                 /* See if device thins it succeeded */
00708                 if (status & SMC_STAT_WRITE_ERR) {
00709                         DEBUG(MTD_DEBUG_LEVEL0,
00710                                 __FUNCTION__"(): Failed write, page 0x%08x, " \
00711                                 "%6i bytes were succesful\n", page, *retlen);
00712                         ret = -EIO;
00713                         goto nand_write_err;
00714                 }
00715 
00716 #ifdef CONFIG_MTD_NAND_VERIFY_WRITE
00717                 /*
00718                  * The NAND device assumes that it is always writing to
00719                  * a cleanly erased page. Hence, it performs its internal
00720                  * write verification only on bits tha transitioned from
00721                  * 1 to 0. The device does NOT verify the whole page on a
00722                  * byte by byte basis. It is possible that the page was
00723                  * not completely erased or the page is becoming unusable
00724                  * due to wear. The read with ECC would catch the error
00725                  * later when the ECC page check fails, but we would rather
00726                  * catch it early in the page write stage. Better to write
00727                  * no data than invalid data.
00728                  */
00729 
00730                 /* Send command to read back the page */
00731 #ifdef USE_256BYTE_NAND_FLASH
00732                 if (*retlen & (sector_size - 1))
00733                         nand_command(mtd, NAND_CMD_READ0, 0x00, page + 1);
00734                 else
00735 #endif
00736                         nand_command(mtd, NAND_CMD_READ0, 0x00, page);
00737 
00738                 this->wait_for_ready();
00739 
00740                 /* Loop through and verify the data */
00741                 for (i = 0; i < page_size; i++) {
00742                         if (this->data_buf[i] != this->read_data()) {
00743                                 DEBUG(MTD_DEBUG_LEVEL0,
00744                                         __FUNCTION__"(): Failed write verify, " \
00745                                         "page 0x%08x, %6i bytes were succesful\n",
00746                                         page, *retlen);
00747                                 ret = -EIO;
00748                                 goto nand_write_err;
00749                         }
00750                 }
00751 #endif
00752                 /* Update written bytes count */
00753                 *retlen += cnt;
00754 
00755                 /* Increment page address */
00756                 page++;
00757         }
00758 
00759         *retlen = len;
00760         ret = 0;
00761 
00762 nand_write_err:
00763         /* De-select the NAND device */
00764         nand_deselect();
00765 
00766         return ret;
00767 }
00768 
00769 /*
00770  * NAND write out-of-band
00771  */
00772 static int
00773 nand_write_oob(struct mtd_info *mtd, loff_t to, size_t len,
00774                size_t *retlen, const u_char *buf)
00775 {
00776         int i, offset, page, status, ret;
00777         struct nand_chip *this = mtd->priv;
00778 
00779         DEBUG(MTD_DEBUG_LEVEL3,
00780                 __FUNCTION__"(): to = 0x%08x, len = %i\n", (unsigned int)to,
00781                 (int)len);
00782 
00783         /* Shift to get page */
00784         page = ((int)to) >> this->page_shift;
00785 
00786         /* Mask to get column */
00787         offset = to & 0x1f;
00788 
00789         /* Initialize return length value */
00790         *retlen = 0;
00791 
00792         /* Do not allow write past end of page */
00793         if ((offset + len) > mtd->oobsize) {
00794                 DEBUG(MTD_DEBUG_LEVEL0,
00795                         __FUNCTION__"(): Attempt to write past end of page\n");
00796                 return -EINVAL;
00797         }
00798 
00799         /* Select the NAND device */
00800         nand_select();
00801 
00802         /* Check the WP bit */
00803         nand_command(mtd, NAND_CMD_STATUS, -1, -1);
00804 
00805         this->wait_for_ready();
00806 
00807         if (!(this->read_data() & SMC_STAT_NOT_WP)) {
00808                 DEBUG(MTD_DEBUG_LEVEL0,
00809                         __FUNCTION__"(): Device is write protected!!!\n");
00810                 ret = -EPERM;
00811                 goto nand_write_oob_err;
00812         }
00813 
00814         /* Write out desired data */
00815         nand_command(mtd, NAND_CMD_SEQIN, offset + mtd->oobblock, page);
00816         this->hwcontrol(NAND_CTL_DAT_OUT);
00817         for (i = 0; i < len; i++)
00818                 this->write_data(buf[i]);
00819         this->hwcontrol(NAND_CTL_DAT_IN);
00820 
00821         /* Send command to program the OOB data */
00822         nand_command(mtd, NAND_CMD_PAGEPROG, -1, -1);
00823 
00824         this->wait_for_ready();
00825 
00826         /*
00827          * Wait for program operation to complete. This could
00828          * take up to 3000us (3ms) on some devices, so we try
00829          * and exit as quickly as possible.
00830          */
00831         status = 0;
00832         for (i = 0; i < 24; i++) {
00833                 /* Delay for 125us */
00834                 udelay(125);
00835 
00836                 /* Check the status */
00837                 nand_command(mtd, NAND_CMD_STATUS, -1, -1);
00838 
00839                 this->wait_for_ready();
00840 
00841                 status = (int)this->read_data();
00842                 if (status & SMC_STAT_READY)
00843                         break;
00844         }
00845 
00846         /* See if device thinks it succeeded */
00847         if (status & SMC_STAT_WRITE_ERR) {
00848                 DEBUG(MTD_DEBUG_LEVEL0,
00849                         __FUNCTION__"(): Failed write, page 0x%08x\n", page);
00850                 ret = -EIO;
00851                 goto nand_write_oob_err;
00852         }
00853 
00854 #ifdef CONFIG_MTD_NAND_VERIFY_WRITE
00855         /* Send command to read back the data */
00856         nand_command(mtd, NAND_CMD_READOOB, offset, page);
00857 
00858         this->wait_for_ready();
00859 
00860         /* Loop through and verify the data */
00861         for (i = 0; i < len; i++) {
00862                 if (buf[i] != this->read_data()) {
00863                         DEBUG(MTD_DEBUG_LEVEL0,
00864                                 __FUNCTION__"(): Failed write verify, page 0x%08x\n",
00865                                 page);
00866                         ret = -EIO;
00867                         goto nand_write_oob_err;
00868                 }
00869         }
00870 #endif
00871 
00872         /* Return happy */
00873         *retlen = len;
00874         ret = 0;
00875         
00876 nand_write_oob_err:
00877         /* De-select the NAND device */
00878         nand_deselect();
00879 
00880         return ret;
00881 }
00882 
00883 /*
00884  * NAND erase a block
00885  */
00886 static int
00887 nand_erase(struct mtd_info *mtd, struct erase_info *instr)
00888 {
00889         int i, page, len, status, pages_per_block;
00890         struct nand_chip *this = mtd->priv;
00891 
00892         DEBUG(MTD_DEBUG_LEVEL3,
00893                 __FUNCTION__"(): start = 0x%08x, len = %i\n",
00894                 (unsigned int)instr->addr, (unsigned int)instr->len);
00895 
00896         /* Start address must aligned on block boundary */
00897         if (instr->addr & (mtd->erasesize - 1)) {
00898                 DEBUG(MTD_DEBUG_LEVEL0,
00899                         __FUNCTION__"(): Unaligned address\n");
00900                 return -EINVAL;
00901         }
00902 
00903         /* Length must align on block boundary */
00904         if (instr->len & (mtd->erasesize - 1)) {
00905                 DEBUG(MTD_DEBUG_LEVEL0,
00906                         __FUNCTION__"(): Length not block aligned\n");
00907                 return -EINVAL;
00908         }
00909 
00910         /* Do not allow erase past end of device */
00911         if ((instr->len + instr->addr) > mtd->size) {
00912                 DEBUG(MTD_DEBUG_LEVEL0,
00913                         __FUNCTION__"(): Erase past end of device\n");
00914                 return -EINVAL;
00915         }
00916 
00917         /* Shift to get first page */
00918         page = (int)(instr->addr >> this->page_shift);
00919 
00920         /* Calculate pages in each block */
00921         pages_per_block = mtd->erasesize / mtd->oobblock;
00922 
00923         /* Select the NAND device */
00924         nand_select();
00925 
00926         /* Check the WP bit */
00927         nand_command(mtd, NAND_CMD_STATUS, -1, -1);
00928 
00929         this->wait_for_ready();
00930 
00931         if (!(this->read_data() & SMC_STAT_NOT_WP)) {
00932                 DEBUG(MTD_DEBUG_LEVEL0,
00933                         __FUNCTION__"(): Device is write protected!!!\n");
00934                 nand_deselect();
00935                 return -EIO;
00936         }
00937 
00938         /* Loop through the pages */
00939         len = instr->len;
00940         while (len) {
00941                 /* Send commands to erase a page */
00942                 nand_command(mtd, NAND_CMD_ERASE1, -1, page);
00943                 nand_command(mtd, NAND_CMD_ERASE2, -1, -1);
00944 
00945                 this->wait_for_ready();
00946 
00947                 /*
00948                  * Wait for program operation to complete. This could
00949                  * take up to 4000us (4ms) on some devices, so we try
00950                  * and exit as quickly as possible.
00951                  */
00952                 status = 0;
00953                 for (i = 0; i < 32; i++) {
00954                         /* Delay for 125us */
00955                         udelay(125);
00956 
00957                         /* Check the status */
00958                         nand_command(mtd, NAND_CMD_STATUS, -1, -1);
00959 
00960                         this->wait_for_ready();
00961 
00962                         status = (int)this->read_data();
00963                         if (status & SMC_STAT_READY)
00964                                 break;
00965                 }
00966 
00967                 /* See if block erase succeeded */
00968                 if (status & SMC_STAT_WRITE_ERR) {
00969                         DEBUG(MTD_DEBUG_LEVEL0,
00970                                 __FUNCTION__"(): Failed erase, page 0x%08x\n", page);
00971                         nand_deselect();
00972 #if 0
00973                         instr->state = MTD_ERASE_FAILED;
00974                         if (instr->callback)
00975                                 instr->callback(instr);
00976 #endif
00977                         return -EIO;
00978                 }
00979 
00980                 /* Increment page address and decrement length */
00981                 len -= mtd->erasesize;
00982                 page += pages_per_block;
00983         }
00984 
00985         /* De-select the NAND device */
00986         nand_deselect();
00987 
00988 #if 0
00989         /* Do call back function */
00990         instr->state = MTD_ERASE_DONE;
00991         if (instr->callback)
00992                 instr->callback(instr);
00993 #endif
00994 
00995         /* Return happy */
00996         return 0;       
00997 }
00998 
00999 /*
01000  * Scan for the SMC device
01001  */
01002 int
01003 smc_scan(struct mtd_info *mtd)
01004 {
01005         int i, nand_maf_id, nand_dev_id;
01006         struct nand_chip *this = mtd->priv;
01007 
01008         /* Select the device */
01009         nand_select();
01010 
01011         /* Send the command for reading device ID */
01012         nand_command(mtd, NAND_CMD_READID, 0x00, -1);
01013 
01014         this->wait_for_ready();
01015 
01016         /* Read manufacturer and device IDs */
01017         nand_maf_id = this->read_data();
01018         nand_dev_id = this->read_data();
01019 
01020         /* Print and sotre flash device information */
01021         for (i = 0; nand_flash_ids[i].name != NULL; i++) {
01022                 if (nand_maf_id == nand_flash_ids[i].manufacture_id &&
01023                     nand_dev_id == nand_flash_ids[i].model_id) {
01024 #ifdef USE_256BYTE_NAND_FLASH
01025                         if (!mtd->size) {
01026                                 mtd->name = nand_flash_ids[i].name;
01027                                 mtd->erasesize = nand_flash_ids[i].erasesize;
01028                                 mtd->size = (1 << nand_flash_ids[i].chipshift);
01029                                 mtd->eccsize = 256;
01030                                 if (nand_flash_ids[i].page256) {
01031                                         mtd->oobblock = 256;
01032                                         mtd->oobsize = 8;
01033                                         this->page_shift = 8;
01034                                 } else {
01035                                         mtd->oobblock = 512;
01036                                         mtd->oobsize = 16;
01037                                         this->page_shift = 9;
01038                                 }
01039                                 this->dev = &nand_smc_info[GET_DI_NUM(nand_flash_ids[i].chipshift)];
01040                         }
01041 #else
01042                         if (!(mtd->size) && !(nand_flash_ids[i].page256)) {
01043                                 mtd->name = nand_flash_ids[i].name;
01044                                 mtd->erasesize = nand_flash_ids[i].erasesize;
01045                                 mtd->size = (1 << nand_flash_ids[i].chipshift);
01046                                 mtd->eccsize = 256;
01047                                 mtd->oobblock = 512;
01048                                 mtd->oobsize = 16;
01049                                 this->page_shift = 9;
01050                                 this->dev = &nand_smc_info[GET_DI_NUM(nand_flash_ids[i].chipshift)];
01051                         }
01052 #endif
01053                         printk("NAND device: Manufacture ID:" \
01054                                 " 0x%02x, Chip ID: 0x%02x (%s)\n",
01055                                 nand_maf_id, nand_dev_id, mtd->name);
01056                         break;
01057                 }           
01058         }
01059 
01060         /* De-select the device */
01061         nand_deselect();
01062 
01063         /* Print warning message for no device */
01064         if (!mtd->size) {
01065                 printk("No NAND device found!!!\n");
01066                 return 1;
01067         }
01068 
01069         /* Fill in remaining MTD driver data */
01070         mtd->type = MTD_NANDFLASH;
01071         mtd->flags = MTD_CAP_NANDFLASH | MTD_ECC;
01072         mtd->module = NULL;
01073         mtd->ecctype = MTD_ECC_SW;
01074         mtd->erase = nand_erase;
01075         mtd->point = NULL;
01076         mtd->unpoint = NULL;
01077         mtd->read = nand_read;
01078         mtd->write = nand_write;
01079         mtd->read_ecc = nand_read_ecc;
01080         mtd->write_ecc = nand_write_ecc;
01081         mtd->read_oob = nand_read_oob;
01082         mtd->write_oob = nand_write_oob;
01083         mtd->lock = NULL;
01084         mtd->unlock = NULL;
01085 
01086         /* Return happy */
01087         return 0;
01088 }
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Defines