Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1 | /* |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2 | * Overview: |
| 3 | * This is the generic MTD driver for NAND flash devices. It should be |
| 4 | * capable of working with almost all NAND chips currently available. |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 5 | * |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 6 | * Additional technical information is available on |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 7 | * http://www.linux-mtd.infradead.org/doc/nand.html |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 8 | * |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 9 | * Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com) |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 10 | * 2002-2006 Thomas Gleixner (tglx@linutronix.de) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 11 | * |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 12 | * Credits: |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 13 | * David Woodhouse for adding multichip support |
| 14 | * |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 15 | * Aleph One Ltd. and Toby Churchill Ltd. for supporting the |
| 16 | * rework for 2K page size chips |
| 17 | * |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 18 | * TODO: |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 19 | * Enable cached programming for 2k page size chips |
| 20 | * Check, if mtd->ecctype should be set to MTD_ECC_HW |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 21 | * if we have HW ECC support. |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 22 | * BBT table is not serialized, has to be fixed |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 23 | * |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 24 | * This program is free software; you can redistribute it and/or modify |
| 25 | * it under the terms of the GNU General Public License version 2 as |
| 26 | * published by the Free Software Foundation. |
| 27 | * |
| 28 | */ |
| 29 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 30 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 31 | #include <common.h> |
Brian Norris | ba6463d | 2016-06-15 21:09:22 +0200 | [diff] [blame] | 32 | #if CONFIG_IS_ENABLED(OF_CONTROL) |
| 33 | #include <fdtdec.h> |
| 34 | #endif |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 35 | #include <log.h> |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 36 | #include <malloc.h> |
| 37 | #include <watchdog.h> |
Simon Glass | d66c5f7 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 38 | #include <dm/devres.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 39 | #include <linux/bitops.h> |
Simon Glass | c06c1be | 2020-05-10 11:40:08 -0600 | [diff] [blame] | 40 | #include <linux/bug.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 41 | #include <linux/delay.h> |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 42 | #include <linux/err.h> |
Mike Frysinger | 11d1a09 | 2012-04-09 13:39:55 +0000 | [diff] [blame] | 43 | #include <linux/compat.h> |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 44 | #include <linux/mtd/mtd.h> |
Masahiro Yamada | 2b7a873 | 2017-11-30 13:45:24 +0900 | [diff] [blame] | 45 | #include <linux/mtd/rawnand.h> |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 46 | #include <linux/mtd/nand_ecc.h> |
Christian Hitz | 55f7bca | 2011-10-12 09:31:59 +0200 | [diff] [blame] | 47 | #include <linux/mtd/nand_bch.h> |
Stefan Roese | fa252ea | 2009-04-24 15:58:33 +0200 | [diff] [blame] | 48 | #ifdef CONFIG_MTD_PARTITIONS |
| 49 | #include <linux/mtd/partitions.h> |
| 50 | #endif |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 51 | #include <asm/io.h> |
Masahiro Yamada | 56a931c | 2016-09-21 11:28:55 +0900 | [diff] [blame] | 52 | #include <linux/errno.h> |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 53 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 54 | /* Define default oob placement schemes for large and small page devices */ |
Gregory CLEMENT | e5b9631 | 2019-04-17 11:22:05 +0200 | [diff] [blame] | 55 | #ifndef CONFIG_SYS_NAND_DRIVER_ECC_LAYOUT |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 56 | static struct nand_ecclayout nand_oob_8 = { |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 57 | .eccbytes = 3, |
| 58 | .eccpos = {0, 1, 2}, |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 59 | .oobfree = { |
| 60 | {.offset = 3, |
| 61 | .length = 2}, |
| 62 | {.offset = 6, |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 63 | .length = 2} } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 64 | }; |
| 65 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 66 | static struct nand_ecclayout nand_oob_16 = { |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 67 | .eccbytes = 6, |
| 68 | .eccpos = {0, 1, 2, 3, 6, 7}, |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 69 | .oobfree = { |
| 70 | {.offset = 8, |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 71 | . length = 8} } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 72 | }; |
| 73 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 74 | static struct nand_ecclayout nand_oob_64 = { |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 75 | .eccbytes = 24, |
| 76 | .eccpos = { |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 77 | 40, 41, 42, 43, 44, 45, 46, 47, |
| 78 | 48, 49, 50, 51, 52, 53, 54, 55, |
| 79 | 56, 57, 58, 59, 60, 61, 62, 63}, |
| 80 | .oobfree = { |
| 81 | {.offset = 2, |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 82 | .length = 38} } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 83 | }; |
| 84 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 85 | static struct nand_ecclayout nand_oob_128 = { |
Sergei Poselenov | 04fbaa0 | 2008-06-06 15:42:43 +0200 | [diff] [blame] | 86 | .eccbytes = 48, |
| 87 | .eccpos = { |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 88 | 80, 81, 82, 83, 84, 85, 86, 87, |
| 89 | 88, 89, 90, 91, 92, 93, 94, 95, |
| 90 | 96, 97, 98, 99, 100, 101, 102, 103, |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 91 | 104, 105, 106, 107, 108, 109, 110, 111, |
| 92 | 112, 113, 114, 115, 116, 117, 118, 119, |
| 93 | 120, 121, 122, 123, 124, 125, 126, 127}, |
| 94 | .oobfree = { |
| 95 | {.offset = 2, |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 96 | .length = 78} } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 97 | }; |
Stefan Agner | bd18614 | 2018-12-06 14:57:09 +0100 | [diff] [blame] | 98 | #endif |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 99 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 100 | static int nand_get_device(struct mtd_info *mtd, int new_state); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 101 | |
| 102 | static int nand_do_write_oob(struct mtd_info *mtd, loff_t to, |
| 103 | struct mtd_oob_ops *ops); |
| 104 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 105 | /* |
| 106 | * For devices which display every fart in the system on a separate LED. Is |
| 107 | * compiled away when LED support is disabled. |
| 108 | */ |
| 109 | DEFINE_LED_TRIGGER(nand_led_trigger); |
Sergei Poselenov | 04fbaa0 | 2008-06-06 15:42:43 +0200 | [diff] [blame] | 110 | |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 111 | static int check_offs_len(struct mtd_info *mtd, |
| 112 | loff_t ofs, uint64_t len) |
| 113 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 114 | struct nand_chip *chip = mtd_to_nand(mtd); |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 115 | int ret = 0; |
| 116 | |
| 117 | /* Start address must align on block boundary */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 118 | if (ofs & ((1ULL << chip->phys_erase_shift) - 1)) { |
| 119 | pr_debug("%s: unaligned address\n", __func__); |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 120 | ret = -EINVAL; |
| 121 | } |
| 122 | |
| 123 | /* Length must align on block boundary */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 124 | if (len & ((1ULL << chip->phys_erase_shift) - 1)) { |
| 125 | pr_debug("%s: length not block aligned\n", __func__); |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 126 | ret = -EINVAL; |
| 127 | } |
| 128 | |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 129 | return ret; |
| 130 | } |
| 131 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 132 | /** |
| 133 | * nand_release_device - [GENERIC] release chip |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 134 | * @mtd: MTD device structure |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 135 | * |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 136 | * Release chip lock and wake up anyone waiting on the device. |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 137 | */ |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 138 | static void nand_release_device(struct mtd_info *mtd) |
Wolfgang Denk | 9a08dd1 | 2005-11-02 14:29:12 +0100 | [diff] [blame] | 139 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 140 | struct nand_chip *chip = mtd_to_nand(mtd); |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 141 | |
| 142 | /* De-select the NAND device */ |
| 143 | chip->select_chip(mtd, -1); |
Wolfgang Denk | 9a08dd1 | 2005-11-02 14:29:12 +0100 | [diff] [blame] | 144 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 145 | |
| 146 | /** |
| 147 | * nand_read_byte - [DEFAULT] read one byte from the chip |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 148 | * @mtd: MTD device structure |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 149 | * |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 150 | * Default read function for 8bit buswidth |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 151 | */ |
Simon Schwarz | 5a9fc19 | 2011-10-31 06:34:44 +0000 | [diff] [blame] | 152 | uint8_t nand_read_byte(struct mtd_info *mtd) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 153 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 154 | struct nand_chip *chip = mtd_to_nand(mtd); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 155 | return readb(chip->IO_ADDR_R); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | /** |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 159 | * nand_read_byte16 - [DEFAULT] read one byte endianness aware from the chip |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 160 | * @mtd: MTD device structure |
| 161 | * |
| 162 | * Default read function for 16bit buswidth with endianness conversion. |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 163 | * |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 164 | */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 165 | static uint8_t nand_read_byte16(struct mtd_info *mtd) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 166 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 167 | struct nand_chip *chip = mtd_to_nand(mtd); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 168 | return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R)); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | /** |
| 172 | * nand_read_word - [DEFAULT] read one word from the chip |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 173 | * @mtd: MTD device structure |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 174 | * |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 175 | * Default read function for 16bit buswidth without endianness conversion. |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 176 | */ |
| 177 | static u16 nand_read_word(struct mtd_info *mtd) |
| 178 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 179 | struct nand_chip *chip = mtd_to_nand(mtd); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 180 | return readw(chip->IO_ADDR_R); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | /** |
| 184 | * nand_select_chip - [DEFAULT] control CE line |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 185 | * @mtd: MTD device structure |
| 186 | * @chipnr: chipnumber to select, -1 for deselect |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 187 | * |
| 188 | * Default select function for 1 chip devices. |
| 189 | */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 190 | static void nand_select_chip(struct mtd_info *mtd, int chipnr) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 191 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 192 | struct nand_chip *chip = mtd_to_nand(mtd); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 193 | |
| 194 | switch (chipnr) { |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 195 | case -1: |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 196 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 197 | break; |
| 198 | case 0: |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 199 | break; |
| 200 | |
| 201 | default: |
| 202 | BUG(); |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | /** |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 207 | * nand_write_byte - [DEFAULT] write single byte to chip |
| 208 | * @mtd: MTD device structure |
| 209 | * @byte: value to write |
| 210 | * |
| 211 | * Default function to write a byte to I/O[7:0] |
| 212 | */ |
| 213 | static void nand_write_byte(struct mtd_info *mtd, uint8_t byte) |
| 214 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 215 | struct nand_chip *chip = mtd_to_nand(mtd); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 216 | |
| 217 | chip->write_buf(mtd, &byte, 1); |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * nand_write_byte16 - [DEFAULT] write single byte to a chip with width 16 |
| 222 | * @mtd: MTD device structure |
| 223 | * @byte: value to write |
| 224 | * |
| 225 | * Default function to write a byte to I/O[7:0] on a 16-bit wide chip. |
| 226 | */ |
| 227 | static void nand_write_byte16(struct mtd_info *mtd, uint8_t byte) |
| 228 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 229 | struct nand_chip *chip = mtd_to_nand(mtd); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 230 | uint16_t word = byte; |
| 231 | |
| 232 | /* |
| 233 | * It's not entirely clear what should happen to I/O[15:8] when writing |
| 234 | * a byte. The ONFi spec (Revision 3.1; 2012-09-19, Section 2.16) reads: |
| 235 | * |
| 236 | * When the host supports a 16-bit bus width, only data is |
| 237 | * transferred at the 16-bit width. All address and command line |
| 238 | * transfers shall use only the lower 8-bits of the data bus. During |
| 239 | * command transfers, the host may place any value on the upper |
| 240 | * 8-bits of the data bus. During address transfers, the host shall |
| 241 | * set the upper 8-bits of the data bus to 00h. |
| 242 | * |
| 243 | * One user of the write_byte callback is nand_onfi_set_features. The |
| 244 | * four parameters are specified to be written to I/O[7:0], but this is |
| 245 | * neither an address nor a command transfer. Let's assume a 0 on the |
| 246 | * upper I/O lines is OK. |
| 247 | */ |
| 248 | chip->write_buf(mtd, (uint8_t *)&word, 2); |
| 249 | } |
| 250 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 251 | static void iowrite8_rep(void *addr, const uint8_t *buf, int len) |
| 252 | { |
| 253 | int i; |
| 254 | |
| 255 | for (i = 0; i < len; i++) |
| 256 | writeb(buf[i], addr); |
| 257 | } |
| 258 | static void ioread8_rep(void *addr, uint8_t *buf, int len) |
| 259 | { |
| 260 | int i; |
| 261 | |
| 262 | for (i = 0; i < len; i++) |
| 263 | buf[i] = readb(addr); |
| 264 | } |
| 265 | |
| 266 | static void ioread16_rep(void *addr, void *buf, int len) |
| 267 | { |
| 268 | int i; |
| 269 | u16 *p = (u16 *) buf; |
Stefan Roese | a9e9954 | 2014-09-05 09:57:01 +0200 | [diff] [blame] | 270 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 271 | for (i = 0; i < len; i++) |
| 272 | p[i] = readw(addr); |
| 273 | } |
| 274 | |
| 275 | static void iowrite16_rep(void *addr, void *buf, int len) |
| 276 | { |
| 277 | int i; |
| 278 | u16 *p = (u16 *) buf; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 279 | |
| 280 | for (i = 0; i < len; i++) |
| 281 | writew(p[i], addr); |
| 282 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 283 | |
| 284 | /** |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 285 | * nand_write_buf - [DEFAULT] write buffer to chip |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 286 | * @mtd: MTD device structure |
| 287 | * @buf: data buffer |
| 288 | * @len: number of bytes to write |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 289 | * |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 290 | * Default write function for 8bit buswidth. |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 291 | */ |
Simon Schwarz | 5a9fc19 | 2011-10-31 06:34:44 +0000 | [diff] [blame] | 292 | void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 293 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 294 | struct nand_chip *chip = mtd_to_nand(mtd); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 295 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 296 | iowrite8_rep(chip->IO_ADDR_W, buf, len); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | /** |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 300 | * nand_read_buf - [DEFAULT] read chip data into buffer |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 301 | * @mtd: MTD device structure |
| 302 | * @buf: buffer to store date |
| 303 | * @len: number of bytes to read |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 304 | * |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 305 | * Default read function for 8bit buswidth. |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 306 | */ |
Simon Schwarz | 4f62e98 | 2011-09-14 15:30:16 -0400 | [diff] [blame] | 307 | void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 308 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 309 | struct nand_chip *chip = mtd_to_nand(mtd); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 310 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 311 | ioread8_rep(chip->IO_ADDR_R, buf, len); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 312 | } |
| 313 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 314 | /** |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 315 | * nand_write_buf16 - [DEFAULT] write buffer to chip |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 316 | * @mtd: MTD device structure |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 317 | * @buf: data buffer |
| 318 | * @len: number of bytes to write |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 319 | * |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 320 | * Default write function for 16bit buswidth. |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 321 | */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 322 | void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 323 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 324 | struct nand_chip *chip = mtd_to_nand(mtd); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 325 | u16 *p = (u16 *) buf; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 326 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 327 | iowrite16_rep(chip->IO_ADDR_W, p, len >> 1); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | /** |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 331 | * nand_read_buf16 - [DEFAULT] read chip data into buffer |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 332 | * @mtd: MTD device structure |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 333 | * @buf: buffer to store date |
| 334 | * @len: number of bytes to read |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 335 | * |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 336 | * Default read function for 16bit buswidth. |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 337 | */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 338 | void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 339 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 340 | struct nand_chip *chip = mtd_to_nand(mtd); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 341 | u16 *p = (u16 *) buf; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 342 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 343 | ioread16_rep(chip->IO_ADDR_R, p, len >> 1); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | /** |
| 347 | * nand_block_bad - [DEFAULT] Read bad block marker from the chip |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 348 | * @mtd: MTD device structure |
| 349 | * @ofs: offset from device start |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 350 | * |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 351 | * Check, if the block is bad. |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 352 | */ |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 353 | static int nand_block_bad(struct mtd_info *mtd, loff_t ofs) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 354 | { |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 355 | int page, res = 0, i = 0; |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 356 | struct nand_chip *chip = mtd_to_nand(mtd); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 357 | u16 bad; |
| 358 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 359 | if (chip->bbt_options & NAND_BBT_SCANLASTPAGE) |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 360 | ofs += mtd->erasesize - mtd->writesize; |
| 361 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 362 | page = (int)(ofs >> chip->page_shift) & chip->pagemask; |
Thomas Knobloch | 9e2aeaf | 2007-05-05 07:04:42 +0200 | [diff] [blame] | 363 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 364 | do { |
| 365 | if (chip->options & NAND_BUSWIDTH_16) { |
| 366 | chip->cmdfunc(mtd, NAND_CMD_READOOB, |
| 367 | chip->badblockpos & 0xFE, page); |
| 368 | bad = cpu_to_le16(chip->read_word(mtd)); |
| 369 | if (chip->badblockpos & 0x1) |
| 370 | bad >>= 8; |
| 371 | else |
| 372 | bad &= 0xFF; |
| 373 | } else { |
| 374 | chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos, |
| 375 | page); |
| 376 | bad = chip->read_byte(mtd); |
| 377 | } |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 378 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 379 | if (likely(chip->badblockbits == 8)) |
| 380 | res = bad != 0xFF; |
| 381 | else |
| 382 | res = hweight8(bad) < chip->badblockbits; |
| 383 | ofs += mtd->writesize; |
| 384 | page = (int)(ofs >> chip->page_shift) & chip->pagemask; |
| 385 | i++; |
| 386 | } while (!res && i < 2 && (chip->bbt_options & NAND_BBT_SCAN2NDPAGE)); |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 387 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 388 | return res; |
| 389 | } |
| 390 | |
| 391 | /** |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 392 | * nand_default_block_markbad - [DEFAULT] mark a block bad via bad block marker |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 393 | * @mtd: MTD device structure |
| 394 | * @ofs: offset from device start |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 395 | * |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 396 | * This is the default implementation, which can be overridden by a hardware |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 397 | * specific driver. It provides the details for writing a bad block marker to a |
| 398 | * block. |
| 399 | */ |
| 400 | static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs) |
| 401 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 402 | struct nand_chip *chip = mtd_to_nand(mtd); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 403 | struct mtd_oob_ops ops; |
| 404 | uint8_t buf[2] = { 0, 0 }; |
| 405 | int ret = 0, res, i = 0; |
| 406 | |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 407 | memset(&ops, 0, sizeof(ops)); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 408 | ops.oobbuf = buf; |
| 409 | ops.ooboffs = chip->badblockpos; |
| 410 | if (chip->options & NAND_BUSWIDTH_16) { |
| 411 | ops.ooboffs &= ~0x01; |
| 412 | ops.len = ops.ooblen = 2; |
| 413 | } else { |
| 414 | ops.len = ops.ooblen = 1; |
| 415 | } |
| 416 | ops.mode = MTD_OPS_PLACE_OOB; |
| 417 | |
| 418 | /* Write to first/last page(s) if necessary */ |
| 419 | if (chip->bbt_options & NAND_BBT_SCANLASTPAGE) |
| 420 | ofs += mtd->erasesize - mtd->writesize; |
| 421 | do { |
| 422 | res = nand_do_write_oob(mtd, ofs, &ops); |
| 423 | if (!ret) |
| 424 | ret = res; |
| 425 | |
| 426 | i++; |
| 427 | ofs += mtd->writesize; |
| 428 | } while ((chip->bbt_options & NAND_BBT_SCAN2NDPAGE) && i < 2); |
| 429 | |
| 430 | return ret; |
| 431 | } |
| 432 | |
| 433 | /** |
| 434 | * nand_block_markbad_lowlevel - mark a block bad |
| 435 | * @mtd: MTD device structure |
| 436 | * @ofs: offset from device start |
| 437 | * |
| 438 | * This function performs the generic NAND bad block marking steps (i.e., bad |
| 439 | * block table(s) and/or marker(s)). We only allow the hardware driver to |
| 440 | * specify how to write bad block markers to OOB (chip->block_markbad). |
| 441 | * |
| 442 | * We try operations in the following order: |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 443 | * (1) erase the affected block, to allow OOB marker to be written cleanly |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 444 | * (2) write bad block marker to OOB area of affected block (unless flag |
| 445 | * NAND_BBT_NO_OOB_BBM is present) |
| 446 | * (3) update the BBT |
| 447 | * Note that we retain the first error encountered in (2) or (3), finish the |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 448 | * procedures, and dump the error in the end. |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 449 | */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 450 | static int nand_block_markbad_lowlevel(struct mtd_info *mtd, loff_t ofs) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 451 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 452 | struct nand_chip *chip = mtd_to_nand(mtd); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 453 | int res, ret = 0; |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 454 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 455 | if (!(chip->bbt_options & NAND_BBT_NO_OOB_BBM)) { |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 456 | struct erase_info einfo; |
| 457 | |
| 458 | /* Attempt erase before marking OOB */ |
| 459 | memset(&einfo, 0, sizeof(einfo)); |
| 460 | einfo.mtd = mtd; |
| 461 | einfo.addr = ofs; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 462 | einfo.len = 1ULL << chip->phys_erase_shift; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 463 | nand_erase_nand(mtd, &einfo, 0); |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 464 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 465 | /* Write bad block marker to OOB */ |
| 466 | nand_get_device(mtd, FL_WRITING); |
| 467 | ret = chip->block_markbad(mtd, ofs); |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 468 | nand_release_device(mtd); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 469 | } |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 470 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 471 | /* Mark block bad in BBT */ |
| 472 | if (chip->bbt) { |
| 473 | res = nand_markbad_bbt(mtd, ofs); |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 474 | if (!ret) |
| 475 | ret = res; |
| 476 | } |
| 477 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 478 | if (!ret) |
| 479 | mtd->ecc_stats.badblocks++; |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 480 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 481 | return ret; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 482 | } |
| 483 | |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 484 | /** |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 485 | * nand_check_wp - [GENERIC] check if the chip is write protected |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 486 | * @mtd: MTD device structure |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 487 | * |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 488 | * Check, if the device is write protected. The function expects, that the |
| 489 | * device is already selected. |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 490 | */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 491 | static int nand_check_wp(struct mtd_info *mtd) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 492 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 493 | struct nand_chip *chip = mtd_to_nand(mtd); |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 494 | u8 status; |
| 495 | int ret; |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 496 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 497 | /* Broken xD cards report WP despite being writable */ |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 498 | if (chip->options & NAND_BROKEN_XD) |
| 499 | return 0; |
| 500 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 501 | /* Check the WP bit */ |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 502 | ret = nand_status_op(chip, &status); |
| 503 | if (ret) |
| 504 | return ret; |
| 505 | |
| 506 | return status & NAND_STATUS_WP ? 0 : 1; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 507 | } |
Markus Klotzbücher | 27eba14 | 2006-03-06 15:04:25 +0100 | [diff] [blame] | 508 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 509 | /** |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 510 | * nand_block_isreserved - [GENERIC] Check if a block is marked reserved. |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 511 | * @mtd: MTD device structure |
| 512 | * @ofs: offset from device start |
Ezequiel Garcia | fc9d57c | 2014-05-21 19:06:12 -0300 | [diff] [blame] | 513 | * |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 514 | * Check if the block is marked as reserved. |
Ezequiel Garcia | fc9d57c | 2014-05-21 19:06:12 -0300 | [diff] [blame] | 515 | */ |
| 516 | static int nand_block_isreserved(struct mtd_info *mtd, loff_t ofs) |
| 517 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 518 | struct nand_chip *chip = mtd_to_nand(mtd); |
Ezequiel Garcia | fc9d57c | 2014-05-21 19:06:12 -0300 | [diff] [blame] | 519 | |
| 520 | if (!chip->bbt) |
| 521 | return 0; |
| 522 | /* Return info from the table */ |
| 523 | return nand_isreserved_bbt(mtd, ofs); |
| 524 | } |
| 525 | |
| 526 | /** |
| 527 | * nand_block_checkbad - [GENERIC] Check if a block is marked bad |
| 528 | * @mtd: MTD device structure |
| 529 | * @ofs: offset from device start |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 530 | * @allowbbt: 1, if its allowed to access the bbt area |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 531 | * |
| 532 | * Check, if the block is bad. Either by reading the bad block table or |
| 533 | * calling of the scan function. |
| 534 | */ |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 535 | static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int allowbbt) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 536 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 537 | struct nand_chip *chip = mtd_to_nand(mtd); |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 538 | |
Masahiro Yamada | 8d10054 | 2014-12-26 22:20:58 +0900 | [diff] [blame] | 539 | if (!(chip->options & NAND_SKIP_BBTSCAN) && |
| 540 | !(chip->options & NAND_BBT_SCANNED)) { |
Rostislav Lisovy | dc17bdc | 2014-10-22 13:40:44 +0200 | [diff] [blame] | 541 | chip->options |= NAND_BBT_SCANNED; |
Masahiro Yamada | 8c6c14a | 2014-12-26 22:20:57 +0900 | [diff] [blame] | 542 | chip->scan_bbt(mtd); |
Rostislav Lisovy | dc17bdc | 2014-10-22 13:40:44 +0200 | [diff] [blame] | 543 | } |
| 544 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 545 | if (!chip->bbt) |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 546 | return chip->block_bad(mtd, ofs); |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 547 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 548 | /* Return info from the table */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 549 | return nand_isbad_bbt(mtd, ofs, allowbbt); |
| 550 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 551 | |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 552 | /** |
| 553 | * nand_wait_ready - [GENERIC] Wait for the ready pin after commands. |
| 554 | * @mtd: MTD device structure |
| 555 | * |
| 556 | * Wait for the ready pin after a command, and warn if a timeout occurs. |
| 557 | */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 558 | void nand_wait_ready(struct mtd_info *mtd) |
| 559 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 560 | struct nand_chip *chip = mtd_to_nand(mtd); |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 561 | u32 timeo = (CONFIG_SYS_HZ * 400) / 1000; |
Reinhard Meyer | 4d1fb18 | 2010-11-18 03:14:26 +0000 | [diff] [blame] | 562 | u32 time_start; |
Stefan Roese | a5c312c | 2008-01-05 16:43:25 +0100 | [diff] [blame] | 563 | |
Reinhard Meyer | 4d1fb18 | 2010-11-18 03:14:26 +0000 | [diff] [blame] | 564 | time_start = get_timer(0); |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 565 | /* Wait until command is processed or timeout occurs */ |
Reinhard Meyer | 4d1fb18 | 2010-11-18 03:14:26 +0000 | [diff] [blame] | 566 | while (get_timer(time_start) < timeo) { |
Stefan Roese | a5c312c | 2008-01-05 16:43:25 +0100 | [diff] [blame] | 567 | if (chip->dev_ready) |
| 568 | if (chip->dev_ready(mtd)) |
| 569 | break; |
| 570 | } |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 571 | |
| 572 | if (!chip->dev_ready(mtd)) |
| 573 | pr_warn("timeout while waiting for chip to become ready\n"); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 574 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 575 | EXPORT_SYMBOL_GPL(nand_wait_ready); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 576 | |
| 577 | /** |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 578 | * nand_wait_status_ready - [GENERIC] Wait for the ready status after commands. |
| 579 | * @mtd: MTD device structure |
| 580 | * @timeo: Timeout in ms |
| 581 | * |
| 582 | * Wait for status ready (i.e. command done) or timeout. |
| 583 | */ |
| 584 | static void nand_wait_status_ready(struct mtd_info *mtd, unsigned long timeo) |
| 585 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 586 | register struct nand_chip *chip = mtd_to_nand(mtd); |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 587 | u32 time_start; |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 588 | int ret; |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 589 | |
| 590 | timeo = (CONFIG_SYS_HZ * timeo) / 1000; |
| 591 | time_start = get_timer(0); |
| 592 | while (get_timer(time_start) < timeo) { |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 593 | u8 status; |
| 594 | |
| 595 | ret = nand_read_data_op(chip, &status, sizeof(status), true); |
| 596 | if (ret) |
| 597 | return; |
| 598 | |
| 599 | if (status & NAND_STATUS_READY) |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 600 | break; |
| 601 | WATCHDOG_RESET(); |
| 602 | } |
| 603 | }; |
| 604 | |
| 605 | /** |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 606 | * nand_command - [DEFAULT] Send command to NAND device |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 607 | * @mtd: MTD device structure |
| 608 | * @command: the command to be sent |
| 609 | * @column: the column address for this command, -1 if none |
| 610 | * @page_addr: the page address for this command, -1 if none |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 611 | * |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 612 | * Send command to NAND device. This function is used for small page devices |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 613 | * (512 Bytes per page). |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 614 | */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 615 | static void nand_command(struct mtd_info *mtd, unsigned int command, |
| 616 | int column, int page_addr) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 617 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 618 | register struct nand_chip *chip = mtd_to_nand(mtd); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 619 | int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 620 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 621 | /* Write out the command to the device */ |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 622 | if (command == NAND_CMD_SEQIN) { |
| 623 | int readcmd; |
| 624 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 625 | if (column >= mtd->writesize) { |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 626 | /* OOB area */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 627 | column -= mtd->writesize; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 628 | readcmd = NAND_CMD_READOOB; |
| 629 | } else if (column < 256) { |
| 630 | /* First 256 bytes --> READ0 */ |
| 631 | readcmd = NAND_CMD_READ0; |
| 632 | } else { |
| 633 | column -= 256; |
| 634 | readcmd = NAND_CMD_READ1; |
| 635 | } |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 636 | chip->cmd_ctrl(mtd, readcmd, ctrl); |
| 637 | ctrl &= ~NAND_CTRL_CHANGE; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 638 | } |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 639 | chip->cmd_ctrl(mtd, command, ctrl); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 640 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 641 | /* Address cycle, when necessary */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 642 | ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE; |
| 643 | /* Serially input address */ |
| 644 | if (column != -1) { |
| 645 | /* Adjust columns for 16 bit buswidth */ |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 646 | if (chip->options & NAND_BUSWIDTH_16 && |
Brian Norris | 6767522 | 2014-05-06 00:46:17 +0530 | [diff] [blame] | 647 | !nand_opcode_8bits(command)) |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 648 | column >>= 1; |
| 649 | chip->cmd_ctrl(mtd, column, ctrl); |
| 650 | ctrl &= ~NAND_CTRL_CHANGE; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 651 | } |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 652 | if (page_addr != -1) { |
| 653 | chip->cmd_ctrl(mtd, page_addr, ctrl); |
| 654 | ctrl &= ~NAND_CTRL_CHANGE; |
| 655 | chip->cmd_ctrl(mtd, page_addr >> 8, ctrl); |
Masahiro Yamada | 984926b | 2017-11-22 02:38:31 +0900 | [diff] [blame] | 656 | if (chip->options & NAND_ROW_ADDR_3) |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 657 | chip->cmd_ctrl(mtd, page_addr >> 16, ctrl); |
| 658 | } |
| 659 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE); |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 660 | |
| 661 | /* |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 662 | * Program and erase have their own busy handlers status and sequential |
| 663 | * in needs no delay |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 664 | */ |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 665 | switch (command) { |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 666 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 667 | case NAND_CMD_PAGEPROG: |
| 668 | case NAND_CMD_ERASE1: |
| 669 | case NAND_CMD_ERASE2: |
| 670 | case NAND_CMD_SEQIN: |
| 671 | case NAND_CMD_STATUS: |
Masahiro Yamada | 7f9baa1 | 2017-09-15 21:44:58 +0900 | [diff] [blame] | 672 | case NAND_CMD_READID: |
Masahiro Yamada | 0cd1018 | 2017-09-15 21:44:59 +0900 | [diff] [blame] | 673 | case NAND_CMD_SET_FEATURES: |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 674 | return; |
| 675 | |
| 676 | case NAND_CMD_RESET: |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 677 | if (chip->dev_ready) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 678 | break; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 679 | udelay(chip->chip_delay); |
| 680 | chip->cmd_ctrl(mtd, NAND_CMD_STATUS, |
| 681 | NAND_CTRL_CLE | NAND_CTRL_CHANGE); |
| 682 | chip->cmd_ctrl(mtd, |
| 683 | NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE); |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 684 | /* EZ-NAND can take upto 250ms as per ONFi v4.0 */ |
| 685 | nand_wait_status_ready(mtd, 250); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 686 | return; |
| 687 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 688 | /* This applies to read commands */ |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 689 | default: |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 690 | /* |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 691 | * If we don't have access to the busy pin, we apply the given |
| 692 | * command delay |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 693 | */ |
| 694 | if (!chip->dev_ready) { |
| 695 | udelay(chip->chip_delay); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 696 | return; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 697 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 698 | } |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 699 | /* |
| 700 | * Apply this short delay always to ensure that we do wait tWB in |
| 701 | * any case on any machine. |
| 702 | */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 703 | ndelay(100); |
| 704 | |
| 705 | nand_wait_ready(mtd); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | /** |
| 709 | * nand_command_lp - [DEFAULT] Send command to NAND large page device |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 710 | * @mtd: MTD device structure |
| 711 | * @command: the command to be sent |
| 712 | * @column: the column address for this command, -1 if none |
| 713 | * @page_addr: the page address for this command, -1 if none |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 714 | * |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 715 | * Send command to NAND device. This is the version for the new large page |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 716 | * devices. We don't have the separate regions as we have in the small page |
| 717 | * devices. We must emulate NAND_CMD_READOOB to keep the code compatible. |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 718 | */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 719 | static void nand_command_lp(struct mtd_info *mtd, unsigned int command, |
| 720 | int column, int page_addr) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 721 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 722 | register struct nand_chip *chip = mtd_to_nand(mtd); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 723 | |
| 724 | /* Emulate NAND_CMD_READOOB */ |
| 725 | if (command == NAND_CMD_READOOB) { |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 726 | column += mtd->writesize; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 727 | command = NAND_CMD_READ0; |
| 728 | } |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 729 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 730 | /* Command latch cycle */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 731 | chip->cmd_ctrl(mtd, command, NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 732 | |
| 733 | if (column != -1 || page_addr != -1) { |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 734 | int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 735 | |
| 736 | /* Serially input address */ |
| 737 | if (column != -1) { |
| 738 | /* Adjust columns for 16 bit buswidth */ |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 739 | if (chip->options & NAND_BUSWIDTH_16 && |
Brian Norris | 6767522 | 2014-05-06 00:46:17 +0530 | [diff] [blame] | 740 | !nand_opcode_8bits(command)) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 741 | column >>= 1; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 742 | chip->cmd_ctrl(mtd, column, ctrl); |
| 743 | ctrl &= ~NAND_CTRL_CHANGE; |
| 744 | chip->cmd_ctrl(mtd, column >> 8, ctrl); |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 745 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 746 | if (page_addr != -1) { |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 747 | chip->cmd_ctrl(mtd, page_addr, ctrl); |
| 748 | chip->cmd_ctrl(mtd, page_addr >> 8, |
| 749 | NAND_NCE | NAND_ALE); |
Masahiro Yamada | 984926b | 2017-11-22 02:38:31 +0900 | [diff] [blame] | 750 | if (chip->options & NAND_ROW_ADDR_3) |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 751 | chip->cmd_ctrl(mtd, page_addr >> 16, |
| 752 | NAND_NCE | NAND_ALE); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 753 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 754 | } |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 755 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE); |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 756 | |
| 757 | /* |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 758 | * Program and erase have their own busy handlers status, sequential |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 759 | * in and status need no delay. |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 760 | */ |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 761 | switch (command) { |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 762 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 763 | case NAND_CMD_CACHEDPROG: |
| 764 | case NAND_CMD_PAGEPROG: |
| 765 | case NAND_CMD_ERASE1: |
| 766 | case NAND_CMD_ERASE2: |
| 767 | case NAND_CMD_SEQIN: |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 768 | case NAND_CMD_RNDIN: |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 769 | case NAND_CMD_STATUS: |
Masahiro Yamada | 7f9baa1 | 2017-09-15 21:44:58 +0900 | [diff] [blame] | 770 | case NAND_CMD_READID: |
Masahiro Yamada | 0cd1018 | 2017-09-15 21:44:59 +0900 | [diff] [blame] | 771 | case NAND_CMD_SET_FEATURES: |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 772 | return; |
| 773 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 774 | case NAND_CMD_RESET: |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 775 | if (chip->dev_ready) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 776 | break; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 777 | udelay(chip->chip_delay); |
| 778 | chip->cmd_ctrl(mtd, NAND_CMD_STATUS, |
| 779 | NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE); |
| 780 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, |
| 781 | NAND_NCE | NAND_CTRL_CHANGE); |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 782 | /* EZ-NAND can take upto 250ms as per ONFi v4.0 */ |
| 783 | nand_wait_status_ready(mtd, 250); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 784 | return; |
| 785 | |
| 786 | case NAND_CMD_RNDOUT: |
| 787 | /* No ready / busy check necessary */ |
| 788 | chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART, |
| 789 | NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE); |
| 790 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, |
| 791 | NAND_NCE | NAND_CTRL_CHANGE); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 792 | return; |
| 793 | |
| 794 | case NAND_CMD_READ0: |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 795 | chip->cmd_ctrl(mtd, NAND_CMD_READSTART, |
| 796 | NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE); |
| 797 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, |
| 798 | NAND_NCE | NAND_CTRL_CHANGE); |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 799 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 800 | /* This applies to read commands */ |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 801 | default: |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 802 | /* |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 803 | * If we don't have access to the busy pin, we apply the given |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 804 | * command delay. |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 805 | */ |
| 806 | if (!chip->dev_ready) { |
| 807 | udelay(chip->chip_delay); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 808 | return; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 809 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 810 | } |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 811 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 812 | /* |
| 813 | * Apply this short delay always to ensure that we do wait tWB in |
| 814 | * any case on any machine. |
| 815 | */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 816 | ndelay(100); |
| 817 | |
| 818 | nand_wait_ready(mtd); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 819 | } |
| 820 | |
| 821 | /** |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 822 | * panic_nand_get_device - [GENERIC] Get chip for selected access |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 823 | * @chip: the nand chip descriptor |
| 824 | * @mtd: MTD device structure |
| 825 | * @new_state: the state which is requested |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 826 | * |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 827 | * Used when in panic, no locks are taken. |
| 828 | */ |
| 829 | static void panic_nand_get_device(struct nand_chip *chip, |
| 830 | struct mtd_info *mtd, int new_state) |
| 831 | { |
| 832 | /* Hardware controller shared among independent devices */ |
| 833 | chip->controller->active = chip; |
| 834 | chip->state = new_state; |
| 835 | } |
| 836 | |
| 837 | /** |
| 838 | * nand_get_device - [GENERIC] Get chip for selected access |
| 839 | * @mtd: MTD device structure |
| 840 | * @new_state: the state which is requested |
| 841 | * |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 842 | * Get the device and lock it for exclusive access |
| 843 | */ |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 844 | static int |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 845 | nand_get_device(struct mtd_info *mtd, int new_state) |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 846 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 847 | struct nand_chip *chip = mtd_to_nand(mtd); |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 848 | chip->state = new_state; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 849 | return 0; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 850 | } |
| 851 | |
| 852 | /** |
| 853 | * panic_nand_wait - [GENERIC] wait until the command is done |
| 854 | * @mtd: MTD device structure |
| 855 | * @chip: NAND chip structure |
| 856 | * @timeo: timeout |
| 857 | * |
| 858 | * Wait for command done. This is a helper function for nand_wait used when |
| 859 | * we are in interrupt context. May happen when in panic and trying to write |
| 860 | * an oops through mtdoops. |
| 861 | */ |
| 862 | static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip, |
| 863 | unsigned long timeo) |
| 864 | { |
| 865 | int i; |
| 866 | for (i = 0; i < timeo; i++) { |
| 867 | if (chip->dev_ready) { |
| 868 | if (chip->dev_ready(mtd)) |
| 869 | break; |
| 870 | } else { |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 871 | int ret; |
| 872 | u8 status; |
| 873 | |
| 874 | ret = nand_read_data_op(chip, &status, sizeof(status), |
| 875 | true); |
| 876 | if (ret) |
| 877 | return; |
| 878 | |
| 879 | if (status & NAND_STATUS_READY) |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 880 | break; |
| 881 | } |
| 882 | mdelay(1); |
| 883 | } |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 884 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 885 | |
| 886 | /** |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 887 | * nand_wait - [DEFAULT] wait until the command is done |
| 888 | * @mtd: MTD device structure |
| 889 | * @chip: NAND chip structure |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 890 | * |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 891 | * Wait for command done. This applies to erase and program only. |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 892 | */ |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 893 | static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 894 | { |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 895 | unsigned long timeo = 400; |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 896 | u8 status; |
| 897 | int ret; |
Wolfgang Denk | 9a08dd1 | 2005-11-02 14:29:12 +0100 | [diff] [blame] | 898 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 899 | led_trigger_event(nand_led_trigger, LED_FULL); |
Wolfgang Denk | 9a08dd1 | 2005-11-02 14:29:12 +0100 | [diff] [blame] | 900 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 901 | /* |
| 902 | * Apply this short delay always to ensure that we do wait tWB in any |
| 903 | * case on any machine. |
| 904 | */ |
| 905 | ndelay(100); |
Wolfgang Denk | 9a08dd1 | 2005-11-02 14:29:12 +0100 | [diff] [blame] | 906 | |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 907 | ret = nand_status_op(chip, NULL); |
| 908 | if (ret) |
| 909 | return ret; |
Wolfgang Denk | 9a08dd1 | 2005-11-02 14:29:12 +0100 | [diff] [blame] | 910 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 911 | u32 timer = (CONFIG_SYS_HZ * timeo) / 1000; |
| 912 | u32 time_start; |
| 913 | |
| 914 | time_start = get_timer(0); |
| 915 | while (get_timer(time_start) < timer) { |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 916 | if (chip->dev_ready) { |
| 917 | if (chip->dev_ready(mtd)) |
Wolfgang Denk | 9a08dd1 | 2005-11-02 14:29:12 +0100 | [diff] [blame] | 918 | break; |
| 919 | } else { |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 920 | ret = nand_read_data_op(chip, &status, |
| 921 | sizeof(status), true); |
| 922 | if (ret) |
| 923 | return ret; |
| 924 | |
| 925 | if (status & NAND_STATUS_READY) |
Wolfgang Denk | 9a08dd1 | 2005-11-02 14:29:12 +0100 | [diff] [blame] | 926 | break; |
| 927 | } |
| 928 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 929 | led_trigger_event(nand_led_trigger, LED_OFF); |
Bartlomiej Sieka | 6eed2ab | 2006-02-24 09:37:22 +0100 | [diff] [blame] | 930 | |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 931 | ret = nand_read_data_op(chip, &status, sizeof(status), true); |
| 932 | if (ret) |
| 933 | return ret; |
| 934 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 935 | /* This can happen if in case of timeout or buggy dev_ready */ |
| 936 | WARN_ON(!(status & NAND_STATUS_READY)); |
| 937 | return status; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 938 | } |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 939 | |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 940 | /** |
Boris Brezillon | e509cba | 2017-11-22 02:38:19 +0900 | [diff] [blame] | 941 | * nand_reset_data_interface - Reset data interface and timings |
| 942 | * @chip: The NAND chip |
Boris Brezillon | 32935f4 | 2017-11-22 02:38:28 +0900 | [diff] [blame] | 943 | * @chipnr: Internal die id |
Boris Brezillon | e509cba | 2017-11-22 02:38:19 +0900 | [diff] [blame] | 944 | * |
| 945 | * Reset the Data interface and timings to ONFI mode 0. |
| 946 | * |
| 947 | * Returns 0 for success or negative error code otherwise. |
| 948 | */ |
Boris Brezillon | 32935f4 | 2017-11-22 02:38:28 +0900 | [diff] [blame] | 949 | static int nand_reset_data_interface(struct nand_chip *chip, int chipnr) |
Boris Brezillon | e509cba | 2017-11-22 02:38:19 +0900 | [diff] [blame] | 950 | { |
| 951 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 952 | const struct nand_data_interface *conf; |
| 953 | int ret; |
| 954 | |
| 955 | if (!chip->setup_data_interface) |
| 956 | return 0; |
| 957 | |
| 958 | /* |
| 959 | * The ONFI specification says: |
| 960 | * " |
| 961 | * To transition from NV-DDR or NV-DDR2 to the SDR data |
| 962 | * interface, the host shall use the Reset (FFh) command |
| 963 | * using SDR timing mode 0. A device in any timing mode is |
| 964 | * required to recognize Reset (FFh) command issued in SDR |
| 965 | * timing mode 0. |
| 966 | * " |
| 967 | * |
| 968 | * Configure the data interface in SDR mode and set the |
| 969 | * timings to timing mode 0. |
| 970 | */ |
| 971 | |
| 972 | conf = nand_get_default_data_interface(); |
Boris Brezillon | 32935f4 | 2017-11-22 02:38:28 +0900 | [diff] [blame] | 973 | ret = chip->setup_data_interface(mtd, chipnr, conf); |
Boris Brezillon | e509cba | 2017-11-22 02:38:19 +0900 | [diff] [blame] | 974 | if (ret) |
| 975 | pr_err("Failed to configure data interface to SDR timing mode 0\n"); |
| 976 | |
| 977 | return ret; |
| 978 | } |
| 979 | |
| 980 | /** |
| 981 | * nand_setup_data_interface - Setup the best data interface and timings |
| 982 | * @chip: The NAND chip |
Boris Brezillon | 32935f4 | 2017-11-22 02:38:28 +0900 | [diff] [blame] | 983 | * @chipnr: Internal die id |
Boris Brezillon | e509cba | 2017-11-22 02:38:19 +0900 | [diff] [blame] | 984 | * |
| 985 | * Find and configure the best data interface and NAND timings supported by |
| 986 | * the chip and the driver. |
| 987 | * First tries to retrieve supported timing modes from ONFI information, |
| 988 | * and if the NAND chip does not support ONFI, relies on the |
| 989 | * ->onfi_timing_mode_default specified in the nand_ids table. |
| 990 | * |
| 991 | * Returns 0 for success or negative error code otherwise. |
| 992 | */ |
Boris Brezillon | 32935f4 | 2017-11-22 02:38:28 +0900 | [diff] [blame] | 993 | static int nand_setup_data_interface(struct nand_chip *chip, int chipnr) |
Boris Brezillon | e509cba | 2017-11-22 02:38:19 +0900 | [diff] [blame] | 994 | { |
| 995 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 996 | int ret; |
| 997 | |
| 998 | if (!chip->setup_data_interface || !chip->data_interface) |
| 999 | return 0; |
| 1000 | |
| 1001 | /* |
| 1002 | * Ensure the timing mode has been changed on the chip side |
| 1003 | * before changing timings on the controller side. |
| 1004 | */ |
| 1005 | if (chip->onfi_version) { |
| 1006 | u8 tmode_param[ONFI_SUBFEATURE_PARAM_LEN] = { |
| 1007 | chip->onfi_timing_mode_default, |
| 1008 | }; |
| 1009 | |
| 1010 | ret = chip->onfi_set_features(mtd, chip, |
| 1011 | ONFI_FEATURE_ADDR_TIMING_MODE, |
| 1012 | tmode_param); |
| 1013 | if (ret) |
| 1014 | goto err; |
| 1015 | } |
| 1016 | |
Boris Brezillon | 32935f4 | 2017-11-22 02:38:28 +0900 | [diff] [blame] | 1017 | ret = chip->setup_data_interface(mtd, chipnr, chip->data_interface); |
Boris Brezillon | e509cba | 2017-11-22 02:38:19 +0900 | [diff] [blame] | 1018 | err: |
| 1019 | return ret; |
| 1020 | } |
| 1021 | |
| 1022 | /** |
| 1023 | * nand_init_data_interface - find the best data interface and timings |
| 1024 | * @chip: The NAND chip |
| 1025 | * |
| 1026 | * Find the best data interface and NAND timings supported by the chip |
| 1027 | * and the driver. |
| 1028 | * First tries to retrieve supported timing modes from ONFI information, |
| 1029 | * and if the NAND chip does not support ONFI, relies on the |
| 1030 | * ->onfi_timing_mode_default specified in the nand_ids table. After this |
| 1031 | * function nand_chip->data_interface is initialized with the best timing mode |
| 1032 | * available. |
| 1033 | * |
| 1034 | * Returns 0 for success or negative error code otherwise. |
| 1035 | */ |
| 1036 | static int nand_init_data_interface(struct nand_chip *chip) |
| 1037 | { |
| 1038 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1039 | int modes, mode, ret; |
| 1040 | |
| 1041 | if (!chip->setup_data_interface) |
| 1042 | return 0; |
| 1043 | |
| 1044 | /* |
| 1045 | * First try to identify the best timings from ONFI parameters and |
| 1046 | * if the NAND does not support ONFI, fallback to the default ONFI |
| 1047 | * timing mode. |
| 1048 | */ |
| 1049 | modes = onfi_get_async_timing_mode(chip); |
| 1050 | if (modes == ONFI_TIMING_MODE_UNKNOWN) { |
| 1051 | if (!chip->onfi_timing_mode_default) |
| 1052 | return 0; |
| 1053 | |
| 1054 | modes = GENMASK(chip->onfi_timing_mode_default, 0); |
| 1055 | } |
| 1056 | |
| 1057 | chip->data_interface = kzalloc(sizeof(*chip->data_interface), |
| 1058 | GFP_KERNEL); |
| 1059 | if (!chip->data_interface) |
| 1060 | return -ENOMEM; |
| 1061 | |
| 1062 | for (mode = fls(modes) - 1; mode >= 0; mode--) { |
| 1063 | ret = onfi_init_data_interface(chip, chip->data_interface, |
| 1064 | NAND_SDR_IFACE, mode); |
| 1065 | if (ret) |
| 1066 | continue; |
| 1067 | |
Boris Brezillon | 32935f4 | 2017-11-22 02:38:28 +0900 | [diff] [blame] | 1068 | /* Pass -1 to only */ |
| 1069 | ret = chip->setup_data_interface(mtd, |
| 1070 | NAND_DATA_IFACE_CHECK_ONLY, |
| 1071 | chip->data_interface); |
Boris Brezillon | e509cba | 2017-11-22 02:38:19 +0900 | [diff] [blame] | 1072 | if (!ret) { |
| 1073 | chip->onfi_timing_mode_default = mode; |
| 1074 | break; |
| 1075 | } |
| 1076 | } |
| 1077 | |
| 1078 | return 0; |
| 1079 | } |
| 1080 | |
| 1081 | static void __maybe_unused nand_release_data_interface(struct nand_chip *chip) |
| 1082 | { |
| 1083 | kfree(chip->data_interface); |
| 1084 | } |
| 1085 | |
| 1086 | /** |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 1087 | * nand_read_page_op - Do a READ PAGE operation |
| 1088 | * @chip: The NAND chip |
| 1089 | * @page: page to read |
| 1090 | * @offset_in_page: offset within the page |
| 1091 | * @buf: buffer used to store the data |
| 1092 | * @len: length of the buffer |
| 1093 | * |
| 1094 | * This function issues a READ PAGE operation. |
| 1095 | * This function does not select/unselect the CS line. |
| 1096 | * |
| 1097 | * Returns 0 on success, a negative error code otherwise. |
| 1098 | */ |
| 1099 | int nand_read_page_op(struct nand_chip *chip, unsigned int page, |
| 1100 | unsigned int offset_in_page, void *buf, unsigned int len) |
| 1101 | { |
| 1102 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1103 | |
| 1104 | if (len && !buf) |
| 1105 | return -EINVAL; |
| 1106 | |
| 1107 | if (offset_in_page + len > mtd->writesize + mtd->oobsize) |
| 1108 | return -EINVAL; |
| 1109 | |
| 1110 | chip->cmdfunc(mtd, NAND_CMD_READ0, offset_in_page, page); |
| 1111 | if (len) |
| 1112 | chip->read_buf(mtd, buf, len); |
| 1113 | |
| 1114 | return 0; |
| 1115 | } |
| 1116 | EXPORT_SYMBOL_GPL(nand_read_page_op); |
| 1117 | |
| 1118 | /** |
| 1119 | * nand_read_param_page_op - Do a READ PARAMETER PAGE operation |
| 1120 | * @chip: The NAND chip |
| 1121 | * @page: parameter page to read |
| 1122 | * @buf: buffer used to store the data |
| 1123 | * @len: length of the buffer |
| 1124 | * |
| 1125 | * This function issues a READ PARAMETER PAGE operation. |
| 1126 | * This function does not select/unselect the CS line. |
| 1127 | * |
| 1128 | * Returns 0 on success, a negative error code otherwise. |
| 1129 | */ |
| 1130 | static int nand_read_param_page_op(struct nand_chip *chip, u8 page, void *buf, |
| 1131 | unsigned int len) |
| 1132 | { |
| 1133 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1134 | unsigned int i; |
| 1135 | u8 *p = buf; |
| 1136 | |
| 1137 | if (len && !buf) |
| 1138 | return -EINVAL; |
| 1139 | |
| 1140 | chip->cmdfunc(mtd, NAND_CMD_PARAM, page, -1); |
| 1141 | for (i = 0; i < len; i++) |
| 1142 | p[i] = chip->read_byte(mtd); |
| 1143 | |
| 1144 | return 0; |
| 1145 | } |
| 1146 | |
| 1147 | /** |
| 1148 | * nand_change_read_column_op - Do a CHANGE READ COLUMN operation |
| 1149 | * @chip: The NAND chip |
| 1150 | * @offset_in_page: offset within the page |
| 1151 | * @buf: buffer used to store the data |
| 1152 | * @len: length of the buffer |
| 1153 | * @force_8bit: force 8-bit bus access |
| 1154 | * |
| 1155 | * This function issues a CHANGE READ COLUMN operation. |
| 1156 | * This function does not select/unselect the CS line. |
| 1157 | * |
| 1158 | * Returns 0 on success, a negative error code otherwise. |
| 1159 | */ |
| 1160 | int nand_change_read_column_op(struct nand_chip *chip, |
| 1161 | unsigned int offset_in_page, void *buf, |
| 1162 | unsigned int len, bool force_8bit) |
| 1163 | { |
| 1164 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1165 | |
| 1166 | if (len && !buf) |
| 1167 | return -EINVAL; |
| 1168 | |
| 1169 | if (offset_in_page + len > mtd->writesize + mtd->oobsize) |
| 1170 | return -EINVAL; |
| 1171 | |
| 1172 | chip->cmdfunc(mtd, NAND_CMD_RNDOUT, offset_in_page, -1); |
| 1173 | if (len) |
| 1174 | chip->read_buf(mtd, buf, len); |
| 1175 | |
| 1176 | return 0; |
| 1177 | } |
| 1178 | EXPORT_SYMBOL_GPL(nand_change_read_column_op); |
| 1179 | |
| 1180 | /** |
| 1181 | * nand_read_oob_op - Do a READ OOB operation |
| 1182 | * @chip: The NAND chip |
| 1183 | * @page: page to read |
| 1184 | * @offset_in_oob: offset within the OOB area |
| 1185 | * @buf: buffer used to store the data |
| 1186 | * @len: length of the buffer |
| 1187 | * |
| 1188 | * This function issues a READ OOB operation. |
| 1189 | * This function does not select/unselect the CS line. |
| 1190 | * |
| 1191 | * Returns 0 on success, a negative error code otherwise. |
| 1192 | */ |
| 1193 | int nand_read_oob_op(struct nand_chip *chip, unsigned int page, |
| 1194 | unsigned int offset_in_oob, void *buf, unsigned int len) |
| 1195 | { |
| 1196 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1197 | |
| 1198 | if (len && !buf) |
| 1199 | return -EINVAL; |
| 1200 | |
| 1201 | if (offset_in_oob + len > mtd->oobsize) |
| 1202 | return -EINVAL; |
| 1203 | |
| 1204 | chip->cmdfunc(mtd, NAND_CMD_READOOB, offset_in_oob, page); |
| 1205 | if (len) |
| 1206 | chip->read_buf(mtd, buf, len); |
| 1207 | |
| 1208 | return 0; |
| 1209 | } |
| 1210 | EXPORT_SYMBOL_GPL(nand_read_oob_op); |
| 1211 | |
| 1212 | /** |
| 1213 | * nand_prog_page_begin_op - starts a PROG PAGE operation |
| 1214 | * @chip: The NAND chip |
| 1215 | * @page: page to write |
| 1216 | * @offset_in_page: offset within the page |
| 1217 | * @buf: buffer containing the data to write to the page |
| 1218 | * @len: length of the buffer |
| 1219 | * |
| 1220 | * This function issues the first half of a PROG PAGE operation. |
| 1221 | * This function does not select/unselect the CS line. |
| 1222 | * |
| 1223 | * Returns 0 on success, a negative error code otherwise. |
| 1224 | */ |
| 1225 | int nand_prog_page_begin_op(struct nand_chip *chip, unsigned int page, |
| 1226 | unsigned int offset_in_page, const void *buf, |
| 1227 | unsigned int len) |
| 1228 | { |
| 1229 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1230 | |
| 1231 | if (len && !buf) |
| 1232 | return -EINVAL; |
| 1233 | |
| 1234 | if (offset_in_page + len > mtd->writesize + mtd->oobsize) |
| 1235 | return -EINVAL; |
| 1236 | |
| 1237 | chip->cmdfunc(mtd, NAND_CMD_SEQIN, offset_in_page, page); |
| 1238 | |
| 1239 | if (buf) |
| 1240 | chip->write_buf(mtd, buf, len); |
| 1241 | |
| 1242 | return 0; |
| 1243 | } |
| 1244 | EXPORT_SYMBOL_GPL(nand_prog_page_begin_op); |
| 1245 | |
| 1246 | /** |
| 1247 | * nand_prog_page_end_op - ends a PROG PAGE operation |
| 1248 | * @chip: The NAND chip |
| 1249 | * |
| 1250 | * This function issues the second half of a PROG PAGE operation. |
| 1251 | * This function does not select/unselect the CS line. |
| 1252 | * |
| 1253 | * Returns 0 on success, a negative error code otherwise. |
| 1254 | */ |
| 1255 | int nand_prog_page_end_op(struct nand_chip *chip) |
| 1256 | { |
| 1257 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1258 | int status; |
| 1259 | |
| 1260 | chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1); |
| 1261 | |
| 1262 | status = chip->waitfunc(mtd, chip); |
| 1263 | if (status & NAND_STATUS_FAIL) |
| 1264 | return -EIO; |
| 1265 | |
| 1266 | return 0; |
| 1267 | } |
| 1268 | EXPORT_SYMBOL_GPL(nand_prog_page_end_op); |
| 1269 | |
| 1270 | /** |
| 1271 | * nand_prog_page_op - Do a full PROG PAGE operation |
| 1272 | * @chip: The NAND chip |
| 1273 | * @page: page to write |
| 1274 | * @offset_in_page: offset within the page |
| 1275 | * @buf: buffer containing the data to write to the page |
| 1276 | * @len: length of the buffer |
| 1277 | * |
| 1278 | * This function issues a full PROG PAGE operation. |
| 1279 | * This function does not select/unselect the CS line. |
| 1280 | * |
| 1281 | * Returns 0 on success, a negative error code otherwise. |
| 1282 | */ |
| 1283 | int nand_prog_page_op(struct nand_chip *chip, unsigned int page, |
| 1284 | unsigned int offset_in_page, const void *buf, |
| 1285 | unsigned int len) |
| 1286 | { |
| 1287 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1288 | int status; |
| 1289 | |
| 1290 | if (!len || !buf) |
| 1291 | return -EINVAL; |
| 1292 | |
| 1293 | if (offset_in_page + len > mtd->writesize + mtd->oobsize) |
| 1294 | return -EINVAL; |
| 1295 | |
| 1296 | chip->cmdfunc(mtd, NAND_CMD_SEQIN, offset_in_page, page); |
| 1297 | chip->write_buf(mtd, buf, len); |
| 1298 | chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1); |
| 1299 | |
| 1300 | status = chip->waitfunc(mtd, chip); |
| 1301 | if (status & NAND_STATUS_FAIL) |
| 1302 | return -EIO; |
| 1303 | |
| 1304 | return 0; |
| 1305 | } |
| 1306 | EXPORT_SYMBOL_GPL(nand_prog_page_op); |
| 1307 | |
| 1308 | /** |
| 1309 | * nand_change_write_column_op - Do a CHANGE WRITE COLUMN operation |
| 1310 | * @chip: The NAND chip |
| 1311 | * @offset_in_page: offset within the page |
| 1312 | * @buf: buffer containing the data to send to the NAND |
| 1313 | * @len: length of the buffer |
| 1314 | * @force_8bit: force 8-bit bus access |
| 1315 | * |
| 1316 | * This function issues a CHANGE WRITE COLUMN operation. |
| 1317 | * This function does not select/unselect the CS line. |
| 1318 | * |
| 1319 | * Returns 0 on success, a negative error code otherwise. |
| 1320 | */ |
| 1321 | int nand_change_write_column_op(struct nand_chip *chip, |
| 1322 | unsigned int offset_in_page, |
| 1323 | const void *buf, unsigned int len, |
| 1324 | bool force_8bit) |
| 1325 | { |
| 1326 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1327 | |
| 1328 | if (len && !buf) |
| 1329 | return -EINVAL; |
| 1330 | |
| 1331 | if (offset_in_page + len > mtd->writesize + mtd->oobsize) |
| 1332 | return -EINVAL; |
| 1333 | |
| 1334 | chip->cmdfunc(mtd, NAND_CMD_RNDIN, offset_in_page, -1); |
| 1335 | if (len) |
| 1336 | chip->write_buf(mtd, buf, len); |
| 1337 | |
| 1338 | return 0; |
| 1339 | } |
| 1340 | EXPORT_SYMBOL_GPL(nand_change_write_column_op); |
| 1341 | |
| 1342 | /** |
| 1343 | * nand_readid_op - Do a READID operation |
| 1344 | * @chip: The NAND chip |
| 1345 | * @addr: address cycle to pass after the READID command |
| 1346 | * @buf: buffer used to store the ID |
| 1347 | * @len: length of the buffer |
| 1348 | * |
| 1349 | * This function sends a READID command and reads back the ID returned by the |
| 1350 | * NAND. |
| 1351 | * This function does not select/unselect the CS line. |
| 1352 | * |
| 1353 | * Returns 0 on success, a negative error code otherwise. |
| 1354 | */ |
| 1355 | int nand_readid_op(struct nand_chip *chip, u8 addr, void *buf, |
| 1356 | unsigned int len) |
| 1357 | { |
| 1358 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1359 | unsigned int i; |
| 1360 | u8 *id = buf; |
| 1361 | |
| 1362 | if (len && !buf) |
| 1363 | return -EINVAL; |
| 1364 | |
| 1365 | chip->cmdfunc(mtd, NAND_CMD_READID, addr, -1); |
| 1366 | |
| 1367 | for (i = 0; i < len; i++) |
| 1368 | id[i] = chip->read_byte(mtd); |
| 1369 | |
| 1370 | return 0; |
| 1371 | } |
| 1372 | EXPORT_SYMBOL_GPL(nand_readid_op); |
| 1373 | |
| 1374 | /** |
| 1375 | * nand_status_op - Do a STATUS operation |
| 1376 | * @chip: The NAND chip |
| 1377 | * @status: out variable to store the NAND status |
| 1378 | * |
| 1379 | * This function sends a STATUS command and reads back the status returned by |
| 1380 | * the NAND. |
| 1381 | * This function does not select/unselect the CS line. |
| 1382 | * |
| 1383 | * Returns 0 on success, a negative error code otherwise. |
| 1384 | */ |
| 1385 | int nand_status_op(struct nand_chip *chip, u8 *status) |
| 1386 | { |
| 1387 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1388 | |
| 1389 | chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1); |
| 1390 | if (status) |
| 1391 | *status = chip->read_byte(mtd); |
| 1392 | |
| 1393 | return 0; |
| 1394 | } |
| 1395 | EXPORT_SYMBOL_GPL(nand_status_op); |
| 1396 | |
| 1397 | /** |
| 1398 | * nand_exit_status_op - Exit a STATUS operation |
| 1399 | * @chip: The NAND chip |
| 1400 | * |
| 1401 | * This function sends a READ0 command to cancel the effect of the STATUS |
| 1402 | * command to avoid reading only the status until a new read command is sent. |
| 1403 | * |
| 1404 | * This function does not select/unselect the CS line. |
| 1405 | * |
| 1406 | * Returns 0 on success, a negative error code otherwise. |
| 1407 | */ |
| 1408 | int nand_exit_status_op(struct nand_chip *chip) |
| 1409 | { |
| 1410 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1411 | |
| 1412 | chip->cmdfunc(mtd, NAND_CMD_READ0, -1, -1); |
| 1413 | |
| 1414 | return 0; |
| 1415 | } |
| 1416 | EXPORT_SYMBOL_GPL(nand_exit_status_op); |
| 1417 | |
| 1418 | /** |
| 1419 | * nand_erase_op - Do an erase operation |
| 1420 | * @chip: The NAND chip |
| 1421 | * @eraseblock: block to erase |
| 1422 | * |
| 1423 | * This function sends an ERASE command and waits for the NAND to be ready |
| 1424 | * before returning. |
| 1425 | * This function does not select/unselect the CS line. |
| 1426 | * |
| 1427 | * Returns 0 on success, a negative error code otherwise. |
| 1428 | */ |
| 1429 | int nand_erase_op(struct nand_chip *chip, unsigned int eraseblock) |
| 1430 | { |
| 1431 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1432 | unsigned int page = eraseblock << |
| 1433 | (chip->phys_erase_shift - chip->page_shift); |
| 1434 | int status; |
| 1435 | |
| 1436 | chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page); |
| 1437 | chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1); |
| 1438 | |
| 1439 | status = chip->waitfunc(mtd, chip); |
| 1440 | if (status < 0) |
| 1441 | return status; |
| 1442 | |
| 1443 | if (status & NAND_STATUS_FAIL) |
| 1444 | return -EIO; |
| 1445 | |
| 1446 | return 0; |
| 1447 | } |
| 1448 | EXPORT_SYMBOL_GPL(nand_erase_op); |
| 1449 | |
| 1450 | /** |
| 1451 | * nand_set_features_op - Do a SET FEATURES operation |
| 1452 | * @chip: The NAND chip |
| 1453 | * @feature: feature id |
| 1454 | * @data: 4 bytes of data |
| 1455 | * |
| 1456 | * This function sends a SET FEATURES command and waits for the NAND to be |
| 1457 | * ready before returning. |
| 1458 | * This function does not select/unselect the CS line. |
| 1459 | * |
| 1460 | * Returns 0 on success, a negative error code otherwise. |
| 1461 | */ |
| 1462 | static int nand_set_features_op(struct nand_chip *chip, u8 feature, |
| 1463 | const void *data) |
| 1464 | { |
| 1465 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1466 | const u8 *params = data; |
| 1467 | int i, status; |
| 1468 | |
| 1469 | chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, feature, -1); |
| 1470 | for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i) |
| 1471 | chip->write_byte(mtd, params[i]); |
| 1472 | |
| 1473 | status = chip->waitfunc(mtd, chip); |
| 1474 | if (status & NAND_STATUS_FAIL) |
| 1475 | return -EIO; |
| 1476 | |
| 1477 | return 0; |
| 1478 | } |
| 1479 | |
| 1480 | /** |
| 1481 | * nand_get_features_op - Do a GET FEATURES operation |
| 1482 | * @chip: The NAND chip |
| 1483 | * @feature: feature id |
| 1484 | * @data: 4 bytes of data |
| 1485 | * |
| 1486 | * This function sends a GET FEATURES command and waits for the NAND to be |
| 1487 | * ready before returning. |
| 1488 | * This function does not select/unselect the CS line. |
| 1489 | * |
| 1490 | * Returns 0 on success, a negative error code otherwise. |
| 1491 | */ |
| 1492 | static int nand_get_features_op(struct nand_chip *chip, u8 feature, |
| 1493 | void *data) |
| 1494 | { |
| 1495 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1496 | u8 *params = data; |
| 1497 | int i; |
| 1498 | |
| 1499 | chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, feature, -1); |
| 1500 | for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i) |
| 1501 | params[i] = chip->read_byte(mtd); |
| 1502 | |
| 1503 | return 0; |
| 1504 | } |
| 1505 | |
| 1506 | /** |
| 1507 | * nand_reset_op - Do a reset operation |
| 1508 | * @chip: The NAND chip |
| 1509 | * |
| 1510 | * This function sends a RESET command and waits for the NAND to be ready |
| 1511 | * before returning. |
| 1512 | * This function does not select/unselect the CS line. |
| 1513 | * |
| 1514 | * Returns 0 on success, a negative error code otherwise. |
| 1515 | */ |
| 1516 | int nand_reset_op(struct nand_chip *chip) |
| 1517 | { |
| 1518 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1519 | |
| 1520 | chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1); |
| 1521 | |
| 1522 | return 0; |
| 1523 | } |
| 1524 | EXPORT_SYMBOL_GPL(nand_reset_op); |
| 1525 | |
| 1526 | /** |
| 1527 | * nand_read_data_op - Read data from the NAND |
| 1528 | * @chip: The NAND chip |
| 1529 | * @buf: buffer used to store the data |
| 1530 | * @len: length of the buffer |
| 1531 | * @force_8bit: force 8-bit bus access |
| 1532 | * |
| 1533 | * This function does a raw data read on the bus. Usually used after launching |
| 1534 | * another NAND operation like nand_read_page_op(). |
| 1535 | * This function does not select/unselect the CS line. |
| 1536 | * |
| 1537 | * Returns 0 on success, a negative error code otherwise. |
| 1538 | */ |
| 1539 | int nand_read_data_op(struct nand_chip *chip, void *buf, unsigned int len, |
| 1540 | bool force_8bit) |
| 1541 | { |
| 1542 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1543 | |
| 1544 | if (!len || !buf) |
| 1545 | return -EINVAL; |
| 1546 | |
| 1547 | if (force_8bit) { |
| 1548 | u8 *p = buf; |
| 1549 | unsigned int i; |
| 1550 | |
| 1551 | for (i = 0; i < len; i++) |
| 1552 | p[i] = chip->read_byte(mtd); |
| 1553 | } else { |
| 1554 | chip->read_buf(mtd, buf, len); |
| 1555 | } |
| 1556 | |
| 1557 | return 0; |
| 1558 | } |
| 1559 | EXPORT_SYMBOL_GPL(nand_read_data_op); |
| 1560 | |
| 1561 | /** |
| 1562 | * nand_write_data_op - Write data from the NAND |
| 1563 | * @chip: The NAND chip |
| 1564 | * @buf: buffer containing the data to send on the bus |
| 1565 | * @len: length of the buffer |
| 1566 | * @force_8bit: force 8-bit bus access |
| 1567 | * |
| 1568 | * This function does a raw data write on the bus. Usually used after launching |
| 1569 | * another NAND operation like nand_write_page_begin_op(). |
| 1570 | * This function does not select/unselect the CS line. |
| 1571 | * |
| 1572 | * Returns 0 on success, a negative error code otherwise. |
| 1573 | */ |
| 1574 | int nand_write_data_op(struct nand_chip *chip, const void *buf, |
| 1575 | unsigned int len, bool force_8bit) |
| 1576 | { |
| 1577 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1578 | |
| 1579 | if (!len || !buf) |
| 1580 | return -EINVAL; |
| 1581 | |
| 1582 | if (force_8bit) { |
| 1583 | const u8 *p = buf; |
| 1584 | unsigned int i; |
| 1585 | |
| 1586 | for (i = 0; i < len; i++) |
| 1587 | chip->write_byte(mtd, p[i]); |
| 1588 | } else { |
| 1589 | chip->write_buf(mtd, buf, len); |
| 1590 | } |
| 1591 | |
| 1592 | return 0; |
| 1593 | } |
| 1594 | EXPORT_SYMBOL_GPL(nand_write_data_op); |
| 1595 | |
| 1596 | /** |
Sascha Hauer | 44ad3b9 | 2017-11-22 02:38:15 +0900 | [diff] [blame] | 1597 | * nand_reset - Reset and initialize a NAND device |
| 1598 | * @chip: The NAND chip |
Boris Brezillon | 7ec6dc5 | 2017-11-22 02:38:20 +0900 | [diff] [blame] | 1599 | * @chipnr: Internal die id |
Sascha Hauer | 44ad3b9 | 2017-11-22 02:38:15 +0900 | [diff] [blame] | 1600 | * |
| 1601 | * Returns 0 for success or negative error code otherwise |
| 1602 | */ |
Boris Brezillon | 7ec6dc5 | 2017-11-22 02:38:20 +0900 | [diff] [blame] | 1603 | int nand_reset(struct nand_chip *chip, int chipnr) |
Sascha Hauer | 44ad3b9 | 2017-11-22 02:38:15 +0900 | [diff] [blame] | 1604 | { |
| 1605 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | e509cba | 2017-11-22 02:38:19 +0900 | [diff] [blame] | 1606 | int ret; |
| 1607 | |
Boris Brezillon | 32935f4 | 2017-11-22 02:38:28 +0900 | [diff] [blame] | 1608 | ret = nand_reset_data_interface(chip, chipnr); |
Boris Brezillon | e509cba | 2017-11-22 02:38:19 +0900 | [diff] [blame] | 1609 | if (ret) |
| 1610 | return ret; |
Sascha Hauer | 44ad3b9 | 2017-11-22 02:38:15 +0900 | [diff] [blame] | 1611 | |
Boris Brezillon | 7ec6dc5 | 2017-11-22 02:38:20 +0900 | [diff] [blame] | 1612 | /* |
| 1613 | * The CS line has to be released before we can apply the new NAND |
| 1614 | * interface settings, hence this weird ->select_chip() dance. |
| 1615 | */ |
| 1616 | chip->select_chip(mtd, chipnr); |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 1617 | ret = nand_reset_op(chip); |
Boris Brezillon | 7ec6dc5 | 2017-11-22 02:38:20 +0900 | [diff] [blame] | 1618 | chip->select_chip(mtd, -1); |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 1619 | if (ret) |
| 1620 | return ret; |
Sascha Hauer | 44ad3b9 | 2017-11-22 02:38:15 +0900 | [diff] [blame] | 1621 | |
Boris Brezillon | 7ec6dc5 | 2017-11-22 02:38:20 +0900 | [diff] [blame] | 1622 | chip->select_chip(mtd, chipnr); |
Boris Brezillon | 32935f4 | 2017-11-22 02:38:28 +0900 | [diff] [blame] | 1623 | ret = nand_setup_data_interface(chip, chipnr); |
Boris Brezillon | 7ec6dc5 | 2017-11-22 02:38:20 +0900 | [diff] [blame] | 1624 | chip->select_chip(mtd, -1); |
Boris Brezillon | e509cba | 2017-11-22 02:38:19 +0900 | [diff] [blame] | 1625 | if (ret) |
| 1626 | return ret; |
| 1627 | |
Sascha Hauer | 44ad3b9 | 2017-11-22 02:38:15 +0900 | [diff] [blame] | 1628 | return 0; |
| 1629 | } |
| 1630 | |
| 1631 | /** |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 1632 | * nand_check_erased_buf - check if a buffer contains (almost) only 0xff data |
| 1633 | * @buf: buffer to test |
| 1634 | * @len: buffer length |
| 1635 | * @bitflips_threshold: maximum number of bitflips |
| 1636 | * |
| 1637 | * Check if a buffer contains only 0xff, which means the underlying region |
| 1638 | * has been erased and is ready to be programmed. |
| 1639 | * The bitflips_threshold specify the maximum number of bitflips before |
| 1640 | * considering the region is not erased. |
| 1641 | * Note: The logic of this function has been extracted from the memweight |
| 1642 | * implementation, except that nand_check_erased_buf function exit before |
| 1643 | * testing the whole buffer if the number of bitflips exceed the |
| 1644 | * bitflips_threshold value. |
| 1645 | * |
| 1646 | * Returns a positive number of bitflips less than or equal to |
| 1647 | * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the |
| 1648 | * threshold. |
| 1649 | */ |
| 1650 | static int nand_check_erased_buf(void *buf, int len, int bitflips_threshold) |
| 1651 | { |
| 1652 | const unsigned char *bitmap = buf; |
| 1653 | int bitflips = 0; |
| 1654 | int weight; |
| 1655 | |
| 1656 | for (; len && ((uintptr_t)bitmap) % sizeof(long); |
| 1657 | len--, bitmap++) { |
| 1658 | weight = hweight8(*bitmap); |
| 1659 | bitflips += BITS_PER_BYTE - weight; |
| 1660 | if (unlikely(bitflips > bitflips_threshold)) |
| 1661 | return -EBADMSG; |
| 1662 | } |
| 1663 | |
| 1664 | for (; len >= 4; len -= 4, bitmap += 4) { |
| 1665 | weight = hweight32(*((u32 *)bitmap)); |
| 1666 | bitflips += 32 - weight; |
| 1667 | if (unlikely(bitflips > bitflips_threshold)) |
| 1668 | return -EBADMSG; |
| 1669 | } |
| 1670 | |
| 1671 | for (; len > 0; len--, bitmap++) { |
| 1672 | weight = hweight8(*bitmap); |
| 1673 | bitflips += BITS_PER_BYTE - weight; |
| 1674 | if (unlikely(bitflips > bitflips_threshold)) |
| 1675 | return -EBADMSG; |
| 1676 | } |
| 1677 | |
| 1678 | return bitflips; |
| 1679 | } |
| 1680 | |
| 1681 | /** |
| 1682 | * nand_check_erased_ecc_chunk - check if an ECC chunk contains (almost) only |
| 1683 | * 0xff data |
| 1684 | * @data: data buffer to test |
| 1685 | * @datalen: data length |
| 1686 | * @ecc: ECC buffer |
| 1687 | * @ecclen: ECC length |
| 1688 | * @extraoob: extra OOB buffer |
| 1689 | * @extraooblen: extra OOB length |
| 1690 | * @bitflips_threshold: maximum number of bitflips |
| 1691 | * |
| 1692 | * Check if a data buffer and its associated ECC and OOB data contains only |
| 1693 | * 0xff pattern, which means the underlying region has been erased and is |
| 1694 | * ready to be programmed. |
| 1695 | * The bitflips_threshold specify the maximum number of bitflips before |
| 1696 | * considering the region as not erased. |
| 1697 | * |
| 1698 | * Note: |
| 1699 | * 1/ ECC algorithms are working on pre-defined block sizes which are usually |
| 1700 | * different from the NAND page size. When fixing bitflips, ECC engines will |
| 1701 | * report the number of errors per chunk, and the NAND core infrastructure |
| 1702 | * expect you to return the maximum number of bitflips for the whole page. |
| 1703 | * This is why you should always use this function on a single chunk and |
| 1704 | * not on the whole page. After checking each chunk you should update your |
| 1705 | * max_bitflips value accordingly. |
| 1706 | * 2/ When checking for bitflips in erased pages you should not only check |
| 1707 | * the payload data but also their associated ECC data, because a user might |
| 1708 | * have programmed almost all bits to 1 but a few. In this case, we |
| 1709 | * shouldn't consider the chunk as erased, and checking ECC bytes prevent |
| 1710 | * this case. |
| 1711 | * 3/ The extraoob argument is optional, and should be used if some of your OOB |
| 1712 | * data are protected by the ECC engine. |
| 1713 | * It could also be used if you support subpages and want to attach some |
| 1714 | * extra OOB data to an ECC chunk. |
| 1715 | * |
| 1716 | * Returns a positive number of bitflips less than or equal to |
| 1717 | * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the |
| 1718 | * threshold. In case of success, the passed buffers are filled with 0xff. |
| 1719 | */ |
| 1720 | int nand_check_erased_ecc_chunk(void *data, int datalen, |
| 1721 | void *ecc, int ecclen, |
| 1722 | void *extraoob, int extraooblen, |
| 1723 | int bitflips_threshold) |
| 1724 | { |
| 1725 | int data_bitflips = 0, ecc_bitflips = 0, extraoob_bitflips = 0; |
| 1726 | |
| 1727 | data_bitflips = nand_check_erased_buf(data, datalen, |
| 1728 | bitflips_threshold); |
| 1729 | if (data_bitflips < 0) |
| 1730 | return data_bitflips; |
| 1731 | |
| 1732 | bitflips_threshold -= data_bitflips; |
| 1733 | |
| 1734 | ecc_bitflips = nand_check_erased_buf(ecc, ecclen, bitflips_threshold); |
| 1735 | if (ecc_bitflips < 0) |
| 1736 | return ecc_bitflips; |
| 1737 | |
| 1738 | bitflips_threshold -= ecc_bitflips; |
| 1739 | |
| 1740 | extraoob_bitflips = nand_check_erased_buf(extraoob, extraooblen, |
| 1741 | bitflips_threshold); |
| 1742 | if (extraoob_bitflips < 0) |
| 1743 | return extraoob_bitflips; |
| 1744 | |
| 1745 | if (data_bitflips) |
| 1746 | memset(data, 0xff, datalen); |
| 1747 | |
| 1748 | if (ecc_bitflips) |
| 1749 | memset(ecc, 0xff, ecclen); |
| 1750 | |
| 1751 | if (extraoob_bitflips) |
| 1752 | memset(extraoob, 0xff, extraooblen); |
| 1753 | |
| 1754 | return data_bitflips + ecc_bitflips + extraoob_bitflips; |
| 1755 | } |
| 1756 | EXPORT_SYMBOL(nand_check_erased_ecc_chunk); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1757 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1758 | /** |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1759 | * nand_read_page_raw - [INTERN] read raw page data without ecc |
| 1760 | * @mtd: mtd info structure |
| 1761 | * @chip: nand chip info structure |
| 1762 | * @buf: buffer to store read data |
| 1763 | * @oob_required: caller requires OOB data read to chip->oob_poi |
| 1764 | * @page: page number to read |
David Brownell | ee86b8d | 2009-11-07 16:27:01 -0500 | [diff] [blame] | 1765 | * |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1766 | * Not for syndrome calculating ECC controllers, which use a special oob layout. |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1767 | */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1768 | static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip, |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1769 | uint8_t *buf, int oob_required, int page) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1770 | { |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 1771 | int ret; |
| 1772 | |
| 1773 | ret = nand_read_data_op(chip, buf, mtd->writesize, false); |
| 1774 | if (ret) |
| 1775 | return ret; |
| 1776 | |
| 1777 | if (oob_required) { |
| 1778 | ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize, |
| 1779 | false); |
| 1780 | if (ret) |
| 1781 | return ret; |
| 1782 | } |
| 1783 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1784 | return 0; |
| 1785 | } |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 1786 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1787 | /** |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1788 | * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc |
| 1789 | * @mtd: mtd info structure |
| 1790 | * @chip: nand chip info structure |
| 1791 | * @buf: buffer to store read data |
| 1792 | * @oob_required: caller requires OOB data read to chip->oob_poi |
| 1793 | * @page: page number to read |
David Brownell | ee86b8d | 2009-11-07 16:27:01 -0500 | [diff] [blame] | 1794 | * |
| 1795 | * We need a special oob layout and handling even when OOB isn't used. |
| 1796 | */ |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 1797 | static int nand_read_page_raw_syndrome(struct mtd_info *mtd, |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1798 | struct nand_chip *chip, uint8_t *buf, |
| 1799 | int oob_required, int page) |
David Brownell | ee86b8d | 2009-11-07 16:27:01 -0500 | [diff] [blame] | 1800 | { |
| 1801 | int eccsize = chip->ecc.size; |
| 1802 | int eccbytes = chip->ecc.bytes; |
| 1803 | uint8_t *oob = chip->oob_poi; |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 1804 | int steps, size, ret; |
David Brownell | ee86b8d | 2009-11-07 16:27:01 -0500 | [diff] [blame] | 1805 | |
| 1806 | for (steps = chip->ecc.steps; steps > 0; steps--) { |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 1807 | ret = nand_read_data_op(chip, buf, eccsize, false); |
| 1808 | if (ret) |
| 1809 | return ret; |
| 1810 | |
David Brownell | ee86b8d | 2009-11-07 16:27:01 -0500 | [diff] [blame] | 1811 | buf += eccsize; |
| 1812 | |
| 1813 | if (chip->ecc.prepad) { |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 1814 | ret = nand_read_data_op(chip, oob, chip->ecc.prepad, |
| 1815 | false); |
| 1816 | if (ret) |
| 1817 | return ret; |
| 1818 | |
David Brownell | ee86b8d | 2009-11-07 16:27:01 -0500 | [diff] [blame] | 1819 | oob += chip->ecc.prepad; |
| 1820 | } |
| 1821 | |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 1822 | ret = nand_read_data_op(chip, oob, eccbytes, false); |
| 1823 | if (ret) |
| 1824 | return ret; |
| 1825 | |
David Brownell | ee86b8d | 2009-11-07 16:27:01 -0500 | [diff] [blame] | 1826 | oob += eccbytes; |
| 1827 | |
| 1828 | if (chip->ecc.postpad) { |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 1829 | ret = nand_read_data_op(chip, oob, chip->ecc.postpad, |
| 1830 | false); |
| 1831 | if (ret) |
| 1832 | return ret; |
| 1833 | |
David Brownell | ee86b8d | 2009-11-07 16:27:01 -0500 | [diff] [blame] | 1834 | oob += chip->ecc.postpad; |
| 1835 | } |
| 1836 | } |
| 1837 | |
| 1838 | size = mtd->oobsize - (oob - chip->oob_poi); |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 1839 | if (size) { |
| 1840 | ret = nand_read_data_op(chip, oob, size, false); |
| 1841 | if (ret) |
| 1842 | return ret; |
| 1843 | } |
David Brownell | ee86b8d | 2009-11-07 16:27:01 -0500 | [diff] [blame] | 1844 | |
| 1845 | return 0; |
| 1846 | } |
| 1847 | |
| 1848 | /** |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1849 | * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function |
| 1850 | * @mtd: mtd info structure |
| 1851 | * @chip: nand chip info structure |
| 1852 | * @buf: buffer to store read data |
| 1853 | * @oob_required: caller requires OOB data read to chip->oob_poi |
| 1854 | * @page: page number to read |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1855 | */ |
| 1856 | static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip, |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1857 | uint8_t *buf, int oob_required, int page) |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1858 | { |
| 1859 | int i, eccsize = chip->ecc.size; |
| 1860 | int eccbytes = chip->ecc.bytes; |
| 1861 | int eccsteps = chip->ecc.steps; |
| 1862 | uint8_t *p = buf; |
| 1863 | uint8_t *ecc_calc = chip->buffers->ecccalc; |
| 1864 | uint8_t *ecc_code = chip->buffers->ecccode; |
| 1865 | uint32_t *eccpos = chip->ecc.layout->eccpos; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1866 | unsigned int max_bitflips = 0; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 1867 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1868 | chip->ecc.read_page_raw(mtd, chip, buf, 1, page); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1869 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1870 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) |
| 1871 | chip->ecc.calculate(mtd, p, &ecc_calc[i]); |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 1872 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1873 | for (i = 0; i < chip->ecc.total; i++) |
| 1874 | ecc_code[i] = chip->oob_poi[eccpos[i]]; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 1875 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1876 | eccsteps = chip->ecc.steps; |
| 1877 | p = buf; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1878 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1879 | for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 1880 | int stat; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1881 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1882 | stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1883 | if (stat < 0) { |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1884 | mtd->ecc_stats.failed++; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1885 | } else { |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1886 | mtd->ecc_stats.corrected += stat; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1887 | max_bitflips = max_t(unsigned int, max_bitflips, stat); |
| 1888 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1889 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1890 | return max_bitflips; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1891 | } |
| 1892 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1893 | /** |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1894 | * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1895 | * @mtd: mtd info structure |
| 1896 | * @chip: nand chip info structure |
| 1897 | * @data_offs: offset of requested data within the page |
| 1898 | * @readlen: data length |
| 1899 | * @bufpoi: buffer to store read data |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 1900 | * @page: page number to read |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 1901 | */ |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 1902 | static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip, |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 1903 | uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi, |
| 1904 | int page) |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 1905 | { |
| 1906 | int start_step, end_step, num_steps; |
| 1907 | uint32_t *eccpos = chip->ecc.layout->eccpos; |
| 1908 | uint8_t *p; |
| 1909 | int data_col_addr, i, gaps = 0; |
| 1910 | int datafrag_len, eccfrag_len, aligned_len, aligned_pos; |
| 1911 | int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1; |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 1912 | int index; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1913 | unsigned int max_bitflips = 0; |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 1914 | int ret; |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 1915 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1916 | /* Column address within the page aligned to ECC size (256bytes) */ |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 1917 | start_step = data_offs / chip->ecc.size; |
| 1918 | end_step = (data_offs + readlen - 1) / chip->ecc.size; |
| 1919 | num_steps = end_step - start_step + 1; |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 1920 | index = start_step * chip->ecc.bytes; |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 1921 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1922 | /* Data size aligned to ECC ecc.size */ |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 1923 | datafrag_len = num_steps * chip->ecc.size; |
| 1924 | eccfrag_len = num_steps * chip->ecc.bytes; |
| 1925 | |
| 1926 | data_col_addr = start_step * chip->ecc.size; |
| 1927 | /* If we read not a page aligned data */ |
| 1928 | if (data_col_addr != 0) |
| 1929 | chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1); |
| 1930 | |
| 1931 | p = bufpoi + data_col_addr; |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 1932 | ret = nand_read_data_op(chip, p, datafrag_len, false); |
| 1933 | if (ret) |
| 1934 | return ret; |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 1935 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1936 | /* Calculate ECC */ |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 1937 | for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) |
| 1938 | chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]); |
| 1939 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1940 | /* |
| 1941 | * The performance is faster if we position offsets according to |
| 1942 | * ecc.pos. Let's make sure that there are no gaps in ECC positions. |
| 1943 | */ |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 1944 | for (i = 0; i < eccfrag_len - 1; i++) { |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 1945 | if (eccpos[i + index] + 1 != eccpos[i + index + 1]) { |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 1946 | gaps = 1; |
| 1947 | break; |
| 1948 | } |
| 1949 | } |
| 1950 | if (gaps) { |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 1951 | ret = nand_change_read_column_op(chip, mtd->writesize, |
| 1952 | chip->oob_poi, mtd->oobsize, |
| 1953 | false); |
| 1954 | if (ret) |
| 1955 | return ret; |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 1956 | } else { |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1957 | /* |
| 1958 | * Send the command to read the particular ECC bytes take care |
| 1959 | * about buswidth alignment in read_buf. |
| 1960 | */ |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 1961 | aligned_pos = eccpos[index] & ~(busw - 1); |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 1962 | aligned_len = eccfrag_len; |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 1963 | if (eccpos[index] & (busw - 1)) |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 1964 | aligned_len++; |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 1965 | if (eccpos[index + (num_steps * chip->ecc.bytes)] & (busw - 1)) |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 1966 | aligned_len++; |
| 1967 | |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 1968 | ret = nand_change_read_column_op(chip, |
| 1969 | mtd->writesize + aligned_pos, |
| 1970 | &chip->oob_poi[aligned_pos], |
| 1971 | aligned_len, false); |
| 1972 | if (ret) |
| 1973 | return ret; |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 1974 | } |
| 1975 | |
| 1976 | for (i = 0; i < eccfrag_len; i++) |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 1977 | chip->buffers->ecccode[i] = chip->oob_poi[eccpos[i + index]]; |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 1978 | |
| 1979 | p = bufpoi + data_col_addr; |
| 1980 | for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) { |
| 1981 | int stat; |
| 1982 | |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 1983 | stat = chip->ecc.correct(mtd, p, |
| 1984 | &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]); |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 1985 | if (stat == -EBADMSG && |
| 1986 | (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) { |
| 1987 | /* check for empty pages with bitflips */ |
| 1988 | stat = nand_check_erased_ecc_chunk(p, chip->ecc.size, |
| 1989 | &chip->buffers->ecccode[i], |
| 1990 | chip->ecc.bytes, |
| 1991 | NULL, 0, |
| 1992 | chip->ecc.strength); |
| 1993 | } |
| 1994 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1995 | if (stat < 0) { |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 1996 | mtd->ecc_stats.failed++; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1997 | } else { |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 1998 | mtd->ecc_stats.corrected += stat; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1999 | max_bitflips = max_t(unsigned int, max_bitflips, stat); |
| 2000 | } |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 2001 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2002 | return max_bitflips; |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 2003 | } |
| 2004 | |
| 2005 | /** |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2006 | * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function |
| 2007 | * @mtd: mtd info structure |
| 2008 | * @chip: nand chip info structure |
| 2009 | * @buf: buffer to store read data |
| 2010 | * @oob_required: caller requires OOB data read to chip->oob_poi |
| 2011 | * @page: page number to read |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2012 | * |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2013 | * Not for syndrome calculating ECC controllers which need a special oob layout. |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2014 | */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2015 | static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip, |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2016 | uint8_t *buf, int oob_required, int page) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2017 | { |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2018 | int i, eccsize = chip->ecc.size; |
| 2019 | int eccbytes = chip->ecc.bytes; |
| 2020 | int eccsteps = chip->ecc.steps; |
| 2021 | uint8_t *p = buf; |
| 2022 | uint8_t *ecc_calc = chip->buffers->ecccalc; |
| 2023 | uint8_t *ecc_code = chip->buffers->ecccode; |
| 2024 | uint32_t *eccpos = chip->ecc.layout->eccpos; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2025 | unsigned int max_bitflips = 0; |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2026 | int ret; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2027 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2028 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 2029 | chip->ecc.hwctl(mtd, NAND_ECC_READ); |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2030 | |
| 2031 | ret = nand_read_data_op(chip, p, eccsize, false); |
| 2032 | if (ret) |
| 2033 | return ret; |
| 2034 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2035 | chip->ecc.calculate(mtd, p, &ecc_calc[i]); |
| 2036 | } |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2037 | |
| 2038 | ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize, false); |
| 2039 | if (ret) |
| 2040 | return ret; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2041 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2042 | for (i = 0; i < chip->ecc.total; i++) |
| 2043 | ecc_code[i] = chip->oob_poi[eccpos[i]]; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2044 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2045 | eccsteps = chip->ecc.steps; |
| 2046 | p = buf; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2047 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2048 | for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 2049 | int stat; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2050 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2051 | stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]); |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 2052 | if (stat == -EBADMSG && |
| 2053 | (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) { |
| 2054 | /* check for empty pages with bitflips */ |
| 2055 | stat = nand_check_erased_ecc_chunk(p, eccsize, |
| 2056 | &ecc_code[i], eccbytes, |
| 2057 | NULL, 0, |
| 2058 | chip->ecc.strength); |
| 2059 | } |
| 2060 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2061 | if (stat < 0) { |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2062 | mtd->ecc_stats.failed++; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2063 | } else { |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2064 | mtd->ecc_stats.corrected += stat; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2065 | max_bitflips = max_t(unsigned int, max_bitflips, stat); |
| 2066 | } |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2067 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2068 | return max_bitflips; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2069 | } |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 2070 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2071 | /** |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2072 | * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first |
| 2073 | * @mtd: mtd info structure |
| 2074 | * @chip: nand chip info structure |
| 2075 | * @buf: buffer to store read data |
| 2076 | * @oob_required: caller requires OOB data read to chip->oob_poi |
| 2077 | * @page: page number to read |
Sandeep Paulraj | dea4070 | 2009-08-10 13:27:56 -0400 | [diff] [blame] | 2078 | * |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2079 | * Hardware ECC for large page chips, require OOB to be read first. For this |
| 2080 | * ECC mode, the write_page method is re-used from ECC_HW. These methods |
| 2081 | * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with |
| 2082 | * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from |
| 2083 | * the data area, by overwriting the NAND manufacturer bad block markings. |
Sandeep Paulraj | dea4070 | 2009-08-10 13:27:56 -0400 | [diff] [blame] | 2084 | */ |
| 2085 | static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd, |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2086 | struct nand_chip *chip, uint8_t *buf, int oob_required, int page) |
Sandeep Paulraj | dea4070 | 2009-08-10 13:27:56 -0400 | [diff] [blame] | 2087 | { |
| 2088 | int i, eccsize = chip->ecc.size; |
| 2089 | int eccbytes = chip->ecc.bytes; |
| 2090 | int eccsteps = chip->ecc.steps; |
| 2091 | uint8_t *p = buf; |
| 2092 | uint8_t *ecc_code = chip->buffers->ecccode; |
| 2093 | uint32_t *eccpos = chip->ecc.layout->eccpos; |
| 2094 | uint8_t *ecc_calc = chip->buffers->ecccalc; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2095 | unsigned int max_bitflips = 0; |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2096 | int ret; |
Sandeep Paulraj | dea4070 | 2009-08-10 13:27:56 -0400 | [diff] [blame] | 2097 | |
| 2098 | /* Read the OOB area first */ |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2099 | ret = nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize); |
| 2100 | if (ret) |
| 2101 | return ret; |
| 2102 | |
| 2103 | ret = nand_read_page_op(chip, page, 0, NULL, 0); |
| 2104 | if (ret) |
| 2105 | return ret; |
Sandeep Paulraj | dea4070 | 2009-08-10 13:27:56 -0400 | [diff] [blame] | 2106 | |
| 2107 | for (i = 0; i < chip->ecc.total; i++) |
| 2108 | ecc_code[i] = chip->oob_poi[eccpos[i]]; |
| 2109 | |
| 2110 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 2111 | int stat; |
| 2112 | |
| 2113 | chip->ecc.hwctl(mtd, NAND_ECC_READ); |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2114 | |
| 2115 | ret = nand_read_data_op(chip, p, eccsize, false); |
| 2116 | if (ret) |
| 2117 | return ret; |
| 2118 | |
Sandeep Paulraj | dea4070 | 2009-08-10 13:27:56 -0400 | [diff] [blame] | 2119 | chip->ecc.calculate(mtd, p, &ecc_calc[i]); |
| 2120 | |
| 2121 | stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL); |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 2122 | if (stat == -EBADMSG && |
| 2123 | (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) { |
| 2124 | /* check for empty pages with bitflips */ |
| 2125 | stat = nand_check_erased_ecc_chunk(p, eccsize, |
| 2126 | &ecc_code[i], eccbytes, |
| 2127 | NULL, 0, |
| 2128 | chip->ecc.strength); |
| 2129 | } |
| 2130 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2131 | if (stat < 0) { |
Sandeep Paulraj | dea4070 | 2009-08-10 13:27:56 -0400 | [diff] [blame] | 2132 | mtd->ecc_stats.failed++; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2133 | } else { |
Sandeep Paulraj | dea4070 | 2009-08-10 13:27:56 -0400 | [diff] [blame] | 2134 | mtd->ecc_stats.corrected += stat; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2135 | max_bitflips = max_t(unsigned int, max_bitflips, stat); |
| 2136 | } |
Sandeep Paulraj | dea4070 | 2009-08-10 13:27:56 -0400 | [diff] [blame] | 2137 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2138 | return max_bitflips; |
Sandeep Paulraj | dea4070 | 2009-08-10 13:27:56 -0400 | [diff] [blame] | 2139 | } |
| 2140 | |
| 2141 | /** |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2142 | * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read |
| 2143 | * @mtd: mtd info structure |
| 2144 | * @chip: nand chip info structure |
| 2145 | * @buf: buffer to store read data |
| 2146 | * @oob_required: caller requires OOB data read to chip->oob_poi |
| 2147 | * @page: page number to read |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2148 | * |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2149 | * The hw generator calculates the error syndrome automatically. Therefore we |
| 2150 | * need a special oob layout and handling. |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2151 | */ |
| 2152 | static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip, |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2153 | uint8_t *buf, int oob_required, int page) |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2154 | { |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2155 | int ret, i, eccsize = chip->ecc.size; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2156 | int eccbytes = chip->ecc.bytes; |
| 2157 | int eccsteps = chip->ecc.steps; |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 2158 | int eccpadbytes = eccbytes + chip->ecc.prepad + chip->ecc.postpad; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2159 | uint8_t *p = buf; |
| 2160 | uint8_t *oob = chip->oob_poi; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2161 | unsigned int max_bitflips = 0; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2162 | |
| 2163 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 2164 | int stat; |
| 2165 | |
| 2166 | chip->ecc.hwctl(mtd, NAND_ECC_READ); |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2167 | |
| 2168 | ret = nand_read_data_op(chip, p, eccsize, false); |
| 2169 | if (ret) |
| 2170 | return ret; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2171 | |
| 2172 | if (chip->ecc.prepad) { |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2173 | ret = nand_read_data_op(chip, oob, chip->ecc.prepad, |
| 2174 | false); |
| 2175 | if (ret) |
| 2176 | return ret; |
| 2177 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2178 | oob += chip->ecc.prepad; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2179 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2180 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2181 | chip->ecc.hwctl(mtd, NAND_ECC_READSYN); |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2182 | |
| 2183 | ret = nand_read_data_op(chip, oob, eccbytes, false); |
| 2184 | if (ret) |
| 2185 | return ret; |
| 2186 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2187 | stat = chip->ecc.correct(mtd, p, oob, NULL); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2188 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2189 | oob += eccbytes; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 2190 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2191 | if (chip->ecc.postpad) { |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2192 | ret = nand_read_data_op(chip, oob, chip->ecc.postpad, |
| 2193 | false); |
| 2194 | if (ret) |
| 2195 | return ret; |
| 2196 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2197 | oob += chip->ecc.postpad; |
| 2198 | } |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 2199 | |
| 2200 | if (stat == -EBADMSG && |
| 2201 | (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) { |
| 2202 | /* check for empty pages with bitflips */ |
| 2203 | stat = nand_check_erased_ecc_chunk(p, chip->ecc.size, |
| 2204 | oob - eccpadbytes, |
| 2205 | eccpadbytes, |
| 2206 | NULL, 0, |
| 2207 | chip->ecc.strength); |
| 2208 | } |
| 2209 | |
| 2210 | if (stat < 0) { |
| 2211 | mtd->ecc_stats.failed++; |
| 2212 | } else { |
| 2213 | mtd->ecc_stats.corrected += stat; |
| 2214 | max_bitflips = max_t(unsigned int, max_bitflips, stat); |
| 2215 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2216 | } |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2217 | |
| 2218 | /* Calculate remaining oob bytes */ |
| 2219 | i = mtd->oobsize - (oob - chip->oob_poi); |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2220 | if (i) { |
| 2221 | ret = nand_read_data_op(chip, oob, i, false); |
| 2222 | if (ret) |
| 2223 | return ret; |
| 2224 | } |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2225 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2226 | return max_bitflips; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2227 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2228 | |
| 2229 | /** |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2230 | * nand_transfer_oob - [INTERN] Transfer oob to client buffer |
| 2231 | * @chip: nand chip structure |
| 2232 | * @oob: oob destination address |
| 2233 | * @ops: oob ops structure |
| 2234 | * @len: size of oob to transfer |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2235 | */ |
| 2236 | static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob, |
| 2237 | struct mtd_oob_ops *ops, size_t len) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2238 | { |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 2239 | switch (ops->mode) { |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2240 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2241 | case MTD_OPS_PLACE_OOB: |
| 2242 | case MTD_OPS_RAW: |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2243 | memcpy(oob, chip->oob_poi + ops->ooboffs, len); |
| 2244 | return oob + len; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2245 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2246 | case MTD_OPS_AUTO_OOB: { |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2247 | struct nand_oobfree *free = chip->ecc.layout->oobfree; |
| 2248 | uint32_t boffs = 0, roffs = ops->ooboffs; |
| 2249 | size_t bytes = 0; |
| 2250 | |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 2251 | for (; free->length && len; free++, len -= bytes) { |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2252 | /* Read request not from offset 0? */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2253 | if (unlikely(roffs)) { |
| 2254 | if (roffs >= free->length) { |
| 2255 | roffs -= free->length; |
| 2256 | continue; |
| 2257 | } |
| 2258 | boffs = free->offset + roffs; |
| 2259 | bytes = min_t(size_t, len, |
| 2260 | (free->length - roffs)); |
| 2261 | roffs = 0; |
| 2262 | } else { |
| 2263 | bytes = min_t(size_t, len, free->length); |
| 2264 | boffs = free->offset; |
| 2265 | } |
| 2266 | memcpy(oob, chip->oob_poi + boffs, bytes); |
| 2267 | oob += bytes; |
| 2268 | } |
| 2269 | return oob; |
| 2270 | } |
| 2271 | default: |
| 2272 | BUG(); |
| 2273 | } |
| 2274 | return NULL; |
| 2275 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2276 | |
| 2277 | /** |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2278 | * nand_setup_read_retry - [INTERN] Set the READ RETRY mode |
| 2279 | * @mtd: MTD device structure |
| 2280 | * @retry_mode: the retry mode to use |
| 2281 | * |
| 2282 | * Some vendors supply a special command to shift the Vt threshold, to be used |
| 2283 | * when there are too many bitflips in a page (i.e., ECC error). After setting |
| 2284 | * a new threshold, the host should retry reading the page. |
| 2285 | */ |
| 2286 | static int nand_setup_read_retry(struct mtd_info *mtd, int retry_mode) |
| 2287 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 2288 | struct nand_chip *chip = mtd_to_nand(mtd); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2289 | |
| 2290 | pr_debug("setting READ RETRY mode %d\n", retry_mode); |
| 2291 | |
| 2292 | if (retry_mode >= chip->read_retries) |
| 2293 | return -EINVAL; |
| 2294 | |
| 2295 | if (!chip->setup_read_retry) |
| 2296 | return -EOPNOTSUPP; |
| 2297 | |
| 2298 | return chip->setup_read_retry(mtd, retry_mode); |
| 2299 | } |
| 2300 | |
| 2301 | /** |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2302 | * nand_do_read_ops - [INTERN] Read data with ECC |
| 2303 | * @mtd: MTD device structure |
| 2304 | * @from: offset to read from |
| 2305 | * @ops: oob ops structure |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2306 | * |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2307 | * Internal function. Called with chip held. |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2308 | */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2309 | static int nand_do_read_ops(struct mtd_info *mtd, loff_t from, |
| 2310 | struct mtd_oob_ops *ops) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2311 | { |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2312 | int chipnr, page, realpage, col, bytes, aligned, oob_required; |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 2313 | struct nand_chip *chip = mtd_to_nand(mtd); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2314 | int ret = 0; |
| 2315 | uint32_t readlen = ops->len; |
| 2316 | uint32_t oobreadlen = ops->ooblen; |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 2317 | uint32_t max_oobsize = mtd_oobavail(mtd, ops); |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 2318 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2319 | uint8_t *bufpoi, *oob, *buf; |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 2320 | int use_bufpoi; |
Paul Burton | 700a76c | 2013-09-04 15:16:56 +0100 | [diff] [blame] | 2321 | unsigned int max_bitflips = 0; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2322 | int retry_mode = 0; |
| 2323 | bool ecc_fail = false; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2324 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2325 | chipnr = (int)(from >> chip->chip_shift); |
| 2326 | chip->select_chip(mtd, chipnr); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2327 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2328 | realpage = (int)(from >> chip->page_shift); |
| 2329 | page = realpage & chip->pagemask; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2330 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2331 | col = (int)(from & (mtd->writesize - 1)); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2332 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2333 | buf = ops->datbuf; |
| 2334 | oob = ops->oobbuf; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2335 | oob_required = oob ? 1 : 0; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 2336 | |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 2337 | while (1) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2338 | unsigned int ecc_failures = mtd->ecc_stats.failed; |
Scott Wood | ea95b64 | 2011-02-02 18:15:57 -0600 | [diff] [blame] | 2339 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2340 | WATCHDOG_RESET(); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2341 | bytes = min(mtd->writesize - col, readlen); |
| 2342 | aligned = (bytes == mtd->writesize); |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 2343 | |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 2344 | if (!aligned) |
| 2345 | use_bufpoi = 1; |
Masahiro Yamada | b9c07b6 | 2017-11-22 02:38:27 +0900 | [diff] [blame] | 2346 | else if (chip->options & NAND_USE_BOUNCE_BUFFER) |
| 2347 | use_bufpoi = !IS_ALIGNED((unsigned long)buf, |
| 2348 | chip->buf_align); |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 2349 | else |
| 2350 | use_bufpoi = 0; |
| 2351 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2352 | /* Is the current page in the buffer? */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2353 | if (realpage != chip->pagebuf || oob) { |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 2354 | bufpoi = use_bufpoi ? chip->buffers->databuf : buf; |
| 2355 | |
| 2356 | if (use_bufpoi && aligned) |
| 2357 | pr_debug("%s: using read bounce buffer for buf@%p\n", |
| 2358 | __func__, buf); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2359 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2360 | read_retry: |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2361 | if (nand_standard_page_accessors(&chip->ecc)) { |
| 2362 | ret = nand_read_page_op(chip, page, 0, NULL, 0); |
| 2363 | if (ret) |
| 2364 | break; |
| 2365 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2366 | |
Paul Burton | 700a76c | 2013-09-04 15:16:56 +0100 | [diff] [blame] | 2367 | /* |
| 2368 | * Now read the page into the buffer. Absent an error, |
| 2369 | * the read methods return max bitflips per ecc step. |
| 2370 | */ |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2371 | if (unlikely(ops->mode == MTD_OPS_RAW)) |
| 2372 | ret = chip->ecc.read_page_raw(mtd, chip, bufpoi, |
| 2373 | oob_required, |
| 2374 | page); |
Joe Hershberger | 7a38ffa | 2012-11-05 06:46:31 +0000 | [diff] [blame] | 2375 | else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) && |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2376 | !oob) |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 2377 | ret = chip->ecc.read_subpage(mtd, chip, |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 2378 | col, bytes, bufpoi, |
| 2379 | page); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2380 | else |
Sandeep Paulraj | 883189e | 2009-08-10 13:27:46 -0400 | [diff] [blame] | 2381 | ret = chip->ecc.read_page(mtd, chip, bufpoi, |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2382 | oob_required, page); |
| 2383 | if (ret < 0) { |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 2384 | if (use_bufpoi) |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2385 | /* Invalidate page cache */ |
| 2386 | chip->pagebuf = -1; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2387 | break; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2388 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2389 | |
Paul Burton | 700a76c | 2013-09-04 15:16:56 +0100 | [diff] [blame] | 2390 | max_bitflips = max_t(unsigned int, max_bitflips, ret); |
| 2391 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2392 | /* Transfer not aligned data */ |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 2393 | if (use_bufpoi) { |
Joe Hershberger | 7a38ffa | 2012-11-05 06:46:31 +0000 | [diff] [blame] | 2394 | if (!NAND_HAS_SUBPAGE_READ(chip) && !oob && |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2395 | !(mtd->ecc_stats.failed - ecc_failures) && |
Paul Burton | 700a76c | 2013-09-04 15:16:56 +0100 | [diff] [blame] | 2396 | (ops->mode != MTD_OPS_RAW)) { |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 2397 | chip->pagebuf = realpage; |
Paul Burton | 700a76c | 2013-09-04 15:16:56 +0100 | [diff] [blame] | 2398 | chip->pagebuf_bitflips = ret; |
| 2399 | } else { |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2400 | /* Invalidate page cache */ |
| 2401 | chip->pagebuf = -1; |
Paul Burton | 700a76c | 2013-09-04 15:16:56 +0100 | [diff] [blame] | 2402 | } |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2403 | memcpy(buf, chip->buffers->databuf + col, bytes); |
| 2404 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2405 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2406 | if (unlikely(oob)) { |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 2407 | int toread = min(oobreadlen, max_oobsize); |
| 2408 | |
| 2409 | if (toread) { |
| 2410 | oob = nand_transfer_oob(chip, |
| 2411 | oob, ops, toread); |
| 2412 | oobreadlen -= toread; |
| 2413 | } |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2414 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2415 | |
| 2416 | if (chip->options & NAND_NEED_READRDY) { |
| 2417 | /* Apply delay or wait for ready/busy pin */ |
| 2418 | if (!chip->dev_ready) |
| 2419 | udelay(chip->chip_delay); |
| 2420 | else |
| 2421 | nand_wait_ready(mtd); |
| 2422 | } |
| 2423 | |
| 2424 | if (mtd->ecc_stats.failed - ecc_failures) { |
| 2425 | if (retry_mode + 1 < chip->read_retries) { |
| 2426 | retry_mode++; |
| 2427 | ret = nand_setup_read_retry(mtd, |
| 2428 | retry_mode); |
| 2429 | if (ret < 0) |
| 2430 | break; |
| 2431 | |
| 2432 | /* Reset failures; retry */ |
| 2433 | mtd->ecc_stats.failed = ecc_failures; |
| 2434 | goto read_retry; |
| 2435 | } else { |
| 2436 | /* No more retry modes; real failure */ |
| 2437 | ecc_fail = true; |
| 2438 | } |
| 2439 | } |
| 2440 | |
| 2441 | buf += bytes; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2442 | } else { |
| 2443 | memcpy(buf, chip->buffers->databuf + col, bytes); |
| 2444 | buf += bytes; |
Paul Burton | 700a76c | 2013-09-04 15:16:56 +0100 | [diff] [blame] | 2445 | max_bitflips = max_t(unsigned int, max_bitflips, |
| 2446 | chip->pagebuf_bitflips); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2447 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2448 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2449 | readlen -= bytes; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 2450 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2451 | /* Reset to retry mode 0 */ |
| 2452 | if (retry_mode) { |
| 2453 | ret = nand_setup_read_retry(mtd, 0); |
| 2454 | if (ret < 0) |
| 2455 | break; |
| 2456 | retry_mode = 0; |
| 2457 | } |
| 2458 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2459 | if (!readlen) |
| 2460 | break; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 2461 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2462 | /* For subsequent reads align to page boundary */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2463 | col = 0; |
| 2464 | /* Increment page address */ |
| 2465 | realpage++; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2466 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2467 | page = realpage & chip->pagemask; |
| 2468 | /* Check, if we cross a chip boundary */ |
| 2469 | if (!page) { |
| 2470 | chipnr++; |
| 2471 | chip->select_chip(mtd, -1); |
| 2472 | chip->select_chip(mtd, chipnr); |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 2473 | } |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2474 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2475 | chip->select_chip(mtd, -1); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2476 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2477 | ops->retlen = ops->len - (size_t) readlen; |
| 2478 | if (oob) |
| 2479 | ops->oobretlen = ops->ooblen - oobreadlen; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 2480 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2481 | if (ret < 0) |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2482 | return ret; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 2483 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2484 | if (ecc_fail) |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2485 | return -EBADMSG; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2486 | |
Paul Burton | 700a76c | 2013-09-04 15:16:56 +0100 | [diff] [blame] | 2487 | return max_bitflips; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2488 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2489 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2490 | /** |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2491 | * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function |
| 2492 | * @mtd: mtd info structure |
| 2493 | * @chip: nand chip info structure |
| 2494 | * @page: page number to read |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2495 | */ |
| 2496 | static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip, |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2497 | int page) |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2498 | { |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2499 | return nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2500 | } |
| 2501 | |
| 2502 | /** |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2503 | * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2504 | * with syndromes |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2505 | * @mtd: mtd info structure |
| 2506 | * @chip: nand chip info structure |
| 2507 | * @page: page number to read |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2508 | */ |
| 2509 | static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip, |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2510 | int page) |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2511 | { |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2512 | int length = mtd->oobsize; |
| 2513 | int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad; |
| 2514 | int eccsize = chip->ecc.size; |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 2515 | uint8_t *bufpoi = chip->oob_poi; |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2516 | int i, toread, sndrnd = 0, pos, ret; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2517 | |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2518 | ret = nand_read_page_op(chip, page, chip->ecc.size, NULL, 0); |
| 2519 | if (ret) |
| 2520 | return ret; |
| 2521 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2522 | for (i = 0; i < chip->ecc.steps; i++) { |
| 2523 | if (sndrnd) { |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2524 | int ret; |
| 2525 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2526 | pos = eccsize + i * (eccsize + chunk); |
| 2527 | if (mtd->writesize > 512) |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2528 | ret = nand_change_read_column_op(chip, pos, |
| 2529 | NULL, 0, |
| 2530 | false); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2531 | else |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2532 | ret = nand_read_page_op(chip, page, pos, NULL, |
| 2533 | 0); |
| 2534 | |
| 2535 | if (ret) |
| 2536 | return ret; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 2537 | } else |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2538 | sndrnd = 1; |
| 2539 | toread = min_t(int, length, chunk); |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2540 | |
| 2541 | ret = nand_read_data_op(chip, bufpoi, toread, false); |
| 2542 | if (ret) |
| 2543 | return ret; |
| 2544 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2545 | bufpoi += toread; |
| 2546 | length -= toread; |
| 2547 | } |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2548 | if (length > 0) { |
| 2549 | ret = nand_read_data_op(chip, bufpoi, length, false); |
| 2550 | if (ret) |
| 2551 | return ret; |
| 2552 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2553 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2554 | return 0; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2555 | } |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 2556 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2557 | /** |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2558 | * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function |
| 2559 | * @mtd: mtd info structure |
| 2560 | * @chip: nand chip info structure |
| 2561 | * @page: page number to write |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2562 | */ |
| 2563 | static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip, |
| 2564 | int page) |
| 2565 | { |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2566 | return nand_prog_page_op(chip, page, mtd->writesize, chip->oob_poi, |
| 2567 | mtd->oobsize); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2568 | } |
| 2569 | |
| 2570 | /** |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2571 | * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC |
| 2572 | * with syndrome - only for large page flash |
| 2573 | * @mtd: mtd info structure |
| 2574 | * @chip: nand chip info structure |
| 2575 | * @page: page number to write |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2576 | */ |
| 2577 | static int nand_write_oob_syndrome(struct mtd_info *mtd, |
| 2578 | struct nand_chip *chip, int page) |
| 2579 | { |
| 2580 | int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad; |
| 2581 | int eccsize = chip->ecc.size, length = mtd->oobsize; |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2582 | int ret, i, len, pos, sndcmd = 0, steps = chip->ecc.steps; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2583 | const uint8_t *bufpoi = chip->oob_poi; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2584 | |
| 2585 | /* |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2586 | * data-ecc-data-ecc ... ecc-oob |
| 2587 | * or |
| 2588 | * data-pad-ecc-pad-data-pad .... ecc-pad-oob |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2589 | */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2590 | if (!chip->ecc.prepad && !chip->ecc.postpad) { |
| 2591 | pos = steps * (eccsize + chunk); |
| 2592 | steps = 0; |
| 2593 | } else |
| 2594 | pos = eccsize; |
| 2595 | |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2596 | ret = nand_prog_page_begin_op(chip, page, pos, NULL, 0); |
| 2597 | if (ret) |
| 2598 | return ret; |
| 2599 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2600 | for (i = 0; i < steps; i++) { |
| 2601 | if (sndcmd) { |
| 2602 | if (mtd->writesize <= 512) { |
| 2603 | uint32_t fill = 0xFFFFFFFF; |
| 2604 | |
| 2605 | len = eccsize; |
| 2606 | while (len > 0) { |
| 2607 | int num = min_t(int, len, 4); |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2608 | |
| 2609 | ret = nand_write_data_op(chip, &fill, |
| 2610 | num, false); |
| 2611 | if (ret) |
| 2612 | return ret; |
| 2613 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2614 | len -= num; |
| 2615 | } |
| 2616 | } else { |
| 2617 | pos = eccsize + i * (eccsize + chunk); |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2618 | ret = nand_change_write_column_op(chip, pos, |
| 2619 | NULL, 0, |
| 2620 | false); |
| 2621 | if (ret) |
| 2622 | return ret; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2623 | } |
| 2624 | } else |
| 2625 | sndcmd = 1; |
| 2626 | len = min_t(int, length, chunk); |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2627 | |
| 2628 | ret = nand_write_data_op(chip, bufpoi, len, false); |
| 2629 | if (ret) |
| 2630 | return ret; |
| 2631 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2632 | bufpoi += len; |
| 2633 | length -= len; |
| 2634 | } |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2635 | if (length > 0) { |
| 2636 | ret = nand_write_data_op(chip, bufpoi, length, false); |
| 2637 | if (ret) |
| 2638 | return ret; |
| 2639 | } |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2640 | |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2641 | return nand_prog_page_end_op(chip); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2642 | } |
| 2643 | |
| 2644 | /** |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2645 | * nand_do_read_oob - [INTERN] NAND read out-of-band |
| 2646 | * @mtd: MTD device structure |
| 2647 | * @from: offset to read from |
| 2648 | * @ops: oob operations description structure |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2649 | * |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2650 | * NAND read out-of-band data from the spare area. |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2651 | */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2652 | static int nand_do_read_oob(struct mtd_info *mtd, loff_t from, |
| 2653 | struct mtd_oob_ops *ops) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2654 | { |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2655 | int page, realpage, chipnr; |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 2656 | struct nand_chip *chip = mtd_to_nand(mtd); |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2657 | struct mtd_ecc_stats stats; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2658 | int readlen = ops->ooblen; |
| 2659 | int len; |
| 2660 | uint8_t *buf = ops->oobbuf; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2661 | int ret = 0; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2662 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2663 | pr_debug("%s: from = 0x%08Lx, len = %i\n", |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 2664 | __func__, (unsigned long long)from, readlen); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2665 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2666 | stats = mtd->ecc_stats; |
| 2667 | |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 2668 | len = mtd_oobavail(mtd, ops); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2669 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2670 | if (unlikely(ops->ooboffs >= len)) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2671 | pr_debug("%s: attempt to start read outside oob\n", |
| 2672 | __func__); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2673 | return -EINVAL; |
| 2674 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2675 | |
| 2676 | /* Do not allow reads past end of device */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2677 | if (unlikely(from >= mtd->size || |
| 2678 | ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) - |
| 2679 | (from >> chip->page_shift)) * len)) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2680 | pr_debug("%s: attempt to read beyond end of device\n", |
| 2681 | __func__); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2682 | return -EINVAL; |
| 2683 | } |
| 2684 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2685 | chipnr = (int)(from >> chip->chip_shift); |
| 2686 | chip->select_chip(mtd, chipnr); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2687 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2688 | /* Shift to get page */ |
| 2689 | realpage = (int)(from >> chip->page_shift); |
| 2690 | page = realpage & chip->pagemask; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2691 | |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 2692 | while (1) { |
Scott Wood | ea95b64 | 2011-02-02 18:15:57 -0600 | [diff] [blame] | 2693 | WATCHDOG_RESET(); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2694 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2695 | if (ops->mode == MTD_OPS_RAW) |
| 2696 | ret = chip->ecc.read_oob_raw(mtd, chip, page); |
| 2697 | else |
| 2698 | ret = chip->ecc.read_oob(mtd, chip, page); |
| 2699 | |
| 2700 | if (ret < 0) |
| 2701 | break; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 2702 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2703 | len = min(len, readlen); |
| 2704 | buf = nand_transfer_oob(chip, buf, ops, len); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2705 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2706 | if (chip->options & NAND_NEED_READRDY) { |
| 2707 | /* Apply delay or wait for ready/busy pin */ |
| 2708 | if (!chip->dev_ready) |
| 2709 | udelay(chip->chip_delay); |
| 2710 | else |
| 2711 | nand_wait_ready(mtd); |
| 2712 | } |
| 2713 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2714 | readlen -= len; |
| 2715 | if (!readlen) |
| 2716 | break; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 2717 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2718 | /* Increment page address */ |
| 2719 | realpage++; |
| 2720 | |
| 2721 | page = realpage & chip->pagemask; |
| 2722 | /* Check, if we cross a chip boundary */ |
| 2723 | if (!page) { |
| 2724 | chipnr++; |
| 2725 | chip->select_chip(mtd, -1); |
| 2726 | chip->select_chip(mtd, chipnr); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2727 | } |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2728 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2729 | chip->select_chip(mtd, -1); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2730 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2731 | ops->oobretlen = ops->ooblen - readlen; |
| 2732 | |
| 2733 | if (ret < 0) |
| 2734 | return ret; |
| 2735 | |
| 2736 | if (mtd->ecc_stats.failed - stats.failed) |
| 2737 | return -EBADMSG; |
| 2738 | |
| 2739 | return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2740 | } |
| 2741 | |
| 2742 | /** |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2743 | * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2744 | * @mtd: MTD device structure |
| 2745 | * @from: offset to read from |
| 2746 | * @ops: oob operation description structure |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2747 | * |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2748 | * NAND read data and/or out-of-band data. |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2749 | */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2750 | static int nand_read_oob(struct mtd_info *mtd, loff_t from, |
| 2751 | struct mtd_oob_ops *ops) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2752 | { |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2753 | int ret = -ENOTSUPP; |
| 2754 | |
| 2755 | ops->retlen = 0; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2756 | |
| 2757 | /* Do not allow reads past end of device */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2758 | if (ops->datbuf && (from + ops->len) > mtd->size) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2759 | pr_debug("%s: attempt to read beyond end of device\n", |
| 2760 | __func__); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2761 | return -EINVAL; |
| 2762 | } |
| 2763 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2764 | nand_get_device(mtd, FL_READING); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2765 | |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 2766 | switch (ops->mode) { |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2767 | case MTD_OPS_PLACE_OOB: |
| 2768 | case MTD_OPS_AUTO_OOB: |
| 2769 | case MTD_OPS_RAW: |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2770 | break; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 2771 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2772 | default: |
| 2773 | goto out; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2774 | } |
| 2775 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2776 | if (!ops->datbuf) |
| 2777 | ret = nand_do_read_oob(mtd, from, ops); |
| 2778 | else |
| 2779 | ret = nand_do_read_ops(mtd, from, ops); |
| 2780 | |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 2781 | out: |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2782 | nand_release_device(mtd); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2783 | return ret; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2784 | } |
| 2785 | |
| 2786 | |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 2787 | /** |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2788 | * nand_write_page_raw - [INTERN] raw page write function |
| 2789 | * @mtd: mtd info structure |
| 2790 | * @chip: nand chip info structure |
| 2791 | * @buf: data buffer |
| 2792 | * @oob_required: must write chip->oob_poi to OOB |
Scott Wood | 46e1310 | 2016-05-30 13:57:57 -0500 | [diff] [blame] | 2793 | * @page: page number to write |
David Brownell | ee86b8d | 2009-11-07 16:27:01 -0500 | [diff] [blame] | 2794 | * |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2795 | * Not for syndrome calculating ECC controllers, which use a special oob layout. |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2796 | */ |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2797 | static int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip, |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 2798 | const uint8_t *buf, int oob_required, int page) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2799 | { |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2800 | int ret; |
| 2801 | |
| 2802 | ret = nand_write_data_op(chip, buf, mtd->writesize, false); |
| 2803 | if (ret) |
| 2804 | return ret; |
| 2805 | |
| 2806 | if (oob_required) { |
| 2807 | ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, |
| 2808 | false); |
| 2809 | if (ret) |
| 2810 | return ret; |
| 2811 | } |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2812 | |
| 2813 | return 0; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2814 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2815 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2816 | /** |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2817 | * nand_write_page_raw_syndrome - [INTERN] raw page write function |
| 2818 | * @mtd: mtd info structure |
| 2819 | * @chip: nand chip info structure |
| 2820 | * @buf: data buffer |
| 2821 | * @oob_required: must write chip->oob_poi to OOB |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 2822 | * @page: page number to write |
David Brownell | ee86b8d | 2009-11-07 16:27:01 -0500 | [diff] [blame] | 2823 | * |
| 2824 | * We need a special oob layout and handling even when ECC isn't checked. |
| 2825 | */ |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2826 | static int nand_write_page_raw_syndrome(struct mtd_info *mtd, |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 2827 | struct nand_chip *chip, |
Scott Wood | 46e1310 | 2016-05-30 13:57:57 -0500 | [diff] [blame] | 2828 | const uint8_t *buf, int oob_required, |
| 2829 | int page) |
David Brownell | ee86b8d | 2009-11-07 16:27:01 -0500 | [diff] [blame] | 2830 | { |
| 2831 | int eccsize = chip->ecc.size; |
| 2832 | int eccbytes = chip->ecc.bytes; |
| 2833 | uint8_t *oob = chip->oob_poi; |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2834 | int steps, size, ret; |
David Brownell | ee86b8d | 2009-11-07 16:27:01 -0500 | [diff] [blame] | 2835 | |
| 2836 | for (steps = chip->ecc.steps; steps > 0; steps--) { |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2837 | ret = nand_write_data_op(chip, buf, eccsize, false); |
| 2838 | if (ret) |
| 2839 | return ret; |
| 2840 | |
David Brownell | ee86b8d | 2009-11-07 16:27:01 -0500 | [diff] [blame] | 2841 | buf += eccsize; |
| 2842 | |
| 2843 | if (chip->ecc.prepad) { |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2844 | ret = nand_write_data_op(chip, oob, chip->ecc.prepad, |
| 2845 | false); |
| 2846 | if (ret) |
| 2847 | return ret; |
| 2848 | |
David Brownell | ee86b8d | 2009-11-07 16:27:01 -0500 | [diff] [blame] | 2849 | oob += chip->ecc.prepad; |
| 2850 | } |
| 2851 | |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2852 | ret = nand_write_data_op(chip, oob, eccbytes, false); |
| 2853 | if (ret) |
| 2854 | return ret; |
| 2855 | |
David Brownell | ee86b8d | 2009-11-07 16:27:01 -0500 | [diff] [blame] | 2856 | oob += eccbytes; |
| 2857 | |
| 2858 | if (chip->ecc.postpad) { |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2859 | ret = nand_write_data_op(chip, oob, chip->ecc.postpad, |
| 2860 | false); |
| 2861 | if (ret) |
| 2862 | return ret; |
| 2863 | |
David Brownell | ee86b8d | 2009-11-07 16:27:01 -0500 | [diff] [blame] | 2864 | oob += chip->ecc.postpad; |
| 2865 | } |
| 2866 | } |
| 2867 | |
| 2868 | size = mtd->oobsize - (oob - chip->oob_poi); |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2869 | if (size) { |
| 2870 | ret = nand_write_data_op(chip, oob, size, false); |
| 2871 | if (ret) |
| 2872 | return ret; |
| 2873 | } |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2874 | |
| 2875 | return 0; |
David Brownell | ee86b8d | 2009-11-07 16:27:01 -0500 | [diff] [blame] | 2876 | } |
| 2877 | /** |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2878 | * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function |
| 2879 | * @mtd: mtd info structure |
| 2880 | * @chip: nand chip info structure |
| 2881 | * @buf: data buffer |
| 2882 | * @oob_required: must write chip->oob_poi to OOB |
Scott Wood | 46e1310 | 2016-05-30 13:57:57 -0500 | [diff] [blame] | 2883 | * @page: page number to write |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2884 | */ |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2885 | static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip, |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 2886 | const uint8_t *buf, int oob_required, |
| 2887 | int page) |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2888 | { |
| 2889 | int i, eccsize = chip->ecc.size; |
| 2890 | int eccbytes = chip->ecc.bytes; |
| 2891 | int eccsteps = chip->ecc.steps; |
| 2892 | uint8_t *ecc_calc = chip->buffers->ecccalc; |
| 2893 | const uint8_t *p = buf; |
| 2894 | uint32_t *eccpos = chip->ecc.layout->eccpos; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2895 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2896 | /* Software ECC calculation */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2897 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) |
| 2898 | chip->ecc.calculate(mtd, p, &ecc_calc[i]); |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 2899 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2900 | for (i = 0; i < chip->ecc.total; i++) |
| 2901 | chip->oob_poi[eccpos[i]] = ecc_calc[i]; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 2902 | |
Scott Wood | 46e1310 | 2016-05-30 13:57:57 -0500 | [diff] [blame] | 2903 | return chip->ecc.write_page_raw(mtd, chip, buf, 1, page); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2904 | } |
| 2905 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2906 | /** |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2907 | * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function |
| 2908 | * @mtd: mtd info structure |
| 2909 | * @chip: nand chip info structure |
| 2910 | * @buf: data buffer |
| 2911 | * @oob_required: must write chip->oob_poi to OOB |
Scott Wood | 46e1310 | 2016-05-30 13:57:57 -0500 | [diff] [blame] | 2912 | * @page: page number to write |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2913 | */ |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2914 | static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip, |
Scott Wood | 46e1310 | 2016-05-30 13:57:57 -0500 | [diff] [blame] | 2915 | const uint8_t *buf, int oob_required, |
| 2916 | int page) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2917 | { |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2918 | int i, eccsize = chip->ecc.size; |
| 2919 | int eccbytes = chip->ecc.bytes; |
| 2920 | int eccsteps = chip->ecc.steps; |
| 2921 | uint8_t *ecc_calc = chip->buffers->ecccalc; |
| 2922 | const uint8_t *p = buf; |
| 2923 | uint32_t *eccpos = chip->ecc.layout->eccpos; |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2924 | int ret; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2925 | |
| 2926 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 2927 | chip->ecc.hwctl(mtd, NAND_ECC_WRITE); |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2928 | |
| 2929 | ret = nand_write_data_op(chip, p, eccsize, false); |
| 2930 | if (ret) |
| 2931 | return ret; |
| 2932 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2933 | chip->ecc.calculate(mtd, p, &ecc_calc[i]); |
| 2934 | } |
| 2935 | |
| 2936 | for (i = 0; i < chip->ecc.total; i++) |
| 2937 | chip->oob_poi[eccpos[i]] = ecc_calc[i]; |
| 2938 | |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2939 | ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false); |
| 2940 | if (ret) |
| 2941 | return ret; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2942 | |
| 2943 | return 0; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2944 | } |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 2945 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2946 | |
| 2947 | /** |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 2948 | * nand_write_subpage_hwecc - [REPLACEABLE] hardware ECC based subpage write |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2949 | * @mtd: mtd info structure |
| 2950 | * @chip: nand chip info structure |
| 2951 | * @offset: column address of subpage within the page |
| 2952 | * @data_len: data length |
| 2953 | * @buf: data buffer |
| 2954 | * @oob_required: must write chip->oob_poi to OOB |
Scott Wood | 46e1310 | 2016-05-30 13:57:57 -0500 | [diff] [blame] | 2955 | * @page: page number to write |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2956 | */ |
| 2957 | static int nand_write_subpage_hwecc(struct mtd_info *mtd, |
| 2958 | struct nand_chip *chip, uint32_t offset, |
| 2959 | uint32_t data_len, const uint8_t *buf, |
Scott Wood | 46e1310 | 2016-05-30 13:57:57 -0500 | [diff] [blame] | 2960 | int oob_required, int page) |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2961 | { |
| 2962 | uint8_t *oob_buf = chip->oob_poi; |
| 2963 | uint8_t *ecc_calc = chip->buffers->ecccalc; |
| 2964 | int ecc_size = chip->ecc.size; |
| 2965 | int ecc_bytes = chip->ecc.bytes; |
| 2966 | int ecc_steps = chip->ecc.steps; |
| 2967 | uint32_t *eccpos = chip->ecc.layout->eccpos; |
| 2968 | uint32_t start_step = offset / ecc_size; |
| 2969 | uint32_t end_step = (offset + data_len - 1) / ecc_size; |
| 2970 | int oob_bytes = mtd->oobsize / ecc_steps; |
| 2971 | int step, i; |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2972 | int ret; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2973 | |
| 2974 | for (step = 0; step < ecc_steps; step++) { |
| 2975 | /* configure controller for WRITE access */ |
| 2976 | chip->ecc.hwctl(mtd, NAND_ECC_WRITE); |
| 2977 | |
| 2978 | /* write data (untouched subpages already masked by 0xFF) */ |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2979 | ret = nand_write_data_op(chip, buf, ecc_size, false); |
| 2980 | if (ret) |
| 2981 | return ret; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2982 | |
| 2983 | /* mask ECC of un-touched subpages by padding 0xFF */ |
| 2984 | if ((step < start_step) || (step > end_step)) |
| 2985 | memset(ecc_calc, 0xff, ecc_bytes); |
| 2986 | else |
| 2987 | chip->ecc.calculate(mtd, buf, ecc_calc); |
| 2988 | |
| 2989 | /* mask OOB of un-touched subpages by padding 0xFF */ |
| 2990 | /* if oob_required, preserve OOB metadata of written subpage */ |
| 2991 | if (!oob_required || (step < start_step) || (step > end_step)) |
| 2992 | memset(oob_buf, 0xff, oob_bytes); |
| 2993 | |
| 2994 | buf += ecc_size; |
| 2995 | ecc_calc += ecc_bytes; |
| 2996 | oob_buf += oob_bytes; |
| 2997 | } |
| 2998 | |
| 2999 | /* copy calculated ECC for whole page to chip->buffer->oob */ |
| 3000 | /* this include masked-value(0xFF) for unwritten subpages */ |
| 3001 | ecc_calc = chip->buffers->ecccalc; |
| 3002 | for (i = 0; i < chip->ecc.total; i++) |
| 3003 | chip->oob_poi[eccpos[i]] = ecc_calc[i]; |
| 3004 | |
| 3005 | /* write OOB buffer to NAND device */ |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 3006 | ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false); |
| 3007 | if (ret) |
| 3008 | return ret; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3009 | |
| 3010 | return 0; |
| 3011 | } |
| 3012 | |
| 3013 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3014 | /** |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3015 | * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write |
| 3016 | * @mtd: mtd info structure |
| 3017 | * @chip: nand chip info structure |
| 3018 | * @buf: data buffer |
| 3019 | * @oob_required: must write chip->oob_poi to OOB |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 3020 | * @page: page number to write |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3021 | * |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3022 | * The hw generator calculates the error syndrome automatically. Therefore we |
| 3023 | * need a special oob layout and handling. |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3024 | */ |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3025 | static int nand_write_page_syndrome(struct mtd_info *mtd, |
| 3026 | struct nand_chip *chip, |
Scott Wood | 46e1310 | 2016-05-30 13:57:57 -0500 | [diff] [blame] | 3027 | const uint8_t *buf, int oob_required, |
| 3028 | int page) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3029 | { |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3030 | int i, eccsize = chip->ecc.size; |
| 3031 | int eccbytes = chip->ecc.bytes; |
| 3032 | int eccsteps = chip->ecc.steps; |
| 3033 | const uint8_t *p = buf; |
| 3034 | uint8_t *oob = chip->oob_poi; |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 3035 | int ret; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3036 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3037 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3038 | chip->ecc.hwctl(mtd, NAND_ECC_WRITE); |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 3039 | |
| 3040 | ret = nand_write_data_op(chip, p, eccsize, false); |
| 3041 | if (ret) |
| 3042 | return ret; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3043 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3044 | if (chip->ecc.prepad) { |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 3045 | ret = nand_write_data_op(chip, oob, chip->ecc.prepad, |
| 3046 | false); |
| 3047 | if (ret) |
| 3048 | return ret; |
| 3049 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3050 | oob += chip->ecc.prepad; |
| 3051 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3052 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3053 | chip->ecc.calculate(mtd, p, oob); |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 3054 | |
| 3055 | ret = nand_write_data_op(chip, oob, eccbytes, false); |
| 3056 | if (ret) |
| 3057 | return ret; |
| 3058 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3059 | oob += eccbytes; |
| 3060 | |
| 3061 | if (chip->ecc.postpad) { |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 3062 | ret = nand_write_data_op(chip, oob, chip->ecc.postpad, |
| 3063 | false); |
| 3064 | if (ret) |
| 3065 | return ret; |
| 3066 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3067 | oob += chip->ecc.postpad; |
| 3068 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3069 | } |
| 3070 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3071 | /* Calculate remaining oob bytes */ |
| 3072 | i = mtd->oobsize - (oob - chip->oob_poi); |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 3073 | if (i) { |
| 3074 | ret = nand_write_data_op(chip, oob, i, false); |
| 3075 | if (ret) |
| 3076 | return ret; |
| 3077 | } |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3078 | |
| 3079 | return 0; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3080 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3081 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3082 | /** |
| 3083 | * nand_write_page - [REPLACEABLE] write one page |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3084 | * @mtd: MTD device structure |
| 3085 | * @chip: NAND chip descriptor |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3086 | * @offset: address offset within the page |
| 3087 | * @data_len: length of actual data to be written |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3088 | * @buf: the data to write |
| 3089 | * @oob_required: must write chip->oob_poi to OOB |
| 3090 | * @page: page number to write |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3091 | * @raw: use _raw version of write_page |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3092 | */ |
| 3093 | static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip, |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3094 | uint32_t offset, int data_len, const uint8_t *buf, |
Boris Brezillon | b9bf43c | 2017-11-22 02:38:24 +0900 | [diff] [blame] | 3095 | int oob_required, int page, int raw) |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3096 | { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3097 | int status, subpage; |
| 3098 | |
| 3099 | if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && |
| 3100 | chip->ecc.write_subpage) |
| 3101 | subpage = offset || (data_len < mtd->writesize); |
| 3102 | else |
| 3103 | subpage = 0; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3104 | |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 3105 | if (nand_standard_page_accessors(&chip->ecc)) { |
| 3106 | status = nand_prog_page_begin_op(chip, page, 0, NULL, 0); |
| 3107 | if (status) |
| 3108 | return status; |
| 3109 | } |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3110 | |
| 3111 | if (unlikely(raw)) |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3112 | status = chip->ecc.write_page_raw(mtd, chip, buf, |
Scott Wood | 46e1310 | 2016-05-30 13:57:57 -0500 | [diff] [blame] | 3113 | oob_required, page); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3114 | else if (subpage) |
| 3115 | status = chip->ecc.write_subpage(mtd, chip, offset, data_len, |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 3116 | buf, oob_required, page); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3117 | else |
Scott Wood | 46e1310 | 2016-05-30 13:57:57 -0500 | [diff] [blame] | 3118 | status = chip->ecc.write_page(mtd, chip, buf, oob_required, |
| 3119 | page); |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3120 | |
| 3121 | if (status < 0) |
| 3122 | return status; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3123 | |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 3124 | if (nand_standard_page_accessors(&chip->ecc)) |
| 3125 | return nand_prog_page_end_op(chip); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3126 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3127 | return 0; |
| 3128 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3129 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3130 | /** |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3131 | * nand_fill_oob - [INTERN] Transfer client buffer to oob |
| 3132 | * @mtd: MTD device structure |
| 3133 | * @oob: oob data buffer |
| 3134 | * @len: oob data write length |
| 3135 | * @ops: oob ops structure |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3136 | */ |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3137 | static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len, |
| 3138 | struct mtd_oob_ops *ops) |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3139 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 3140 | struct nand_chip *chip = mtd_to_nand(mtd); |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3141 | |
| 3142 | /* |
| 3143 | * Initialise to all 0xFF, to avoid the possibility of left over OOB |
| 3144 | * data from a previous OOB read. |
| 3145 | */ |
| 3146 | memset(chip->oob_poi, 0xff, mtd->oobsize); |
| 3147 | |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 3148 | switch (ops->mode) { |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 3149 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3150 | case MTD_OPS_PLACE_OOB: |
| 3151 | case MTD_OPS_RAW: |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3152 | memcpy(chip->oob_poi + ops->ooboffs, oob, len); |
| 3153 | return oob + len; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3154 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3155 | case MTD_OPS_AUTO_OOB: { |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3156 | struct nand_oobfree *free = chip->ecc.layout->oobfree; |
| 3157 | uint32_t boffs = 0, woffs = ops->ooboffs; |
| 3158 | size_t bytes = 0; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3159 | |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 3160 | for (; free->length && len; free++, len -= bytes) { |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3161 | /* Write request not from offset 0? */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3162 | if (unlikely(woffs)) { |
| 3163 | if (woffs >= free->length) { |
| 3164 | woffs -= free->length; |
| 3165 | continue; |
| 3166 | } |
| 3167 | boffs = free->offset + woffs; |
| 3168 | bytes = min_t(size_t, len, |
| 3169 | (free->length - woffs)); |
| 3170 | woffs = 0; |
| 3171 | } else { |
| 3172 | bytes = min_t(size_t, len, free->length); |
| 3173 | boffs = free->offset; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3174 | } |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3175 | memcpy(chip->oob_poi + boffs, oob, bytes); |
| 3176 | oob += bytes; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3177 | } |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3178 | return oob; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3179 | } |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3180 | default: |
| 3181 | BUG(); |
| 3182 | } |
| 3183 | return NULL; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3184 | } |
| 3185 | |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 3186 | #define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3187 | |
| 3188 | /** |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3189 | * nand_do_write_ops - [INTERN] NAND write with ECC |
| 3190 | * @mtd: MTD device structure |
| 3191 | * @to: offset to write to |
| 3192 | * @ops: oob operations description structure |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3193 | * |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3194 | * NAND write with ECC. |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3195 | */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3196 | static int nand_do_write_ops(struct mtd_info *mtd, loff_t to, |
| 3197 | struct mtd_oob_ops *ops) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3198 | { |
Boris Brezillon | b9bf43c | 2017-11-22 02:38:24 +0900 | [diff] [blame] | 3199 | int chipnr, realpage, page, column; |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 3200 | struct nand_chip *chip = mtd_to_nand(mtd); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3201 | uint32_t writelen = ops->len; |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 3202 | |
| 3203 | uint32_t oobwritelen = ops->ooblen; |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 3204 | uint32_t oobmaxlen = mtd_oobavail(mtd, ops); |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 3205 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3206 | uint8_t *oob = ops->oobbuf; |
| 3207 | uint8_t *buf = ops->datbuf; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3208 | int ret; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3209 | int oob_required = oob ? 1 : 0; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3210 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3211 | ops->retlen = 0; |
| 3212 | if (!writelen) |
| 3213 | return 0; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3214 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3215 | /* Reject writes, which are not page aligned */ |
| 3216 | if (NOTALIGNED(to)) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3217 | pr_notice("%s: attempt to write non page aligned data\n", |
| 3218 | __func__); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3219 | return -EINVAL; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3220 | } |
| 3221 | |
| 3222 | column = to & (mtd->writesize - 1); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3223 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3224 | chipnr = (int)(to >> chip->chip_shift); |
| 3225 | chip->select_chip(mtd, chipnr); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3226 | |
| 3227 | /* Check, if it is write protected */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3228 | if (nand_check_wp(mtd)) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3229 | ret = -EIO; |
| 3230 | goto err_out; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3231 | } |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 3232 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3233 | realpage = (int)(to >> chip->page_shift); |
| 3234 | page = realpage & chip->pagemask; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3235 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3236 | /* Invalidate the page cache, when we write to the cached page */ |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 3237 | if (to <= ((loff_t)chip->pagebuf << chip->page_shift) && |
| 3238 | ((loff_t)chip->pagebuf << chip->page_shift) < (to + ops->len)) |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3239 | chip->pagebuf = -1; |
| 3240 | |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 3241 | /* Don't allow multipage oob writes with offset */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3242 | if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) { |
| 3243 | ret = -EINVAL; |
| 3244 | goto err_out; |
| 3245 | } |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 3246 | |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 3247 | while (1) { |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3248 | int bytes = mtd->writesize; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3249 | uint8_t *wbuf = buf; |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 3250 | int use_bufpoi; |
Hector Palacios | e4fcdbb | 2016-07-18 09:37:41 +0200 | [diff] [blame] | 3251 | int part_pagewr = (column || writelen < mtd->writesize); |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 3252 | |
| 3253 | if (part_pagewr) |
| 3254 | use_bufpoi = 1; |
Masahiro Yamada | b9c07b6 | 2017-11-22 02:38:27 +0900 | [diff] [blame] | 3255 | else if (chip->options & NAND_USE_BOUNCE_BUFFER) |
| 3256 | use_bufpoi = !IS_ALIGNED((unsigned long)buf, |
| 3257 | chip->buf_align); |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 3258 | else |
| 3259 | use_bufpoi = 0; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3260 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3261 | WATCHDOG_RESET(); |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 3262 | /* Partial page write?, or need to use bounce buffer */ |
| 3263 | if (use_bufpoi) { |
| 3264 | pr_debug("%s: using write bounce buffer for buf@%p\n", |
| 3265 | __func__, buf); |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 3266 | if (part_pagewr) |
| 3267 | bytes = min_t(int, bytes - column, writelen); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3268 | chip->pagebuf = -1; |
| 3269 | memset(chip->buffers->databuf, 0xff, mtd->writesize); |
| 3270 | memcpy(&chip->buffers->databuf[column], buf, bytes); |
| 3271 | wbuf = chip->buffers->databuf; |
Sergei Poselenov | 04fbaa0 | 2008-06-06 15:42:43 +0200 | [diff] [blame] | 3272 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3273 | |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 3274 | if (unlikely(oob)) { |
| 3275 | size_t len = min(oobwritelen, oobmaxlen); |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3276 | oob = nand_fill_oob(mtd, oob, len, ops); |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 3277 | oobwritelen -= len; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3278 | } else { |
| 3279 | /* We still need to erase leftover OOB data */ |
| 3280 | memset(chip->oob_poi, 0xff, mtd->oobsize); |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 3281 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3282 | ret = chip->write_page(mtd, chip, column, bytes, wbuf, |
Boris Brezillon | b9bf43c | 2017-11-22 02:38:24 +0900 | [diff] [blame] | 3283 | oob_required, page, |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3284 | (ops->mode == MTD_OPS_RAW)); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3285 | if (ret) |
| 3286 | break; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3287 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3288 | writelen -= bytes; |
| 3289 | if (!writelen) |
| 3290 | break; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3291 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3292 | column = 0; |
| 3293 | buf += bytes; |
| 3294 | realpage++; |
| 3295 | |
| 3296 | page = realpage & chip->pagemask; |
| 3297 | /* Check, if we cross a chip boundary */ |
| 3298 | if (!page) { |
| 3299 | chipnr++; |
| 3300 | chip->select_chip(mtd, -1); |
| 3301 | chip->select_chip(mtd, chipnr); |
| 3302 | } |
| 3303 | } |
| 3304 | |
| 3305 | ops->retlen = ops->len - writelen; |
| 3306 | if (unlikely(oob)) |
| 3307 | ops->oobretlen = ops->ooblen; |
| 3308 | |
| 3309 | err_out: |
| 3310 | chip->select_chip(mtd, -1); |
| 3311 | return ret; |
| 3312 | } |
| 3313 | |
| 3314 | /** |
| 3315 | * panic_nand_write - [MTD Interface] NAND write with ECC |
| 3316 | * @mtd: MTD device structure |
| 3317 | * @to: offset to write to |
| 3318 | * @len: number of bytes to write |
| 3319 | * @retlen: pointer to variable to store the number of written bytes |
| 3320 | * @buf: the data to write |
| 3321 | * |
| 3322 | * NAND write with ECC. Used when performing writes in interrupt context, this |
| 3323 | * may for example be called by mtdoops when writing an oops while in panic. |
| 3324 | */ |
| 3325 | static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len, |
| 3326 | size_t *retlen, const uint8_t *buf) |
| 3327 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 3328 | struct nand_chip *chip = mtd_to_nand(mtd); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3329 | struct mtd_oob_ops ops; |
| 3330 | int ret; |
| 3331 | |
| 3332 | /* Wait for the device to get ready */ |
| 3333 | panic_nand_wait(mtd, chip, 400); |
| 3334 | |
| 3335 | /* Grab the device */ |
| 3336 | panic_nand_get_device(chip, mtd, FL_WRITING); |
| 3337 | |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 3338 | memset(&ops, 0, sizeof(ops)); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3339 | ops.len = len; |
| 3340 | ops.datbuf = (uint8_t *)buf; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3341 | ops.mode = MTD_OPS_PLACE_OOB; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3342 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3343 | ret = nand_do_write_ops(mtd, to, &ops); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3344 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3345 | *retlen = ops.retlen; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3346 | return ret; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3347 | } |
| 3348 | |
| 3349 | /** |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3350 | * nand_do_write_oob - [MTD Interface] NAND write out-of-band |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3351 | * @mtd: MTD device structure |
| 3352 | * @to: offset to write to |
| 3353 | * @ops: oob operation description structure |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3354 | * |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3355 | * NAND write out-of-band. |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3356 | */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3357 | static int nand_do_write_oob(struct mtd_info *mtd, loff_t to, |
| 3358 | struct mtd_oob_ops *ops) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3359 | { |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3360 | int chipnr, page, status, len; |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 3361 | struct nand_chip *chip = mtd_to_nand(mtd); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3362 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3363 | pr_debug("%s: to = 0x%08x, len = %i\n", |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 3364 | __func__, (unsigned int)to, (int)ops->ooblen); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3365 | |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 3366 | len = mtd_oobavail(mtd, ops); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3367 | |
| 3368 | /* Do not allow write past end of page */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3369 | if ((ops->ooboffs + ops->ooblen) > len) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3370 | pr_debug("%s: attempt to write past end of page\n", |
| 3371 | __func__); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3372 | return -EINVAL; |
| 3373 | } |
| 3374 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3375 | if (unlikely(ops->ooboffs >= len)) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3376 | pr_debug("%s: attempt to start write outside oob\n", |
| 3377 | __func__); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3378 | return -EINVAL; |
| 3379 | } |
| 3380 | |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 3381 | /* Do not allow write past end of device */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3382 | if (unlikely(to >= mtd->size || |
| 3383 | ops->ooboffs + ops->ooblen > |
| 3384 | ((mtd->size >> chip->page_shift) - |
| 3385 | (to >> chip->page_shift)) * len)) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3386 | pr_debug("%s: attempt to write beyond end of device\n", |
| 3387 | __func__); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3388 | return -EINVAL; |
| 3389 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3390 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3391 | chipnr = (int)(to >> chip->chip_shift); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3392 | |
| 3393 | /* |
| 3394 | * Reset the chip. Some chips (like the Toshiba TC5832DC found in one |
| 3395 | * of my DiskOnChip 2000 test units) will clear the whole data page too |
| 3396 | * if we don't do this. I have no clue why, but I seem to have 'fixed' |
| 3397 | * it in the doc2000 driver in August 1999. dwmw2. |
| 3398 | */ |
Boris Brezillon | 7ec6dc5 | 2017-11-22 02:38:20 +0900 | [diff] [blame] | 3399 | nand_reset(chip, chipnr); |
| 3400 | |
| 3401 | chip->select_chip(mtd, chipnr); |
| 3402 | |
| 3403 | /* Shift to get page */ |
| 3404 | page = (int)(to >> chip->page_shift); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3405 | |
| 3406 | /* Check, if it is write protected */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3407 | if (nand_check_wp(mtd)) { |
| 3408 | chip->select_chip(mtd, -1); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3409 | return -EROFS; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3410 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3411 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3412 | /* Invalidate the page cache, if we write to the cached page */ |
| 3413 | if (page == chip->pagebuf) |
| 3414 | chip->pagebuf = -1; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3415 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3416 | nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops); |
| 3417 | |
| 3418 | if (ops->mode == MTD_OPS_RAW) |
| 3419 | status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask); |
| 3420 | else |
| 3421 | status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3422 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3423 | chip->select_chip(mtd, -1); |
| 3424 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3425 | if (status) |
| 3426 | return status; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3427 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3428 | ops->oobretlen = ops->ooblen; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3429 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3430 | return 0; |
| 3431 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3432 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3433 | /** |
| 3434 | * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3435 | * @mtd: MTD device structure |
| 3436 | * @to: offset to write to |
| 3437 | * @ops: oob operation description structure |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3438 | */ |
| 3439 | static int nand_write_oob(struct mtd_info *mtd, loff_t to, |
| 3440 | struct mtd_oob_ops *ops) |
| 3441 | { |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3442 | int ret = -ENOTSUPP; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 3443 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3444 | ops->retlen = 0; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3445 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3446 | /* Do not allow writes past end of device */ |
| 3447 | if (ops->datbuf && (to + ops->len) > mtd->size) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3448 | pr_debug("%s: attempt to write beyond end of device\n", |
| 3449 | __func__); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3450 | return -EINVAL; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3451 | } |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3452 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3453 | nand_get_device(mtd, FL_WRITING); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3454 | |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 3455 | switch (ops->mode) { |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3456 | case MTD_OPS_PLACE_OOB: |
| 3457 | case MTD_OPS_AUTO_OOB: |
| 3458 | case MTD_OPS_RAW: |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3459 | break; |
| 3460 | |
| 3461 | default: |
| 3462 | goto out; |
| 3463 | } |
| 3464 | |
| 3465 | if (!ops->datbuf) |
| 3466 | ret = nand_do_write_oob(mtd, to, ops); |
| 3467 | else |
| 3468 | ret = nand_do_write_ops(mtd, to, ops); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3469 | |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 3470 | out: |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3471 | nand_release_device(mtd); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3472 | return ret; |
| 3473 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3474 | |
| 3475 | /** |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 3476 | * single_erase - [GENERIC] NAND standard block erase command function |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3477 | * @mtd: MTD device structure |
| 3478 | * @page: the page address of the block which will be erased |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3479 | * |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 3480 | * Standard erase command for NAND chips. Returns NAND status. |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3481 | */ |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 3482 | static int single_erase(struct mtd_info *mtd, int page) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3483 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 3484 | struct nand_chip *chip = mtd_to_nand(mtd); |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 3485 | unsigned int eraseblock; |
| 3486 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3487 | /* Send commands to erase a block */ |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 3488 | eraseblock = page >> (chip->phys_erase_shift - chip->page_shift); |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 3489 | |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 3490 | return nand_erase_op(chip, eraseblock); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3491 | } |
| 3492 | |
| 3493 | /** |
| 3494 | * nand_erase - [MTD Interface] erase block(s) |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3495 | * @mtd: MTD device structure |
| 3496 | * @instr: erase instruction |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3497 | * |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3498 | * Erase one ore more blocks. |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3499 | */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3500 | static int nand_erase(struct mtd_info *mtd, struct erase_info *instr) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3501 | { |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3502 | return nand_erase_nand(mtd, instr, 0); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3503 | } |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 3504 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3505 | /** |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3506 | * nand_erase_nand - [INTERN] erase block(s) |
| 3507 | * @mtd: MTD device structure |
| 3508 | * @instr: erase instruction |
| 3509 | * @allowbbt: allow erasing the bbt area |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3510 | * |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3511 | * Erase one ore more blocks. |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3512 | */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3513 | int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr, |
| 3514 | int allowbbt) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3515 | { |
Sandeep Paulraj | eab580c | 2009-10-30 13:51:23 -0400 | [diff] [blame] | 3516 | int page, status, pages_per_block, ret, chipnr; |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 3517 | struct nand_chip *chip = mtd_to_nand(mtd); |
Sandeep Paulraj | eab580c | 2009-10-30 13:51:23 -0400 | [diff] [blame] | 3518 | loff_t len; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3519 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3520 | pr_debug("%s: start = 0x%012llx, len = %llu\n", |
| 3521 | __func__, (unsigned long long)instr->addr, |
| 3522 | (unsigned long long)instr->len); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3523 | |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 3524 | if (check_offs_len(mtd, instr->addr, instr->len)) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3525 | return -EINVAL; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3526 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3527 | /* Grab the lock and see if the device is available */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3528 | nand_get_device(mtd, FL_ERASING); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3529 | |
| 3530 | /* Shift to get first page */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3531 | page = (int)(instr->addr >> chip->page_shift); |
| 3532 | chipnr = (int)(instr->addr >> chip->chip_shift); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3533 | |
| 3534 | /* Calculate pages in each block */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3535 | pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift); |
William Juul | b76ec38 | 2007-11-08 10:39:53 +0100 | [diff] [blame] | 3536 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3537 | /* Select the NAND device */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3538 | chip->select_chip(mtd, chipnr); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3539 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3540 | /* Check, if it is write protected */ |
| 3541 | if (nand_check_wp(mtd)) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3542 | pr_debug("%s: device is write protected!\n", |
| 3543 | __func__); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3544 | instr->state = MTD_ERASE_FAILED; |
| 3545 | goto erase_exit; |
| 3546 | } |
| 3547 | |
| 3548 | /* Loop through the pages */ |
| 3549 | len = instr->len; |
| 3550 | |
| 3551 | instr->state = MTD_ERASING; |
| 3552 | |
| 3553 | while (len) { |
Scott Wood | ea95b64 | 2011-02-02 18:15:57 -0600 | [diff] [blame] | 3554 | WATCHDOG_RESET(); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3555 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3556 | /* Check if we have a bad block, we do not erase bad blocks! */ |
Masahiro Yamada | f5a1902 | 2014-12-16 15:36:33 +0900 | [diff] [blame] | 3557 | if (!instr->scrub && nand_block_checkbad(mtd, ((loff_t) page) << |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 3558 | chip->page_shift, allowbbt)) { |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3559 | pr_warn("%s: attempt to erase a bad block at page 0x%08x\n", |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3560 | __func__, page); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3561 | instr->state = MTD_ERASE_FAILED; |
Farhan Ali | 7c05319 | 2021-02-24 15:25:53 -0800 | [diff] [blame] | 3562 | instr->fail_addr = |
| 3563 | ((loff_t)page << chip->page_shift); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3564 | goto erase_exit; |
| 3565 | } |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3566 | |
| 3567 | /* |
| 3568 | * Invalidate the page cache, if we erase the block which |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3569 | * contains the current cached page. |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3570 | */ |
| 3571 | if (page <= chip->pagebuf && chip->pagebuf < |
| 3572 | (page + pages_per_block)) |
| 3573 | chip->pagebuf = -1; |
| 3574 | |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 3575 | status = chip->erase(mtd, page & chip->pagemask); |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 3576 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3577 | /* See if block erase succeeded */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3578 | if (status & NAND_STATUS_FAIL) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3579 | pr_debug("%s: failed erase, page 0x%08x\n", |
| 3580 | __func__, page); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3581 | instr->state = MTD_ERASE_FAILED; |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 3582 | instr->fail_addr = |
| 3583 | ((loff_t)page << chip->page_shift); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3584 | goto erase_exit; |
| 3585 | } |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 3586 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3587 | /* Increment page address and decrement length */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3588 | len -= (1ULL << chip->phys_erase_shift); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3589 | page += pages_per_block; |
| 3590 | |
| 3591 | /* Check, if we cross a chip boundary */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3592 | if (len && !(page & chip->pagemask)) { |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3593 | chipnr++; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3594 | chip->select_chip(mtd, -1); |
| 3595 | chip->select_chip(mtd, chipnr); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3596 | } |
| 3597 | } |
| 3598 | instr->state = MTD_ERASE_DONE; |
| 3599 | |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 3600 | erase_exit: |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3601 | |
| 3602 | ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3603 | |
| 3604 | /* Deselect and wake up anyone waiting on the device */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3605 | chip->select_chip(mtd, -1); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3606 | nand_release_device(mtd); |
| 3607 | |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 3608 | /* Do call back function */ |
| 3609 | if (!ret) |
| 3610 | mtd_erase_callback(instr); |
| 3611 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3612 | /* Return more or less happy */ |
| 3613 | return ret; |
| 3614 | } |
| 3615 | |
| 3616 | /** |
| 3617 | * nand_sync - [MTD Interface] sync |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3618 | * @mtd: MTD device structure |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3619 | * |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3620 | * Sync is actually a wait for chip ready function. |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3621 | */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3622 | static void nand_sync(struct mtd_info *mtd) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3623 | { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3624 | pr_debug("%s: called\n", __func__); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3625 | |
| 3626 | /* Grab the lock and see if the device is available */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3627 | nand_get_device(mtd, FL_SYNCING); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3628 | /* Release it and go back */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3629 | nand_release_device(mtd); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3630 | } |
| 3631 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3632 | /** |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3633 | * nand_block_isbad - [MTD Interface] Check if block at offset is bad |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3634 | * @mtd: MTD device structure |
| 3635 | * @offs: offset relative to mtd start |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3636 | */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3637 | static int nand_block_isbad(struct mtd_info *mtd, loff_t offs) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3638 | { |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 3639 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 3640 | int chipnr = (int)(offs >> chip->chip_shift); |
| 3641 | int ret; |
| 3642 | |
| 3643 | /* Select the NAND device */ |
| 3644 | nand_get_device(mtd, FL_READING); |
| 3645 | chip->select_chip(mtd, chipnr); |
| 3646 | |
| 3647 | ret = nand_block_checkbad(mtd, offs, 0); |
| 3648 | |
| 3649 | chip->select_chip(mtd, -1); |
| 3650 | nand_release_device(mtd); |
| 3651 | |
| 3652 | return ret; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3653 | } |
| 3654 | |
| 3655 | /** |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3656 | * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3657 | * @mtd: MTD device structure |
| 3658 | * @ofs: offset relative to mtd start |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3659 | */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3660 | static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3661 | { |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3662 | int ret; |
| 3663 | |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 3664 | ret = nand_block_isbad(mtd, ofs); |
| 3665 | if (ret) { |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3666 | /* If it was bad already, return success and do nothing */ |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3667 | if (ret > 0) |
| 3668 | return 0; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 3669 | return ret; |
| 3670 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3671 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3672 | return nand_block_markbad_lowlevel(mtd, ofs); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3673 | } |
| 3674 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3675 | /** |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3676 | * nand_onfi_set_features- [REPLACEABLE] set features for ONFI nand |
| 3677 | * @mtd: MTD device structure |
| 3678 | * @chip: nand chip info structure |
| 3679 | * @addr: feature address. |
| 3680 | * @subfeature_param: the subfeature parameters, a four bytes array. |
| 3681 | */ |
| 3682 | static int nand_onfi_set_features(struct mtd_info *mtd, struct nand_chip *chip, |
| 3683 | int addr, uint8_t *subfeature_param) |
| 3684 | { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3685 | #ifdef CONFIG_SYS_NAND_ONFI_DETECTION |
| 3686 | if (!chip->onfi_version || |
| 3687 | !(le16_to_cpu(chip->onfi_params.opt_cmd) |
| 3688 | & ONFI_OPT_CMD_SET_GET_FEATURES)) |
Mylène Josserand | c21946b | 2018-07-13 18:10:23 +0200 | [diff] [blame] | 3689 | return -ENOTSUPP; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3690 | #endif |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3691 | |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 3692 | return nand_set_features_op(chip, addr, subfeature_param); |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3693 | } |
| 3694 | |
| 3695 | /** |
| 3696 | * nand_onfi_get_features- [REPLACEABLE] get features for ONFI nand |
| 3697 | * @mtd: MTD device structure |
| 3698 | * @chip: nand chip info structure |
| 3699 | * @addr: feature address. |
| 3700 | * @subfeature_param: the subfeature parameters, a four bytes array. |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3701 | */ |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3702 | static int nand_onfi_get_features(struct mtd_info *mtd, struct nand_chip *chip, |
| 3703 | int addr, uint8_t *subfeature_param) |
| 3704 | { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3705 | #ifdef CONFIG_SYS_NAND_ONFI_DETECTION |
| 3706 | if (!chip->onfi_version || |
| 3707 | !(le16_to_cpu(chip->onfi_params.opt_cmd) |
| 3708 | & ONFI_OPT_CMD_SET_GET_FEATURES)) |
Mylène Josserand | c21946b | 2018-07-13 18:10:23 +0200 | [diff] [blame] | 3709 | return -ENOTSUPP; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3710 | #endif |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3711 | |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 3712 | return nand_get_features_op(chip, addr, subfeature_param); |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3713 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3714 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3715 | /* Set default functions */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3716 | static void nand_set_defaults(struct nand_chip *chip, int busw) |
| 3717 | { |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3718 | /* check for proper chip_delay setup, set 20us if not */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3719 | if (!chip->chip_delay) |
| 3720 | chip->chip_delay = 20; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3721 | |
| 3722 | /* check, if a user supplied command function given */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3723 | if (chip->cmdfunc == NULL) |
| 3724 | chip->cmdfunc = nand_command; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3725 | |
| 3726 | /* check, if a user supplied wait function given */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3727 | if (chip->waitfunc == NULL) |
| 3728 | chip->waitfunc = nand_wait; |
| 3729 | |
| 3730 | if (!chip->select_chip) |
| 3731 | chip->select_chip = nand_select_chip; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3732 | |
| 3733 | /* set for ONFI nand */ |
| 3734 | if (!chip->onfi_set_features) |
| 3735 | chip->onfi_set_features = nand_onfi_set_features; |
| 3736 | if (!chip->onfi_get_features) |
| 3737 | chip->onfi_get_features = nand_onfi_get_features; |
| 3738 | |
| 3739 | /* If called twice, pointers that depend on busw may need to be reset */ |
| 3740 | if (!chip->read_byte || chip->read_byte == nand_read_byte) |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3741 | chip->read_byte = busw ? nand_read_byte16 : nand_read_byte; |
| 3742 | if (!chip->read_word) |
| 3743 | chip->read_word = nand_read_word; |
| 3744 | if (!chip->block_bad) |
| 3745 | chip->block_bad = nand_block_bad; |
| 3746 | if (!chip->block_markbad) |
| 3747 | chip->block_markbad = nand_default_block_markbad; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3748 | if (!chip->write_buf || chip->write_buf == nand_write_buf) |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3749 | chip->write_buf = busw ? nand_write_buf16 : nand_write_buf; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3750 | if (!chip->write_byte || chip->write_byte == nand_write_byte) |
| 3751 | chip->write_byte = busw ? nand_write_byte16 : nand_write_byte; |
| 3752 | if (!chip->read_buf || chip->read_buf == nand_read_buf) |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3753 | chip->read_buf = busw ? nand_read_buf16 : nand_read_buf; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3754 | if (!chip->scan_bbt) |
| 3755 | chip->scan_bbt = nand_default_bbt; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3756 | |
| 3757 | if (!chip->controller) { |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3758 | chip->controller = &chip->hwcontrol; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3759 | spin_lock_init(&chip->controller->lock); |
| 3760 | init_waitqueue_head(&chip->controller->wq); |
| 3761 | } |
| 3762 | |
Masahiro Yamada | b9c07b6 | 2017-11-22 02:38:27 +0900 | [diff] [blame] | 3763 | if (!chip->buf_align) |
| 3764 | chip->buf_align = 1; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3765 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3766 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3767 | /* Sanitize ONFI strings so we can safely print them */ |
Christian Hitz | 6f1c9e0 | 2011-10-12 09:32:05 +0200 | [diff] [blame] | 3768 | static void sanitize_string(char *s, size_t len) |
| 3769 | { |
| 3770 | ssize_t i; |
| 3771 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3772 | /* Null terminate */ |
Christian Hitz | 6f1c9e0 | 2011-10-12 09:32:05 +0200 | [diff] [blame] | 3773 | s[len - 1] = 0; |
| 3774 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3775 | /* Remove non printable chars */ |
Christian Hitz | 6f1c9e0 | 2011-10-12 09:32:05 +0200 | [diff] [blame] | 3776 | for (i = 0; i < len - 1; i++) { |
| 3777 | if (s[i] < ' ' || s[i] > 127) |
| 3778 | s[i] = '?'; |
| 3779 | } |
| 3780 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3781 | /* Remove trailing spaces */ |
Christian Hitz | 6f1c9e0 | 2011-10-12 09:32:05 +0200 | [diff] [blame] | 3782 | strim(s); |
| 3783 | } |
| 3784 | |
Florian Fainelli | c98a935 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3785 | static u16 onfi_crc16(u16 crc, u8 const *p, size_t len) |
| 3786 | { |
| 3787 | int i; |
Florian Fainelli | c98a935 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3788 | while (len--) { |
| 3789 | crc ^= *p++ << 8; |
| 3790 | for (i = 0; i < 8; i++) |
| 3791 | crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0); |
| 3792 | } |
| 3793 | |
| 3794 | return crc; |
| 3795 | } |
| 3796 | |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 3797 | #ifdef CONFIG_SYS_NAND_ONFI_DETECTION |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3798 | /* Parse the Extended Parameter Page. */ |
| 3799 | static int nand_flash_detect_ext_param_page(struct mtd_info *mtd, |
| 3800 | struct nand_chip *chip, struct nand_onfi_params *p) |
| 3801 | { |
| 3802 | struct onfi_ext_param_page *ep; |
| 3803 | struct onfi_ext_section *s; |
| 3804 | struct onfi_ext_ecc_info *ecc; |
| 3805 | uint8_t *cursor; |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 3806 | int ret; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3807 | int len; |
| 3808 | int i; |
| 3809 | |
| 3810 | len = le16_to_cpu(p->ext_param_page_length) * 16; |
| 3811 | ep = kmalloc(len, GFP_KERNEL); |
| 3812 | if (!ep) |
| 3813 | return -ENOMEM; |
| 3814 | |
| 3815 | /* Send our own NAND_CMD_PARAM. */ |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 3816 | ret = nand_read_param_page_op(chip, 0, NULL, 0); |
| 3817 | if (ret) |
| 3818 | goto ext_out; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3819 | |
| 3820 | /* Use the Change Read Column command to skip the ONFI param pages. */ |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 3821 | ret = nand_change_read_column_op(chip, |
| 3822 | sizeof(*p) * p->num_of_param_pages, |
| 3823 | ep, len, true); |
| 3824 | if (ret) |
| 3825 | goto ext_out; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3826 | |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 3827 | ret = -EINVAL; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3828 | if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2) |
| 3829 | != le16_to_cpu(ep->crc))) { |
| 3830 | pr_debug("fail in the CRC.\n"); |
| 3831 | goto ext_out; |
| 3832 | } |
| 3833 | |
| 3834 | /* |
| 3835 | * Check the signature. |
| 3836 | * Do not strictly follow the ONFI spec, maybe changed in future. |
| 3837 | */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3838 | if (strncmp((char *)ep->sig, "EPPS", 4)) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3839 | pr_debug("The signature is invalid.\n"); |
| 3840 | goto ext_out; |
| 3841 | } |
| 3842 | |
| 3843 | /* find the ECC section. */ |
| 3844 | cursor = (uint8_t *)(ep + 1); |
| 3845 | for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) { |
| 3846 | s = ep->sections + i; |
| 3847 | if (s->type == ONFI_SECTION_TYPE_2) |
| 3848 | break; |
| 3849 | cursor += s->length * 16; |
| 3850 | } |
| 3851 | if (i == ONFI_EXT_SECTION_MAX) { |
| 3852 | pr_debug("We can not find the ECC section.\n"); |
| 3853 | goto ext_out; |
| 3854 | } |
| 3855 | |
| 3856 | /* get the info we want. */ |
| 3857 | ecc = (struct onfi_ext_ecc_info *)cursor; |
| 3858 | |
| 3859 | if (!ecc->codeword_size) { |
| 3860 | pr_debug("Invalid codeword size\n"); |
| 3861 | goto ext_out; |
| 3862 | } |
| 3863 | |
| 3864 | chip->ecc_strength_ds = ecc->ecc_bits; |
| 3865 | chip->ecc_step_ds = 1 << ecc->codeword_size; |
| 3866 | ret = 0; |
| 3867 | |
| 3868 | ext_out: |
| 3869 | kfree(ep); |
| 3870 | return ret; |
| 3871 | } |
| 3872 | |
| 3873 | static int nand_setup_read_retry_micron(struct mtd_info *mtd, int retry_mode) |
| 3874 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 3875 | struct nand_chip *chip = mtd_to_nand(mtd); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3876 | uint8_t feature[ONFI_SUBFEATURE_PARAM_LEN] = {retry_mode}; |
| 3877 | |
| 3878 | return chip->onfi_set_features(mtd, chip, ONFI_FEATURE_ADDR_READ_RETRY, |
| 3879 | feature); |
| 3880 | } |
| 3881 | |
| 3882 | /* |
| 3883 | * Configure chip properties from Micron vendor-specific ONFI table |
| 3884 | */ |
| 3885 | static void nand_onfi_detect_micron(struct nand_chip *chip, |
| 3886 | struct nand_onfi_params *p) |
| 3887 | { |
| 3888 | struct nand_onfi_vendor_micron *micron = (void *)p->vendor; |
| 3889 | |
| 3890 | if (le16_to_cpu(p->vendor_revision) < 1) |
| 3891 | return; |
| 3892 | |
| 3893 | chip->read_retries = micron->read_retry_options; |
| 3894 | chip->setup_read_retry = nand_setup_read_retry_micron; |
| 3895 | } |
| 3896 | |
Florian Fainelli | c98a935 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3897 | /* |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3898 | * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise. |
Florian Fainelli | c98a935 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3899 | */ |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 3900 | static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip, |
Florian Fainelli | c98a935 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3901 | int *busw) |
| 3902 | { |
| 3903 | struct nand_onfi_params *p = &chip->onfi_params; |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 3904 | char id[4]; |
| 3905 | int i, ret, val; |
Florian Fainelli | c98a935 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3906 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3907 | /* Try ONFI for unknown chip or LP */ |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 3908 | ret = nand_readid_op(chip, 0x20, id, sizeof(id)); |
| 3909 | if (ret || strncmp(id, "ONFI", 4)) |
| 3910 | return 0; |
| 3911 | |
| 3912 | ret = nand_read_param_page_op(chip, 0, NULL, 0); |
| 3913 | if (ret) |
Florian Fainelli | c98a935 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3914 | return 0; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3915 | |
Florian Fainelli | c98a935 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3916 | for (i = 0; i < 3; i++) { |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 3917 | ret = nand_read_data_op(chip, p, sizeof(*p), true); |
| 3918 | if (ret) |
| 3919 | return 0; |
| 3920 | |
Florian Fainelli | c98a935 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3921 | if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) == |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 3922 | le16_to_cpu(p->crc)) { |
Florian Fainelli | c98a935 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3923 | break; |
| 3924 | } |
| 3925 | } |
| 3926 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3927 | if (i == 3) { |
| 3928 | pr_err("Could not find valid ONFI parameter page; aborting\n"); |
Florian Fainelli | c98a935 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3929 | return 0; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3930 | } |
Florian Fainelli | c98a935 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3931 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3932 | /* Check version */ |
Florian Fainelli | c98a935 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3933 | val = le16_to_cpu(p->revision); |
Florian Fainelli | 3eb4fc6 | 2011-04-03 18:23:56 +0200 | [diff] [blame] | 3934 | if (val & (1 << 5)) |
| 3935 | chip->onfi_version = 23; |
| 3936 | else if (val & (1 << 4)) |
Florian Fainelli | c98a935 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3937 | chip->onfi_version = 22; |
| 3938 | else if (val & (1 << 3)) |
| 3939 | chip->onfi_version = 21; |
| 3940 | else if (val & (1 << 2)) |
| 3941 | chip->onfi_version = 20; |
Florian Fainelli | 3eb4fc6 | 2011-04-03 18:23:56 +0200 | [diff] [blame] | 3942 | else if (val & (1 << 1)) |
Florian Fainelli | c98a935 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3943 | chip->onfi_version = 10; |
Florian Fainelli | 3eb4fc6 | 2011-04-03 18:23:56 +0200 | [diff] [blame] | 3944 | |
| 3945 | if (!chip->onfi_version) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3946 | pr_info("unsupported ONFI version: %d\n", val); |
Florian Fainelli | 3eb4fc6 | 2011-04-03 18:23:56 +0200 | [diff] [blame] | 3947 | return 0; |
| 3948 | } |
Florian Fainelli | c98a935 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3949 | |
Christian Hitz | 6f1c9e0 | 2011-10-12 09:32:05 +0200 | [diff] [blame] | 3950 | sanitize_string(p->manufacturer, sizeof(p->manufacturer)); |
| 3951 | sanitize_string(p->model, sizeof(p->model)); |
Florian Fainelli | c98a935 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3952 | if (!mtd->name) |
| 3953 | mtd->name = p->model; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3954 | |
Florian Fainelli | c98a935 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3955 | mtd->writesize = le32_to_cpu(p->byte_per_page); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3956 | |
| 3957 | /* |
| 3958 | * pages_per_block and blocks_per_lun may not be a power-of-2 size |
| 3959 | * (don't ask me who thought of this...). MTD assumes that these |
| 3960 | * dimensions will be power-of-2, so just truncate the remaining area. |
| 3961 | */ |
| 3962 | mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1); |
| 3963 | mtd->erasesize *= mtd->writesize; |
| 3964 | |
Florian Fainelli | c98a935 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3965 | mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3966 | |
| 3967 | /* See erasesize comment */ |
| 3968 | chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1); |
Matthieu CASTET | b20b6f2 | 2012-03-19 15:35:25 +0100 | [diff] [blame] | 3969 | chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3970 | chip->bits_per_cell = p->bits_per_cell; |
| 3971 | |
| 3972 | if (onfi_feature(chip) & ONFI_FEATURE_16_BIT_BUS) |
Florian Fainelli | c98a935 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3973 | *busw = NAND_BUSWIDTH_16; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3974 | else |
| 3975 | *busw = 0; |
| 3976 | |
| 3977 | if (p->ecc_bits != 0xff) { |
| 3978 | chip->ecc_strength_ds = p->ecc_bits; |
| 3979 | chip->ecc_step_ds = 512; |
| 3980 | } else if (chip->onfi_version >= 21 && |
| 3981 | (onfi_feature(chip) & ONFI_FEATURE_EXT_PARAM_PAGE)) { |
| 3982 | |
| 3983 | /* |
| 3984 | * The nand_flash_detect_ext_param_page() uses the |
| 3985 | * Change Read Column command which maybe not supported |
| 3986 | * by the chip->cmdfunc. So try to update the chip->cmdfunc |
| 3987 | * now. We do not replace user supplied command function. |
| 3988 | */ |
| 3989 | if (mtd->writesize > 512 && chip->cmdfunc == nand_command) |
| 3990 | chip->cmdfunc = nand_command_lp; |
| 3991 | |
| 3992 | /* The Extended Parameter Page is supported since ONFI 2.1. */ |
| 3993 | if (nand_flash_detect_ext_param_page(mtd, chip, p)) |
| 3994 | pr_warn("Failed to detect ONFI extended param page\n"); |
| 3995 | } else { |
| 3996 | pr_warn("Could not retrieve ONFI ECC requirements\n"); |
| 3997 | } |
| 3998 | |
| 3999 | if (p->jedec_id == NAND_MFR_MICRON) |
| 4000 | nand_onfi_detect_micron(chip, p); |
Florian Fainelli | c98a935 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 4001 | |
| 4002 | return 1; |
| 4003 | } |
| 4004 | #else |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4005 | static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip, |
Florian Fainelli | c98a935 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 4006 | int *busw) |
| 4007 | { |
| 4008 | return 0; |
| 4009 | } |
| 4010 | #endif |
| 4011 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4012 | /* |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 4013 | * Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise. |
| 4014 | */ |
| 4015 | static int nand_flash_detect_jedec(struct mtd_info *mtd, struct nand_chip *chip, |
| 4016 | int *busw) |
| 4017 | { |
| 4018 | struct nand_jedec_params *p = &chip->jedec_params; |
| 4019 | struct jedec_ecc_info *ecc; |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 4020 | char id[5]; |
| 4021 | int i, val, ret; |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 4022 | |
| 4023 | /* Try JEDEC for unknown chip or LP */ |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 4024 | ret = nand_readid_op(chip, 0x40, id, sizeof(id)); |
| 4025 | if (ret || strncmp(id, "JEDEC", sizeof(id))) |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 4026 | return 0; |
| 4027 | |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 4028 | ret = nand_read_param_page_op(chip, 0x40, NULL, 0); |
| 4029 | if (ret) |
| 4030 | return 0; |
| 4031 | |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 4032 | for (i = 0; i < 3; i++) { |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 4033 | ret = nand_read_data_op(chip, p, sizeof(*p), true); |
| 4034 | if (ret) |
| 4035 | return 0; |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 4036 | |
| 4037 | if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 510) == |
| 4038 | le16_to_cpu(p->crc)) |
| 4039 | break; |
| 4040 | } |
| 4041 | |
| 4042 | if (i == 3) { |
| 4043 | pr_err("Could not find valid JEDEC parameter page; aborting\n"); |
| 4044 | return 0; |
| 4045 | } |
| 4046 | |
| 4047 | /* Check version */ |
| 4048 | val = le16_to_cpu(p->revision); |
| 4049 | if (val & (1 << 2)) |
| 4050 | chip->jedec_version = 10; |
| 4051 | else if (val & (1 << 1)) |
| 4052 | chip->jedec_version = 1; /* vendor specific version */ |
| 4053 | |
| 4054 | if (!chip->jedec_version) { |
| 4055 | pr_info("unsupported JEDEC version: %d\n", val); |
| 4056 | return 0; |
| 4057 | } |
| 4058 | |
| 4059 | sanitize_string(p->manufacturer, sizeof(p->manufacturer)); |
| 4060 | sanitize_string(p->model, sizeof(p->model)); |
| 4061 | if (!mtd->name) |
| 4062 | mtd->name = p->model; |
| 4063 | |
| 4064 | mtd->writesize = le32_to_cpu(p->byte_per_page); |
| 4065 | |
| 4066 | /* Please reference to the comment for nand_flash_detect_onfi. */ |
| 4067 | mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1); |
| 4068 | mtd->erasesize *= mtd->writesize; |
| 4069 | |
| 4070 | mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page); |
| 4071 | |
| 4072 | /* Please reference to the comment for nand_flash_detect_onfi. */ |
| 4073 | chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1); |
| 4074 | chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count; |
| 4075 | chip->bits_per_cell = p->bits_per_cell; |
| 4076 | |
| 4077 | if (jedec_feature(chip) & JEDEC_FEATURE_16_BIT_BUS) |
| 4078 | *busw = NAND_BUSWIDTH_16; |
| 4079 | else |
| 4080 | *busw = 0; |
| 4081 | |
| 4082 | /* ECC info */ |
| 4083 | ecc = &p->ecc_info[0]; |
| 4084 | |
| 4085 | if (ecc->codeword_size >= 9) { |
| 4086 | chip->ecc_strength_ds = ecc->ecc_bits; |
| 4087 | chip->ecc_step_ds = 1 << ecc->codeword_size; |
| 4088 | } else { |
| 4089 | pr_warn("Invalid codeword size\n"); |
| 4090 | } |
| 4091 | |
| 4092 | return 1; |
| 4093 | } |
| 4094 | |
| 4095 | /* |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4096 | * nand_id_has_period - Check if an ID string has a given wraparound period |
| 4097 | * @id_data: the ID string |
| 4098 | * @arrlen: the length of the @id_data array |
| 4099 | * @period: the period of repitition |
| 4100 | * |
| 4101 | * Check if an ID string is repeated within a given sequence of bytes at |
| 4102 | * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4103 | * period of 3). This is a helper function for nand_id_len(). Returns non-zero |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4104 | * if the repetition has a period of @period; otherwise, returns zero. |
| 4105 | */ |
| 4106 | static int nand_id_has_period(u8 *id_data, int arrlen, int period) |
| 4107 | { |
| 4108 | int i, j; |
| 4109 | for (i = 0; i < period; i++) |
| 4110 | for (j = i + period; j < arrlen; j += period) |
| 4111 | if (id_data[i] != id_data[j]) |
| 4112 | return 0; |
| 4113 | return 1; |
| 4114 | } |
| 4115 | |
| 4116 | /* |
| 4117 | * nand_id_len - Get the length of an ID string returned by CMD_READID |
| 4118 | * @id_data: the ID string |
| 4119 | * @arrlen: the length of the @id_data array |
| 4120 | |
| 4121 | * Returns the length of the ID string, according to known wraparound/trailing |
| 4122 | * zero patterns. If no pattern exists, returns the length of the array. |
| 4123 | */ |
| 4124 | static int nand_id_len(u8 *id_data, int arrlen) |
| 4125 | { |
| 4126 | int last_nonzero, period; |
| 4127 | |
| 4128 | /* Find last non-zero byte */ |
| 4129 | for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--) |
| 4130 | if (id_data[last_nonzero]) |
| 4131 | break; |
| 4132 | |
| 4133 | /* All zeros */ |
| 4134 | if (last_nonzero < 0) |
| 4135 | return 0; |
| 4136 | |
| 4137 | /* Calculate wraparound period */ |
| 4138 | for (period = 1; period < arrlen; period++) |
| 4139 | if (nand_id_has_period(id_data, arrlen, period)) |
| 4140 | break; |
| 4141 | |
| 4142 | /* There's a repeated pattern */ |
| 4143 | if (period < arrlen) |
| 4144 | return period; |
| 4145 | |
| 4146 | /* There are trailing zeros */ |
| 4147 | if (last_nonzero < arrlen - 1) |
| 4148 | return last_nonzero + 1; |
| 4149 | |
| 4150 | /* No pattern detected */ |
| 4151 | return arrlen; |
| 4152 | } |
| 4153 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4154 | /* Extract the bits of per cell from the 3rd byte of the extended ID */ |
| 4155 | static int nand_get_bits_per_cell(u8 cellinfo) |
| 4156 | { |
| 4157 | int bits; |
| 4158 | |
| 4159 | bits = cellinfo & NAND_CI_CELLTYPE_MSK; |
| 4160 | bits >>= NAND_CI_CELLTYPE_SHIFT; |
| 4161 | return bits + 1; |
| 4162 | } |
| 4163 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4164 | /* |
| 4165 | * Many new NAND share similar device ID codes, which represent the size of the |
| 4166 | * chip. The rest of the parameters must be decoded according to generic or |
| 4167 | * manufacturer-specific "extended ID" decoding patterns. |
| 4168 | */ |
| 4169 | static void nand_decode_ext_id(struct mtd_info *mtd, struct nand_chip *chip, |
| 4170 | u8 id_data[8], int *busw) |
| 4171 | { |
| 4172 | int extid, id_len; |
| 4173 | /* The 3rd id byte holds MLC / multichip data */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4174 | chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]); |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4175 | /* The 4th id byte is the important one */ |
| 4176 | extid = id_data[3]; |
| 4177 | |
| 4178 | id_len = nand_id_len(id_data, 8); |
| 4179 | |
| 4180 | /* |
| 4181 | * Field definitions are in the following datasheets: |
| 4182 | * Old style (4,5 byte ID): Samsung K9GAG08U0M (p.32) |
| 4183 | * New Samsung (6 byte ID): Samsung K9GAG08U0F (p.44) |
| 4184 | * Hynix MLC (6 byte ID): Hynix H27UBG8T2B (p.22) |
| 4185 | * |
| 4186 | * Check for ID length, non-zero 6th byte, cell type, and Hynix/Samsung |
| 4187 | * ID to decide what to do. |
| 4188 | */ |
| 4189 | if (id_len == 6 && id_data[0] == NAND_MFR_SAMSUNG && |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4190 | !nand_is_slc(chip) && id_data[5] != 0x00) { |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4191 | /* Calc pagesize */ |
| 4192 | mtd->writesize = 2048 << (extid & 0x03); |
| 4193 | extid >>= 2; |
| 4194 | /* Calc oobsize */ |
| 4195 | switch (((extid >> 2) & 0x04) | (extid & 0x03)) { |
| 4196 | case 1: |
| 4197 | mtd->oobsize = 128; |
| 4198 | break; |
| 4199 | case 2: |
| 4200 | mtd->oobsize = 218; |
| 4201 | break; |
| 4202 | case 3: |
| 4203 | mtd->oobsize = 400; |
| 4204 | break; |
| 4205 | case 4: |
| 4206 | mtd->oobsize = 436; |
| 4207 | break; |
| 4208 | case 5: |
| 4209 | mtd->oobsize = 512; |
| 4210 | break; |
| 4211 | case 6: |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4212 | mtd->oobsize = 640; |
| 4213 | break; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4214 | case 7: |
| 4215 | default: /* Other cases are "reserved" (unknown) */ |
| 4216 | mtd->oobsize = 1024; |
| 4217 | break; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4218 | } |
| 4219 | extid >>= 2; |
| 4220 | /* Calc blocksize */ |
| 4221 | mtd->erasesize = (128 * 1024) << |
| 4222 | (((extid >> 1) & 0x04) | (extid & 0x03)); |
| 4223 | *busw = 0; |
| 4224 | } else if (id_len == 6 && id_data[0] == NAND_MFR_HYNIX && |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4225 | !nand_is_slc(chip)) { |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4226 | unsigned int tmp; |
| 4227 | |
| 4228 | /* Calc pagesize */ |
| 4229 | mtd->writesize = 2048 << (extid & 0x03); |
| 4230 | extid >>= 2; |
| 4231 | /* Calc oobsize */ |
| 4232 | switch (((extid >> 2) & 0x04) | (extid & 0x03)) { |
| 4233 | case 0: |
| 4234 | mtd->oobsize = 128; |
| 4235 | break; |
| 4236 | case 1: |
| 4237 | mtd->oobsize = 224; |
| 4238 | break; |
| 4239 | case 2: |
| 4240 | mtd->oobsize = 448; |
| 4241 | break; |
| 4242 | case 3: |
| 4243 | mtd->oobsize = 64; |
| 4244 | break; |
| 4245 | case 4: |
| 4246 | mtd->oobsize = 32; |
| 4247 | break; |
| 4248 | case 5: |
| 4249 | mtd->oobsize = 16; |
| 4250 | break; |
| 4251 | default: |
| 4252 | mtd->oobsize = 640; |
| 4253 | break; |
| 4254 | } |
| 4255 | extid >>= 2; |
| 4256 | /* Calc blocksize */ |
| 4257 | tmp = ((extid >> 1) & 0x04) | (extid & 0x03); |
| 4258 | if (tmp < 0x03) |
| 4259 | mtd->erasesize = (128 * 1024) << tmp; |
| 4260 | else if (tmp == 0x03) |
| 4261 | mtd->erasesize = 768 * 1024; |
| 4262 | else |
| 4263 | mtd->erasesize = (64 * 1024) << tmp; |
| 4264 | *busw = 0; |
| 4265 | } else { |
| 4266 | /* Calc pagesize */ |
| 4267 | mtd->writesize = 1024 << (extid & 0x03); |
| 4268 | extid >>= 2; |
| 4269 | /* Calc oobsize */ |
| 4270 | mtd->oobsize = (8 << (extid & 0x01)) * |
| 4271 | (mtd->writesize >> 9); |
| 4272 | extid >>= 2; |
| 4273 | /* Calc blocksize. Blocksize is multiples of 64KiB */ |
| 4274 | mtd->erasesize = (64 * 1024) << (extid & 0x03); |
| 4275 | extid >>= 2; |
| 4276 | /* Get buswidth information */ |
| 4277 | *busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4278 | |
| 4279 | /* |
| 4280 | * Toshiba 24nm raw SLC (i.e., not BENAND) have 32B OOB per |
| 4281 | * 512B page. For Toshiba SLC, we decode the 5th/6th byte as |
| 4282 | * follows: |
| 4283 | * - ID byte 6, bits[2:0]: 100b -> 43nm, 101b -> 32nm, |
| 4284 | * 110b -> 24nm |
| 4285 | * - ID byte 5, bit[7]: 1 -> BENAND, 0 -> raw SLC |
| 4286 | */ |
| 4287 | if (id_len >= 6 && id_data[0] == NAND_MFR_TOSHIBA && |
| 4288 | nand_is_slc(chip) && |
| 4289 | (id_data[5] & 0x7) == 0x6 /* 24nm */ && |
| 4290 | !(id_data[4] & 0x80) /* !BENAND */) { |
| 4291 | mtd->oobsize = 32 * mtd->writesize >> 9; |
| 4292 | } |
| 4293 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4294 | } |
| 4295 | } |
| 4296 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4297 | /* |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4298 | * Old devices have chip data hardcoded in the device ID table. nand_decode_id |
| 4299 | * decodes a matching ID table entry and assigns the MTD size parameters for |
| 4300 | * the chip. |
| 4301 | */ |
| 4302 | static void nand_decode_id(struct mtd_info *mtd, struct nand_chip *chip, |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4303 | struct nand_flash_dev *type, u8 id_data[8], |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4304 | int *busw) |
| 4305 | { |
| 4306 | int maf_id = id_data[0]; |
| 4307 | |
| 4308 | mtd->erasesize = type->erasesize; |
| 4309 | mtd->writesize = type->pagesize; |
| 4310 | mtd->oobsize = mtd->writesize / 32; |
| 4311 | *busw = type->options & NAND_BUSWIDTH_16; |
| 4312 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4313 | /* All legacy ID NAND are small-page, SLC */ |
| 4314 | chip->bits_per_cell = 1; |
| 4315 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4316 | /* |
| 4317 | * Check for Spansion/AMD ID + repeating 5th, 6th byte since |
| 4318 | * some Spansion chips have erasesize that conflicts with size |
| 4319 | * listed in nand_ids table. |
| 4320 | * Data sheet (5 byte ID): Spansion S30ML-P ORNAND (p.39) |
| 4321 | */ |
| 4322 | if (maf_id == NAND_MFR_AMD && id_data[4] != 0x00 && id_data[5] == 0x00 |
| 4323 | && id_data[6] == 0x00 && id_data[7] == 0x00 |
| 4324 | && mtd->writesize == 512) { |
| 4325 | mtd->erasesize = 128 * 1024; |
| 4326 | mtd->erasesize <<= ((id_data[3] & 0x03) << 1); |
| 4327 | } |
| 4328 | } |
| 4329 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4330 | /* |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4331 | * Set the bad block marker/indicator (BBM/BBI) patterns according to some |
| 4332 | * heuristic patterns using various detected parameters (e.g., manufacturer, |
| 4333 | * page size, cell-type information). |
| 4334 | */ |
| 4335 | static void nand_decode_bbm_options(struct mtd_info *mtd, |
| 4336 | struct nand_chip *chip, u8 id_data[8]) |
| 4337 | { |
| 4338 | int maf_id = id_data[0]; |
| 4339 | |
| 4340 | /* Set the bad block position */ |
| 4341 | if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16)) |
| 4342 | chip->badblockpos = NAND_LARGE_BADBLOCK_POS; |
| 4343 | else |
| 4344 | chip->badblockpos = NAND_SMALL_BADBLOCK_POS; |
| 4345 | |
| 4346 | /* |
| 4347 | * Bad block marker is stored in the last page of each block on Samsung |
| 4348 | * and Hynix MLC devices; stored in first two pages of each block on |
| 4349 | * Micron devices with 2KiB pages and on SLC Samsung, Hynix, Toshiba, |
| 4350 | * AMD/Spansion, and Macronix. All others scan only the first page. |
| 4351 | */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4352 | if (!nand_is_slc(chip) && |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4353 | (maf_id == NAND_MFR_SAMSUNG || |
| 4354 | maf_id == NAND_MFR_HYNIX)) |
| 4355 | chip->bbt_options |= NAND_BBT_SCANLASTPAGE; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4356 | else if ((nand_is_slc(chip) && |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4357 | (maf_id == NAND_MFR_SAMSUNG || |
| 4358 | maf_id == NAND_MFR_HYNIX || |
| 4359 | maf_id == NAND_MFR_TOSHIBA || |
| 4360 | maf_id == NAND_MFR_AMD || |
| 4361 | maf_id == NAND_MFR_MACRONIX)) || |
| 4362 | (mtd->writesize == 2048 && |
| 4363 | maf_id == NAND_MFR_MICRON)) |
| 4364 | chip->bbt_options |= NAND_BBT_SCAN2NDPAGE; |
| 4365 | } |
| 4366 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4367 | static inline bool is_full_id_nand(struct nand_flash_dev *type) |
| 4368 | { |
| 4369 | return type->id_len; |
| 4370 | } |
| 4371 | |
| 4372 | static bool find_full_id_nand(struct mtd_info *mtd, struct nand_chip *chip, |
| 4373 | struct nand_flash_dev *type, u8 *id_data, int *busw) |
| 4374 | { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4375 | if (!strncmp((char *)type->id, (char *)id_data, type->id_len)) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4376 | mtd->writesize = type->pagesize; |
| 4377 | mtd->erasesize = type->erasesize; |
| 4378 | mtd->oobsize = type->oobsize; |
| 4379 | |
| 4380 | chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]); |
| 4381 | chip->chipsize = (uint64_t)type->chipsize << 20; |
| 4382 | chip->options |= type->options; |
| 4383 | chip->ecc_strength_ds = NAND_ECC_STRENGTH(type); |
| 4384 | chip->ecc_step_ds = NAND_ECC_STEP(type); |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 4385 | chip->onfi_timing_mode_default = |
| 4386 | type->onfi_timing_mode_default; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4387 | |
| 4388 | *busw = type->options & NAND_BUSWIDTH_16; |
| 4389 | |
| 4390 | if (!mtd->name) |
| 4391 | mtd->name = type->name; |
| 4392 | |
| 4393 | return true; |
| 4394 | } |
| 4395 | return false; |
| 4396 | } |
| 4397 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4398 | /* |
| 4399 | * Get the flash and manufacturer id and lookup if the type is supported. |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4400 | */ |
Jörg Krause | 929fb44 | 2018-01-14 19:26:37 +0100 | [diff] [blame] | 4401 | struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd, |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4402 | struct nand_chip *chip, |
Florian Fainelli | c98a935 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 4403 | int *maf_id, int *dev_id, |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4404 | struct nand_flash_dev *type) |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4405 | { |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 4406 | int busw, ret; |
| 4407 | int maf_idx; |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 4408 | u8 id_data[8]; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 4409 | |
Karl Beldan | b6322fc | 2008-09-15 16:08:03 +0200 | [diff] [blame] | 4410 | /* |
| 4411 | * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx) |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4412 | * after power-up. |
Karl Beldan | b6322fc | 2008-09-15 16:08:03 +0200 | [diff] [blame] | 4413 | */ |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 4414 | ret = nand_reset(chip, 0); |
| 4415 | if (ret) |
| 4416 | return ERR_PTR(ret); |
Boris Brezillon | 7ec6dc5 | 2017-11-22 02:38:20 +0900 | [diff] [blame] | 4417 | |
| 4418 | /* Select the device */ |
| 4419 | chip->select_chip(mtd, 0); |
Karl Beldan | b6322fc | 2008-09-15 16:08:03 +0200 | [diff] [blame] | 4420 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 4421 | /* Send the command for reading device ID */ |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 4422 | ret = nand_readid_op(chip, 0, id_data, 2); |
| 4423 | if (ret) |
| 4424 | return ERR_PTR(ret); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 4425 | |
| 4426 | /* Read manufacturer and device IDs */ |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 4427 | *maf_id = id_data[0]; |
| 4428 | *dev_id = id_data[1]; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 4429 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4430 | /* |
| 4431 | * Try again to make sure, as some systems the bus-hold or other |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 4432 | * interface concerns can cause random data which looks like a |
| 4433 | * possibly credible NAND flash to appear. If the two results do |
| 4434 | * not match, ignore the device completely. |
| 4435 | */ |
| 4436 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4437 | /* Read entire ID string */ |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 4438 | ret = nand_readid_op(chip, 0, id_data, 8); |
| 4439 | if (ret) |
| 4440 | return ERR_PTR(ret); |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 4441 | |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 4442 | if (id_data[0] != *maf_id || id_data[1] != *dev_id) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4443 | pr_info("second ID read did not match %02x,%02x against %02x,%02x\n", |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4444 | *maf_id, *dev_id, id_data[0], id_data[1]); |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 4445 | return ERR_PTR(-ENODEV); |
| 4446 | } |
| 4447 | |
Lei Wen | 75bde94 | 2011-01-06 09:48:18 +0800 | [diff] [blame] | 4448 | if (!type) |
| 4449 | type = nand_flash_ids; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 4450 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4451 | for (; type->name != NULL; type++) { |
| 4452 | if (is_full_id_nand(type)) { |
| 4453 | if (find_full_id_nand(mtd, chip, type, id_data, &busw)) |
| 4454 | goto ident_done; |
| 4455 | } else if (*dev_id == type->dev_id) { |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 4456 | break; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4457 | } |
| 4458 | } |
Lei Wen | 75bde94 | 2011-01-06 09:48:18 +0800 | [diff] [blame] | 4459 | |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 4460 | chip->onfi_version = 0; |
| 4461 | if (!type->name || !type->pagesize) { |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 4462 | /* Check if the chip is ONFI compliant */ |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4463 | if (nand_flash_detect_onfi(mtd, chip, &busw)) |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 4464 | goto ident_done; |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 4465 | |
| 4466 | /* Check if the chip is JEDEC compliant */ |
| 4467 | if (nand_flash_detect_jedec(mtd, chip, &busw)) |
| 4468 | goto ident_done; |
Florian Fainelli | d619189 | 2010-06-12 20:59:25 +0200 | [diff] [blame] | 4469 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 4470 | |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 4471 | if (!type->name) |
| 4472 | return ERR_PTR(-ENODEV); |
| 4473 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4474 | if (!mtd->name) |
| 4475 | mtd->name = type->name; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 4476 | |
Sandeep Paulraj | eab580c | 2009-10-30 13:51:23 -0400 | [diff] [blame] | 4477 | chip->chipsize = (uint64_t)type->chipsize << 20; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 4478 | |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 4479 | if (!type->pagesize) { |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4480 | /* Decode parameters from extended ID */ |
| 4481 | nand_decode_ext_id(mtd, chip, id_data, &busw); |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 4482 | } else { |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4483 | nand_decode_id(mtd, chip, type, id_data, &busw); |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 4484 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4485 | /* Get chip options */ |
Marek Vasut | fc41719 | 2012-08-30 13:39:38 +0000 | [diff] [blame] | 4486 | chip->options |= type->options; |
Florian Fainelli | c98a935 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 4487 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4488 | /* |
| 4489 | * Check if chip is not a Samsung device. Do not clear the |
| 4490 | * options for chips which do not have an extended id. |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 4491 | */ |
| 4492 | if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize) |
| 4493 | chip->options &= ~NAND_SAMSUNG_LP_OPTIONS; |
| 4494 | ident_done: |
| 4495 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4496 | /* Try to identify manufacturer */ |
| 4497 | for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) { |
| 4498 | if (nand_manuf_ids[maf_idx].id == *maf_id) |
| 4499 | break; |
| 4500 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 4501 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4502 | if (chip->options & NAND_BUSWIDTH_AUTO) { |
| 4503 | WARN_ON(chip->options & NAND_BUSWIDTH_16); |
| 4504 | chip->options |= busw; |
| 4505 | nand_set_defaults(chip, busw); |
| 4506 | } else if (busw != (chip->options & NAND_BUSWIDTH_16)) { |
| 4507 | /* |
| 4508 | * Check, if buswidth is correct. Hardware drivers should set |
| 4509 | * chip correct! |
| 4510 | */ |
| 4511 | pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n", |
| 4512 | *maf_id, *dev_id); |
| 4513 | pr_info("%s %s\n", nand_manuf_ids[maf_idx].name, mtd->name); |
| 4514 | pr_warn("bus width %d instead %d bit\n", |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4515 | (chip->options & NAND_BUSWIDTH_16) ? 16 : 8, |
| 4516 | busw ? 16 : 8); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4517 | return ERR_PTR(-EINVAL); |
| 4518 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 4519 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4520 | nand_decode_bbm_options(mtd, chip, id_data); |
| 4521 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4522 | /* Calculate the address shift from the page size */ |
| 4523 | chip->page_shift = ffs(mtd->writesize) - 1; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4524 | /* Convert chipsize to number of pages per chip -1 */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4525 | chip->pagemask = (chip->chipsize >> chip->page_shift) - 1; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 4526 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4527 | chip->bbt_erase_shift = chip->phys_erase_shift = |
| 4528 | ffs(mtd->erasesize) - 1; |
Sandeep Paulraj | eab580c | 2009-10-30 13:51:23 -0400 | [diff] [blame] | 4529 | if (chip->chipsize & 0xffffffff) |
Sandeep Paulraj | 1bc877c | 2009-11-07 14:24:06 -0500 | [diff] [blame] | 4530 | chip->chip_shift = ffs((unsigned)chip->chipsize) - 1; |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 4531 | else { |
| 4532 | chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32)); |
| 4533 | chip->chip_shift += 32 - 1; |
| 4534 | } |
| 4535 | |
Masahiro Yamada | 984926b | 2017-11-22 02:38:31 +0900 | [diff] [blame] | 4536 | if (chip->chip_shift - chip->page_shift > 16) |
| 4537 | chip->options |= NAND_ROW_ADDR_3; |
| 4538 | |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 4539 | chip->badblockbits = 8; |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 4540 | chip->erase = single_erase; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 4541 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4542 | /* Do not replace user supplied command function! */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4543 | if (mtd->writesize > 512 && chip->cmdfunc == nand_command) |
| 4544 | chip->cmdfunc = nand_command_lp; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 4545 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4546 | pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n", |
| 4547 | *maf_id, *dev_id); |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 4548 | |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 4549 | #ifdef CONFIG_SYS_NAND_ONFI_DETECTION |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 4550 | if (chip->onfi_version) |
| 4551 | pr_info("%s %s\n", nand_manuf_ids[maf_idx].name, |
| 4552 | chip->onfi_params.model); |
| 4553 | else if (chip->jedec_version) |
| 4554 | pr_info("%s %s\n", nand_manuf_ids[maf_idx].name, |
| 4555 | chip->jedec_params.model); |
| 4556 | else |
| 4557 | pr_info("%s %s\n", nand_manuf_ids[maf_idx].name, |
| 4558 | type->name); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4559 | #else |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 4560 | if (chip->jedec_version) |
| 4561 | pr_info("%s %s\n", nand_manuf_ids[maf_idx].name, |
| 4562 | chip->jedec_params.model); |
| 4563 | else |
| 4564 | pr_info("%s %s\n", nand_manuf_ids[maf_idx].name, |
| 4565 | type->name); |
| 4566 | |
| 4567 | pr_info("%s %s\n", nand_manuf_ids[maf_idx].name, |
| 4568 | type->name); |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 4569 | #endif |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 4570 | |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 4571 | pr_info("%d MiB, %s, erase size: %d KiB, page size: %d, OOB size: %d\n", |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4572 | (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC", |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 4573 | mtd->erasesize >> 10, mtd->writesize, mtd->oobsize); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4574 | return type; |
| 4575 | } |
Jörg Krause | 929fb44 | 2018-01-14 19:26:37 +0100 | [diff] [blame] | 4576 | EXPORT_SYMBOL(nand_get_flash_type); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4577 | |
Brian Norris | ba6463d | 2016-06-15 21:09:22 +0200 | [diff] [blame] | 4578 | #if CONFIG_IS_ENABLED(OF_CONTROL) |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 4579 | #include <asm/global_data.h> |
Brian Norris | ba6463d | 2016-06-15 21:09:22 +0200 | [diff] [blame] | 4580 | DECLARE_GLOBAL_DATA_PTR; |
| 4581 | |
| 4582 | static int nand_dt_init(struct mtd_info *mtd, struct nand_chip *chip, int node) |
| 4583 | { |
| 4584 | int ret, ecc_mode = -1, ecc_strength, ecc_step; |
| 4585 | const void *blob = gd->fdt_blob; |
| 4586 | const char *str; |
| 4587 | |
| 4588 | ret = fdtdec_get_int(blob, node, "nand-bus-width", -1); |
| 4589 | if (ret == 16) |
| 4590 | chip->options |= NAND_BUSWIDTH_16; |
| 4591 | |
| 4592 | if (fdtdec_get_bool(blob, node, "nand-on-flash-bbt")) |
| 4593 | chip->bbt_options |= NAND_BBT_USE_FLASH; |
| 4594 | |
| 4595 | str = fdt_getprop(blob, node, "nand-ecc-mode", NULL); |
| 4596 | if (str) { |
| 4597 | if (!strcmp(str, "none")) |
| 4598 | ecc_mode = NAND_ECC_NONE; |
| 4599 | else if (!strcmp(str, "soft")) |
| 4600 | ecc_mode = NAND_ECC_SOFT; |
| 4601 | else if (!strcmp(str, "hw")) |
| 4602 | ecc_mode = NAND_ECC_HW; |
| 4603 | else if (!strcmp(str, "hw_syndrome")) |
| 4604 | ecc_mode = NAND_ECC_HW_SYNDROME; |
| 4605 | else if (!strcmp(str, "hw_oob_first")) |
| 4606 | ecc_mode = NAND_ECC_HW_OOB_FIRST; |
| 4607 | else if (!strcmp(str, "soft_bch")) |
| 4608 | ecc_mode = NAND_ECC_SOFT_BCH; |
| 4609 | } |
| 4610 | |
| 4611 | |
| 4612 | ecc_strength = fdtdec_get_int(blob, node, "nand-ecc-strength", -1); |
| 4613 | ecc_step = fdtdec_get_int(blob, node, "nand-ecc-step-size", -1); |
| 4614 | |
| 4615 | if ((ecc_step >= 0 && !(ecc_strength >= 0)) || |
| 4616 | (!(ecc_step >= 0) && ecc_strength >= 0)) { |
| 4617 | pr_err("must set both strength and step size in DT\n"); |
| 4618 | return -EINVAL; |
| 4619 | } |
| 4620 | |
| 4621 | if (ecc_mode >= 0) |
| 4622 | chip->ecc.mode = ecc_mode; |
| 4623 | |
| 4624 | if (ecc_strength >= 0) |
| 4625 | chip->ecc.strength = ecc_strength; |
| 4626 | |
| 4627 | if (ecc_step > 0) |
| 4628 | chip->ecc.size = ecc_step; |
| 4629 | |
Boris Brezillon | f1a54b0 | 2017-11-22 02:38:13 +0900 | [diff] [blame] | 4630 | if (fdt_getprop(blob, node, "nand-ecc-maximize", NULL)) |
| 4631 | chip->ecc.options |= NAND_ECC_MAXIMIZE; |
| 4632 | |
Brian Norris | ba6463d | 2016-06-15 21:09:22 +0200 | [diff] [blame] | 4633 | return 0; |
| 4634 | } |
| 4635 | #else |
| 4636 | static int nand_dt_init(struct mtd_info *mtd, struct nand_chip *chip, int node) |
| 4637 | { |
| 4638 | return 0; |
| 4639 | } |
| 4640 | #endif /* CONFIG_IS_ENABLED(OF_CONTROL) */ |
| 4641 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4642 | /** |
| 4643 | * nand_scan_ident - [NAND Interface] Scan for the NAND device |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4644 | * @mtd: MTD device structure |
| 4645 | * @maxchips: number of chips to scan for |
| 4646 | * @table: alternative NAND ID table |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4647 | * |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4648 | * This is the first phase of the normal nand_scan() function. It reads the |
| 4649 | * flash ID and sets up MTD fields accordingly. |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4650 | * |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4651 | */ |
Lei Wen | 75bde94 | 2011-01-06 09:48:18 +0800 | [diff] [blame] | 4652 | int nand_scan_ident(struct mtd_info *mtd, int maxchips, |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4653 | struct nand_flash_dev *table) |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4654 | { |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 4655 | int i, nand_maf_id, nand_dev_id; |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 4656 | struct nand_chip *chip = mtd_to_nand(mtd); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4657 | struct nand_flash_dev *type; |
Brian Norris | ba6463d | 2016-06-15 21:09:22 +0200 | [diff] [blame] | 4658 | int ret; |
| 4659 | |
| 4660 | if (chip->flash_node) { |
| 4661 | ret = nand_dt_init(mtd, chip, chip->flash_node); |
| 4662 | if (ret) |
| 4663 | return ret; |
| 4664 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 4665 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4666 | /* Set the default functions */ |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 4667 | nand_set_defaults(chip, chip->options & NAND_BUSWIDTH_16); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4668 | |
| 4669 | /* Read the flash type */ |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 4670 | type = nand_get_flash_type(mtd, chip, &nand_maf_id, |
| 4671 | &nand_dev_id, table); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4672 | |
| 4673 | if (IS_ERR(type)) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4674 | if (!(chip->options & NAND_SCAN_SILENT_NODEV)) |
| 4675 | pr_warn("No NAND device found\n"); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4676 | chip->select_chip(mtd, -1); |
| 4677 | return PTR_ERR(type); |
| 4678 | } |
| 4679 | |
Boris Brezillon | 7ec6dc5 | 2017-11-22 02:38:20 +0900 | [diff] [blame] | 4680 | /* Initialize the ->data_interface field. */ |
Boris Brezillon | e509cba | 2017-11-22 02:38:19 +0900 | [diff] [blame] | 4681 | ret = nand_init_data_interface(chip); |
| 4682 | if (ret) |
| 4683 | return ret; |
| 4684 | |
Boris Brezillon | 7ec6dc5 | 2017-11-22 02:38:20 +0900 | [diff] [blame] | 4685 | /* |
| 4686 | * Setup the data interface correctly on the chip and controller side. |
| 4687 | * This explicit call to nand_setup_data_interface() is only required |
| 4688 | * for the first die, because nand_reset() has been called before |
| 4689 | * ->data_interface and ->default_onfi_timing_mode were set. |
| 4690 | * For the other dies, nand_reset() will automatically switch to the |
| 4691 | * best mode for us. |
| 4692 | */ |
Boris Brezillon | 32935f4 | 2017-11-22 02:38:28 +0900 | [diff] [blame] | 4693 | ret = nand_setup_data_interface(chip, 0); |
Boris Brezillon | 7ec6dc5 | 2017-11-22 02:38:20 +0900 | [diff] [blame] | 4694 | if (ret) |
| 4695 | return ret; |
| 4696 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4697 | chip->select_chip(mtd, -1); |
| 4698 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4699 | /* Check for a chip array */ |
| 4700 | for (i = 1; i < maxchips; i++) { |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 4701 | u8 id[2]; |
| 4702 | |
Karl Beldan | b6322fc | 2008-09-15 16:08:03 +0200 | [diff] [blame] | 4703 | /* See comment in nand_get_flash_type for reset */ |
Boris Brezillon | 7ec6dc5 | 2017-11-22 02:38:20 +0900 | [diff] [blame] | 4704 | nand_reset(chip, i); |
| 4705 | |
| 4706 | chip->select_chip(mtd, i); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4707 | /* Send the command for reading device ID */ |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 4708 | nand_readid_op(chip, 0, id, sizeof(id)); |
| 4709 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 4710 | /* Read manufacturer and device IDs */ |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 4711 | if (nand_maf_id != id[0] || nand_dev_id != id[1]) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4712 | chip->select_chip(mtd, -1); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 4713 | break; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4714 | } |
| 4715 | chip->select_chip(mtd, -1); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 4716 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4717 | |
Wolfgang Grandegger | b325d7e | 2009-02-11 18:38:20 +0100 | [diff] [blame] | 4718 | #ifdef DEBUG |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 4719 | if (i > 1) |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4720 | pr_info("%d chips detected\n", i); |
Wolfgang Grandegger | b325d7e | 2009-02-11 18:38:20 +0100 | [diff] [blame] | 4721 | #endif |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 4722 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4723 | /* Store the number of chips and calc total size for mtd */ |
| 4724 | chip->numchips = i; |
| 4725 | mtd->size = i * chip->chipsize; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 4726 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4727 | return 0; |
| 4728 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4729 | EXPORT_SYMBOL(nand_scan_ident); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 4730 | |
Masahiro Yamada | 820eb48 | 2017-11-22 02:38:29 +0900 | [diff] [blame] | 4731 | /** |
| 4732 | * nand_check_ecc_caps - check the sanity of preset ECC settings |
| 4733 | * @chip: nand chip info structure |
| 4734 | * @caps: ECC caps info structure |
| 4735 | * @oobavail: OOB size that the ECC engine can use |
| 4736 | * |
| 4737 | * When ECC step size and strength are already set, check if they are supported |
| 4738 | * by the controller and the calculated ECC bytes fit within the chip's OOB. |
| 4739 | * On success, the calculated ECC bytes is set. |
| 4740 | */ |
| 4741 | int nand_check_ecc_caps(struct nand_chip *chip, |
| 4742 | const struct nand_ecc_caps *caps, int oobavail) |
| 4743 | { |
| 4744 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 4745 | const struct nand_ecc_step_info *stepinfo; |
| 4746 | int preset_step = chip->ecc.size; |
| 4747 | int preset_strength = chip->ecc.strength; |
| 4748 | int nsteps, ecc_bytes; |
| 4749 | int i, j; |
| 4750 | |
| 4751 | if (WARN_ON(oobavail < 0)) |
| 4752 | return -EINVAL; |
| 4753 | |
| 4754 | if (!preset_step || !preset_strength) |
| 4755 | return -ENODATA; |
| 4756 | |
| 4757 | nsteps = mtd->writesize / preset_step; |
| 4758 | |
| 4759 | for (i = 0; i < caps->nstepinfos; i++) { |
| 4760 | stepinfo = &caps->stepinfos[i]; |
| 4761 | |
| 4762 | if (stepinfo->stepsize != preset_step) |
| 4763 | continue; |
| 4764 | |
| 4765 | for (j = 0; j < stepinfo->nstrengths; j++) { |
| 4766 | if (stepinfo->strengths[j] != preset_strength) |
| 4767 | continue; |
| 4768 | |
| 4769 | ecc_bytes = caps->calc_ecc_bytes(preset_step, |
| 4770 | preset_strength); |
| 4771 | if (WARN_ON_ONCE(ecc_bytes < 0)) |
| 4772 | return ecc_bytes; |
| 4773 | |
| 4774 | if (ecc_bytes * nsteps > oobavail) { |
| 4775 | pr_err("ECC (step, strength) = (%d, %d) does not fit in OOB", |
| 4776 | preset_step, preset_strength); |
| 4777 | return -ENOSPC; |
| 4778 | } |
| 4779 | |
| 4780 | chip->ecc.bytes = ecc_bytes; |
| 4781 | |
| 4782 | return 0; |
| 4783 | } |
| 4784 | } |
| 4785 | |
| 4786 | pr_err("ECC (step, strength) = (%d, %d) not supported on this controller", |
| 4787 | preset_step, preset_strength); |
| 4788 | |
| 4789 | return -ENOTSUPP; |
| 4790 | } |
| 4791 | EXPORT_SYMBOL_GPL(nand_check_ecc_caps); |
| 4792 | |
| 4793 | /** |
| 4794 | * nand_match_ecc_req - meet the chip's requirement with least ECC bytes |
| 4795 | * @chip: nand chip info structure |
| 4796 | * @caps: ECC engine caps info structure |
| 4797 | * @oobavail: OOB size that the ECC engine can use |
| 4798 | * |
| 4799 | * If a chip's ECC requirement is provided, try to meet it with the least |
| 4800 | * number of ECC bytes (i.e. with the largest number of OOB-free bytes). |
| 4801 | * On success, the chosen ECC settings are set. |
| 4802 | */ |
| 4803 | int nand_match_ecc_req(struct nand_chip *chip, |
| 4804 | const struct nand_ecc_caps *caps, int oobavail) |
| 4805 | { |
| 4806 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 4807 | const struct nand_ecc_step_info *stepinfo; |
| 4808 | int req_step = chip->ecc_step_ds; |
| 4809 | int req_strength = chip->ecc_strength_ds; |
| 4810 | int req_corr, step_size, strength, nsteps, ecc_bytes, ecc_bytes_total; |
| 4811 | int best_step, best_strength, best_ecc_bytes; |
| 4812 | int best_ecc_bytes_total = INT_MAX; |
| 4813 | int i, j; |
| 4814 | |
| 4815 | if (WARN_ON(oobavail < 0)) |
| 4816 | return -EINVAL; |
| 4817 | |
| 4818 | /* No information provided by the NAND chip */ |
| 4819 | if (!req_step || !req_strength) |
| 4820 | return -ENOTSUPP; |
| 4821 | |
| 4822 | /* number of correctable bits the chip requires in a page */ |
| 4823 | req_corr = mtd->writesize / req_step * req_strength; |
| 4824 | |
| 4825 | for (i = 0; i < caps->nstepinfos; i++) { |
| 4826 | stepinfo = &caps->stepinfos[i]; |
| 4827 | step_size = stepinfo->stepsize; |
| 4828 | |
| 4829 | for (j = 0; j < stepinfo->nstrengths; j++) { |
| 4830 | strength = stepinfo->strengths[j]; |
| 4831 | |
| 4832 | /* |
| 4833 | * If both step size and strength are smaller than the |
| 4834 | * chip's requirement, it is not easy to compare the |
| 4835 | * resulted reliability. |
| 4836 | */ |
| 4837 | if (step_size < req_step && strength < req_strength) |
| 4838 | continue; |
| 4839 | |
| 4840 | if (mtd->writesize % step_size) |
| 4841 | continue; |
| 4842 | |
| 4843 | nsteps = mtd->writesize / step_size; |
| 4844 | |
| 4845 | ecc_bytes = caps->calc_ecc_bytes(step_size, strength); |
| 4846 | if (WARN_ON_ONCE(ecc_bytes < 0)) |
| 4847 | continue; |
| 4848 | ecc_bytes_total = ecc_bytes * nsteps; |
| 4849 | |
| 4850 | if (ecc_bytes_total > oobavail || |
| 4851 | strength * nsteps < req_corr) |
| 4852 | continue; |
| 4853 | |
| 4854 | /* |
| 4855 | * We assume the best is to meet the chip's requrement |
| 4856 | * with the least number of ECC bytes. |
| 4857 | */ |
| 4858 | if (ecc_bytes_total < best_ecc_bytes_total) { |
| 4859 | best_ecc_bytes_total = ecc_bytes_total; |
| 4860 | best_step = step_size; |
| 4861 | best_strength = strength; |
| 4862 | best_ecc_bytes = ecc_bytes; |
| 4863 | } |
| 4864 | } |
| 4865 | } |
| 4866 | |
| 4867 | if (best_ecc_bytes_total == INT_MAX) |
| 4868 | return -ENOTSUPP; |
| 4869 | |
| 4870 | chip->ecc.size = best_step; |
| 4871 | chip->ecc.strength = best_strength; |
| 4872 | chip->ecc.bytes = best_ecc_bytes; |
| 4873 | |
| 4874 | return 0; |
| 4875 | } |
| 4876 | EXPORT_SYMBOL_GPL(nand_match_ecc_req); |
| 4877 | |
| 4878 | /** |
| 4879 | * nand_maximize_ecc - choose the max ECC strength available |
| 4880 | * @chip: nand chip info structure |
| 4881 | * @caps: ECC engine caps info structure |
| 4882 | * @oobavail: OOB size that the ECC engine can use |
| 4883 | * |
| 4884 | * Choose the max ECC strength that is supported on the controller, and can fit |
| 4885 | * within the chip's OOB. On success, the chosen ECC settings are set. |
| 4886 | */ |
| 4887 | int nand_maximize_ecc(struct nand_chip *chip, |
| 4888 | const struct nand_ecc_caps *caps, int oobavail) |
| 4889 | { |
| 4890 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 4891 | const struct nand_ecc_step_info *stepinfo; |
| 4892 | int step_size, strength, nsteps, ecc_bytes, corr; |
| 4893 | int best_corr = 0; |
| 4894 | int best_step = 0; |
| 4895 | int best_strength, best_ecc_bytes; |
| 4896 | int i, j; |
| 4897 | |
| 4898 | if (WARN_ON(oobavail < 0)) |
| 4899 | return -EINVAL; |
| 4900 | |
| 4901 | for (i = 0; i < caps->nstepinfos; i++) { |
| 4902 | stepinfo = &caps->stepinfos[i]; |
| 4903 | step_size = stepinfo->stepsize; |
| 4904 | |
| 4905 | /* If chip->ecc.size is already set, respect it */ |
| 4906 | if (chip->ecc.size && step_size != chip->ecc.size) |
| 4907 | continue; |
| 4908 | |
| 4909 | for (j = 0; j < stepinfo->nstrengths; j++) { |
| 4910 | strength = stepinfo->strengths[j]; |
| 4911 | |
| 4912 | if (mtd->writesize % step_size) |
| 4913 | continue; |
| 4914 | |
| 4915 | nsteps = mtd->writesize / step_size; |
| 4916 | |
| 4917 | ecc_bytes = caps->calc_ecc_bytes(step_size, strength); |
| 4918 | if (WARN_ON_ONCE(ecc_bytes < 0)) |
| 4919 | continue; |
| 4920 | |
| 4921 | if (ecc_bytes * nsteps > oobavail) |
| 4922 | continue; |
| 4923 | |
| 4924 | corr = strength * nsteps; |
| 4925 | |
| 4926 | /* |
| 4927 | * If the number of correctable bits is the same, |
| 4928 | * bigger step_size has more reliability. |
| 4929 | */ |
| 4930 | if (corr > best_corr || |
| 4931 | (corr == best_corr && step_size > best_step)) { |
| 4932 | best_corr = corr; |
| 4933 | best_step = step_size; |
| 4934 | best_strength = strength; |
| 4935 | best_ecc_bytes = ecc_bytes; |
| 4936 | } |
| 4937 | } |
| 4938 | } |
| 4939 | |
| 4940 | if (!best_corr) |
| 4941 | return -ENOTSUPP; |
| 4942 | |
| 4943 | chip->ecc.size = best_step; |
| 4944 | chip->ecc.strength = best_strength; |
| 4945 | chip->ecc.bytes = best_ecc_bytes; |
| 4946 | |
| 4947 | return 0; |
| 4948 | } |
| 4949 | EXPORT_SYMBOL_GPL(nand_maximize_ecc); |
| 4950 | |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 4951 | /* |
| 4952 | * Check if the chip configuration meet the datasheet requirements. |
| 4953 | |
| 4954 | * If our configuration corrects A bits per B bytes and the minimum |
| 4955 | * required correction level is X bits per Y bytes, then we must ensure |
| 4956 | * both of the following are true: |
| 4957 | * |
| 4958 | * (1) A / B >= X / Y |
| 4959 | * (2) A >= X |
| 4960 | * |
| 4961 | * Requirement (1) ensures we can correct for the required bitflip density. |
| 4962 | * Requirement (2) ensures we can correct even when all bitflips are clumped |
| 4963 | * in the same sector. |
| 4964 | */ |
| 4965 | static bool nand_ecc_strength_good(struct mtd_info *mtd) |
| 4966 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 4967 | struct nand_chip *chip = mtd_to_nand(mtd); |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 4968 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 4969 | int corr, ds_corr; |
| 4970 | |
| 4971 | if (ecc->size == 0 || chip->ecc_step_ds == 0) |
| 4972 | /* Not enough information */ |
| 4973 | return true; |
| 4974 | |
| 4975 | /* |
| 4976 | * We get the number of corrected bits per page to compare |
| 4977 | * the correction density. |
| 4978 | */ |
| 4979 | corr = (mtd->writesize * ecc->strength) / ecc->size; |
| 4980 | ds_corr = (mtd->writesize * chip->ecc_strength_ds) / chip->ecc_step_ds; |
| 4981 | |
| 4982 | return corr >= ds_corr && ecc->strength >= chip->ecc_strength_ds; |
| 4983 | } |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4984 | |
Marc Gonzalez | c3a2985 | 2017-11-22 02:38:22 +0900 | [diff] [blame] | 4985 | static bool invalid_ecc_page_accessors(struct nand_chip *chip) |
| 4986 | { |
| 4987 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 4988 | |
| 4989 | if (nand_standard_page_accessors(ecc)) |
| 4990 | return false; |
| 4991 | |
| 4992 | /* |
| 4993 | * NAND_ECC_CUSTOM_PAGE_ACCESS flag is set, make sure the NAND |
| 4994 | * controller driver implements all the page accessors because |
| 4995 | * default helpers are not suitable when the core does not |
| 4996 | * send the READ0/PAGEPROG commands. |
| 4997 | */ |
| 4998 | return (!ecc->read_page || !ecc->write_page || |
| 4999 | !ecc->read_page_raw || !ecc->write_page_raw || |
| 5000 | (NAND_HAS_SUBPAGE_READ(chip) && !ecc->read_subpage) || |
| 5001 | (NAND_HAS_SUBPAGE_WRITE(chip) && !ecc->write_subpage && |
| 5002 | ecc->hwctl && ecc->calculate)); |
| 5003 | } |
| 5004 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5005 | /** |
| 5006 | * nand_scan_tail - [NAND Interface] Scan for the NAND device |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 5007 | * @mtd: MTD device structure |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5008 | * |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 5009 | * This is the second phase of the normal nand_scan() function. It fills out |
| 5010 | * all the uninitialized function pointers with the defaults and scans for a |
| 5011 | * bad block table if appropriate. |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5012 | */ |
| 5013 | int nand_scan_tail(struct mtd_info *mtd) |
| 5014 | { |
| 5015 | int i; |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 5016 | struct nand_chip *chip = mtd_to_nand(mtd); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5017 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 5018 | struct nand_buffers *nbuf; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 5019 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 5020 | /* New bad blocks should be marked in OOB, flash-based BBT, or both */ |
| 5021 | BUG_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) && |
| 5022 | !(chip->bbt_options & NAND_BBT_USE_FLASH)); |
| 5023 | |
Marc Gonzalez | c3a2985 | 2017-11-22 02:38:22 +0900 | [diff] [blame] | 5024 | if (invalid_ecc_page_accessors(chip)) { |
| 5025 | pr_err("Invalid ECC page accessors setup\n"); |
| 5026 | return -EINVAL; |
| 5027 | } |
| 5028 | |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 5029 | if (!(chip->options & NAND_OWN_BUFFERS)) { |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 5030 | nbuf = kzalloc(sizeof(struct nand_buffers), GFP_KERNEL); |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 5031 | chip->buffers = nbuf; |
| 5032 | } else { |
| 5033 | if (!chip->buffers) |
| 5034 | return -ENOMEM; |
| 5035 | } |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5036 | |
| 5037 | /* Set the internal oob buffer location, just after the page data */ |
| 5038 | chip->oob_poi = chip->buffers->databuf + mtd->writesize; |
| 5039 | |
| 5040 | /* |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 5041 | * If no default placement scheme is given, select an appropriate one. |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5042 | */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5043 | if (!ecc->layout && (ecc->mode != NAND_ECC_SOFT_BCH)) { |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 5044 | switch (mtd->oobsize) { |
Gregory CLEMENT | e5b9631 | 2019-04-17 11:22:05 +0200 | [diff] [blame] | 5045 | #ifndef CONFIG_SYS_NAND_DRIVER_ECC_LAYOUT |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 5046 | case 8: |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5047 | ecc->layout = &nand_oob_8; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 5048 | break; |
| 5049 | case 16: |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5050 | ecc->layout = &nand_oob_16; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 5051 | break; |
| 5052 | case 64: |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5053 | ecc->layout = &nand_oob_64; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 5054 | break; |
Sergei Poselenov | 04fbaa0 | 2008-06-06 15:42:43 +0200 | [diff] [blame] | 5055 | case 128: |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5056 | ecc->layout = &nand_oob_128; |
Sergei Poselenov | 04fbaa0 | 2008-06-06 15:42:43 +0200 | [diff] [blame] | 5057 | break; |
Stefan Agner | bd18614 | 2018-12-06 14:57:09 +0100 | [diff] [blame] | 5058 | #endif |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 5059 | default: |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 5060 | pr_warn("No oob scheme defined for oobsize %d\n", |
| 5061 | mtd->oobsize); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5062 | BUG(); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 5063 | } |
| 5064 | } |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 5065 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5066 | if (!chip->write_page) |
| 5067 | chip->write_page = nand_write_page; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 5068 | |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 5069 | /* |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 5070 | * Check ECC mode, default to software if 3byte/512byte hardware ECC is |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5071 | * selected and we have 256 byte pagesize fallback to software ECC |
| 5072 | */ |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 5073 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5074 | switch (ecc->mode) { |
Sandeep Paulraj | dea4070 | 2009-08-10 13:27:56 -0400 | [diff] [blame] | 5075 | case NAND_ECC_HW_OOB_FIRST: |
| 5076 | /* Similar to NAND_ECC_HW, but a separate read_page handle */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5077 | if (!ecc->calculate || !ecc->correct || !ecc->hwctl) { |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 5078 | pr_warn("No ECC functions supplied; hardware ECC not possible\n"); |
Sandeep Paulraj | dea4070 | 2009-08-10 13:27:56 -0400 | [diff] [blame] | 5079 | BUG(); |
| 5080 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5081 | if (!ecc->read_page) |
| 5082 | ecc->read_page = nand_read_page_hwecc_oob_first; |
Sandeep Paulraj | dea4070 | 2009-08-10 13:27:56 -0400 | [diff] [blame] | 5083 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5084 | case NAND_ECC_HW: |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 5085 | /* Use standard hwecc read page function? */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5086 | if (!ecc->read_page) |
| 5087 | ecc->read_page = nand_read_page_hwecc; |
| 5088 | if (!ecc->write_page) |
| 5089 | ecc->write_page = nand_write_page_hwecc; |
| 5090 | if (!ecc->read_page_raw) |
| 5091 | ecc->read_page_raw = nand_read_page_raw; |
| 5092 | if (!ecc->write_page_raw) |
| 5093 | ecc->write_page_raw = nand_write_page_raw; |
| 5094 | if (!ecc->read_oob) |
| 5095 | ecc->read_oob = nand_read_oob_std; |
| 5096 | if (!ecc->write_oob) |
| 5097 | ecc->write_oob = nand_write_oob_std; |
| 5098 | if (!ecc->read_subpage) |
| 5099 | ecc->read_subpage = nand_read_subpage; |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 5100 | if (!ecc->write_subpage && ecc->hwctl && ecc->calculate) |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5101 | ecc->write_subpage = nand_write_subpage_hwecc; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 5102 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5103 | case NAND_ECC_HW_SYNDROME: |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5104 | if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) && |
| 5105 | (!ecc->read_page || |
| 5106 | ecc->read_page == nand_read_page_hwecc || |
| 5107 | !ecc->write_page || |
| 5108 | ecc->write_page == nand_write_page_hwecc)) { |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 5109 | pr_warn("No ECC functions supplied; hardware ECC not possible\n"); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5110 | BUG(); |
| 5111 | } |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 5112 | /* Use standard syndrome read/write page function? */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5113 | if (!ecc->read_page) |
| 5114 | ecc->read_page = nand_read_page_syndrome; |
| 5115 | if (!ecc->write_page) |
| 5116 | ecc->write_page = nand_write_page_syndrome; |
| 5117 | if (!ecc->read_page_raw) |
| 5118 | ecc->read_page_raw = nand_read_page_raw_syndrome; |
| 5119 | if (!ecc->write_page_raw) |
| 5120 | ecc->write_page_raw = nand_write_page_raw_syndrome; |
| 5121 | if (!ecc->read_oob) |
| 5122 | ecc->read_oob = nand_read_oob_syndrome; |
| 5123 | if (!ecc->write_oob) |
| 5124 | ecc->write_oob = nand_write_oob_syndrome; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 5125 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5126 | if (mtd->writesize >= ecc->size) { |
| 5127 | if (!ecc->strength) { |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 5128 | pr_warn("Driver must set ecc.strength when using hardware ECC\n"); |
| 5129 | BUG(); |
| 5130 | } |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5131 | break; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 5132 | } |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 5133 | pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n", |
| 5134 | ecc->size, mtd->writesize); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5135 | ecc->mode = NAND_ECC_SOFT; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 5136 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5137 | case NAND_ECC_SOFT: |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5138 | ecc->calculate = nand_calculate_ecc; |
| 5139 | ecc->correct = nand_correct_data; |
| 5140 | ecc->read_page = nand_read_page_swecc; |
| 5141 | ecc->read_subpage = nand_read_subpage; |
| 5142 | ecc->write_page = nand_write_page_swecc; |
| 5143 | ecc->read_page_raw = nand_read_page_raw; |
| 5144 | ecc->write_page_raw = nand_write_page_raw; |
| 5145 | ecc->read_oob = nand_read_oob_std; |
| 5146 | ecc->write_oob = nand_write_oob_std; |
| 5147 | if (!ecc->size) |
| 5148 | ecc->size = 256; |
| 5149 | ecc->bytes = 3; |
| 5150 | ecc->strength = 1; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 5151 | break; |
| 5152 | |
Christian Hitz | 55f7bca | 2011-10-12 09:31:59 +0200 | [diff] [blame] | 5153 | case NAND_ECC_SOFT_BCH: |
| 5154 | if (!mtd_nand_has_bch()) { |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 5155 | pr_warn("CONFIG_MTD_NAND_ECC_BCH not enabled\n"); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5156 | BUG(); |
Christian Hitz | 55f7bca | 2011-10-12 09:31:59 +0200 | [diff] [blame] | 5157 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5158 | ecc->calculate = nand_bch_calculate_ecc; |
| 5159 | ecc->correct = nand_bch_correct_data; |
| 5160 | ecc->read_page = nand_read_page_swecc; |
| 5161 | ecc->read_subpage = nand_read_subpage; |
| 5162 | ecc->write_page = nand_write_page_swecc; |
| 5163 | ecc->read_page_raw = nand_read_page_raw; |
| 5164 | ecc->write_page_raw = nand_write_page_raw; |
| 5165 | ecc->read_oob = nand_read_oob_std; |
| 5166 | ecc->write_oob = nand_write_oob_std; |
Christian Hitz | 55f7bca | 2011-10-12 09:31:59 +0200 | [diff] [blame] | 5167 | /* |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 5168 | * Board driver should supply ecc.size and ecc.strength values |
| 5169 | * to select how many bits are correctable. Otherwise, default |
| 5170 | * to 4 bits for large page devices. |
Christian Hitz | 55f7bca | 2011-10-12 09:31:59 +0200 | [diff] [blame] | 5171 | */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5172 | if (!ecc->size && (mtd->oobsize >= 64)) { |
| 5173 | ecc->size = 512; |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 5174 | ecc->strength = 4; |
Christian Hitz | 55f7bca | 2011-10-12 09:31:59 +0200 | [diff] [blame] | 5175 | } |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 5176 | |
| 5177 | /* See nand_bch_init() for details. */ |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 5178 | ecc->bytes = 0; |
| 5179 | ecc->priv = nand_bch_init(mtd); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5180 | if (!ecc->priv) { |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 5181 | pr_warn("BCH ECC initialization failed!\n"); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5182 | BUG(); |
| 5183 | } |
Christian Hitz | 55f7bca | 2011-10-12 09:31:59 +0200 | [diff] [blame] | 5184 | break; |
| 5185 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5186 | case NAND_ECC_NONE: |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 5187 | pr_warn("NAND_ECC_NONE selected by board driver. This is not recommended!\n"); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5188 | ecc->read_page = nand_read_page_raw; |
| 5189 | ecc->write_page = nand_write_page_raw; |
| 5190 | ecc->read_oob = nand_read_oob_std; |
| 5191 | ecc->read_page_raw = nand_read_page_raw; |
| 5192 | ecc->write_page_raw = nand_write_page_raw; |
| 5193 | ecc->write_oob = nand_write_oob_std; |
| 5194 | ecc->size = mtd->writesize; |
| 5195 | ecc->bytes = 0; |
| 5196 | ecc->strength = 0; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 5197 | break; |
| 5198 | |
| 5199 | default: |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5200 | pr_warn("Invalid NAND_ECC_MODE %d\n", ecc->mode); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5201 | BUG(); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 5202 | } |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 5203 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 5204 | /* For many systems, the standard OOB write also works for raw */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5205 | if (!ecc->read_oob_raw) |
| 5206 | ecc->read_oob_raw = ecc->read_oob; |
| 5207 | if (!ecc->write_oob_raw) |
| 5208 | ecc->write_oob_raw = ecc->write_oob; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 5209 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5210 | /* |
| 5211 | * The number of bytes available for a client to place data into |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 5212 | * the out of band area. |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5213 | */ |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 5214 | mtd->oobavail = 0; |
| 5215 | if (ecc->layout) { |
| 5216 | for (i = 0; ecc->layout->oobfree[i].length; i++) |
| 5217 | mtd->oobavail += ecc->layout->oobfree[i].length; |
| 5218 | } |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 5219 | |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 5220 | /* ECC sanity check: warn if it's too weak */ |
| 5221 | if (!nand_ecc_strength_good(mtd)) |
| 5222 | pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n", |
| 5223 | mtd->name); |
| 5224 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5225 | /* |
| 5226 | * Set the number of read / write steps for one page depending on ECC |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 5227 | * mode. |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5228 | */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5229 | ecc->steps = mtd->writesize / ecc->size; |
| 5230 | if (ecc->steps * ecc->size != mtd->writesize) { |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 5231 | pr_warn("Invalid ECC parameters\n"); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5232 | BUG(); |
| 5233 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5234 | ecc->total = ecc->steps * ecc->bytes; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 5235 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 5236 | /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5237 | if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) { |
| 5238 | switch (ecc->steps) { |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5239 | case 2: |
| 5240 | mtd->subpage_sft = 1; |
| 5241 | break; |
| 5242 | case 4: |
| 5243 | case 8: |
Sandeep Paulraj | fd9874d | 2009-11-07 14:24:34 -0500 | [diff] [blame] | 5244 | case 16: |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5245 | mtd->subpage_sft = 2; |
| 5246 | break; |
| 5247 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 5248 | } |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5249 | chip->subpagesize = mtd->writesize >> mtd->subpage_sft; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 5250 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5251 | /* Initialize state */ |
| 5252 | chip->state = FL_READY; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 5253 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 5254 | /* Invalidate the pagebuffer reference */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5255 | chip->pagebuf = -1; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 5256 | |
Joe Hershberger | 7a38ffa | 2012-11-05 06:46:31 +0000 | [diff] [blame] | 5257 | /* Large page NAND with SOFT_ECC should support subpage reads */ |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 5258 | switch (ecc->mode) { |
| 5259 | case NAND_ECC_SOFT: |
| 5260 | case NAND_ECC_SOFT_BCH: |
| 5261 | if (chip->page_shift > 9) |
| 5262 | chip->options |= NAND_SUBPAGE_READ; |
| 5263 | break; |
| 5264 | |
| 5265 | default: |
| 5266 | break; |
| 5267 | } |
Joe Hershberger | 7a38ffa | 2012-11-05 06:46:31 +0000 | [diff] [blame] | 5268 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 5269 | /* Fill in remaining MTD driver data */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5270 | mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH; |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 5271 | mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM : |
| 5272 | MTD_CAP_NANDFLASH; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 5273 | mtd->_erase = nand_erase; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5274 | mtd->_panic_write = panic_nand_write; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 5275 | mtd->_read_oob = nand_read_oob; |
| 5276 | mtd->_write_oob = nand_write_oob; |
| 5277 | mtd->_sync = nand_sync; |
| 5278 | mtd->_lock = NULL; |
| 5279 | mtd->_unlock = NULL; |
Ezequiel Garcia | fc9d57c | 2014-05-21 19:06:12 -0300 | [diff] [blame] | 5280 | mtd->_block_isreserved = nand_block_isreserved; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 5281 | mtd->_block_isbad = nand_block_isbad; |
| 5282 | mtd->_block_markbad = nand_block_markbad; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5283 | mtd->writebufsize = mtd->writesize; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 5284 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 5285 | /* propagate ecc info to mtd_info */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5286 | mtd->ecclayout = ecc->layout; |
| 5287 | mtd->ecc_strength = ecc->strength; |
| 5288 | mtd->ecc_step_size = ecc->size; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 5289 | /* |
| 5290 | * Initialize bitflip_threshold to its default prior scan_bbt() call. |
| 5291 | * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be |
| 5292 | * properly set. |
| 5293 | */ |
| 5294 | if (!mtd->bitflip_threshold) |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 5295 | mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5296 | |
Rostislav Lisovy | dc17bdc | 2014-10-22 13:40:44 +0200 | [diff] [blame] | 5297 | return 0; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5298 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5299 | EXPORT_SYMBOL(nand_scan_tail); |
| 5300 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5301 | /** |
| 5302 | * nand_scan - [NAND Interface] Scan for the NAND device |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 5303 | * @mtd: MTD device structure |
| 5304 | * @maxchips: number of chips to scan for |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5305 | * |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 5306 | * This fills out all the uninitialized function pointers with the defaults. |
| 5307 | * The flash ID is read and the mtd/chip structures are filled with the |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 5308 | * appropriate values. |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5309 | */ |
| 5310 | int nand_scan(struct mtd_info *mtd, int maxchips) |
| 5311 | { |
| 5312 | int ret; |
| 5313 | |
Lei Wen | 75bde94 | 2011-01-06 09:48:18 +0800 | [diff] [blame] | 5314 | ret = nand_scan_ident(mtd, maxchips, NULL); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5315 | if (!ret) |
| 5316 | ret = nand_scan_tail(mtd); |
| 5317 | return ret; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 5318 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5319 | EXPORT_SYMBOL(nand_scan); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 5320 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5321 | MODULE_LICENSE("GPL"); |
| 5322 | MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>"); |
| 5323 | MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>"); |
| 5324 | MODULE_DESCRIPTION("Generic NAND flash driver code"); |