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/nand.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 struct nand_ecclayout spinand_oob_64 = {
49 17, 18, 19, 20, 21, 22,
50 33, 34, 35, 36, 37, 38,
51 49, 50, 51, 52, 53, 54, },
66 * spinand_cmd - process a command to send to the SPI Nand
68 * Set up the command buffer to send to the SPI controller.
69 * The command buffer has to initialized to 0.
72 static int spinand_cmd(struct spi_device *spi, struct spinand_cmd *cmd)
74 struct spi_message message;
75 struct spi_transfer x[4];
78 spi_message_init(&message);
79 memset(x, 0, sizeof(x));
82 x[0].tx_buf = &cmd->cmd;
83 spi_message_add_tail(&x[0], &message);
86 x[1].len = cmd->n_addr;
87 x[1].tx_buf = cmd->addr;
88 spi_message_add_tail(&x[1], &message);
92 x[2].len = cmd->n_dummy;
94 spi_message_add_tail(&x[2], &message);
99 x[3].tx_buf = cmd->tx_buf;
100 spi_message_add_tail(&x[3], &message);
104 x[3].len = cmd->n_rx;
105 x[3].rx_buf = cmd->rx_buf;
106 spi_message_add_tail(&x[3], &message);
109 return spi_sync(spi, &message);
113 * spinand_read_id - Read SPI Nand ID
115 * read two ID bytes from the SPI Nand device
117 static int spinand_read_id(struct spi_device *spi_nand, u8 *id)
121 struct spinand_cmd cmd = {0};
123 cmd.cmd = CMD_READ_ID;
125 cmd.rx_buf = &nand_id[0];
127 retval = spinand_cmd(spi_nand, &cmd);
129 dev_err(&spi_nand->dev, "error %d reading id\n", retval);
138 * spinand_read_status - send command 0xf to the SPI Nand status register
140 * After read, write, or erase, the Nand device is expected to set the
142 * This function is to allow reading the status of the command: read,
144 * Once the status turns to be ready, the other status bits also are
147 static int spinand_read_status(struct spi_device *spi_nand, u8 *status)
149 struct spinand_cmd cmd = {0};
152 cmd.cmd = CMD_READ_REG;
154 cmd.addr[0] = REG_STATUS;
158 ret = spinand_cmd(spi_nand, &cmd);
160 dev_err(&spi_nand->dev, "err: %d read status register\n", ret);
165 #define MAX_WAIT_JIFFIES (40 * HZ)
166 static int wait_till_ready(struct spi_device *spi_nand)
168 unsigned long deadline;
172 deadline = jiffies + MAX_WAIT_JIFFIES;
174 retval = spinand_read_status(spi_nand, &stat);
181 } while (!time_after_eq(jiffies, deadline));
183 if ((stat & 0x1) == 0)
190 * spinand_get_otp - send command 0xf to read the SPI Nand OTP register
192 * There is one bit( bit 0x10 ) to set or to clear the internal ECC.
193 * Enable chip internal ECC, set the bit to 1
194 * Disable chip internal ECC, clear the bit to 0
196 static int spinand_get_otp(struct spi_device *spi_nand, u8 *otp)
198 struct spinand_cmd cmd = {0};
201 cmd.cmd = CMD_READ_REG;
203 cmd.addr[0] = REG_OTP;
207 retval = spinand_cmd(spi_nand, &cmd);
209 dev_err(&spi_nand->dev, "error %d get otp\n", retval);
214 * spinand_set_otp - send command 0x1f to write the SPI Nand OTP register
216 * There is one bit( bit 0x10 ) to set or to clear the internal ECC.
217 * Enable chip internal ECC, set the bit to 1
218 * Disable chip internal ECC, clear the bit to 0
220 static int spinand_set_otp(struct spi_device *spi_nand, u8 *otp)
223 struct spinand_cmd cmd = {0};
225 cmd.cmd = CMD_WRITE_REG;
227 cmd.addr[0] = REG_OTP;
231 retval = spinand_cmd(spi_nand, &cmd);
233 dev_err(&spi_nand->dev, "error %d set otp\n", retval);
238 #ifdef CONFIG_MTD_SPINAND_ONDIEECC
240 * spinand_enable_ecc - send command 0x1f to write the SPI Nand OTP register
242 * There is one bit( bit 0x10 ) to set or to clear the internal ECC.
243 * Enable chip internal ECC, set the bit to 1
244 * Disable chip internal ECC, clear the bit to 0
246 static int spinand_enable_ecc(struct spi_device *spi_nand)
251 retval = spinand_get_otp(spi_nand, &otp);
255 if ((otp & OTP_ECC_MASK) == OTP_ECC_MASK)
258 retval = spinand_set_otp(spi_nand, &otp);
261 return spinand_get_otp(spi_nand, &otp);
265 static int spinand_disable_ecc(struct spi_device *spi_nand)
270 retval = spinand_get_otp(spi_nand, &otp);
274 if ((otp & OTP_ECC_MASK) == OTP_ECC_MASK) {
275 otp &= ~OTP_ECC_MASK;
276 retval = spinand_set_otp(spi_nand, &otp);
279 return spinand_get_otp(spi_nand, &otp);
285 * spinand_write_enable - send command 0x06 to enable write or erase the
288 * Before write and erase the Nand cells, the write enable has to be set.
289 * After the write or erase, the write enable bit is automatically
290 * cleared (status register bit 2)
291 * Set the bit 2 of the status register has the same effect
293 static int spinand_write_enable(struct spi_device *spi_nand)
295 struct spinand_cmd cmd = {0};
297 cmd.cmd = CMD_WR_ENABLE;
298 return spinand_cmd(spi_nand, &cmd);
301 static int spinand_read_page_to_cache(struct spi_device *spi_nand, u16 page_id)
303 struct spinand_cmd cmd = {0};
309 cmd.addr[1] = (u8)((row & 0xff00) >> 8);
310 cmd.addr[2] = (u8)(row & 0x00ff);
312 return spinand_cmd(spi_nand, &cmd);
316 * spinand_read_from_cache - send command 0x03 to read out the data from the
317 * cache register (2112 bytes max)
319 * The read can specify 1 to 2112 bytes of data read at the corresponding
323 static int spinand_read_from_cache(struct spi_device *spi_nand, u16 page_id,
324 u16 byte_id, u16 len, u8 *rbuf)
326 struct spinand_cmd cmd = {0};
330 cmd.cmd = CMD_READ_RDM;
332 cmd.addr[0] = (u8)((column & 0xff00) >> 8);
333 cmd.addr[0] |= (u8)(((page_id >> 6) & 0x1) << 4);
334 cmd.addr[1] = (u8)(column & 0x00ff);
335 cmd.addr[2] = (u8)(0xff);
340 return spinand_cmd(spi_nand, &cmd);
344 * spinand_read_page - read a page
345 * @page_id: the physical page number
346 * @offset: the location from 0 to 2111
347 * @len: number of bytes to read
348 * @rbuf: read buffer to hold @len bytes
351 * The read includes two commands to the Nand - 0x13 and 0x03 commands
352 * Poll to read status to wait for tRD time.
354 static int spinand_read_page(struct spi_device *spi_nand, u16 page_id,
355 u16 offset, u16 len, u8 *rbuf)
360 #ifdef CONFIG_MTD_SPINAND_ONDIEECC
361 if (enable_read_hw_ecc) {
362 if (spinand_enable_ecc(spi_nand) < 0)
363 dev_err(&spi_nand->dev, "enable HW ECC failed!");
366 ret = spinand_read_page_to_cache(spi_nand, page_id);
370 if (wait_till_ready(spi_nand))
371 dev_err(&spi_nand->dev, "WAIT timedout!!!\n");
374 ret = spinand_read_status(spi_nand, &status);
376 dev_err(&spi_nand->dev,
377 "err %d read status register\n", ret);
381 if ((status & STATUS_OIP_MASK) == STATUS_READY) {
382 if ((status & STATUS_ECC_MASK) == STATUS_ECC_ERROR) {
383 dev_err(&spi_nand->dev, "ecc error, page=%d\n",
391 ret = spinand_read_from_cache(spi_nand, page_id, offset, len, rbuf);
393 dev_err(&spi_nand->dev, "read from cache failed!!\n");
397 #ifdef CONFIG_MTD_SPINAND_ONDIEECC
398 if (enable_read_hw_ecc) {
399 ret = spinand_disable_ecc(spi_nand);
401 dev_err(&spi_nand->dev, "disable ecc failed!!\n");
404 enable_read_hw_ecc = 0;
411 * spinand_program_data_to_cache - write a page to cache
412 * @byte_id: the location to write to the cache
413 * @len: number of bytes to write
414 * @wbuf: write buffer holding @len bytes
417 * The write command used here is 0x84--indicating that the cache is
419 * Since it is writing the data to cache, there is no tPROG time.
421 static int spinand_program_data_to_cache(struct spi_device *spi_nand,
422 u16 page_id, u16 byte_id,
425 struct spinand_cmd cmd = {0};
429 cmd.cmd = CMD_PROG_PAGE_CLRCACHE;
431 cmd.addr[0] = (u8)((column & 0xff00) >> 8);
432 cmd.addr[0] |= (u8)(((page_id >> 6) & 0x1) << 4);
433 cmd.addr[1] = (u8)(column & 0x00ff);
437 return spinand_cmd(spi_nand, &cmd);
441 * spinand_program_execute - write a page from cache to the Nand array
442 * @page_id: the physical page location to write the page.
445 * The write command used here is 0x10--indicating the cache is writing to
447 * Need to wait for tPROG time to finish the transaction.
449 static int spinand_program_execute(struct spi_device *spi_nand, u16 page_id)
451 struct spinand_cmd cmd = {0};
455 cmd.cmd = CMD_PROG_PAGE_EXC;
457 cmd.addr[1] = (u8)((row & 0xff00) >> 8);
458 cmd.addr[2] = (u8)(row & 0x00ff);
460 return spinand_cmd(spi_nand, &cmd);
464 * spinand_program_page - write a page
465 * @page_id: the physical page location to write the page.
466 * @offset: the location from the cache starting from 0 to 2111
467 * @len: the number of bytes to write
468 * @buf: the buffer holding @len bytes
471 * The commands used here are 0x06, 0x84, and 0x10--indicating that
472 * the write enable is first sent, the write cache command, and the
473 * write execute command.
474 * Poll to wait for the tPROG time to finish the transaction.
476 static int spinand_program_page(struct spi_device *spi_nand,
477 u16 page_id, u16 offset, u16 len, u8 *buf)
482 #ifdef CONFIG_MTD_SPINAND_ONDIEECC
485 wbuf = devm_kzalloc(&spi_nand->dev, CACHE_BUF, GFP_KERNEL);
489 enable_read_hw_ecc = 0;
490 spinand_read_page(spi_nand, page_id, 0, CACHE_BUF, wbuf);
492 for (i = offset, j = 0; i < len; i++, j++)
496 retval = spinand_enable_ecc(spi_nand);
498 dev_err(&spi_nand->dev, "enable ecc failed!!\n");
505 retval = spinand_write_enable(spi_nand);
507 dev_err(&spi_nand->dev, "write enable failed!!\n");
510 if (wait_till_ready(spi_nand))
511 dev_err(&spi_nand->dev, "wait timedout!!!\n");
513 retval = spinand_program_data_to_cache(spi_nand, page_id,
517 retval = spinand_program_execute(spi_nand, page_id);
521 retval = spinand_read_status(spi_nand, &status);
523 dev_err(&spi_nand->dev,
524 "error %d reading status register\n", retval);
528 if ((status & STATUS_OIP_MASK) == STATUS_READY) {
529 if ((status & STATUS_P_FAIL_MASK) == STATUS_P_FAIL) {
530 dev_err(&spi_nand->dev,
531 "program error, page %d\n", page_id);
537 #ifdef CONFIG_MTD_SPINAND_ONDIEECC
539 retval = spinand_disable_ecc(spi_nand);
541 dev_err(&spi_nand->dev, "disable ecc failed!!\n");
552 * spinand_erase_block_erase - erase a page
553 * @block_id: the physical block location to erase.
556 * The command used here is 0xd8--indicating an erase command to erase
557 * one block--64 pages
558 * Need to wait for tERS.
560 static int spinand_erase_block_erase(struct spi_device *spi_nand, u16 block_id)
562 struct spinand_cmd cmd = {0};
566 cmd.cmd = CMD_ERASE_BLK;
568 cmd.addr[1] = (u8)((row & 0xff00) >> 8);
569 cmd.addr[2] = (u8)(row & 0x00ff);
571 return spinand_cmd(spi_nand, &cmd);
575 * spinand_erase_block - erase a page
576 * @block_id: the physical block location to erase.
579 * The commands used here are 0x06 and 0xd8--indicating an erase
580 * command to erase one block--64 pages
581 * It will first to enable the write enable bit (0x06 command),
582 * and then send the 0xd8 erase command
583 * Poll to wait for the tERS time to complete the tranaction.
585 static int spinand_erase_block(struct spi_device *spi_nand, u16 block_id)
590 retval = spinand_write_enable(spi_nand);
591 if (wait_till_ready(spi_nand))
592 dev_err(&spi_nand->dev, "wait timedout!!!\n");
594 retval = spinand_erase_block_erase(spi_nand, block_id);
596 retval = spinand_read_status(spi_nand, &status);
598 dev_err(&spi_nand->dev,
599 "error %d reading status register\n", retval);
603 if ((status & STATUS_OIP_MASK) == STATUS_READY) {
604 if ((status & STATUS_E_FAIL_MASK) == STATUS_E_FAIL) {
605 dev_err(&spi_nand->dev,
606 "erase error, block %d\n", block_id);
615 #ifdef CONFIG_MTD_SPINAND_ONDIEECC
616 static int spinand_write_page_hwecc(struct mtd_info *mtd,
617 struct nand_chip *chip,
618 const u8 *buf, int oob_required,
622 int eccsize = chip->ecc.size;
623 int eccsteps = chip->ecc.steps;
626 chip->write_buf(mtd, p, eccsize * eccsteps);
630 static int spinand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
631 u8 *buf, int oob_required, int page)
636 int eccsize = chip->ecc.size;
637 int eccsteps = chip->ecc.steps;
638 struct spinand_info *info = nand_get_controller_data(chip);
640 enable_read_hw_ecc = 1;
642 chip->read_buf(mtd, p, eccsize * eccsteps);
644 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
647 retval = spinand_read_status(info->spi, &status);
650 "error %d reading status register\n", retval);
654 if ((status & STATUS_OIP_MASK) == STATUS_READY) {
655 if ((status & STATUS_ECC_MASK) == STATUS_ECC_ERROR) {
656 pr_info("spinand: ECC error\n");
657 mtd->ecc_stats.failed++;
658 } else if ((status & STATUS_ECC_MASK) ==
659 STATUS_ECC_1BIT_CORRECTED)
660 mtd->ecc_stats.corrected++;
668 static void spinand_select_chip(struct mtd_info *mtd, int dev)
672 static u8 spinand_read_byte(struct mtd_info *mtd)
674 struct spinand_state *state = mtd_to_state(mtd);
677 data = state->buf[state->buf_ptr];
682 static int spinand_wait(struct mtd_info *mtd, struct nand_chip *chip)
684 struct spinand_info *info = nand_get_controller_data(chip);
686 unsigned long timeo = jiffies;
687 int retval, state = chip->state;
690 if (state == FL_ERASING)
691 timeo += (HZ * 400) / 1000;
693 timeo += (HZ * 20) / 1000;
695 while (time_before(jiffies, timeo)) {
696 retval = spinand_read_status(info->spi, &status);
699 "error %d reading status register\n", retval);
703 if ((status & STATUS_OIP_MASK) == STATUS_READY)
711 static void spinand_write_buf(struct mtd_info *mtd, const u8 *buf, int len)
713 struct spinand_state *state = mtd_to_state(mtd);
715 memcpy(state->buf + state->buf_ptr, buf, len);
716 state->buf_ptr += len;
719 static void spinand_read_buf(struct mtd_info *mtd, u8 *buf, int len)
721 struct spinand_state *state = mtd_to_state(mtd);
723 memcpy(buf, state->buf + state->buf_ptr, len);
724 state->buf_ptr += len;
728 * spinand_reset- send RESET command "0xff" to the Nand device.
730 static void spinand_reset(struct spi_device *spi_nand)
732 struct spinand_cmd cmd = {0};
736 if (spinand_cmd(spi_nand, &cmd) < 0)
737 pr_info("spinand reset failed!\n");
739 /* elapse 1ms before issuing any other command */
740 usleep_range(1000, 2000);
742 if (wait_till_ready(spi_nand))
743 dev_err(&spi_nand->dev, "wait timedout!\n");
746 static void spinand_cmdfunc(struct mtd_info *mtd, unsigned int command,
747 int column, int page)
749 struct nand_chip *chip = mtd_to_nand(mtd);
750 struct spinand_info *info = nand_get_controller_data(chip);
751 struct spinand_state *state = info->priv;
755 * READ0 - read in first 0x800 bytes
760 spinand_read_page(info->spi, page, 0x0, 0x840, state->buf);
762 /* READOOB reads only the OOB because no ECC is performed. */
763 case NAND_CMD_READOOB:
765 spinand_read_page(info->spi, page, 0x800, 0x40, state->buf);
767 case NAND_CMD_RNDOUT:
768 state->buf_ptr = column;
770 case NAND_CMD_READID:
772 spinand_read_id(info->spi, state->buf);
777 /* ERASE1 stores the block and page address */
778 case NAND_CMD_ERASE1:
779 spinand_erase_block(info->spi, page);
781 /* ERASE2 uses the block and page address from ERASE1 */
782 case NAND_CMD_ERASE2:
784 /* SEQIN sets up the addr buffer and all registers except the length */
790 /* PAGEPROG reuses all of the setup from SEQIN and adds the length */
791 case NAND_CMD_PAGEPROG:
792 spinand_program_page(info->spi, state->row, state->col,
793 state->buf_ptr, state->buf);
795 case NAND_CMD_STATUS:
796 spinand_get_otp(info->spi, state->buf);
797 if (!(state->buf[0] & 0x80))
798 state->buf[0] = 0x80;
803 if (wait_till_ready(info->spi))
804 dev_err(&info->spi->dev, "WAIT timedout!!!\n");
805 /* a minimum of 250us must elapse before issuing RESET cmd*/
806 usleep_range(250, 1000);
807 spinand_reset(info->spi);
810 dev_err(&mtd->dev, "Unknown CMD: 0x%x\n", command);
815 * spinand_lock_block - send write register 0x1f command to the Nand device
818 * After power up, all the Nand blocks are locked. This function allows
819 * one to unlock the blocks, and so it can be written or erased.
821 static int spinand_lock_block(struct spi_device *spi_nand, u8 lock)
823 struct spinand_cmd cmd = {0};
827 ret = spinand_get_otp(spi_nand, &otp);
829 cmd.cmd = CMD_WRITE_REG;
831 cmd.addr[0] = REG_BLOCK_LOCK;
835 ret = spinand_cmd(spi_nand, &cmd);
837 dev_err(&spi_nand->dev, "error %d lock block\n", ret);
843 * spinand_probe - [spinand Interface]
844 * @spi_nand: registered device driver.
847 * Set up the device driver parameters to make the device available.
849 static int spinand_probe(struct spi_device *spi_nand)
851 struct mtd_info *mtd;
852 struct nand_chip *chip;
853 struct spinand_info *info;
854 struct spinand_state *state;
856 info = devm_kzalloc(&spi_nand->dev, sizeof(struct spinand_info),
861 info->spi = spi_nand;
863 spinand_lock_block(spi_nand, BL_ALL_UNLOCKED);
865 state = devm_kzalloc(&spi_nand->dev, sizeof(struct spinand_state),
872 state->buf = devm_kzalloc(&spi_nand->dev, BUFSIZE, GFP_KERNEL);
876 chip = devm_kzalloc(&spi_nand->dev, sizeof(struct nand_chip),
881 #ifdef CONFIG_MTD_SPINAND_ONDIEECC
882 chip->ecc.mode = NAND_ECC_HW;
883 chip->ecc.size = 0x200;
884 chip->ecc.bytes = 0x6;
885 chip->ecc.steps = 0x4;
887 chip->ecc.strength = 1;
888 chip->ecc.total = chip->ecc.steps * chip->ecc.bytes;
889 chip->ecc.layout = &spinand_oob_64;
890 chip->ecc.read_page = spinand_read_page_hwecc;
891 chip->ecc.write_page = spinand_write_page_hwecc;
893 chip->ecc.mode = NAND_ECC_SOFT;
894 if (spinand_disable_ecc(spi_nand) < 0)
895 dev_info(&spi_nand->dev, "%s: disable ecc failed!\n",
899 nand_set_flash_node(chip, spi_nand->dev.of_node);
900 nand_set_controller_data(chip, info);
901 chip->read_buf = spinand_read_buf;
902 chip->write_buf = spinand_write_buf;
903 chip->read_byte = spinand_read_byte;
904 chip->cmdfunc = spinand_cmdfunc;
905 chip->waitfunc = spinand_wait;
906 chip->options |= NAND_CACHEPRG;
907 chip->select_chip = spinand_select_chip;
909 mtd = nand_to_mtd(chip);
911 dev_set_drvdata(&spi_nand->dev, mtd);
913 mtd->dev.parent = &spi_nand->dev;
916 if (nand_scan(mtd, 1))
919 return mtd_device_register(mtd, NULL, 0);
923 * spinand_remove - remove the device driver
924 * @spi: the spi device.
927 * Remove the device driver parameters and free up allocated memories.
929 static int spinand_remove(struct spi_device *spi)
931 mtd_device_unregister(dev_get_drvdata(&spi->dev));
936 static const struct of_device_id spinand_dt[] = {
937 { .compatible = "spinand,mt29f", },
940 MODULE_DEVICE_TABLE(of, spinand_dt);
943 * Device name structure description
945 static struct spi_driver spinand_driver = {
948 .of_match_table = spinand_dt,
950 .probe = spinand_probe,
951 .remove = spinand_remove,
954 module_spi_driver(spinand_driver);
956 MODULE_DESCRIPTION("SPI NAND driver for Micron");
957 MODULE_AUTHOR("Henry Pan <hspan@micron.com>, Kamlakant Patel <kamlakant.patel@broadcom.com>");
958 MODULE_LICENSE("GPL v2");