2 * Copyright (c) 2003-2013 Broadcom Corporation
4 * Copyright (c) 2009-2010 Micron Technology, Inc.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
17 #include <linux/module.h>
18 #include <linux/delay.h>
19 #include <linux/mtd/mtd.h>
20 #include <linux/mtd/partitions.h>
21 #include <linux/mtd/rawnand.h>
22 #include <linux/spi/spi.h>
24 #include "mt29f_spinand.h"
26 #define BUFSIZE (10 * 64 * 2048)
27 #define CACHE_BUF 2112
29 * OOB area specification layout: Total 32 available free bytes.
32 static inline struct spinand_state *mtd_to_state(struct mtd_info *mtd)
34 struct nand_chip *chip = mtd_to_nand(mtd);
35 struct spinand_info *info = nand_get_controller_data(chip);
36 struct spinand_state *state = info->priv;
41 #ifdef CONFIG_MTD_SPINAND_ONDIEECC
42 static int enable_hw_ecc;
43 static int enable_read_hw_ecc;
45 static int spinand_ooblayout_64_ecc(struct mtd_info *mtd, int section,
46 struct mtd_oob_region *oobregion)
51 oobregion->offset = (section * 16) + 1;
52 oobregion->length = 6;
57 static int spinand_ooblayout_64_free(struct mtd_info *mtd, int section,
58 struct mtd_oob_region *oobregion)
63 oobregion->offset = (section * 16) + 8;
64 oobregion->length = 8;
69 static const struct mtd_ooblayout_ops spinand_oob_64_ops = {
70 .ecc = spinand_ooblayout_64_ecc,
71 .free = spinand_ooblayout_64_free,
76 * spinand_cmd - process a command to send to the SPI Nand
78 * Set up the command buffer to send to the SPI controller.
79 * The command buffer has to initialized to 0.
82 static int spinand_cmd(struct spi_device *spi, struct spinand_cmd *cmd)
84 struct spi_message message;
85 struct spi_transfer x[4];
88 spi_message_init(&message);
89 memset(x, 0, sizeof(x));
92 x[0].tx_buf = &cmd->cmd;
93 spi_message_add_tail(&x[0], &message);
96 x[1].len = cmd->n_addr;
97 x[1].tx_buf = cmd->addr;
98 spi_message_add_tail(&x[1], &message);
102 x[2].len = cmd->n_dummy;
103 x[2].tx_buf = &dummy;
104 spi_message_add_tail(&x[2], &message);
108 x[3].len = cmd->n_tx;
109 x[3].tx_buf = cmd->tx_buf;
110 spi_message_add_tail(&x[3], &message);
114 x[3].len = cmd->n_rx;
115 x[3].rx_buf = cmd->rx_buf;
116 spi_message_add_tail(&x[3], &message);
119 return spi_sync(spi, &message);
123 * spinand_read_id - Read SPI Nand ID
125 * read two ID bytes from the SPI Nand device
127 static int spinand_read_id(struct spi_device *spi_nand, u8 *id)
131 struct spinand_cmd cmd = {0};
133 cmd.cmd = CMD_READ_ID;
135 cmd.rx_buf = &nand_id[0];
137 retval = spinand_cmd(spi_nand, &cmd);
139 dev_err(&spi_nand->dev, "error %d reading id\n", retval);
148 * spinand_read_status - send command 0xf to the SPI Nand status register
150 * After read, write, or erase, the Nand device is expected to set the
152 * This function is to allow reading the status of the command: read,
154 * Once the status turns to be ready, the other status bits also are
157 static int spinand_read_status(struct spi_device *spi_nand, u8 *status)
159 struct spinand_cmd cmd = {0};
162 cmd.cmd = CMD_READ_REG;
164 cmd.addr[0] = REG_STATUS;
168 ret = spinand_cmd(spi_nand, &cmd);
170 dev_err(&spi_nand->dev, "err: %d read status register\n", ret);
175 #define MAX_WAIT_JIFFIES (40 * HZ)
176 static int wait_till_ready(struct spi_device *spi_nand)
178 unsigned long deadline;
182 deadline = jiffies + MAX_WAIT_JIFFIES;
184 retval = spinand_read_status(spi_nand, &stat);
191 } while (!time_after_eq(jiffies, deadline));
193 if ((stat & 0x1) == 0)
200 * spinand_get_otp - send command 0xf to read the SPI Nand OTP register
202 * There is one bit( bit 0x10 ) to set or to clear the internal ECC.
203 * Enable chip internal ECC, set the bit to 1
204 * Disable chip internal ECC, clear the bit to 0
206 static int spinand_get_otp(struct spi_device *spi_nand, u8 *otp)
208 struct spinand_cmd cmd = {0};
211 cmd.cmd = CMD_READ_REG;
213 cmd.addr[0] = REG_OTP;
217 retval = spinand_cmd(spi_nand, &cmd);
219 dev_err(&spi_nand->dev, "error %d get otp\n", retval);
224 * spinand_set_otp - send command 0x1f to write the SPI Nand OTP register
226 * There is one bit( bit 0x10 ) to set or to clear the internal ECC.
227 * Enable chip internal ECC, set the bit to 1
228 * Disable chip internal ECC, clear the bit to 0
230 static int spinand_set_otp(struct spi_device *spi_nand, u8 *otp)
233 struct spinand_cmd cmd = {0};
235 cmd.cmd = CMD_WRITE_REG;
237 cmd.addr[0] = REG_OTP;
241 retval = spinand_cmd(spi_nand, &cmd);
243 dev_err(&spi_nand->dev, "error %d set otp\n", retval);
248 #ifdef CONFIG_MTD_SPINAND_ONDIEECC
250 * spinand_enable_ecc - send command 0x1f to write the SPI Nand OTP register
252 * There is one bit( bit 0x10 ) to set or to clear the internal ECC.
253 * Enable chip internal ECC, set the bit to 1
254 * Disable chip internal ECC, clear the bit to 0
256 static int spinand_enable_ecc(struct spi_device *spi_nand)
261 retval = spinand_get_otp(spi_nand, &otp);
265 if ((otp & OTP_ECC_MASK) == OTP_ECC_MASK)
268 retval = spinand_set_otp(spi_nand, &otp);
271 return spinand_get_otp(spi_nand, &otp);
275 static int spinand_disable_ecc(struct spi_device *spi_nand)
280 retval = spinand_get_otp(spi_nand, &otp);
284 if ((otp & OTP_ECC_MASK) == OTP_ECC_MASK) {
285 otp &= ~OTP_ECC_MASK;
286 retval = spinand_set_otp(spi_nand, &otp);
289 return spinand_get_otp(spi_nand, &otp);
295 * spinand_write_enable - send command 0x06 to enable write or erase the
298 * Before write and erase the Nand cells, the write enable has to be set.
299 * After the write or erase, the write enable bit is automatically
300 * cleared (status register bit 2)
301 * Set the bit 2 of the status register has the same effect
303 static int spinand_write_enable(struct spi_device *spi_nand)
305 struct spinand_cmd cmd = {0};
307 cmd.cmd = CMD_WR_ENABLE;
308 return spinand_cmd(spi_nand, &cmd);
311 static int spinand_read_page_to_cache(struct spi_device *spi_nand, u16 page_id)
313 struct spinand_cmd cmd = {0};
319 cmd.addr[1] = (u8)((row & 0xff00) >> 8);
320 cmd.addr[2] = (u8)(row & 0x00ff);
322 return spinand_cmd(spi_nand, &cmd);
326 * spinand_read_from_cache - send command 0x03 to read out the data from the
327 * cache register (2112 bytes max)
329 * The read can specify 1 to 2112 bytes of data read at the corresponding
333 static int spinand_read_from_cache(struct spi_device *spi_nand, u16 page_id,
334 u16 byte_id, u16 len, u8 *rbuf)
336 struct spinand_cmd cmd = {0};
340 cmd.cmd = CMD_READ_RDM;
342 cmd.addr[0] = (u8)((column & 0xff00) >> 8);
343 cmd.addr[0] |= (u8)(((page_id >> 6) & 0x1) << 4);
344 cmd.addr[1] = (u8)(column & 0x00ff);
345 cmd.addr[2] = (u8)(0xff);
350 return spinand_cmd(spi_nand, &cmd);
354 * spinand_read_page - read a page
355 * @page_id: the physical page number
356 * @offset: the location from 0 to 2111
357 * @len: number of bytes to read
358 * @rbuf: read buffer to hold @len bytes
361 * The read includes two commands to the Nand - 0x13 and 0x03 commands
362 * Poll to read status to wait for tRD time.
364 static int spinand_read_page(struct spi_device *spi_nand, u16 page_id,
365 u16 offset, u16 len, u8 *rbuf)
370 #ifdef CONFIG_MTD_SPINAND_ONDIEECC
371 if (enable_read_hw_ecc) {
372 if (spinand_enable_ecc(spi_nand) < 0)
373 dev_err(&spi_nand->dev, "enable HW ECC failed!");
376 ret = spinand_read_page_to_cache(spi_nand, page_id);
380 if (wait_till_ready(spi_nand))
381 dev_err(&spi_nand->dev, "WAIT timedout!!!\n");
384 ret = spinand_read_status(spi_nand, &status);
386 dev_err(&spi_nand->dev,
387 "err %d read status register\n", ret);
391 if ((status & STATUS_OIP_MASK) == STATUS_READY) {
392 if ((status & STATUS_ECC_MASK) == STATUS_ECC_ERROR) {
393 dev_err(&spi_nand->dev, "ecc error, page=%d\n",
401 ret = spinand_read_from_cache(spi_nand, page_id, offset, len, rbuf);
403 dev_err(&spi_nand->dev, "read from cache failed!!\n");
407 #ifdef CONFIG_MTD_SPINAND_ONDIEECC
408 if (enable_read_hw_ecc) {
409 ret = spinand_disable_ecc(spi_nand);
411 dev_err(&spi_nand->dev, "disable ecc failed!!\n");
414 enable_read_hw_ecc = 0;
421 * spinand_program_data_to_cache - write a page to cache
422 * @byte_id: the location to write to the cache
423 * @len: number of bytes to write
424 * @wbuf: write buffer holding @len bytes
427 * The write command used here is 0x84--indicating that the cache is
429 * Since it is writing the data to cache, there is no tPROG time.
431 static int spinand_program_data_to_cache(struct spi_device *spi_nand,
432 u16 page_id, u16 byte_id,
435 struct spinand_cmd cmd = {0};
439 cmd.cmd = CMD_PROG_PAGE_CLRCACHE;
441 cmd.addr[0] = (u8)((column & 0xff00) >> 8);
442 cmd.addr[0] |= (u8)(((page_id >> 6) & 0x1) << 4);
443 cmd.addr[1] = (u8)(column & 0x00ff);
447 return spinand_cmd(spi_nand, &cmd);
451 * spinand_program_execute - write a page from cache to the Nand array
452 * @page_id: the physical page location to write the page.
455 * The write command used here is 0x10--indicating the cache is writing to
457 * Need to wait for tPROG time to finish the transaction.
459 static int spinand_program_execute(struct spi_device *spi_nand, u16 page_id)
461 struct spinand_cmd cmd = {0};
465 cmd.cmd = CMD_PROG_PAGE_EXC;
467 cmd.addr[1] = (u8)((row & 0xff00) >> 8);
468 cmd.addr[2] = (u8)(row & 0x00ff);
470 return spinand_cmd(spi_nand, &cmd);
474 * spinand_program_page - write a page
475 * @page_id: the physical page location to write the page.
476 * @offset: the location from the cache starting from 0 to 2111
477 * @len: the number of bytes to write
478 * @buf: the buffer holding @len bytes
481 * The commands used here are 0x06, 0x84, and 0x10--indicating that
482 * the write enable is first sent, the write cache command, and the
483 * write execute command.
484 * Poll to wait for the tPROG time to finish the transaction.
486 static int spinand_program_page(struct spi_device *spi_nand,
487 u16 page_id, u16 offset, u16 len, u8 *buf)
492 #ifdef CONFIG_MTD_SPINAND_ONDIEECC
495 wbuf = devm_kzalloc(&spi_nand->dev, CACHE_BUF, GFP_KERNEL);
499 enable_read_hw_ecc = 1;
500 retval = spinand_read_page(spi_nand, page_id, 0, CACHE_BUF, wbuf);
502 dev_err(&spi_nand->dev, "ecc error on read page!!!\n");
506 for (i = offset, j = 0; i < len; i++, j++)
510 retval = spinand_enable_ecc(spi_nand);
512 dev_err(&spi_nand->dev, "enable ecc failed!!\n");
519 retval = spinand_write_enable(spi_nand);
521 dev_err(&spi_nand->dev, "write enable failed!!\n");
524 if (wait_till_ready(spi_nand))
525 dev_err(&spi_nand->dev, "wait timedout!!!\n");
527 retval = spinand_program_data_to_cache(spi_nand, page_id,
531 retval = spinand_program_execute(spi_nand, page_id);
535 retval = spinand_read_status(spi_nand, &status);
537 dev_err(&spi_nand->dev,
538 "error %d reading status register\n", retval);
542 if ((status & STATUS_OIP_MASK) == STATUS_READY) {
543 if ((status & STATUS_P_FAIL_MASK) == STATUS_P_FAIL) {
544 dev_err(&spi_nand->dev,
545 "program error, page %d\n", page_id);
551 #ifdef CONFIG_MTD_SPINAND_ONDIEECC
553 retval = spinand_disable_ecc(spi_nand);
555 dev_err(&spi_nand->dev, "disable ecc failed!!\n");
566 * spinand_erase_block_erase - erase a page
567 * @block_id: the physical block location to erase.
570 * The command used here is 0xd8--indicating an erase command to erase
571 * one block--64 pages
572 * Need to wait for tERS.
574 static int spinand_erase_block_erase(struct spi_device *spi_nand, u16 block_id)
576 struct spinand_cmd cmd = {0};
580 cmd.cmd = CMD_ERASE_BLK;
582 cmd.addr[1] = (u8)((row & 0xff00) >> 8);
583 cmd.addr[2] = (u8)(row & 0x00ff);
585 return spinand_cmd(spi_nand, &cmd);
589 * spinand_erase_block - erase a page
590 * @block_id: the physical block location to erase.
593 * The commands used here are 0x06 and 0xd8--indicating an erase
594 * command to erase one block--64 pages
595 * It will first to enable the write enable bit (0x06 command),
596 * and then send the 0xd8 erase command
597 * Poll to wait for the tERS time to complete the tranaction.
599 static int spinand_erase_block(struct spi_device *spi_nand, u16 block_id)
604 retval = spinand_write_enable(spi_nand);
605 if (wait_till_ready(spi_nand))
606 dev_err(&spi_nand->dev, "wait timedout!!!\n");
608 retval = spinand_erase_block_erase(spi_nand, block_id);
610 retval = spinand_read_status(spi_nand, &status);
612 dev_err(&spi_nand->dev,
613 "error %d reading status register\n", retval);
617 if ((status & STATUS_OIP_MASK) == STATUS_READY) {
618 if ((status & STATUS_E_FAIL_MASK) == STATUS_E_FAIL) {
619 dev_err(&spi_nand->dev,
620 "erase error, block %d\n", block_id);
629 #ifdef CONFIG_MTD_SPINAND_ONDIEECC
630 static int spinand_write_page_hwecc(struct mtd_info *mtd,
631 struct nand_chip *chip,
632 const u8 *buf, int oob_required,
636 int eccsize = chip->ecc.size;
637 int eccsteps = chip->ecc.steps;
640 chip->write_buf(mtd, p, eccsize * eccsteps);
644 static int spinand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
645 u8 *buf, int oob_required, int page)
650 int eccsize = chip->ecc.size;
651 int eccsteps = chip->ecc.steps;
652 struct spinand_info *info = nand_get_controller_data(chip);
654 enable_read_hw_ecc = 1;
656 chip->read_buf(mtd, p, eccsize * eccsteps);
658 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
661 retval = spinand_read_status(info->spi, &status);
664 "error %d reading status register\n", retval);
668 if ((status & STATUS_OIP_MASK) == STATUS_READY) {
669 if ((status & STATUS_ECC_MASK) == STATUS_ECC_ERROR) {
670 pr_info("spinand: ECC error\n");
671 mtd->ecc_stats.failed++;
672 } else if ((status & STATUS_ECC_MASK) ==
673 STATUS_ECC_1BIT_CORRECTED)
674 mtd->ecc_stats.corrected++;
682 static void spinand_select_chip(struct mtd_info *mtd, int dev)
686 static u8 spinand_read_byte(struct mtd_info *mtd)
688 struct spinand_state *state = mtd_to_state(mtd);
691 data = state->buf[state->buf_ptr];
696 static int spinand_wait(struct mtd_info *mtd, struct nand_chip *chip)
698 struct spinand_info *info = nand_get_controller_data(chip);
700 unsigned long timeo = jiffies;
701 int retval, state = chip->state;
704 if (state == FL_ERASING)
705 timeo += (HZ * 400) / 1000;
707 timeo += (HZ * 20) / 1000;
709 while (time_before(jiffies, timeo)) {
710 retval = spinand_read_status(info->spi, &status);
713 "error %d reading status register\n", retval);
717 if ((status & STATUS_OIP_MASK) == STATUS_READY)
725 static void spinand_write_buf(struct mtd_info *mtd, const u8 *buf, int len)
727 struct spinand_state *state = mtd_to_state(mtd);
729 memcpy(state->buf + state->buf_ptr, buf, len);
730 state->buf_ptr += len;
733 static void spinand_read_buf(struct mtd_info *mtd, u8 *buf, int len)
735 struct spinand_state *state = mtd_to_state(mtd);
737 memcpy(buf, state->buf + state->buf_ptr, len);
738 state->buf_ptr += len;
742 * spinand_reset- send RESET command "0xff" to the Nand device.
744 static void spinand_reset(struct spi_device *spi_nand)
746 struct spinand_cmd cmd = {0};
750 if (spinand_cmd(spi_nand, &cmd) < 0)
751 pr_info("spinand reset failed!\n");
753 /* elapse 1ms before issuing any other command */
754 usleep_range(1000, 2000);
756 if (wait_till_ready(spi_nand))
757 dev_err(&spi_nand->dev, "wait timedout!\n");
760 static void spinand_cmdfunc(struct mtd_info *mtd, unsigned int command,
761 int column, int page)
763 struct nand_chip *chip = mtd_to_nand(mtd);
764 struct spinand_info *info = nand_get_controller_data(chip);
765 struct spinand_state *state = info->priv;
769 * READ0 - read in first 0x800 bytes
774 spinand_read_page(info->spi, page, 0x0, 0x840, state->buf);
776 /* READOOB reads only the OOB because no ECC is performed. */
777 case NAND_CMD_READOOB:
779 spinand_read_page(info->spi, page, 0x800, 0x40, state->buf);
781 case NAND_CMD_RNDOUT:
782 state->buf_ptr = column;
784 case NAND_CMD_READID:
786 spinand_read_id(info->spi, state->buf);
791 /* ERASE1 stores the block and page address */
792 case NAND_CMD_ERASE1:
793 spinand_erase_block(info->spi, page);
795 /* ERASE2 uses the block and page address from ERASE1 */
796 case NAND_CMD_ERASE2:
798 /* SEQIN sets up the addr buffer and all registers except the length */
804 /* PAGEPROG reuses all of the setup from SEQIN and adds the length */
805 case NAND_CMD_PAGEPROG:
806 spinand_program_page(info->spi, state->row, state->col,
807 state->buf_ptr, state->buf);
809 case NAND_CMD_STATUS:
810 spinand_get_otp(info->spi, state->buf);
811 if (!(state->buf[0] & 0x80))
812 state->buf[0] = 0x80;
817 if (wait_till_ready(info->spi))
818 dev_err(&info->spi->dev, "WAIT timedout!!!\n");
819 /* a minimum of 250us must elapse before issuing RESET cmd*/
820 usleep_range(250, 1000);
821 spinand_reset(info->spi);
824 dev_err(&mtd->dev, "Unknown CMD: 0x%x\n", command);
829 * spinand_lock_block - send write register 0x1f command to the Nand device
832 * After power up, all the Nand blocks are locked. This function allows
833 * one to unlock the blocks, and so it can be written or erased.
835 static int spinand_lock_block(struct spi_device *spi_nand, u8 lock)
837 struct spinand_cmd cmd = {0};
841 ret = spinand_get_otp(spi_nand, &otp);
843 cmd.cmd = CMD_WRITE_REG;
845 cmd.addr[0] = REG_BLOCK_LOCK;
849 ret = spinand_cmd(spi_nand, &cmd);
851 dev_err(&spi_nand->dev, "error %d lock block\n", ret);
857 * spinand_probe - [spinand Interface]
858 * @spi_nand: registered device driver.
861 * Set up the device driver parameters to make the device available.
863 static int spinand_probe(struct spi_device *spi_nand)
865 struct mtd_info *mtd;
866 struct nand_chip *chip;
867 struct spinand_info *info;
868 struct spinand_state *state;
870 info = devm_kzalloc(&spi_nand->dev, sizeof(struct spinand_info),
875 info->spi = spi_nand;
877 spinand_lock_block(spi_nand, BL_ALL_UNLOCKED);
879 state = devm_kzalloc(&spi_nand->dev, sizeof(struct spinand_state),
886 state->buf = devm_kzalloc(&spi_nand->dev, BUFSIZE, GFP_KERNEL);
890 chip = devm_kzalloc(&spi_nand->dev, sizeof(struct nand_chip),
895 #ifdef CONFIG_MTD_SPINAND_ONDIEECC
896 chip->ecc.mode = NAND_ECC_HW;
897 chip->ecc.size = 0x200;
898 chip->ecc.bytes = 0x6;
899 chip->ecc.steps = 0x4;
901 chip->ecc.strength = 1;
902 chip->ecc.total = chip->ecc.steps * chip->ecc.bytes;
903 chip->ecc.read_page = spinand_read_page_hwecc;
904 chip->ecc.write_page = spinand_write_page_hwecc;
906 chip->ecc.mode = NAND_ECC_SOFT;
907 chip->ecc.algo = NAND_ECC_HAMMING;
908 if (spinand_disable_ecc(spi_nand) < 0)
909 dev_info(&spi_nand->dev, "%s: disable ecc failed!\n",
913 nand_set_flash_node(chip, spi_nand->dev.of_node);
914 nand_set_controller_data(chip, info);
915 chip->read_buf = spinand_read_buf;
916 chip->write_buf = spinand_write_buf;
917 chip->read_byte = spinand_read_byte;
918 chip->cmdfunc = spinand_cmdfunc;
919 chip->waitfunc = spinand_wait;
920 chip->options |= NAND_CACHEPRG;
921 chip->select_chip = spinand_select_chip;
922 chip->onfi_set_features = nand_onfi_get_set_features_notsupp;
923 chip->onfi_get_features = nand_onfi_get_set_features_notsupp;
925 mtd = nand_to_mtd(chip);
927 dev_set_drvdata(&spi_nand->dev, mtd);
929 mtd->dev.parent = &spi_nand->dev;
931 #ifdef CONFIG_MTD_SPINAND_ONDIEECC
932 mtd_set_ooblayout(mtd, &spinand_oob_64_ops);
935 if (nand_scan(mtd, 1))
938 return mtd_device_register(mtd, NULL, 0);
942 * spinand_remove - remove the device driver
943 * @spi: the spi device.
946 * Remove the device driver parameters and free up allocated memories.
948 static int spinand_remove(struct spi_device *spi)
950 mtd_device_unregister(dev_get_drvdata(&spi->dev));
955 static const struct of_device_id spinand_dt[] = {
956 { .compatible = "spinand,mt29f", },
959 MODULE_DEVICE_TABLE(of, spinand_dt);
962 * Device name structure description
964 static struct spi_driver spinand_driver = {
967 .of_match_table = spinand_dt,
969 .probe = spinand_probe,
970 .remove = spinand_remove,
973 module_spi_driver(spinand_driver);
975 MODULE_DESCRIPTION("SPI NAND driver for Micron");
976 MODULE_AUTHOR("Henry Pan <hspan@micron.com>, Kamlakant Patel <kamlakant.patel@broadcom.com>");
977 MODULE_LICENSE("GPL v2");