Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1 | /* |
Wolfgang Denk | 932394a | 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 | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 5 | * |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 6 | * Additional technical information is available on |
Scott Wood | c45912d | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 7 | * http://www.linux-mtd.infradead.org/doc/nand.html |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 8 | * |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 9 | * Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com) |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 10 | * 2002-2006 Thomas Gleixner (tglx@linutronix.de) |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 11 | * |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 12 | * Credits: |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 13 | * David Woodhouse for adding multichip support |
| 14 | * |
Wolfgang Denk | 932394a | 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 | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 18 | * TODO: |
Wolfgang Denk | 932394a | 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 | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 21 | * if we have HW ECC support. |
Scott Wood | c45912d | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 22 | * BBT table is not serialized, has to be fixed |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 23 | * |
Wolfgang Denk | 932394a | 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 | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 30 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 31 | #include <common.h> |
Brian Norris | 42bd19c | 2016-06-15 21:09:22 +0200 | [diff] [blame] | 32 | #if CONFIG_IS_ENABLED(OF_CONTROL) |
| 33 | #include <fdtdec.h> |
| 34 | #endif |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 35 | #include <malloc.h> |
| 36 | #include <watchdog.h> |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 37 | #include <linux/err.h> |
Mike Frysinger | 7b15e2b | 2012-04-09 13:39:55 +0000 | [diff] [blame] | 38 | #include <linux/compat.h> |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 39 | #include <linux/mtd/mtd.h> |
| 40 | #include <linux/mtd/nand.h> |
| 41 | #include <linux/mtd/nand_ecc.h> |
Christian Hitz | 4c6de85 | 2011-10-12 09:31:59 +0200 | [diff] [blame] | 42 | #include <linux/mtd/nand_bch.h> |
Stefan Roese | 10bb62d | 2009-04-24 15:58:33 +0200 | [diff] [blame] | 43 | #ifdef CONFIG_MTD_PARTITIONS |
| 44 | #include <linux/mtd/partitions.h> |
| 45 | #endif |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 46 | #include <asm/io.h> |
Masahiro Yamada | 1221ce4 | 2016-09-21 11:28:55 +0900 | [diff] [blame] | 47 | #include <linux/errno.h> |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 48 | |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 49 | /* Define default oob placement schemes for large and small page devices */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 50 | static struct nand_ecclayout nand_oob_8 = { |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 51 | .eccbytes = 3, |
| 52 | .eccpos = {0, 1, 2}, |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 53 | .oobfree = { |
| 54 | {.offset = 3, |
| 55 | .length = 2}, |
| 56 | {.offset = 6, |
Christian Hitz | 90e3f39 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 57 | .length = 2} } |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 58 | }; |
| 59 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 60 | static struct nand_ecclayout nand_oob_16 = { |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 61 | .eccbytes = 6, |
| 62 | .eccpos = {0, 1, 2, 3, 6, 7}, |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 63 | .oobfree = { |
| 64 | {.offset = 8, |
Christian Hitz | 90e3f39 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 65 | . length = 8} } |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 66 | }; |
| 67 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 68 | static struct nand_ecclayout nand_oob_64 = { |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 69 | .eccbytes = 24, |
| 70 | .eccpos = { |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 71 | 40, 41, 42, 43, 44, 45, 46, 47, |
| 72 | 48, 49, 50, 51, 52, 53, 54, 55, |
| 73 | 56, 57, 58, 59, 60, 61, 62, 63}, |
| 74 | .oobfree = { |
| 75 | {.offset = 2, |
Christian Hitz | 90e3f39 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 76 | .length = 38} } |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 77 | }; |
| 78 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 79 | static struct nand_ecclayout nand_oob_128 = { |
Sergei Poselenov | 248ae5c | 2008-06-06 15:42:43 +0200 | [diff] [blame] | 80 | .eccbytes = 48, |
| 81 | .eccpos = { |
Christian Hitz | 90e3f39 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 82 | 80, 81, 82, 83, 84, 85, 86, 87, |
| 83 | 88, 89, 90, 91, 92, 93, 94, 95, |
| 84 | 96, 97, 98, 99, 100, 101, 102, 103, |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 85 | 104, 105, 106, 107, 108, 109, 110, 111, |
| 86 | 112, 113, 114, 115, 116, 117, 118, 119, |
| 87 | 120, 121, 122, 123, 124, 125, 126, 127}, |
| 88 | .oobfree = { |
| 89 | {.offset = 2, |
Christian Hitz | 90e3f39 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 90 | .length = 78} } |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 91 | }; |
| 92 | |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 93 | static int nand_get_device(struct mtd_info *mtd, int new_state); |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 94 | |
| 95 | static int nand_do_write_oob(struct mtd_info *mtd, loff_t to, |
| 96 | struct mtd_oob_ops *ops); |
| 97 | |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 98 | /* |
| 99 | * For devices which display every fart in the system on a separate LED. Is |
| 100 | * compiled away when LED support is disabled. |
| 101 | */ |
| 102 | DEFINE_LED_TRIGGER(nand_led_trigger); |
Sergei Poselenov | 248ae5c | 2008-06-06 15:42:43 +0200 | [diff] [blame] | 103 | |
Christian Hitz | 2a8e0fc | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 104 | static int check_offs_len(struct mtd_info *mtd, |
| 105 | loff_t ofs, uint64_t len) |
| 106 | { |
Scott Wood | 17cb4b8 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 107 | struct nand_chip *chip = mtd_to_nand(mtd); |
Christian Hitz | 2a8e0fc | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 108 | int ret = 0; |
| 109 | |
| 110 | /* Start address must align on block boundary */ |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 111 | if (ofs & ((1ULL << chip->phys_erase_shift) - 1)) { |
| 112 | pr_debug("%s: unaligned address\n", __func__); |
Christian Hitz | 2a8e0fc | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 113 | ret = -EINVAL; |
| 114 | } |
| 115 | |
| 116 | /* Length must align on block boundary */ |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 117 | if (len & ((1ULL << chip->phys_erase_shift) - 1)) { |
| 118 | pr_debug("%s: length not block aligned\n", __func__); |
Christian Hitz | 2a8e0fc | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 119 | ret = -EINVAL; |
| 120 | } |
| 121 | |
Christian Hitz | 2a8e0fc | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 122 | return ret; |
| 123 | } |
| 124 | |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 125 | /** |
| 126 | * nand_release_device - [GENERIC] release chip |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 127 | * @mtd: MTD device structure |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 128 | * |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 129 | * Release chip lock and wake up anyone waiting on the device. |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 130 | */ |
Christian Hitz | 90e3f39 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 131 | static void nand_release_device(struct mtd_info *mtd) |
Wolfgang Denk | 8e9655f | 2005-11-02 14:29:12 +0100 | [diff] [blame] | 132 | { |
Scott Wood | 17cb4b8 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 133 | struct nand_chip *chip = mtd_to_nand(mtd); |
Christian Hitz | 2a8e0fc | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 134 | |
| 135 | /* De-select the NAND device */ |
| 136 | chip->select_chip(mtd, -1); |
Wolfgang Denk | 8e9655f | 2005-11-02 14:29:12 +0100 | [diff] [blame] | 137 | } |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 138 | |
| 139 | /** |
| 140 | * nand_read_byte - [DEFAULT] read one byte from the chip |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 141 | * @mtd: MTD device structure |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 142 | * |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 143 | * Default read function for 8bit buswidth |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 144 | */ |
Simon Schwarz | 82645f8 | 2011-10-31 06:34:44 +0000 | [diff] [blame] | 145 | uint8_t nand_read_byte(struct mtd_info *mtd) |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 146 | { |
Scott Wood | 17cb4b8 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 147 | struct nand_chip *chip = mtd_to_nand(mtd); |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 148 | return readb(chip->IO_ADDR_R); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | /** |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 152 | * nand_read_byte16 - [DEFAULT] read one byte endianness aware from the chip |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 153 | * @mtd: MTD device structure |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 154 | * |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 155 | * Default read function for 16bit buswidth with endianness conversion. |
| 156 | * |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 157 | */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 158 | static uint8_t nand_read_byte16(struct mtd_info *mtd) |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 159 | { |
Scott Wood | 17cb4b8 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 160 | struct nand_chip *chip = mtd_to_nand(mtd); |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 161 | return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R)); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | /** |
| 165 | * nand_read_word - [DEFAULT] read one word from the chip |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 166 | * @mtd: MTD device structure |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 167 | * |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 168 | * Default read function for 16bit buswidth without endianness conversion. |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 169 | */ |
| 170 | static u16 nand_read_word(struct mtd_info *mtd) |
| 171 | { |
Scott Wood | 17cb4b8 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 172 | struct nand_chip *chip = mtd_to_nand(mtd); |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 173 | return readw(chip->IO_ADDR_R); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | /** |
| 177 | * nand_select_chip - [DEFAULT] control CE line |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 178 | * @mtd: MTD device structure |
| 179 | * @chipnr: chipnumber to select, -1 for deselect |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 180 | * |
| 181 | * Default select function for 1 chip devices. |
| 182 | */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 183 | static void nand_select_chip(struct mtd_info *mtd, int chipnr) |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 184 | { |
Scott Wood | 17cb4b8 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 185 | struct nand_chip *chip = mtd_to_nand(mtd); |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 186 | |
| 187 | switch (chipnr) { |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 188 | case -1: |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 189 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 190 | break; |
| 191 | case 0: |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 192 | break; |
| 193 | |
| 194 | default: |
| 195 | BUG(); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | /** |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 200 | * nand_write_byte - [DEFAULT] write single byte to chip |
| 201 | * @mtd: MTD device structure |
| 202 | * @byte: value to write |
| 203 | * |
| 204 | * Default function to write a byte to I/O[7:0] |
| 205 | */ |
| 206 | static void nand_write_byte(struct mtd_info *mtd, uint8_t byte) |
| 207 | { |
Scott Wood | 17cb4b8 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 208 | struct nand_chip *chip = mtd_to_nand(mtd); |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 209 | |
| 210 | chip->write_buf(mtd, &byte, 1); |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * nand_write_byte16 - [DEFAULT] write single byte to a chip with width 16 |
| 215 | * @mtd: MTD device structure |
| 216 | * @byte: value to write |
| 217 | * |
| 218 | * Default function to write a byte to I/O[7:0] on a 16-bit wide chip. |
| 219 | */ |
| 220 | static void nand_write_byte16(struct mtd_info *mtd, uint8_t byte) |
| 221 | { |
Scott Wood | 17cb4b8 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 222 | struct nand_chip *chip = mtd_to_nand(mtd); |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 223 | uint16_t word = byte; |
| 224 | |
| 225 | /* |
| 226 | * It's not entirely clear what should happen to I/O[15:8] when writing |
| 227 | * a byte. The ONFi spec (Revision 3.1; 2012-09-19, Section 2.16) reads: |
| 228 | * |
| 229 | * When the host supports a 16-bit bus width, only data is |
| 230 | * transferred at the 16-bit width. All address and command line |
| 231 | * transfers shall use only the lower 8-bits of the data bus. During |
| 232 | * command transfers, the host may place any value on the upper |
| 233 | * 8-bits of the data bus. During address transfers, the host shall |
| 234 | * set the upper 8-bits of the data bus to 00h. |
| 235 | * |
| 236 | * One user of the write_byte callback is nand_onfi_set_features. The |
| 237 | * four parameters are specified to be written to I/O[7:0], but this is |
| 238 | * neither an address nor a command transfer. Let's assume a 0 on the |
| 239 | * upper I/O lines is OK. |
| 240 | */ |
| 241 | chip->write_buf(mtd, (uint8_t *)&word, 2); |
| 242 | } |
| 243 | |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 244 | static void iowrite8_rep(void *addr, const uint8_t *buf, int len) |
| 245 | { |
| 246 | int i; |
| 247 | |
| 248 | for (i = 0; i < len; i++) |
| 249 | writeb(buf[i], addr); |
| 250 | } |
| 251 | static void ioread8_rep(void *addr, uint8_t *buf, int len) |
| 252 | { |
| 253 | int i; |
| 254 | |
| 255 | for (i = 0; i < len; i++) |
| 256 | buf[i] = readb(addr); |
| 257 | } |
| 258 | |
| 259 | static void ioread16_rep(void *addr, void *buf, int len) |
| 260 | { |
| 261 | int i; |
| 262 | u16 *p = (u16 *) buf; |
Stefan Roese | be16aba | 2014-09-05 09:57:01 +0200 | [diff] [blame] | 263 | |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 264 | for (i = 0; i < len; i++) |
| 265 | p[i] = readw(addr); |
| 266 | } |
| 267 | |
| 268 | static void iowrite16_rep(void *addr, void *buf, int len) |
| 269 | { |
| 270 | int i; |
| 271 | u16 *p = (u16 *) buf; |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 272 | |
| 273 | for (i = 0; i < len; i++) |
| 274 | writew(p[i], addr); |
| 275 | } |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 276 | |
| 277 | /** |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 278 | * nand_write_buf - [DEFAULT] write buffer to chip |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 279 | * @mtd: MTD device structure |
| 280 | * @buf: data buffer |
| 281 | * @len: number of bytes to write |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 282 | * |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 283 | * Default write function for 8bit buswidth. |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 284 | */ |
Simon Schwarz | 82645f8 | 2011-10-31 06:34:44 +0000 | [diff] [blame] | 285 | void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len) |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 286 | { |
Scott Wood | 17cb4b8 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 287 | struct nand_chip *chip = mtd_to_nand(mtd); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 288 | |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 289 | iowrite8_rep(chip->IO_ADDR_W, buf, len); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | /** |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 293 | * nand_read_buf - [DEFAULT] read chip data into buffer |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 294 | * @mtd: MTD device structure |
| 295 | * @buf: buffer to store date |
| 296 | * @len: number of bytes to read |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 297 | * |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 298 | * Default read function for 8bit buswidth. |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 299 | */ |
Simon Schwarz | 12c2f1e | 2011-09-14 15:30:16 -0400 | [diff] [blame] | 300 | void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len) |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 301 | { |
Scott Wood | 17cb4b8 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 302 | struct nand_chip *chip = mtd_to_nand(mtd); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 303 | |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 304 | ioread8_rep(chip->IO_ADDR_R, buf, len); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 305 | } |
| 306 | |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 307 | /** |
| 308 | * nand_write_buf16 - [DEFAULT] write buffer to chip |
| 309 | * @mtd: MTD device structure |
| 310 | * @buf: data buffer |
| 311 | * @len: number of bytes to write |
| 312 | * |
| 313 | * Default write function for 16bit buswidth. |
| 314 | */ |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 315 | void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len) |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 316 | { |
Scott Wood | 17cb4b8 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 317 | struct nand_chip *chip = mtd_to_nand(mtd); |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 318 | u16 *p = (u16 *) buf; |
| 319 | |
| 320 | iowrite16_rep(chip->IO_ADDR_W, p, len >> 1); |
| 321 | } |
| 322 | |
| 323 | /** |
| 324 | * nand_read_buf16 - [DEFAULT] read chip data into buffer |
| 325 | * @mtd: MTD device structure |
| 326 | * @buf: buffer to store date |
| 327 | * @len: number of bytes to read |
| 328 | * |
| 329 | * Default read function for 16bit buswidth. |
| 330 | */ |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 331 | void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len) |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 332 | { |
Scott Wood | 17cb4b8 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 333 | struct nand_chip *chip = mtd_to_nand(mtd); |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 334 | u16 *p = (u16 *) buf; |
| 335 | |
| 336 | ioread16_rep(chip->IO_ADDR_R, p, len >> 1); |
| 337 | } |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 338 | |
| 339 | /** |
| 340 | * nand_block_bad - [DEFAULT] Read bad block marker from the chip |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 341 | * @mtd: MTD device structure |
| 342 | * @ofs: offset from device start |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 343 | * |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 344 | * Check, if the block is bad. |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 345 | */ |
Scott Wood | ceee07b | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 346 | static int nand_block_bad(struct mtd_info *mtd, loff_t ofs) |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 347 | { |
Scott Wood | ceee07b | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 348 | int page, res = 0, i = 0; |
Scott Wood | 17cb4b8 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 349 | struct nand_chip *chip = mtd_to_nand(mtd); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 350 | u16 bad; |
| 351 | |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 352 | if (chip->bbt_options & NAND_BBT_SCANLASTPAGE) |
Christian Hitz | 2a8e0fc | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 353 | ofs += mtd->erasesize - mtd->writesize; |
| 354 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 355 | page = (int)(ofs >> chip->page_shift) & chip->pagemask; |
Thomas Knobloch | a798865 | 2007-05-05 07:04:42 +0200 | [diff] [blame] | 356 | |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 357 | do { |
| 358 | if (chip->options & NAND_BUSWIDTH_16) { |
| 359 | chip->cmdfunc(mtd, NAND_CMD_READOOB, |
| 360 | chip->badblockpos & 0xFE, page); |
| 361 | bad = cpu_to_le16(chip->read_word(mtd)); |
| 362 | if (chip->badblockpos & 0x1) |
| 363 | bad >>= 8; |
| 364 | else |
| 365 | bad &= 0xFF; |
| 366 | } else { |
| 367 | chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos, |
| 368 | page); |
| 369 | bad = chip->read_byte(mtd); |
| 370 | } |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 371 | |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 372 | if (likely(chip->badblockbits == 8)) |
| 373 | res = bad != 0xFF; |
| 374 | else |
| 375 | res = hweight8(bad) < chip->badblockbits; |
| 376 | ofs += mtd->writesize; |
| 377 | page = (int)(ofs >> chip->page_shift) & chip->pagemask; |
| 378 | i++; |
| 379 | } while (!res && i < 2 && (chip->bbt_options & NAND_BBT_SCAN2NDPAGE)); |
Christian Hitz | 2a8e0fc | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 380 | |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 381 | return res; |
| 382 | } |
| 383 | |
| 384 | /** |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 385 | * nand_default_block_markbad - [DEFAULT] mark a block bad via bad block marker |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 386 | * @mtd: MTD device structure |
| 387 | * @ofs: offset from device start |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 388 | * |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 389 | * This is the default implementation, which can be overridden by a hardware |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 390 | * specific driver. It provides the details for writing a bad block marker to a |
| 391 | * block. |
| 392 | */ |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 393 | static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs) |
| 394 | { |
Scott Wood | 17cb4b8 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 395 | struct nand_chip *chip = mtd_to_nand(mtd); |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 396 | struct mtd_oob_ops ops; |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 397 | uint8_t buf[2] = { 0, 0 }; |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 398 | int ret = 0, res, i = 0; |
Christian Hitz | 2a8e0fc | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 399 | |
Scott Wood | d396372 | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 400 | memset(&ops, 0, sizeof(ops)); |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 401 | ops.oobbuf = buf; |
| 402 | ops.ooboffs = chip->badblockpos; |
| 403 | if (chip->options & NAND_BUSWIDTH_16) { |
| 404 | ops.ooboffs &= ~0x01; |
| 405 | ops.len = ops.ooblen = 2; |
| 406 | } else { |
| 407 | ops.len = ops.ooblen = 1; |
| 408 | } |
| 409 | ops.mode = MTD_OPS_PLACE_OOB; |
| 410 | |
| 411 | /* Write to first/last page(s) if necessary */ |
| 412 | if (chip->bbt_options & NAND_BBT_SCANLASTPAGE) |
| 413 | ofs += mtd->erasesize - mtd->writesize; |
| 414 | do { |
| 415 | res = nand_do_write_oob(mtd, ofs, &ops); |
| 416 | if (!ret) |
| 417 | ret = res; |
| 418 | |
| 419 | i++; |
| 420 | ofs += mtd->writesize; |
| 421 | } while ((chip->bbt_options & NAND_BBT_SCAN2NDPAGE) && i < 2); |
| 422 | |
| 423 | return ret; |
| 424 | } |
| 425 | |
| 426 | /** |
| 427 | * nand_block_markbad_lowlevel - mark a block bad |
| 428 | * @mtd: MTD device structure |
| 429 | * @ofs: offset from device start |
| 430 | * |
| 431 | * This function performs the generic NAND bad block marking steps (i.e., bad |
| 432 | * block table(s) and/or marker(s)). We only allow the hardware driver to |
| 433 | * specify how to write bad block markers to OOB (chip->block_markbad). |
| 434 | * |
| 435 | * We try operations in the following order: |
| 436 | * (1) erase the affected block, to allow OOB marker to be written cleanly |
| 437 | * (2) write bad block marker to OOB area of affected block (unless flag |
| 438 | * NAND_BBT_NO_OOB_BBM is present) |
| 439 | * (3) update the BBT |
| 440 | * Note that we retain the first error encountered in (2) or (3), finish the |
| 441 | * procedures, and dump the error in the end. |
| 442 | */ |
| 443 | static int nand_block_markbad_lowlevel(struct mtd_info *mtd, loff_t ofs) |
| 444 | { |
Scott Wood | 17cb4b8 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 445 | struct nand_chip *chip = mtd_to_nand(mtd); |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 446 | int res, ret = 0; |
| 447 | |
| 448 | if (!(chip->bbt_options & NAND_BBT_NO_OOB_BBM)) { |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 449 | struct erase_info einfo; |
| 450 | |
| 451 | /* Attempt erase before marking OOB */ |
| 452 | memset(&einfo, 0, sizeof(einfo)); |
| 453 | einfo.mtd = mtd; |
| 454 | einfo.addr = ofs; |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 455 | einfo.len = 1ULL << chip->phys_erase_shift; |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 456 | nand_erase_nand(mtd, &einfo, 0); |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 457 | |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 458 | /* Write bad block marker to OOB */ |
| 459 | nand_get_device(mtd, FL_WRITING); |
| 460 | ret = chip->block_markbad(mtd, ofs); |
Scott Wood | c45912d | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 461 | nand_release_device(mtd); |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 462 | } |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 463 | |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 464 | /* Mark block bad in BBT */ |
| 465 | if (chip->bbt) { |
| 466 | res = nand_markbad_bbt(mtd, ofs); |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 467 | if (!ret) |
| 468 | ret = res; |
| 469 | } |
| 470 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 471 | if (!ret) |
| 472 | mtd->ecc_stats.badblocks++; |
Scott Wood | c45912d | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 473 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 474 | return ret; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 475 | } |
| 476 | |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 477 | /** |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 478 | * nand_check_wp - [GENERIC] check if the chip is write protected |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 479 | * @mtd: MTD device structure |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 480 | * |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 481 | * Check, if the device is write protected. The function expects, that the |
| 482 | * device is already selected. |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 483 | */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 484 | static int nand_check_wp(struct mtd_info *mtd) |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 485 | { |
Scott Wood | 17cb4b8 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 486 | struct nand_chip *chip = mtd_to_nand(mtd); |
Christian Hitz | 2a8e0fc | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 487 | |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 488 | /* Broken xD cards report WP despite being writable */ |
Christian Hitz | 2a8e0fc | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 489 | if (chip->options & NAND_BROKEN_XD) |
| 490 | return 0; |
| 491 | |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 492 | /* Check the WP bit */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 493 | chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1); |
| 494 | return (chip->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 495 | } |
Markus Klotzbücher | 43638c6 | 2006-03-06 15:04:25 +0100 | [diff] [blame] | 496 | |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 497 | /** |
Scott Wood | d396372 | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 498 | * nand_block_isreserved - [GENERIC] Check if a block is marked reserved. |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 499 | * @mtd: MTD device structure |
| 500 | * @ofs: offset from device start |
Ezequiel Garcia | 86a720a | 2014-05-21 19:06:12 -0300 | [diff] [blame] | 501 | * |
Scott Wood | d396372 | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 502 | * Check if the block is marked as reserved. |
Ezequiel Garcia | 86a720a | 2014-05-21 19:06:12 -0300 | [diff] [blame] | 503 | */ |
| 504 | static int nand_block_isreserved(struct mtd_info *mtd, loff_t ofs) |
| 505 | { |
Scott Wood | 17cb4b8 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 506 | struct nand_chip *chip = mtd_to_nand(mtd); |
Ezequiel Garcia | 86a720a | 2014-05-21 19:06:12 -0300 | [diff] [blame] | 507 | |
| 508 | if (!chip->bbt) |
| 509 | return 0; |
| 510 | /* Return info from the table */ |
| 511 | return nand_isreserved_bbt(mtd, ofs); |
| 512 | } |
| 513 | |
| 514 | /** |
| 515 | * nand_block_checkbad - [GENERIC] Check if a block is marked bad |
| 516 | * @mtd: MTD device structure |
| 517 | * @ofs: offset from device start |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 518 | * @allowbbt: 1, if its allowed to access the bbt area |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 519 | * |
| 520 | * Check, if the block is bad. Either by reading the bad block table or |
| 521 | * calling of the scan function. |
| 522 | */ |
Scott Wood | ceee07b | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 523 | static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int allowbbt) |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 524 | { |
Scott Wood | 17cb4b8 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 525 | struct nand_chip *chip = mtd_to_nand(mtd); |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 526 | |
Masahiro Yamada | ab37b76 | 2014-12-26 22:20:58 +0900 | [diff] [blame] | 527 | if (!(chip->options & NAND_SKIP_BBTSCAN) && |
| 528 | !(chip->options & NAND_BBT_SCANNED)) { |
Rostislav Lisovy | 35c204d | 2014-10-22 13:40:44 +0200 | [diff] [blame] | 529 | chip->options |= NAND_BBT_SCANNED; |
Masahiro Yamada | bf80ee6 | 2014-12-26 22:20:57 +0900 | [diff] [blame] | 530 | chip->scan_bbt(mtd); |
Rostislav Lisovy | 35c204d | 2014-10-22 13:40:44 +0200 | [diff] [blame] | 531 | } |
| 532 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 533 | if (!chip->bbt) |
Scott Wood | ceee07b | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 534 | return chip->block_bad(mtd, ofs); |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 535 | |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 536 | /* Return info from the table */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 537 | return nand_isbad_bbt(mtd, ofs, allowbbt); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 538 | } |
| 539 | |
Scott Wood | ceee07b | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 540 | /** |
| 541 | * nand_wait_ready - [GENERIC] Wait for the ready pin after commands. |
| 542 | * @mtd: MTD device structure |
| 543 | * |
| 544 | * Wait for the ready pin after a command, and warn if a timeout occurs. |
| 545 | */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 546 | void nand_wait_ready(struct mtd_info *mtd) |
| 547 | { |
Scott Wood | 17cb4b8 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 548 | struct nand_chip *chip = mtd_to_nand(mtd); |
Scott Wood | ceee07b | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 549 | u32 timeo = (CONFIG_SYS_HZ * 400) / 1000; |
Reinhard Meyer | 7a8fc36 | 2010-11-18 03:14:26 +0000 | [diff] [blame] | 550 | u32 time_start; |
Stefan Roese | 1207226 | 2008-01-05 16:43:25 +0100 | [diff] [blame] | 551 | |
Reinhard Meyer | 7a8fc36 | 2010-11-18 03:14:26 +0000 | [diff] [blame] | 552 | time_start = get_timer(0); |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 553 | /* Wait until command is processed or timeout occurs */ |
Reinhard Meyer | 7a8fc36 | 2010-11-18 03:14:26 +0000 | [diff] [blame] | 554 | while (get_timer(time_start) < timeo) { |
Stefan Roese | 1207226 | 2008-01-05 16:43:25 +0100 | [diff] [blame] | 555 | if (chip->dev_ready) |
| 556 | if (chip->dev_ready(mtd)) |
| 557 | break; |
| 558 | } |
Scott Wood | ceee07b | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 559 | |
| 560 | if (!chip->dev_ready(mtd)) |
| 561 | pr_warn("timeout while waiting for chip to become ready\n"); |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 562 | } |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 563 | EXPORT_SYMBOL_GPL(nand_wait_ready); |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 564 | |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 565 | /** |
Scott Wood | d396372 | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 566 | * nand_wait_status_ready - [GENERIC] Wait for the ready status after commands. |
| 567 | * @mtd: MTD device structure |
| 568 | * @timeo: Timeout in ms |
| 569 | * |
| 570 | * Wait for status ready (i.e. command done) or timeout. |
| 571 | */ |
| 572 | static void nand_wait_status_ready(struct mtd_info *mtd, unsigned long timeo) |
| 573 | { |
Scott Wood | 17cb4b8 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 574 | register struct nand_chip *chip = mtd_to_nand(mtd); |
Scott Wood | d396372 | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 575 | u32 time_start; |
| 576 | |
| 577 | timeo = (CONFIG_SYS_HZ * timeo) / 1000; |
| 578 | time_start = get_timer(0); |
| 579 | while (get_timer(time_start) < timeo) { |
| 580 | if ((chip->read_byte(mtd) & NAND_STATUS_READY)) |
| 581 | break; |
| 582 | WATCHDOG_RESET(); |
| 583 | } |
| 584 | }; |
| 585 | |
| 586 | /** |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 587 | * nand_command - [DEFAULT] Send command to NAND device |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 588 | * @mtd: MTD device structure |
| 589 | * @command: the command to be sent |
| 590 | * @column: the column address for this command, -1 if none |
| 591 | * @page_addr: the page address for this command, -1 if none |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 592 | * |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 593 | * Send command to NAND device. This function is used for small page devices |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 594 | * (512 Bytes per page). |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 595 | */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 596 | static void nand_command(struct mtd_info *mtd, unsigned int command, |
| 597 | int column, int page_addr) |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 598 | { |
Scott Wood | 17cb4b8 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 599 | register struct nand_chip *chip = mtd_to_nand(mtd); |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 600 | int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 601 | |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 602 | /* Write out the command to the device */ |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 603 | if (command == NAND_CMD_SEQIN) { |
| 604 | int readcmd; |
| 605 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 606 | if (column >= mtd->writesize) { |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 607 | /* OOB area */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 608 | column -= mtd->writesize; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 609 | readcmd = NAND_CMD_READOOB; |
| 610 | } else if (column < 256) { |
| 611 | /* First 256 bytes --> READ0 */ |
| 612 | readcmd = NAND_CMD_READ0; |
| 613 | } else { |
| 614 | column -= 256; |
| 615 | readcmd = NAND_CMD_READ1; |
| 616 | } |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 617 | chip->cmd_ctrl(mtd, readcmd, ctrl); |
| 618 | ctrl &= ~NAND_CTRL_CHANGE; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 619 | } |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 620 | chip->cmd_ctrl(mtd, command, ctrl); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 621 | |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 622 | /* Address cycle, when necessary */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 623 | ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE; |
| 624 | /* Serially input address */ |
| 625 | if (column != -1) { |
| 626 | /* Adjust columns for 16 bit buswidth */ |
Heiko Schocher | 4e67c57 | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 627 | if (chip->options & NAND_BUSWIDTH_16 && |
Brian Norris | 27ce9e4 | 2014-05-06 00:46:17 +0530 | [diff] [blame] | 628 | !nand_opcode_8bits(command)) |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 629 | column >>= 1; |
| 630 | chip->cmd_ctrl(mtd, column, ctrl); |
| 631 | ctrl &= ~NAND_CTRL_CHANGE; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 632 | } |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 633 | if (page_addr != -1) { |
| 634 | chip->cmd_ctrl(mtd, page_addr, ctrl); |
| 635 | ctrl &= ~NAND_CTRL_CHANGE; |
| 636 | chip->cmd_ctrl(mtd, page_addr >> 8, ctrl); |
| 637 | /* One more address cycle for devices > 32MiB */ |
| 638 | if (chip->chipsize > (32 << 20)) |
| 639 | chip->cmd_ctrl(mtd, page_addr >> 16, ctrl); |
| 640 | } |
| 641 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE); |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 642 | |
| 643 | /* |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 644 | * Program and erase have their own busy handlers status and sequential |
| 645 | * in needs no delay |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 646 | */ |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 647 | switch (command) { |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 648 | |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 649 | case NAND_CMD_PAGEPROG: |
| 650 | case NAND_CMD_ERASE1: |
| 651 | case NAND_CMD_ERASE2: |
| 652 | case NAND_CMD_SEQIN: |
| 653 | case NAND_CMD_STATUS: |
Masahiro Yamada | 6f29c7a | 2017-09-15 21:44:58 +0900 | [diff] [blame] | 654 | case NAND_CMD_READID: |
Masahiro Yamada | fe3fddf | 2017-09-15 21:44:59 +0900 | [diff] [blame] | 655 | case NAND_CMD_SET_FEATURES: |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 656 | return; |
| 657 | |
| 658 | case NAND_CMD_RESET: |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 659 | if (chip->dev_ready) |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 660 | break; |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 661 | udelay(chip->chip_delay); |
| 662 | chip->cmd_ctrl(mtd, NAND_CMD_STATUS, |
| 663 | NAND_CTRL_CLE | NAND_CTRL_CHANGE); |
| 664 | chip->cmd_ctrl(mtd, |
| 665 | NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE); |
Scott Wood | d396372 | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 666 | /* EZ-NAND can take upto 250ms as per ONFi v4.0 */ |
| 667 | nand_wait_status_ready(mtd, 250); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 668 | return; |
| 669 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 670 | /* This applies to read commands */ |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 671 | default: |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 672 | /* |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 673 | * If we don't have access to the busy pin, we apply the given |
| 674 | * command delay |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 675 | */ |
| 676 | if (!chip->dev_ready) { |
| 677 | udelay(chip->chip_delay); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 678 | return; |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 679 | } |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 680 | } |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 681 | /* |
| 682 | * Apply this short delay always to ensure that we do wait tWB in |
| 683 | * any case on any machine. |
| 684 | */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 685 | ndelay(100); |
| 686 | |
| 687 | nand_wait_ready(mtd); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | /** |
| 691 | * nand_command_lp - [DEFAULT] Send command to NAND large page device |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 692 | * @mtd: MTD device structure |
| 693 | * @command: the command to be sent |
| 694 | * @column: the column address for this command, -1 if none |
| 695 | * @page_addr: the page address for this command, -1 if none |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 696 | * |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 697 | * Send command to NAND device. This is the version for the new large page |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 698 | * devices. We don't have the separate regions as we have in the small page |
| 699 | * devices. We must emulate NAND_CMD_READOOB to keep the code compatible. |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 700 | */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 701 | static void nand_command_lp(struct mtd_info *mtd, unsigned int command, |
| 702 | int column, int page_addr) |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 703 | { |
Scott Wood | 17cb4b8 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 704 | register struct nand_chip *chip = mtd_to_nand(mtd); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 705 | |
| 706 | /* Emulate NAND_CMD_READOOB */ |
| 707 | if (command == NAND_CMD_READOOB) { |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 708 | column += mtd->writesize; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 709 | command = NAND_CMD_READ0; |
| 710 | } |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 711 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 712 | /* Command latch cycle */ |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 713 | chip->cmd_ctrl(mtd, command, NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 714 | |
| 715 | if (column != -1 || page_addr != -1) { |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 716 | int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 717 | |
| 718 | /* Serially input address */ |
| 719 | if (column != -1) { |
| 720 | /* Adjust columns for 16 bit buswidth */ |
Heiko Schocher | 4e67c57 | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 721 | if (chip->options & NAND_BUSWIDTH_16 && |
Brian Norris | 27ce9e4 | 2014-05-06 00:46:17 +0530 | [diff] [blame] | 722 | !nand_opcode_8bits(command)) |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 723 | column >>= 1; |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 724 | chip->cmd_ctrl(mtd, column, ctrl); |
| 725 | ctrl &= ~NAND_CTRL_CHANGE; |
| 726 | chip->cmd_ctrl(mtd, column >> 8, ctrl); |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 727 | } |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 728 | if (page_addr != -1) { |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 729 | chip->cmd_ctrl(mtd, page_addr, ctrl); |
| 730 | chip->cmd_ctrl(mtd, page_addr >> 8, |
| 731 | NAND_NCE | NAND_ALE); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 732 | /* One more address cycle for devices > 128MiB */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 733 | if (chip->chipsize > (128 << 20)) |
| 734 | chip->cmd_ctrl(mtd, page_addr >> 16, |
| 735 | NAND_NCE | NAND_ALE); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 736 | } |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 737 | } |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 738 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE); |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 739 | |
| 740 | /* |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 741 | * Program and erase have their own busy handlers status, sequential |
Scott Wood | d396372 | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 742 | * in and status need no delay. |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 743 | */ |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 744 | switch (command) { |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 745 | |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 746 | case NAND_CMD_CACHEDPROG: |
| 747 | case NAND_CMD_PAGEPROG: |
| 748 | case NAND_CMD_ERASE1: |
| 749 | case NAND_CMD_ERASE2: |
| 750 | case NAND_CMD_SEQIN: |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 751 | case NAND_CMD_RNDIN: |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 752 | case NAND_CMD_STATUS: |
Masahiro Yamada | 6f29c7a | 2017-09-15 21:44:58 +0900 | [diff] [blame] | 753 | case NAND_CMD_READID: |
Masahiro Yamada | fe3fddf | 2017-09-15 21:44:59 +0900 | [diff] [blame] | 754 | case NAND_CMD_SET_FEATURES: |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 755 | return; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 756 | |
| 757 | case NAND_CMD_RESET: |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 758 | if (chip->dev_ready) |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 759 | break; |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 760 | udelay(chip->chip_delay); |
| 761 | chip->cmd_ctrl(mtd, NAND_CMD_STATUS, |
| 762 | NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE); |
| 763 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, |
| 764 | NAND_NCE | NAND_CTRL_CHANGE); |
Scott Wood | d396372 | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 765 | /* EZ-NAND can take upto 250ms as per ONFi v4.0 */ |
| 766 | nand_wait_status_ready(mtd, 250); |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 767 | return; |
| 768 | |
| 769 | case NAND_CMD_RNDOUT: |
| 770 | /* No ready / busy check necessary */ |
| 771 | chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART, |
| 772 | NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE); |
| 773 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, |
| 774 | NAND_NCE | NAND_CTRL_CHANGE); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 775 | return; |
| 776 | |
| 777 | case NAND_CMD_READ0: |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 778 | chip->cmd_ctrl(mtd, NAND_CMD_READSTART, |
| 779 | NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE); |
| 780 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, |
| 781 | NAND_NCE | NAND_CTRL_CHANGE); |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 782 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 783 | /* This applies to read commands */ |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 784 | default: |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 785 | /* |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 786 | * If we don't have access to the busy pin, we apply the given |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 787 | * command delay. |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 788 | */ |
| 789 | if (!chip->dev_ready) { |
| 790 | udelay(chip->chip_delay); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 791 | return; |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 792 | } |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 793 | } |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 794 | |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 795 | /* |
| 796 | * Apply this short delay always to ensure that we do wait tWB in |
| 797 | * any case on any machine. |
| 798 | */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 799 | ndelay(100); |
| 800 | |
| 801 | nand_wait_ready(mtd); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 802 | } |
| 803 | |
| 804 | /** |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 805 | * panic_nand_get_device - [GENERIC] Get chip for selected access |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 806 | * @chip: the nand chip descriptor |
| 807 | * @mtd: MTD device structure |
| 808 | * @new_state: the state which is requested |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 809 | * |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 810 | * Used when in panic, no locks are taken. |
| 811 | */ |
| 812 | static void panic_nand_get_device(struct nand_chip *chip, |
| 813 | struct mtd_info *mtd, int new_state) |
| 814 | { |
| 815 | /* Hardware controller shared among independent devices */ |
| 816 | chip->controller->active = chip; |
| 817 | chip->state = new_state; |
| 818 | } |
| 819 | |
| 820 | /** |
| 821 | * nand_get_device - [GENERIC] Get chip for selected access |
| 822 | * @mtd: MTD device structure |
| 823 | * @new_state: the state which is requested |
| 824 | * |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 825 | * Get the device and lock it for exclusive access |
| 826 | */ |
Christian Hitz | 2a8e0fc | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 827 | static int |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 828 | nand_get_device(struct mtd_info *mtd, int new_state) |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 829 | { |
Scott Wood | 17cb4b8 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 830 | struct nand_chip *chip = mtd_to_nand(mtd); |
Christian Hitz | 2a8e0fc | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 831 | chip->state = new_state; |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 832 | return 0; |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 833 | } |
| 834 | |
| 835 | /** |
| 836 | * panic_nand_wait - [GENERIC] wait until the command is done |
| 837 | * @mtd: MTD device structure |
| 838 | * @chip: NAND chip structure |
| 839 | * @timeo: timeout |
| 840 | * |
| 841 | * Wait for command done. This is a helper function for nand_wait used when |
| 842 | * we are in interrupt context. May happen when in panic and trying to write |
| 843 | * an oops through mtdoops. |
| 844 | */ |
| 845 | static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip, |
| 846 | unsigned long timeo) |
| 847 | { |
| 848 | int i; |
| 849 | for (i = 0; i < timeo; i++) { |
| 850 | if (chip->dev_ready) { |
| 851 | if (chip->dev_ready(mtd)) |
| 852 | break; |
| 853 | } else { |
| 854 | if (chip->read_byte(mtd) & NAND_STATUS_READY) |
| 855 | break; |
| 856 | } |
| 857 | mdelay(1); |
| 858 | } |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 859 | } |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 860 | |
| 861 | /** |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 862 | * nand_wait - [DEFAULT] wait until the command is done |
| 863 | * @mtd: MTD device structure |
| 864 | * @chip: NAND chip structure |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 865 | * |
Scott Wood | ceee07b | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 866 | * Wait for command done. This applies to erase and program only. |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 867 | */ |
Christian Hitz | 2a8e0fc | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 868 | static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip) |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 869 | { |
Scott Wood | ceee07b | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 870 | int status; |
| 871 | unsigned long timeo = 400; |
Wolfgang Denk | 8e9655f | 2005-11-02 14:29:12 +0100 | [diff] [blame] | 872 | |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 873 | led_trigger_event(nand_led_trigger, LED_FULL); |
Wolfgang Denk | 8e9655f | 2005-11-02 14:29:12 +0100 | [diff] [blame] | 874 | |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 875 | /* |
| 876 | * Apply this short delay always to ensure that we do wait tWB in any |
| 877 | * case on any machine. |
| 878 | */ |
| 879 | ndelay(100); |
Wolfgang Denk | 8e9655f | 2005-11-02 14:29:12 +0100 | [diff] [blame] | 880 | |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 881 | chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1); |
| 882 | |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 883 | u32 timer = (CONFIG_SYS_HZ * timeo) / 1000; |
| 884 | u32 time_start; |
| 885 | |
| 886 | time_start = get_timer(0); |
| 887 | while (get_timer(time_start) < timer) { |
Christian Hitz | 2a8e0fc | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 888 | if (chip->dev_ready) { |
| 889 | if (chip->dev_ready(mtd)) |
Wolfgang Denk | 8e9655f | 2005-11-02 14:29:12 +0100 | [diff] [blame] | 890 | break; |
| 891 | } else { |
Christian Hitz | 2a8e0fc | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 892 | if (chip->read_byte(mtd) & NAND_STATUS_READY) |
Wolfgang Denk | 8e9655f | 2005-11-02 14:29:12 +0100 | [diff] [blame] | 893 | break; |
| 894 | } |
| 895 | } |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 896 | led_trigger_event(nand_led_trigger, LED_OFF); |
Bartlomiej Sieka | 038ccac | 2006-02-24 09:37:22 +0100 | [diff] [blame] | 897 | |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 898 | status = (int)chip->read_byte(mtd); |
| 899 | /* This can happen if in case of timeout or buggy dev_ready */ |
| 900 | WARN_ON(!(status & NAND_STATUS_READY)); |
| 901 | return status; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 902 | } |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 903 | |
Scott Wood | ceee07b | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 904 | #define BITS_PER_BYTE 8 |
| 905 | |
| 906 | /** |
| 907 | * nand_check_erased_buf - check if a buffer contains (almost) only 0xff data |
| 908 | * @buf: buffer to test |
| 909 | * @len: buffer length |
| 910 | * @bitflips_threshold: maximum number of bitflips |
| 911 | * |
| 912 | * Check if a buffer contains only 0xff, which means the underlying region |
| 913 | * has been erased and is ready to be programmed. |
| 914 | * The bitflips_threshold specify the maximum number of bitflips before |
| 915 | * considering the region is not erased. |
| 916 | * Note: The logic of this function has been extracted from the memweight |
| 917 | * implementation, except that nand_check_erased_buf function exit before |
| 918 | * testing the whole buffer if the number of bitflips exceed the |
| 919 | * bitflips_threshold value. |
| 920 | * |
| 921 | * Returns a positive number of bitflips less than or equal to |
| 922 | * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the |
| 923 | * threshold. |
| 924 | */ |
| 925 | static int nand_check_erased_buf(void *buf, int len, int bitflips_threshold) |
| 926 | { |
| 927 | const unsigned char *bitmap = buf; |
| 928 | int bitflips = 0; |
| 929 | int weight; |
| 930 | |
| 931 | for (; len && ((uintptr_t)bitmap) % sizeof(long); |
| 932 | len--, bitmap++) { |
| 933 | weight = hweight8(*bitmap); |
| 934 | bitflips += BITS_PER_BYTE - weight; |
| 935 | if (unlikely(bitflips > bitflips_threshold)) |
| 936 | return -EBADMSG; |
| 937 | } |
| 938 | |
| 939 | for (; len >= 4; len -= 4, bitmap += 4) { |
| 940 | weight = hweight32(*((u32 *)bitmap)); |
| 941 | bitflips += 32 - weight; |
| 942 | if (unlikely(bitflips > bitflips_threshold)) |
| 943 | return -EBADMSG; |
| 944 | } |
| 945 | |
| 946 | for (; len > 0; len--, bitmap++) { |
| 947 | weight = hweight8(*bitmap); |
| 948 | bitflips += BITS_PER_BYTE - weight; |
| 949 | if (unlikely(bitflips > bitflips_threshold)) |
| 950 | return -EBADMSG; |
| 951 | } |
| 952 | |
| 953 | return bitflips; |
| 954 | } |
| 955 | |
| 956 | /** |
| 957 | * nand_check_erased_ecc_chunk - check if an ECC chunk contains (almost) only |
| 958 | * 0xff data |
| 959 | * @data: data buffer to test |
| 960 | * @datalen: data length |
| 961 | * @ecc: ECC buffer |
| 962 | * @ecclen: ECC length |
| 963 | * @extraoob: extra OOB buffer |
| 964 | * @extraooblen: extra OOB length |
| 965 | * @bitflips_threshold: maximum number of bitflips |
| 966 | * |
| 967 | * Check if a data buffer and its associated ECC and OOB data contains only |
| 968 | * 0xff pattern, which means the underlying region has been erased and is |
| 969 | * ready to be programmed. |
| 970 | * The bitflips_threshold specify the maximum number of bitflips before |
| 971 | * considering the region as not erased. |
| 972 | * |
| 973 | * Note: |
| 974 | * 1/ ECC algorithms are working on pre-defined block sizes which are usually |
| 975 | * different from the NAND page size. When fixing bitflips, ECC engines will |
| 976 | * report the number of errors per chunk, and the NAND core infrastructure |
| 977 | * expect you to return the maximum number of bitflips for the whole page. |
| 978 | * This is why you should always use this function on a single chunk and |
| 979 | * not on the whole page. After checking each chunk you should update your |
| 980 | * max_bitflips value accordingly. |
| 981 | * 2/ When checking for bitflips in erased pages you should not only check |
| 982 | * the payload data but also their associated ECC data, because a user might |
| 983 | * have programmed almost all bits to 1 but a few. In this case, we |
| 984 | * shouldn't consider the chunk as erased, and checking ECC bytes prevent |
| 985 | * this case. |
| 986 | * 3/ The extraoob argument is optional, and should be used if some of your OOB |
| 987 | * data are protected by the ECC engine. |
| 988 | * It could also be used if you support subpages and want to attach some |
| 989 | * extra OOB data to an ECC chunk. |
| 990 | * |
| 991 | * Returns a positive number of bitflips less than or equal to |
| 992 | * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the |
| 993 | * threshold. In case of success, the passed buffers are filled with 0xff. |
| 994 | */ |
| 995 | int nand_check_erased_ecc_chunk(void *data, int datalen, |
| 996 | void *ecc, int ecclen, |
| 997 | void *extraoob, int extraooblen, |
| 998 | int bitflips_threshold) |
| 999 | { |
| 1000 | int data_bitflips = 0, ecc_bitflips = 0, extraoob_bitflips = 0; |
| 1001 | |
| 1002 | data_bitflips = nand_check_erased_buf(data, datalen, |
| 1003 | bitflips_threshold); |
| 1004 | if (data_bitflips < 0) |
| 1005 | return data_bitflips; |
| 1006 | |
| 1007 | bitflips_threshold -= data_bitflips; |
| 1008 | |
| 1009 | ecc_bitflips = nand_check_erased_buf(ecc, ecclen, bitflips_threshold); |
| 1010 | if (ecc_bitflips < 0) |
| 1011 | return ecc_bitflips; |
| 1012 | |
| 1013 | bitflips_threshold -= ecc_bitflips; |
| 1014 | |
| 1015 | extraoob_bitflips = nand_check_erased_buf(extraoob, extraooblen, |
| 1016 | bitflips_threshold); |
| 1017 | if (extraoob_bitflips < 0) |
| 1018 | return extraoob_bitflips; |
| 1019 | |
| 1020 | if (data_bitflips) |
| 1021 | memset(data, 0xff, datalen); |
| 1022 | |
| 1023 | if (ecc_bitflips) |
| 1024 | memset(ecc, 0xff, ecclen); |
| 1025 | |
| 1026 | if (extraoob_bitflips) |
| 1027 | memset(extraoob, 0xff, extraooblen); |
| 1028 | |
| 1029 | return data_bitflips + ecc_bitflips + extraoob_bitflips; |
| 1030 | } |
| 1031 | EXPORT_SYMBOL(nand_check_erased_ecc_chunk); |
| 1032 | |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1033 | /** |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1034 | * nand_read_page_raw - [INTERN] read raw page data without ecc |
| 1035 | * @mtd: mtd info structure |
| 1036 | * @chip: nand chip info structure |
| 1037 | * @buf: buffer to store read data |
| 1038 | * @oob_required: caller requires OOB data read to chip->oob_poi |
| 1039 | * @page: page number to read |
David Brownell | 7e86661 | 2009-11-07 16:27:01 -0500 | [diff] [blame] | 1040 | * |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1041 | * Not for syndrome calculating ECC controllers, which use a special oob layout. |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1042 | */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1043 | static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip, |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1044 | uint8_t *buf, int oob_required, int page) |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1045 | { |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1046 | chip->read_buf(mtd, buf, mtd->writesize); |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1047 | if (oob_required) |
| 1048 | chip->read_buf(mtd, chip->oob_poi, mtd->oobsize); |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1049 | return 0; |
| 1050 | } |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 1051 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1052 | /** |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1053 | * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc |
| 1054 | * @mtd: mtd info structure |
| 1055 | * @chip: nand chip info structure |
| 1056 | * @buf: buffer to store read data |
| 1057 | * @oob_required: caller requires OOB data read to chip->oob_poi |
| 1058 | * @page: page number to read |
David Brownell | 7e86661 | 2009-11-07 16:27:01 -0500 | [diff] [blame] | 1059 | * |
| 1060 | * We need a special oob layout and handling even when OOB isn't used. |
| 1061 | */ |
Christian Hitz | 90e3f39 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 1062 | static int nand_read_page_raw_syndrome(struct mtd_info *mtd, |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1063 | struct nand_chip *chip, uint8_t *buf, |
| 1064 | int oob_required, int page) |
David Brownell | 7e86661 | 2009-11-07 16:27:01 -0500 | [diff] [blame] | 1065 | { |
| 1066 | int eccsize = chip->ecc.size; |
| 1067 | int eccbytes = chip->ecc.bytes; |
| 1068 | uint8_t *oob = chip->oob_poi; |
| 1069 | int steps, size; |
| 1070 | |
| 1071 | for (steps = chip->ecc.steps; steps > 0; steps--) { |
| 1072 | chip->read_buf(mtd, buf, eccsize); |
| 1073 | buf += eccsize; |
| 1074 | |
| 1075 | if (chip->ecc.prepad) { |
| 1076 | chip->read_buf(mtd, oob, chip->ecc.prepad); |
| 1077 | oob += chip->ecc.prepad; |
| 1078 | } |
| 1079 | |
| 1080 | chip->read_buf(mtd, oob, eccbytes); |
| 1081 | oob += eccbytes; |
| 1082 | |
| 1083 | if (chip->ecc.postpad) { |
| 1084 | chip->read_buf(mtd, oob, chip->ecc.postpad); |
| 1085 | oob += chip->ecc.postpad; |
| 1086 | } |
| 1087 | } |
| 1088 | |
| 1089 | size = mtd->oobsize - (oob - chip->oob_poi); |
| 1090 | if (size) |
| 1091 | chip->read_buf(mtd, oob, size); |
| 1092 | |
| 1093 | return 0; |
| 1094 | } |
| 1095 | |
| 1096 | /** |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1097 | * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function |
| 1098 | * @mtd: mtd info structure |
| 1099 | * @chip: nand chip info structure |
| 1100 | * @buf: buffer to store read data |
| 1101 | * @oob_required: caller requires OOB data read to chip->oob_poi |
| 1102 | * @page: page number to read |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1103 | */ |
| 1104 | static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip, |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1105 | uint8_t *buf, int oob_required, int page) |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1106 | { |
| 1107 | int i, eccsize = chip->ecc.size; |
| 1108 | int eccbytes = chip->ecc.bytes; |
| 1109 | int eccsteps = chip->ecc.steps; |
| 1110 | uint8_t *p = buf; |
| 1111 | uint8_t *ecc_calc = chip->buffers->ecccalc; |
| 1112 | uint8_t *ecc_code = chip->buffers->ecccode; |
| 1113 | uint32_t *eccpos = chip->ecc.layout->eccpos; |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1114 | unsigned int max_bitflips = 0; |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 1115 | |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1116 | chip->ecc.read_page_raw(mtd, chip, buf, 1, page); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1117 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1118 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) |
| 1119 | chip->ecc.calculate(mtd, p, &ecc_calc[i]); |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 1120 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1121 | for (i = 0; i < chip->ecc.total; i++) |
| 1122 | ecc_code[i] = chip->oob_poi[eccpos[i]]; |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 1123 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1124 | eccsteps = chip->ecc.steps; |
| 1125 | p = buf; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1126 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1127 | for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 1128 | int stat; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1129 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1130 | stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]); |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1131 | if (stat < 0) { |
Scott Wood | c45912d | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 1132 | mtd->ecc_stats.failed++; |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1133 | } else { |
Scott Wood | c45912d | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 1134 | mtd->ecc_stats.corrected += stat; |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1135 | max_bitflips = max_t(unsigned int, max_bitflips, stat); |
| 1136 | } |
Scott Wood | c45912d | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 1137 | } |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1138 | return max_bitflips; |
Scott Wood | c45912d | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 1139 | } |
| 1140 | |
| 1141 | /** |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1142 | * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1143 | * @mtd: mtd info structure |
| 1144 | * @chip: nand chip info structure |
| 1145 | * @data_offs: offset of requested data within the page |
| 1146 | * @readlen: data length |
| 1147 | * @bufpoi: buffer to store read data |
Heiko Schocher | 4e67c57 | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 1148 | * @page: page number to read |
Scott Wood | c45912d | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 1149 | */ |
Christian Hitz | 90e3f39 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 1150 | static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip, |
Heiko Schocher | 4e67c57 | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 1151 | uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi, |
| 1152 | int page) |
Scott Wood | c45912d | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 1153 | { |
| 1154 | int start_step, end_step, num_steps; |
| 1155 | uint32_t *eccpos = chip->ecc.layout->eccpos; |
| 1156 | uint8_t *p; |
| 1157 | int data_col_addr, i, gaps = 0; |
| 1158 | int datafrag_len, eccfrag_len, aligned_len, aligned_pos; |
| 1159 | int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1; |
Heiko Schocher | 4e67c57 | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 1160 | int index; |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1161 | unsigned int max_bitflips = 0; |
Scott Wood | c45912d | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 1162 | |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1163 | /* Column address within the page aligned to ECC size (256bytes) */ |
Scott Wood | c45912d | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 1164 | start_step = data_offs / chip->ecc.size; |
| 1165 | end_step = (data_offs + readlen - 1) / chip->ecc.size; |
| 1166 | num_steps = end_step - start_step + 1; |
Heiko Schocher | 4e67c57 | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 1167 | index = start_step * chip->ecc.bytes; |
Scott Wood | c45912d | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 1168 | |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1169 | /* Data size aligned to ECC ecc.size */ |
Scott Wood | c45912d | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 1170 | datafrag_len = num_steps * chip->ecc.size; |
| 1171 | eccfrag_len = num_steps * chip->ecc.bytes; |
| 1172 | |
| 1173 | data_col_addr = start_step * chip->ecc.size; |
| 1174 | /* If we read not a page aligned data */ |
| 1175 | if (data_col_addr != 0) |
| 1176 | chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1); |
| 1177 | |
| 1178 | p = bufpoi + data_col_addr; |
| 1179 | chip->read_buf(mtd, p, datafrag_len); |
| 1180 | |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1181 | /* Calculate ECC */ |
Scott Wood | c45912d | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 1182 | for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) |
| 1183 | chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]); |
| 1184 | |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1185 | /* |
| 1186 | * The performance is faster if we position offsets according to |
| 1187 | * ecc.pos. Let's make sure that there are no gaps in ECC positions. |
| 1188 | */ |
Scott Wood | c45912d | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 1189 | for (i = 0; i < eccfrag_len - 1; i++) { |
Scott Wood | d396372 | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 1190 | if (eccpos[i + index] + 1 != eccpos[i + index + 1]) { |
Scott Wood | c45912d | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 1191 | gaps = 1; |
| 1192 | break; |
| 1193 | } |
| 1194 | } |
| 1195 | if (gaps) { |
| 1196 | chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1); |
| 1197 | chip->read_buf(mtd, chip->oob_poi, mtd->oobsize); |
| 1198 | } else { |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1199 | /* |
| 1200 | * Send the command to read the particular ECC bytes take care |
| 1201 | * about buswidth alignment in read_buf. |
| 1202 | */ |
Christian Hitz | 2a8e0fc | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 1203 | aligned_pos = eccpos[index] & ~(busw - 1); |
Scott Wood | c45912d | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 1204 | aligned_len = eccfrag_len; |
Christian Hitz | 2a8e0fc | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 1205 | if (eccpos[index] & (busw - 1)) |
Scott Wood | c45912d | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 1206 | aligned_len++; |
Christian Hitz | 2a8e0fc | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 1207 | if (eccpos[index + (num_steps * chip->ecc.bytes)] & (busw - 1)) |
Scott Wood | c45912d | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 1208 | aligned_len++; |
| 1209 | |
Christian Hitz | 2a8e0fc | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 1210 | chip->cmdfunc(mtd, NAND_CMD_RNDOUT, |
| 1211 | mtd->writesize + aligned_pos, -1); |
Scott Wood | c45912d | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 1212 | chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len); |
| 1213 | } |
| 1214 | |
| 1215 | for (i = 0; i < eccfrag_len; i++) |
Christian Hitz | 2a8e0fc | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 1216 | chip->buffers->ecccode[i] = chip->oob_poi[eccpos[i + index]]; |
Scott Wood | c45912d | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 1217 | |
| 1218 | p = bufpoi + data_col_addr; |
| 1219 | for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) { |
| 1220 | int stat; |
| 1221 | |
Christian Hitz | 2a8e0fc | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 1222 | stat = chip->ecc.correct(mtd, p, |
| 1223 | &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]); |
Scott Wood | ceee07b | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 1224 | if (stat == -EBADMSG && |
| 1225 | (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) { |
| 1226 | /* check for empty pages with bitflips */ |
| 1227 | stat = nand_check_erased_ecc_chunk(p, chip->ecc.size, |
| 1228 | &chip->buffers->ecccode[i], |
| 1229 | chip->ecc.bytes, |
| 1230 | NULL, 0, |
| 1231 | chip->ecc.strength); |
| 1232 | } |
| 1233 | |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1234 | if (stat < 0) { |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1235 | mtd->ecc_stats.failed++; |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1236 | } else { |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1237 | mtd->ecc_stats.corrected += stat; |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1238 | max_bitflips = max_t(unsigned int, max_bitflips, stat); |
| 1239 | } |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1240 | } |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1241 | return max_bitflips; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1242 | } |
| 1243 | |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1244 | /** |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1245 | * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function |
| 1246 | * @mtd: mtd info structure |
| 1247 | * @chip: nand chip info structure |
| 1248 | * @buf: buffer to store read data |
| 1249 | * @oob_required: caller requires OOB data read to chip->oob_poi |
| 1250 | * @page: page number to read |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1251 | * |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1252 | * Not for syndrome calculating ECC controllers which need a special oob layout. |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1253 | */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1254 | static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip, |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1255 | uint8_t *buf, int oob_required, int page) |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1256 | { |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1257 | int i, eccsize = chip->ecc.size; |
| 1258 | int eccbytes = chip->ecc.bytes; |
| 1259 | int eccsteps = chip->ecc.steps; |
| 1260 | uint8_t *p = buf; |
| 1261 | uint8_t *ecc_calc = chip->buffers->ecccalc; |
| 1262 | uint8_t *ecc_code = chip->buffers->ecccode; |
| 1263 | uint32_t *eccpos = chip->ecc.layout->eccpos; |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1264 | unsigned int max_bitflips = 0; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1265 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1266 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 1267 | chip->ecc.hwctl(mtd, NAND_ECC_READ); |
| 1268 | chip->read_buf(mtd, p, eccsize); |
| 1269 | chip->ecc.calculate(mtd, p, &ecc_calc[i]); |
| 1270 | } |
| 1271 | chip->read_buf(mtd, chip->oob_poi, mtd->oobsize); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1272 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1273 | for (i = 0; i < chip->ecc.total; i++) |
| 1274 | ecc_code[i] = chip->oob_poi[eccpos[i]]; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1275 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1276 | eccsteps = chip->ecc.steps; |
| 1277 | p = buf; |
| 1278 | |
| 1279 | for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 1280 | int stat; |
| 1281 | |
| 1282 | stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]); |
Scott Wood | ceee07b | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 1283 | if (stat == -EBADMSG && |
| 1284 | (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) { |
| 1285 | /* check for empty pages with bitflips */ |
| 1286 | stat = nand_check_erased_ecc_chunk(p, eccsize, |
| 1287 | &ecc_code[i], eccbytes, |
| 1288 | NULL, 0, |
| 1289 | chip->ecc.strength); |
| 1290 | } |
| 1291 | |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1292 | if (stat < 0) { |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1293 | mtd->ecc_stats.failed++; |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1294 | } else { |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1295 | mtd->ecc_stats.corrected += stat; |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1296 | max_bitflips = max_t(unsigned int, max_bitflips, stat); |
| 1297 | } |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1298 | } |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1299 | return max_bitflips; |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1300 | } |
| 1301 | |
| 1302 | /** |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1303 | * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first |
| 1304 | * @mtd: mtd info structure |
| 1305 | * @chip: nand chip info structure |
| 1306 | * @buf: buffer to store read data |
| 1307 | * @oob_required: caller requires OOB data read to chip->oob_poi |
| 1308 | * @page: page number to read |
Sandeep Paulraj | f83b7f9 | 2009-08-10 13:27:56 -0400 | [diff] [blame] | 1309 | * |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1310 | * Hardware ECC for large page chips, require OOB to be read first. For this |
| 1311 | * ECC mode, the write_page method is re-used from ECC_HW. These methods |
| 1312 | * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with |
| 1313 | * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from |
| 1314 | * the data area, by overwriting the NAND manufacturer bad block markings. |
Sandeep Paulraj | f83b7f9 | 2009-08-10 13:27:56 -0400 | [diff] [blame] | 1315 | */ |
| 1316 | static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd, |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1317 | struct nand_chip *chip, uint8_t *buf, int oob_required, int page) |
Sandeep Paulraj | f83b7f9 | 2009-08-10 13:27:56 -0400 | [diff] [blame] | 1318 | { |
| 1319 | int i, eccsize = chip->ecc.size; |
| 1320 | int eccbytes = chip->ecc.bytes; |
| 1321 | int eccsteps = chip->ecc.steps; |
| 1322 | uint8_t *p = buf; |
| 1323 | uint8_t *ecc_code = chip->buffers->ecccode; |
| 1324 | uint32_t *eccpos = chip->ecc.layout->eccpos; |
| 1325 | uint8_t *ecc_calc = chip->buffers->ecccalc; |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1326 | unsigned int max_bitflips = 0; |
Sandeep Paulraj | f83b7f9 | 2009-08-10 13:27:56 -0400 | [diff] [blame] | 1327 | |
| 1328 | /* Read the OOB area first */ |
| 1329 | chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page); |
| 1330 | chip->read_buf(mtd, chip->oob_poi, mtd->oobsize); |
| 1331 | chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page); |
| 1332 | |
| 1333 | for (i = 0; i < chip->ecc.total; i++) |
| 1334 | ecc_code[i] = chip->oob_poi[eccpos[i]]; |
| 1335 | |
| 1336 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 1337 | int stat; |
| 1338 | |
| 1339 | chip->ecc.hwctl(mtd, NAND_ECC_READ); |
| 1340 | chip->read_buf(mtd, p, eccsize); |
| 1341 | chip->ecc.calculate(mtd, p, &ecc_calc[i]); |
| 1342 | |
| 1343 | stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL); |
Scott Wood | ceee07b | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 1344 | if (stat == -EBADMSG && |
| 1345 | (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) { |
| 1346 | /* check for empty pages with bitflips */ |
| 1347 | stat = nand_check_erased_ecc_chunk(p, eccsize, |
| 1348 | &ecc_code[i], eccbytes, |
| 1349 | NULL, 0, |
| 1350 | chip->ecc.strength); |
| 1351 | } |
| 1352 | |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1353 | if (stat < 0) { |
Sandeep Paulraj | f83b7f9 | 2009-08-10 13:27:56 -0400 | [diff] [blame] | 1354 | mtd->ecc_stats.failed++; |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1355 | } else { |
Sandeep Paulraj | f83b7f9 | 2009-08-10 13:27:56 -0400 | [diff] [blame] | 1356 | mtd->ecc_stats.corrected += stat; |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1357 | max_bitflips = max_t(unsigned int, max_bitflips, stat); |
| 1358 | } |
Sandeep Paulraj | f83b7f9 | 2009-08-10 13:27:56 -0400 | [diff] [blame] | 1359 | } |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1360 | return max_bitflips; |
Sandeep Paulraj | f83b7f9 | 2009-08-10 13:27:56 -0400 | [diff] [blame] | 1361 | } |
| 1362 | |
| 1363 | /** |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1364 | * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read |
| 1365 | * @mtd: mtd info structure |
| 1366 | * @chip: nand chip info structure |
| 1367 | * @buf: buffer to store read data |
| 1368 | * @oob_required: caller requires OOB data read to chip->oob_poi |
| 1369 | * @page: page number to read |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1370 | * |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1371 | * The hw generator calculates the error syndrome automatically. Therefore we |
| 1372 | * need a special oob layout and handling. |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1373 | */ |
| 1374 | static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip, |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1375 | uint8_t *buf, int oob_required, int page) |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1376 | { |
| 1377 | int i, eccsize = chip->ecc.size; |
| 1378 | int eccbytes = chip->ecc.bytes; |
| 1379 | int eccsteps = chip->ecc.steps; |
Scott Wood | ceee07b | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 1380 | int eccpadbytes = eccbytes + chip->ecc.prepad + chip->ecc.postpad; |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1381 | uint8_t *p = buf; |
| 1382 | uint8_t *oob = chip->oob_poi; |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1383 | unsigned int max_bitflips = 0; |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1384 | |
| 1385 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 1386 | int stat; |
| 1387 | |
| 1388 | chip->ecc.hwctl(mtd, NAND_ECC_READ); |
| 1389 | chip->read_buf(mtd, p, eccsize); |
| 1390 | |
| 1391 | if (chip->ecc.prepad) { |
| 1392 | chip->read_buf(mtd, oob, chip->ecc.prepad); |
| 1393 | oob += chip->ecc.prepad; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1394 | } |
| 1395 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1396 | chip->ecc.hwctl(mtd, NAND_ECC_READSYN); |
| 1397 | chip->read_buf(mtd, oob, eccbytes); |
| 1398 | stat = chip->ecc.correct(mtd, p, oob, NULL); |
| 1399 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1400 | oob += eccbytes; |
| 1401 | |
| 1402 | if (chip->ecc.postpad) { |
| 1403 | chip->read_buf(mtd, oob, chip->ecc.postpad); |
| 1404 | oob += chip->ecc.postpad; |
| 1405 | } |
Scott Wood | ceee07b | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 1406 | |
| 1407 | if (stat == -EBADMSG && |
| 1408 | (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) { |
| 1409 | /* check for empty pages with bitflips */ |
| 1410 | stat = nand_check_erased_ecc_chunk(p, chip->ecc.size, |
| 1411 | oob - eccpadbytes, |
| 1412 | eccpadbytes, |
| 1413 | NULL, 0, |
| 1414 | chip->ecc.strength); |
| 1415 | } |
| 1416 | |
| 1417 | if (stat < 0) { |
| 1418 | mtd->ecc_stats.failed++; |
| 1419 | } else { |
| 1420 | mtd->ecc_stats.corrected += stat; |
| 1421 | max_bitflips = max_t(unsigned int, max_bitflips, stat); |
| 1422 | } |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1423 | } |
| 1424 | |
| 1425 | /* Calculate remaining oob bytes */ |
| 1426 | i = mtd->oobsize - (oob - chip->oob_poi); |
| 1427 | if (i) |
| 1428 | chip->read_buf(mtd, oob, i); |
| 1429 | |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1430 | return max_bitflips; |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1431 | } |
| 1432 | |
| 1433 | /** |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1434 | * nand_transfer_oob - [INTERN] Transfer oob to client buffer |
| 1435 | * @chip: nand chip structure |
| 1436 | * @oob: oob destination address |
| 1437 | * @ops: oob ops structure |
| 1438 | * @len: size of oob to transfer |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1439 | */ |
| 1440 | static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob, |
| 1441 | struct mtd_oob_ops *ops, size_t len) |
| 1442 | { |
Christian Hitz | 90e3f39 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 1443 | switch (ops->mode) { |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1444 | |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1445 | case MTD_OPS_PLACE_OOB: |
| 1446 | case MTD_OPS_RAW: |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1447 | memcpy(oob, chip->oob_poi + ops->ooboffs, len); |
| 1448 | return oob + len; |
| 1449 | |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1450 | case MTD_OPS_AUTO_OOB: { |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1451 | struct nand_oobfree *free = chip->ecc.layout->oobfree; |
| 1452 | uint32_t boffs = 0, roffs = ops->ooboffs; |
| 1453 | size_t bytes = 0; |
| 1454 | |
Christian Hitz | 90e3f39 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 1455 | for (; free->length && len; free++, len -= bytes) { |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1456 | /* Read request not from offset 0? */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1457 | if (unlikely(roffs)) { |
| 1458 | if (roffs >= free->length) { |
| 1459 | roffs -= free->length; |
| 1460 | continue; |
| 1461 | } |
| 1462 | boffs = free->offset + roffs; |
| 1463 | bytes = min_t(size_t, len, |
| 1464 | (free->length - roffs)); |
| 1465 | roffs = 0; |
| 1466 | } else { |
| 1467 | bytes = min_t(size_t, len, free->length); |
| 1468 | boffs = free->offset; |
| 1469 | } |
| 1470 | memcpy(oob, chip->oob_poi + boffs, bytes); |
| 1471 | oob += bytes; |
| 1472 | } |
| 1473 | return oob; |
| 1474 | } |
| 1475 | default: |
| 1476 | BUG(); |
| 1477 | } |
| 1478 | return NULL; |
| 1479 | } |
| 1480 | |
| 1481 | /** |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1482 | * nand_setup_read_retry - [INTERN] Set the READ RETRY mode |
| 1483 | * @mtd: MTD device structure |
| 1484 | * @retry_mode: the retry mode to use |
| 1485 | * |
| 1486 | * Some vendors supply a special command to shift the Vt threshold, to be used |
| 1487 | * when there are too many bitflips in a page (i.e., ECC error). After setting |
| 1488 | * a new threshold, the host should retry reading the page. |
| 1489 | */ |
| 1490 | static int nand_setup_read_retry(struct mtd_info *mtd, int retry_mode) |
| 1491 | { |
Scott Wood | 17cb4b8 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 1492 | struct nand_chip *chip = mtd_to_nand(mtd); |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1493 | |
| 1494 | pr_debug("setting READ RETRY mode %d\n", retry_mode); |
| 1495 | |
| 1496 | if (retry_mode >= chip->read_retries) |
| 1497 | return -EINVAL; |
| 1498 | |
| 1499 | if (!chip->setup_read_retry) |
| 1500 | return -EOPNOTSUPP; |
| 1501 | |
| 1502 | return chip->setup_read_retry(mtd, retry_mode); |
| 1503 | } |
| 1504 | |
| 1505 | /** |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1506 | * nand_do_read_ops - [INTERN] Read data with ECC |
| 1507 | * @mtd: MTD device structure |
| 1508 | * @from: offset to read from |
| 1509 | * @ops: oob ops structure |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1510 | * |
| 1511 | * Internal function. Called with chip held. |
| 1512 | */ |
| 1513 | static int nand_do_read_ops(struct mtd_info *mtd, loff_t from, |
| 1514 | struct mtd_oob_ops *ops) |
| 1515 | { |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1516 | int chipnr, page, realpage, col, bytes, aligned, oob_required; |
Scott Wood | 17cb4b8 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 1517 | struct nand_chip *chip = mtd_to_nand(mtd); |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1518 | int ret = 0; |
| 1519 | uint32_t readlen = ops->len; |
| 1520 | uint32_t oobreadlen = ops->ooblen; |
Scott Wood | ceee07b | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 1521 | uint32_t max_oobsize = mtd_oobavail(mtd, ops); |
Christian Hitz | 2a8e0fc | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 1522 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1523 | uint8_t *bufpoi, *oob, *buf; |
Scott Wood | d396372 | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 1524 | int use_bufpoi; |
Paul Burton | 40462e5 | 2013-09-04 15:16:56 +0100 | [diff] [blame] | 1525 | unsigned int max_bitflips = 0; |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1526 | int retry_mode = 0; |
| 1527 | bool ecc_fail = false; |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1528 | |
| 1529 | chipnr = (int)(from >> chip->chip_shift); |
| 1530 | chip->select_chip(mtd, chipnr); |
| 1531 | |
| 1532 | realpage = (int)(from >> chip->page_shift); |
| 1533 | page = realpage & chip->pagemask; |
| 1534 | |
| 1535 | col = (int)(from & (mtd->writesize - 1)); |
| 1536 | |
| 1537 | buf = ops->datbuf; |
| 1538 | oob = ops->oobbuf; |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1539 | oob_required = oob ? 1 : 0; |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1540 | |
Christian Hitz | 90e3f39 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 1541 | while (1) { |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1542 | unsigned int ecc_failures = mtd->ecc_stats.failed; |
Scott Wood | 6f2ffc3 | 2011-02-02 18:15:57 -0600 | [diff] [blame] | 1543 | |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1544 | WATCHDOG_RESET(); |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1545 | bytes = min(mtd->writesize - col, readlen); |
| 1546 | aligned = (bytes == mtd->writesize); |
| 1547 | |
Scott Wood | d396372 | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 1548 | if (!aligned) |
| 1549 | use_bufpoi = 1; |
| 1550 | else |
| 1551 | use_bufpoi = 0; |
| 1552 | |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1553 | /* Is the current page in the buffer? */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1554 | if (realpage != chip->pagebuf || oob) { |
Scott Wood | d396372 | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 1555 | bufpoi = use_bufpoi ? chip->buffers->databuf : buf; |
| 1556 | |
| 1557 | if (use_bufpoi && aligned) |
| 1558 | pr_debug("%s: using read bounce buffer for buf@%p\n", |
| 1559 | __func__, buf); |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1560 | |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1561 | read_retry: |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1562 | chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page); |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1563 | |
Paul Burton | 40462e5 | 2013-09-04 15:16:56 +0100 | [diff] [blame] | 1564 | /* |
| 1565 | * Now read the page into the buffer. Absent an error, |
| 1566 | * the read methods return max bitflips per ecc step. |
| 1567 | */ |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1568 | if (unlikely(ops->mode == MTD_OPS_RAW)) |
| 1569 | ret = chip->ecc.read_page_raw(mtd, chip, bufpoi, |
| 1570 | oob_required, |
| 1571 | page); |
Joe Hershberger | c788ecf | 2012-11-05 06:46:31 +0000 | [diff] [blame] | 1572 | else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) && |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1573 | !oob) |
Christian Hitz | 90e3f39 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 1574 | ret = chip->ecc.read_subpage(mtd, chip, |
Heiko Schocher | 4e67c57 | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 1575 | col, bytes, bufpoi, |
| 1576 | page); |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1577 | else |
Sandeep Paulraj | a2c65b4 | 2009-08-10 13:27:46 -0400 | [diff] [blame] | 1578 | ret = chip->ecc.read_page(mtd, chip, bufpoi, |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1579 | oob_required, page); |
| 1580 | if (ret < 0) { |
Scott Wood | d396372 | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 1581 | if (use_bufpoi) |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1582 | /* Invalidate page cache */ |
| 1583 | chip->pagebuf = -1; |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1584 | break; |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1585 | } |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1586 | |
Paul Burton | 40462e5 | 2013-09-04 15:16:56 +0100 | [diff] [blame] | 1587 | max_bitflips = max_t(unsigned int, max_bitflips, ret); |
| 1588 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1589 | /* Transfer not aligned data */ |
Scott Wood | d396372 | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 1590 | if (use_bufpoi) { |
Joe Hershberger | c788ecf | 2012-11-05 06:46:31 +0000 | [diff] [blame] | 1591 | if (!NAND_HAS_SUBPAGE_READ(chip) && !oob && |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1592 | !(mtd->ecc_stats.failed - ecc_failures) && |
Paul Burton | 40462e5 | 2013-09-04 15:16:56 +0100 | [diff] [blame] | 1593 | (ops->mode != MTD_OPS_RAW)) { |
Scott Wood | c45912d | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 1594 | chip->pagebuf = realpage; |
Paul Burton | 40462e5 | 2013-09-04 15:16:56 +0100 | [diff] [blame] | 1595 | chip->pagebuf_bitflips = ret; |
| 1596 | } else { |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1597 | /* Invalidate page cache */ |
| 1598 | chip->pagebuf = -1; |
Paul Burton | 40462e5 | 2013-09-04 15:16:56 +0100 | [diff] [blame] | 1599 | } |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1600 | memcpy(buf, chip->buffers->databuf + col, bytes); |
| 1601 | } |
| 1602 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1603 | if (unlikely(oob)) { |
Christian Hitz | 2a8e0fc | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 1604 | int toread = min(oobreadlen, max_oobsize); |
| 1605 | |
| 1606 | if (toread) { |
| 1607 | oob = nand_transfer_oob(chip, |
| 1608 | oob, ops, toread); |
| 1609 | oobreadlen -= toread; |
| 1610 | } |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1611 | } |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1612 | |
| 1613 | if (chip->options & NAND_NEED_READRDY) { |
| 1614 | /* Apply delay or wait for ready/busy pin */ |
| 1615 | if (!chip->dev_ready) |
| 1616 | udelay(chip->chip_delay); |
| 1617 | else |
| 1618 | nand_wait_ready(mtd); |
| 1619 | } |
| 1620 | |
| 1621 | if (mtd->ecc_stats.failed - ecc_failures) { |
| 1622 | if (retry_mode + 1 < chip->read_retries) { |
| 1623 | retry_mode++; |
| 1624 | ret = nand_setup_read_retry(mtd, |
| 1625 | retry_mode); |
| 1626 | if (ret < 0) |
| 1627 | break; |
| 1628 | |
| 1629 | /* Reset failures; retry */ |
| 1630 | mtd->ecc_stats.failed = ecc_failures; |
| 1631 | goto read_retry; |
| 1632 | } else { |
| 1633 | /* No more retry modes; real failure */ |
| 1634 | ecc_fail = true; |
| 1635 | } |
| 1636 | } |
| 1637 | |
| 1638 | buf += bytes; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1639 | } else { |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1640 | memcpy(buf, chip->buffers->databuf + col, bytes); |
| 1641 | buf += bytes; |
Paul Burton | 40462e5 | 2013-09-04 15:16:56 +0100 | [diff] [blame] | 1642 | max_bitflips = max_t(unsigned int, max_bitflips, |
| 1643 | chip->pagebuf_bitflips); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1644 | } |
| 1645 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1646 | readlen -= bytes; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1647 | |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1648 | /* Reset to retry mode 0 */ |
| 1649 | if (retry_mode) { |
| 1650 | ret = nand_setup_read_retry(mtd, 0); |
| 1651 | if (ret < 0) |
| 1652 | break; |
| 1653 | retry_mode = 0; |
| 1654 | } |
| 1655 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1656 | if (!readlen) |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 1657 | break; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1658 | |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1659 | /* For subsequent reads align to page boundary */ |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1660 | col = 0; |
| 1661 | /* Increment page address */ |
| 1662 | realpage++; |
| 1663 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1664 | page = realpage & chip->pagemask; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1665 | /* Check, if we cross a chip boundary */ |
| 1666 | if (!page) { |
| 1667 | chipnr++; |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1668 | chip->select_chip(mtd, -1); |
| 1669 | chip->select_chip(mtd, chipnr); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1670 | } |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1671 | } |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1672 | chip->select_chip(mtd, -1); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1673 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1674 | ops->retlen = ops->len - (size_t) readlen; |
| 1675 | if (oob) |
| 1676 | ops->oobretlen = ops->ooblen - oobreadlen; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1677 | |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1678 | if (ret < 0) |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1679 | return ret; |
| 1680 | |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1681 | if (ecc_fail) |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1682 | return -EBADMSG; |
| 1683 | |
Paul Burton | 40462e5 | 2013-09-04 15:16:56 +0100 | [diff] [blame] | 1684 | return max_bitflips; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1685 | } |
| 1686 | |
| 1687 | /** |
Christian Hitz | 90e3f39 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 1688 | * nand_read - [MTD Interface] MTD compatibility function for nand_do_read_ecc |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1689 | * @mtd: MTD device structure |
| 1690 | * @from: offset to read from |
| 1691 | * @len: number of bytes to read |
| 1692 | * @retlen: pointer to variable to store the number of read bytes |
| 1693 | * @buf: the databuffer to put data |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1694 | * |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1695 | * Get hold of the chip and call nand_do_read. |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1696 | */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1697 | static int nand_read(struct mtd_info *mtd, loff_t from, size_t len, |
| 1698 | size_t *retlen, uint8_t *buf) |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1699 | { |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1700 | struct mtd_oob_ops ops; |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1701 | int ret; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1702 | |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1703 | nand_get_device(mtd, FL_READING); |
Scott Wood | d396372 | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 1704 | memset(&ops, 0, sizeof(ops)); |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1705 | ops.len = len; |
| 1706 | ops.datbuf = buf; |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1707 | ops.mode = MTD_OPS_PLACE_OOB; |
| 1708 | ret = nand_do_read_ops(mtd, from, &ops); |
| 1709 | *retlen = ops.retlen; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1710 | nand_release_device(mtd); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1711 | return ret; |
| 1712 | } |
| 1713 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1714 | /** |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1715 | * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function |
| 1716 | * @mtd: mtd info structure |
| 1717 | * @chip: nand chip info structure |
| 1718 | * @page: page number to read |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1719 | */ |
| 1720 | static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip, |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1721 | int page) |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1722 | { |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1723 | chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page); |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1724 | chip->read_buf(mtd, chip->oob_poi, mtd->oobsize); |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1725 | return 0; |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1726 | } |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1727 | |
| 1728 | /** |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1729 | * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1730 | * with syndromes |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1731 | * @mtd: mtd info structure |
| 1732 | * @chip: nand chip info structure |
| 1733 | * @page: page number to read |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1734 | */ |
| 1735 | static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip, |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1736 | int page) |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1737 | { |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1738 | int length = mtd->oobsize; |
| 1739 | int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad; |
| 1740 | int eccsize = chip->ecc.size; |
Scott Wood | d396372 | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 1741 | uint8_t *bufpoi = chip->oob_poi; |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1742 | int i, toread, sndrnd = 0, pos; |
| 1743 | |
| 1744 | chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page); |
| 1745 | for (i = 0; i < chip->ecc.steps; i++) { |
| 1746 | if (sndrnd) { |
| 1747 | pos = eccsize + i * (eccsize + chunk); |
| 1748 | if (mtd->writesize > 512) |
| 1749 | chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1); |
| 1750 | else |
| 1751 | chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page); |
| 1752 | } else |
| 1753 | sndrnd = 1; |
| 1754 | toread = min_t(int, length, chunk); |
| 1755 | chip->read_buf(mtd, bufpoi, toread); |
| 1756 | bufpoi += toread; |
| 1757 | length -= toread; |
| 1758 | } |
| 1759 | if (length > 0) |
| 1760 | chip->read_buf(mtd, bufpoi, length); |
| 1761 | |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1762 | return 0; |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1763 | } |
| 1764 | |
| 1765 | /** |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1766 | * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function |
| 1767 | * @mtd: mtd info structure |
| 1768 | * @chip: nand chip info structure |
| 1769 | * @page: page number to write |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1770 | */ |
| 1771 | static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip, |
| 1772 | int page) |
| 1773 | { |
| 1774 | int status = 0; |
| 1775 | const uint8_t *buf = chip->oob_poi; |
| 1776 | int length = mtd->oobsize; |
| 1777 | |
| 1778 | chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page); |
| 1779 | chip->write_buf(mtd, buf, length); |
| 1780 | /* Send command to program the OOB data */ |
| 1781 | chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1); |
| 1782 | |
| 1783 | status = chip->waitfunc(mtd, chip); |
| 1784 | |
| 1785 | return status & NAND_STATUS_FAIL ? -EIO : 0; |
| 1786 | } |
| 1787 | |
| 1788 | /** |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1789 | * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC |
| 1790 | * with syndrome - only for large page flash |
| 1791 | * @mtd: mtd info structure |
| 1792 | * @chip: nand chip info structure |
| 1793 | * @page: page number to write |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1794 | */ |
| 1795 | static int nand_write_oob_syndrome(struct mtd_info *mtd, |
| 1796 | struct nand_chip *chip, int page) |
| 1797 | { |
| 1798 | int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad; |
| 1799 | int eccsize = chip->ecc.size, length = mtd->oobsize; |
| 1800 | int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps; |
| 1801 | const uint8_t *bufpoi = chip->oob_poi; |
| 1802 | |
| 1803 | /* |
| 1804 | * data-ecc-data-ecc ... ecc-oob |
| 1805 | * or |
| 1806 | * data-pad-ecc-pad-data-pad .... ecc-pad-oob |
| 1807 | */ |
| 1808 | if (!chip->ecc.prepad && !chip->ecc.postpad) { |
| 1809 | pos = steps * (eccsize + chunk); |
| 1810 | steps = 0; |
| 1811 | } else |
| 1812 | pos = eccsize; |
| 1813 | |
| 1814 | chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page); |
| 1815 | for (i = 0; i < steps; i++) { |
| 1816 | if (sndcmd) { |
| 1817 | if (mtd->writesize <= 512) { |
| 1818 | uint32_t fill = 0xFFFFFFFF; |
| 1819 | |
| 1820 | len = eccsize; |
| 1821 | while (len > 0) { |
| 1822 | int num = min_t(int, len, 4); |
| 1823 | chip->write_buf(mtd, (uint8_t *)&fill, |
| 1824 | num); |
| 1825 | len -= num; |
| 1826 | } |
| 1827 | } else { |
| 1828 | pos = eccsize + i * (eccsize + chunk); |
| 1829 | chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1); |
| 1830 | } |
| 1831 | } else |
| 1832 | sndcmd = 1; |
| 1833 | len = min_t(int, length, chunk); |
| 1834 | chip->write_buf(mtd, bufpoi, len); |
| 1835 | bufpoi += len; |
| 1836 | length -= len; |
| 1837 | } |
| 1838 | if (length > 0) |
| 1839 | chip->write_buf(mtd, bufpoi, length); |
| 1840 | |
| 1841 | chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1); |
| 1842 | status = chip->waitfunc(mtd, chip); |
| 1843 | |
| 1844 | return status & NAND_STATUS_FAIL ? -EIO : 0; |
| 1845 | } |
| 1846 | |
| 1847 | /** |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1848 | * nand_do_read_oob - [INTERN] NAND read out-of-band |
| 1849 | * @mtd: MTD device structure |
| 1850 | * @from: offset to read from |
| 1851 | * @ops: oob operations description structure |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1852 | * |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1853 | * NAND read out-of-band data from the spare area. |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1854 | */ |
| 1855 | static int nand_do_read_oob(struct mtd_info *mtd, loff_t from, |
| 1856 | struct mtd_oob_ops *ops) |
| 1857 | { |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1858 | int page, realpage, chipnr; |
Scott Wood | 17cb4b8 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 1859 | struct nand_chip *chip = mtd_to_nand(mtd); |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1860 | struct mtd_ecc_stats stats; |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1861 | int readlen = ops->ooblen; |
| 1862 | int len; |
| 1863 | uint8_t *buf = ops->oobbuf; |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1864 | int ret = 0; |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1865 | |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1866 | pr_debug("%s: from = 0x%08Lx, len = %i\n", |
Christian Hitz | 90e3f39 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 1867 | __func__, (unsigned long long)from, readlen); |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1868 | |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1869 | stats = mtd->ecc_stats; |
| 1870 | |
Scott Wood | ceee07b | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 1871 | len = mtd_oobavail(mtd, ops); |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1872 | |
| 1873 | if (unlikely(ops->ooboffs >= len)) { |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1874 | pr_debug("%s: attempt to start read outside oob\n", |
| 1875 | __func__); |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1876 | return -EINVAL; |
| 1877 | } |
| 1878 | |
| 1879 | /* Do not allow reads past end of device */ |
| 1880 | if (unlikely(from >= mtd->size || |
| 1881 | ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) - |
| 1882 | (from >> chip->page_shift)) * len)) { |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1883 | pr_debug("%s: attempt to read beyond end of device\n", |
| 1884 | __func__); |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1885 | return -EINVAL; |
| 1886 | } |
| 1887 | |
| 1888 | chipnr = (int)(from >> chip->chip_shift); |
| 1889 | chip->select_chip(mtd, chipnr); |
| 1890 | |
| 1891 | /* Shift to get page */ |
| 1892 | realpage = (int)(from >> chip->page_shift); |
| 1893 | page = realpage & chip->pagemask; |
| 1894 | |
Christian Hitz | 90e3f39 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 1895 | while (1) { |
Scott Wood | 6f2ffc3 | 2011-02-02 18:15:57 -0600 | [diff] [blame] | 1896 | WATCHDOG_RESET(); |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1897 | |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1898 | if (ops->mode == MTD_OPS_RAW) |
| 1899 | ret = chip->ecc.read_oob_raw(mtd, chip, page); |
| 1900 | else |
| 1901 | ret = chip->ecc.read_oob(mtd, chip, page); |
| 1902 | |
| 1903 | if (ret < 0) |
| 1904 | break; |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1905 | |
| 1906 | len = min(len, readlen); |
| 1907 | buf = nand_transfer_oob(chip, buf, ops, len); |
| 1908 | |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1909 | if (chip->options & NAND_NEED_READRDY) { |
| 1910 | /* Apply delay or wait for ready/busy pin */ |
| 1911 | if (!chip->dev_ready) |
| 1912 | udelay(chip->chip_delay); |
| 1913 | else |
| 1914 | nand_wait_ready(mtd); |
| 1915 | } |
| 1916 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1917 | readlen -= len; |
| 1918 | if (!readlen) |
| 1919 | break; |
| 1920 | |
| 1921 | /* Increment page address */ |
| 1922 | realpage++; |
| 1923 | |
| 1924 | page = realpage & chip->pagemask; |
| 1925 | /* Check, if we cross a chip boundary */ |
| 1926 | if (!page) { |
| 1927 | chipnr++; |
| 1928 | chip->select_chip(mtd, -1); |
| 1929 | chip->select_chip(mtd, chipnr); |
| 1930 | } |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1931 | } |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1932 | chip->select_chip(mtd, -1); |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1933 | |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1934 | ops->oobretlen = ops->ooblen - readlen; |
| 1935 | |
| 1936 | if (ret < 0) |
| 1937 | return ret; |
| 1938 | |
| 1939 | if (mtd->ecc_stats.failed - stats.failed) |
| 1940 | return -EBADMSG; |
| 1941 | |
| 1942 | return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0; |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1943 | } |
| 1944 | |
| 1945 | /** |
| 1946 | * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1947 | * @mtd: MTD device structure |
| 1948 | * @from: offset to read from |
| 1949 | * @ops: oob operation description structure |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1950 | * |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1951 | * NAND read data and/or out-of-band data. |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1952 | */ |
| 1953 | static int nand_read_oob(struct mtd_info *mtd, loff_t from, |
| 1954 | struct mtd_oob_ops *ops) |
| 1955 | { |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1956 | int ret = -ENOTSUPP; |
| 1957 | |
| 1958 | ops->retlen = 0; |
| 1959 | |
| 1960 | /* Do not allow reads past end of device */ |
| 1961 | if (ops->datbuf && (from + ops->len) > mtd->size) { |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1962 | pr_debug("%s: attempt to read beyond end of device\n", |
| 1963 | __func__); |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1964 | return -EINVAL; |
| 1965 | } |
| 1966 | |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1967 | nand_get_device(mtd, FL_READING); |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1968 | |
Christian Hitz | 90e3f39 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 1969 | switch (ops->mode) { |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1970 | case MTD_OPS_PLACE_OOB: |
| 1971 | case MTD_OPS_AUTO_OOB: |
| 1972 | case MTD_OPS_RAW: |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1973 | break; |
| 1974 | |
| 1975 | default: |
| 1976 | goto out; |
| 1977 | } |
| 1978 | |
| 1979 | if (!ops->datbuf) |
| 1980 | ret = nand_do_read_oob(mtd, from, ops); |
| 1981 | else |
| 1982 | ret = nand_do_read_ops(mtd, from, ops); |
| 1983 | |
Christian Hitz | 90e3f39 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 1984 | out: |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1985 | nand_release_device(mtd); |
| 1986 | return ret; |
| 1987 | } |
| 1988 | |
| 1989 | |
| 1990 | /** |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1991 | * nand_write_page_raw - [INTERN] raw page write function |
| 1992 | * @mtd: mtd info structure |
| 1993 | * @chip: nand chip info structure |
| 1994 | * @buf: data buffer |
| 1995 | * @oob_required: must write chip->oob_poi to OOB |
Scott Wood | 81c7725 | 2016-05-30 13:57:57 -0500 | [diff] [blame] | 1996 | * @page: page number to write |
David Brownell | 7e86661 | 2009-11-07 16:27:01 -0500 | [diff] [blame] | 1997 | * |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1998 | * Not for syndrome calculating ECC controllers, which use a special oob layout. |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1999 | */ |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2000 | static int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip, |
Scott Wood | ceee07b | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 2001 | const uint8_t *buf, int oob_required, int page) |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2002 | { |
| 2003 | chip->write_buf(mtd, buf, mtd->writesize); |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2004 | if (oob_required) |
| 2005 | chip->write_buf(mtd, chip->oob_poi, mtd->oobsize); |
| 2006 | |
| 2007 | return 0; |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2008 | } |
| 2009 | |
| 2010 | /** |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2011 | * nand_write_page_raw_syndrome - [INTERN] raw page write function |
| 2012 | * @mtd: mtd info structure |
| 2013 | * @chip: nand chip info structure |
| 2014 | * @buf: data buffer |
| 2015 | * @oob_required: must write chip->oob_poi to OOB |
Scott Wood | ceee07b | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 2016 | * @page: page number to write |
David Brownell | 7e86661 | 2009-11-07 16:27:01 -0500 | [diff] [blame] | 2017 | * |
| 2018 | * We need a special oob layout and handling even when ECC isn't checked. |
| 2019 | */ |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2020 | static int nand_write_page_raw_syndrome(struct mtd_info *mtd, |
Christian Hitz | 90e3f39 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 2021 | struct nand_chip *chip, |
Scott Wood | 81c7725 | 2016-05-30 13:57:57 -0500 | [diff] [blame] | 2022 | const uint8_t *buf, int oob_required, |
| 2023 | int page) |
David Brownell | 7e86661 | 2009-11-07 16:27:01 -0500 | [diff] [blame] | 2024 | { |
| 2025 | int eccsize = chip->ecc.size; |
| 2026 | int eccbytes = chip->ecc.bytes; |
| 2027 | uint8_t *oob = chip->oob_poi; |
| 2028 | int steps, size; |
| 2029 | |
| 2030 | for (steps = chip->ecc.steps; steps > 0; steps--) { |
| 2031 | chip->write_buf(mtd, buf, eccsize); |
| 2032 | buf += eccsize; |
| 2033 | |
| 2034 | if (chip->ecc.prepad) { |
| 2035 | chip->write_buf(mtd, oob, chip->ecc.prepad); |
| 2036 | oob += chip->ecc.prepad; |
| 2037 | } |
| 2038 | |
Heiko Schocher | 4e67c57 | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 2039 | chip->write_buf(mtd, oob, eccbytes); |
David Brownell | 7e86661 | 2009-11-07 16:27:01 -0500 | [diff] [blame] | 2040 | oob += eccbytes; |
| 2041 | |
| 2042 | if (chip->ecc.postpad) { |
| 2043 | chip->write_buf(mtd, oob, chip->ecc.postpad); |
| 2044 | oob += chip->ecc.postpad; |
| 2045 | } |
| 2046 | } |
| 2047 | |
| 2048 | size = mtd->oobsize - (oob - chip->oob_poi); |
| 2049 | if (size) |
| 2050 | chip->write_buf(mtd, oob, size); |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2051 | |
| 2052 | return 0; |
David Brownell | 7e86661 | 2009-11-07 16:27:01 -0500 | [diff] [blame] | 2053 | } |
| 2054 | /** |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2055 | * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function |
| 2056 | * @mtd: mtd info structure |
| 2057 | * @chip: nand chip info structure |
| 2058 | * @buf: data buffer |
| 2059 | * @oob_required: must write chip->oob_poi to OOB |
Scott Wood | 81c7725 | 2016-05-30 13:57:57 -0500 | [diff] [blame] | 2060 | * @page: page number to write |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2061 | */ |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2062 | static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip, |
Scott Wood | ceee07b | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 2063 | const uint8_t *buf, int oob_required, |
| 2064 | int page) |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2065 | { |
| 2066 | int i, eccsize = chip->ecc.size; |
| 2067 | int eccbytes = chip->ecc.bytes; |
| 2068 | int eccsteps = chip->ecc.steps; |
| 2069 | uint8_t *ecc_calc = chip->buffers->ecccalc; |
| 2070 | const uint8_t *p = buf; |
| 2071 | uint32_t *eccpos = chip->ecc.layout->eccpos; |
| 2072 | |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2073 | /* Software ECC calculation */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2074 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) |
| 2075 | chip->ecc.calculate(mtd, p, &ecc_calc[i]); |
| 2076 | |
| 2077 | for (i = 0; i < chip->ecc.total; i++) |
| 2078 | chip->oob_poi[eccpos[i]] = ecc_calc[i]; |
| 2079 | |
Scott Wood | 81c7725 | 2016-05-30 13:57:57 -0500 | [diff] [blame] | 2080 | return chip->ecc.write_page_raw(mtd, chip, buf, 1, page); |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2081 | } |
| 2082 | |
| 2083 | /** |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2084 | * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function |
| 2085 | * @mtd: mtd info structure |
| 2086 | * @chip: nand chip info structure |
| 2087 | * @buf: data buffer |
| 2088 | * @oob_required: must write chip->oob_poi to OOB |
Scott Wood | 81c7725 | 2016-05-30 13:57:57 -0500 | [diff] [blame] | 2089 | * @page: page number to write |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2090 | */ |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2091 | static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip, |
Scott Wood | 81c7725 | 2016-05-30 13:57:57 -0500 | [diff] [blame] | 2092 | const uint8_t *buf, int oob_required, |
| 2093 | int page) |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2094 | { |
| 2095 | int i, eccsize = chip->ecc.size; |
| 2096 | int eccbytes = chip->ecc.bytes; |
| 2097 | int eccsteps = chip->ecc.steps; |
| 2098 | uint8_t *ecc_calc = chip->buffers->ecccalc; |
| 2099 | const uint8_t *p = buf; |
| 2100 | uint32_t *eccpos = chip->ecc.layout->eccpos; |
| 2101 | |
| 2102 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 2103 | chip->ecc.hwctl(mtd, NAND_ECC_WRITE); |
| 2104 | chip->write_buf(mtd, p, eccsize); |
| 2105 | chip->ecc.calculate(mtd, p, &ecc_calc[i]); |
| 2106 | } |
| 2107 | |
| 2108 | for (i = 0; i < chip->ecc.total; i++) |
| 2109 | chip->oob_poi[eccpos[i]] = ecc_calc[i]; |
| 2110 | |
| 2111 | chip->write_buf(mtd, chip->oob_poi, mtd->oobsize); |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2112 | |
| 2113 | return 0; |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2114 | } |
| 2115 | |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2116 | |
| 2117 | /** |
Scott Wood | d396372 | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 2118 | * nand_write_subpage_hwecc - [REPLACEABLE] hardware ECC based subpage write |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2119 | * @mtd: mtd info structure |
| 2120 | * @chip: nand chip info structure |
| 2121 | * @offset: column address of subpage within the page |
| 2122 | * @data_len: data length |
| 2123 | * @buf: data buffer |
| 2124 | * @oob_required: must write chip->oob_poi to OOB |
Scott Wood | 81c7725 | 2016-05-30 13:57:57 -0500 | [diff] [blame] | 2125 | * @page: page number to write |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2126 | */ |
| 2127 | static int nand_write_subpage_hwecc(struct mtd_info *mtd, |
| 2128 | struct nand_chip *chip, uint32_t offset, |
| 2129 | uint32_t data_len, const uint8_t *buf, |
Scott Wood | 81c7725 | 2016-05-30 13:57:57 -0500 | [diff] [blame] | 2130 | int oob_required, int page) |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2131 | { |
| 2132 | uint8_t *oob_buf = chip->oob_poi; |
| 2133 | uint8_t *ecc_calc = chip->buffers->ecccalc; |
| 2134 | int ecc_size = chip->ecc.size; |
| 2135 | int ecc_bytes = chip->ecc.bytes; |
| 2136 | int ecc_steps = chip->ecc.steps; |
| 2137 | uint32_t *eccpos = chip->ecc.layout->eccpos; |
| 2138 | uint32_t start_step = offset / ecc_size; |
| 2139 | uint32_t end_step = (offset + data_len - 1) / ecc_size; |
| 2140 | int oob_bytes = mtd->oobsize / ecc_steps; |
| 2141 | int step, i; |
| 2142 | |
| 2143 | for (step = 0; step < ecc_steps; step++) { |
| 2144 | /* configure controller for WRITE access */ |
| 2145 | chip->ecc.hwctl(mtd, NAND_ECC_WRITE); |
| 2146 | |
| 2147 | /* write data (untouched subpages already masked by 0xFF) */ |
| 2148 | chip->write_buf(mtd, buf, ecc_size); |
| 2149 | |
| 2150 | /* mask ECC of un-touched subpages by padding 0xFF */ |
| 2151 | if ((step < start_step) || (step > end_step)) |
| 2152 | memset(ecc_calc, 0xff, ecc_bytes); |
| 2153 | else |
| 2154 | chip->ecc.calculate(mtd, buf, ecc_calc); |
| 2155 | |
| 2156 | /* mask OOB of un-touched subpages by padding 0xFF */ |
| 2157 | /* if oob_required, preserve OOB metadata of written subpage */ |
| 2158 | if (!oob_required || (step < start_step) || (step > end_step)) |
| 2159 | memset(oob_buf, 0xff, oob_bytes); |
| 2160 | |
| 2161 | buf += ecc_size; |
| 2162 | ecc_calc += ecc_bytes; |
| 2163 | oob_buf += oob_bytes; |
| 2164 | } |
| 2165 | |
| 2166 | /* copy calculated ECC for whole page to chip->buffer->oob */ |
| 2167 | /* this include masked-value(0xFF) for unwritten subpages */ |
| 2168 | ecc_calc = chip->buffers->ecccalc; |
| 2169 | for (i = 0; i < chip->ecc.total; i++) |
| 2170 | chip->oob_poi[eccpos[i]] = ecc_calc[i]; |
| 2171 | |
| 2172 | /* write OOB buffer to NAND device */ |
| 2173 | chip->write_buf(mtd, chip->oob_poi, mtd->oobsize); |
| 2174 | |
| 2175 | return 0; |
| 2176 | } |
| 2177 | |
| 2178 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2179 | /** |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2180 | * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write |
| 2181 | * @mtd: mtd info structure |
| 2182 | * @chip: nand chip info structure |
| 2183 | * @buf: data buffer |
| 2184 | * @oob_required: must write chip->oob_poi to OOB |
Scott Wood | ceee07b | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 2185 | * @page: page number to write |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2186 | * |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2187 | * The hw generator calculates the error syndrome automatically. Therefore we |
| 2188 | * need a special oob layout and handling. |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2189 | */ |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2190 | static int nand_write_page_syndrome(struct mtd_info *mtd, |
| 2191 | struct nand_chip *chip, |
Scott Wood | 81c7725 | 2016-05-30 13:57:57 -0500 | [diff] [blame] | 2192 | const uint8_t *buf, int oob_required, |
| 2193 | int page) |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2194 | { |
| 2195 | int i, eccsize = chip->ecc.size; |
| 2196 | int eccbytes = chip->ecc.bytes; |
| 2197 | int eccsteps = chip->ecc.steps; |
| 2198 | const uint8_t *p = buf; |
| 2199 | uint8_t *oob = chip->oob_poi; |
| 2200 | |
| 2201 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 2202 | |
| 2203 | chip->ecc.hwctl(mtd, NAND_ECC_WRITE); |
| 2204 | chip->write_buf(mtd, p, eccsize); |
| 2205 | |
| 2206 | if (chip->ecc.prepad) { |
| 2207 | chip->write_buf(mtd, oob, chip->ecc.prepad); |
| 2208 | oob += chip->ecc.prepad; |
| 2209 | } |
| 2210 | |
| 2211 | chip->ecc.calculate(mtd, p, oob); |
| 2212 | chip->write_buf(mtd, oob, eccbytes); |
| 2213 | oob += eccbytes; |
| 2214 | |
| 2215 | if (chip->ecc.postpad) { |
| 2216 | chip->write_buf(mtd, oob, chip->ecc.postpad); |
| 2217 | oob += chip->ecc.postpad; |
| 2218 | } |
| 2219 | } |
| 2220 | |
| 2221 | /* Calculate remaining oob bytes */ |
| 2222 | i = mtd->oobsize - (oob - chip->oob_poi); |
| 2223 | if (i) |
| 2224 | chip->write_buf(mtd, oob, i); |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2225 | |
| 2226 | return 0; |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2227 | } |
| 2228 | |
| 2229 | /** |
| 2230 | * nand_write_page - [REPLACEABLE] write one page |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2231 | * @mtd: MTD device structure |
| 2232 | * @chip: NAND chip descriptor |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2233 | * @offset: address offset within the page |
| 2234 | * @data_len: length of actual data to be written |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2235 | * @buf: the data to write |
| 2236 | * @oob_required: must write chip->oob_poi to OOB |
| 2237 | * @page: page number to write |
| 2238 | * @cached: cached programming |
| 2239 | * @raw: use _raw version of write_page |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2240 | */ |
| 2241 | static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip, |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2242 | uint32_t offset, int data_len, const uint8_t *buf, |
| 2243 | int oob_required, int page, int cached, int raw) |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2244 | { |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2245 | int status, subpage; |
| 2246 | |
| 2247 | if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && |
| 2248 | chip->ecc.write_subpage) |
| 2249 | subpage = offset || (data_len < mtd->writesize); |
| 2250 | else |
| 2251 | subpage = 0; |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2252 | |
| 2253 | chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page); |
| 2254 | |
| 2255 | if (unlikely(raw)) |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2256 | status = chip->ecc.write_page_raw(mtd, chip, buf, |
Scott Wood | 81c7725 | 2016-05-30 13:57:57 -0500 | [diff] [blame] | 2257 | oob_required, page); |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2258 | else if (subpage) |
| 2259 | status = chip->ecc.write_subpage(mtd, chip, offset, data_len, |
Scott Wood | ceee07b | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 2260 | buf, oob_required, page); |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2261 | else |
Scott Wood | 81c7725 | 2016-05-30 13:57:57 -0500 | [diff] [blame] | 2262 | status = chip->ecc.write_page(mtd, chip, buf, oob_required, |
| 2263 | page); |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2264 | |
| 2265 | if (status < 0) |
| 2266 | return status; |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2267 | |
| 2268 | /* |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2269 | * Cached progamming disabled for now. Not sure if it's worth the |
| 2270 | * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s). |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2271 | */ |
| 2272 | cached = 0; |
| 2273 | |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2274 | if (!cached || !NAND_HAS_CACHEPROG(chip)) { |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2275 | |
| 2276 | chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1); |
| 2277 | status = chip->waitfunc(mtd, chip); |
| 2278 | /* |
| 2279 | * See if operation failed and additional status checks are |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2280 | * available. |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2281 | */ |
| 2282 | if ((status & NAND_STATUS_FAIL) && (chip->errstat)) |
| 2283 | status = chip->errstat(mtd, chip, FL_WRITING, status, |
| 2284 | page); |
| 2285 | |
| 2286 | if (status & NAND_STATUS_FAIL) |
| 2287 | return -EIO; |
| 2288 | } else { |
| 2289 | chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1); |
| 2290 | status = chip->waitfunc(mtd, chip); |
| 2291 | } |
| 2292 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2293 | return 0; |
| 2294 | } |
| 2295 | |
| 2296 | /** |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2297 | * nand_fill_oob - [INTERN] Transfer client buffer to oob |
| 2298 | * @mtd: MTD device structure |
| 2299 | * @oob: oob data buffer |
| 2300 | * @len: oob data write length |
| 2301 | * @ops: oob ops structure |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2302 | */ |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2303 | static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len, |
| 2304 | struct mtd_oob_ops *ops) |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2305 | { |
Scott Wood | 17cb4b8 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 2306 | struct nand_chip *chip = mtd_to_nand(mtd); |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2307 | |
| 2308 | /* |
| 2309 | * Initialise to all 0xFF, to avoid the possibility of left over OOB |
| 2310 | * data from a previous OOB read. |
| 2311 | */ |
| 2312 | memset(chip->oob_poi, 0xff, mtd->oobsize); |
| 2313 | |
Christian Hitz | 90e3f39 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 2314 | switch (ops->mode) { |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2315 | |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2316 | case MTD_OPS_PLACE_OOB: |
| 2317 | case MTD_OPS_RAW: |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2318 | memcpy(chip->oob_poi + ops->ooboffs, oob, len); |
| 2319 | return oob + len; |
| 2320 | |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2321 | case MTD_OPS_AUTO_OOB: { |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2322 | struct nand_oobfree *free = chip->ecc.layout->oobfree; |
| 2323 | uint32_t boffs = 0, woffs = ops->ooboffs; |
| 2324 | size_t bytes = 0; |
| 2325 | |
Christian Hitz | 90e3f39 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 2326 | for (; free->length && len; free++, len -= bytes) { |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2327 | /* Write request not from offset 0? */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2328 | if (unlikely(woffs)) { |
| 2329 | if (woffs >= free->length) { |
| 2330 | woffs -= free->length; |
| 2331 | continue; |
| 2332 | } |
| 2333 | boffs = free->offset + woffs; |
| 2334 | bytes = min_t(size_t, len, |
| 2335 | (free->length - woffs)); |
| 2336 | woffs = 0; |
| 2337 | } else { |
| 2338 | bytes = min_t(size_t, len, free->length); |
| 2339 | boffs = free->offset; |
| 2340 | } |
| 2341 | memcpy(chip->oob_poi + boffs, oob, bytes); |
| 2342 | oob += bytes; |
| 2343 | } |
| 2344 | return oob; |
| 2345 | } |
| 2346 | default: |
| 2347 | BUG(); |
| 2348 | } |
| 2349 | return NULL; |
| 2350 | } |
| 2351 | |
Christian Hitz | 2a8e0fc | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 2352 | #define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0) |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2353 | |
| 2354 | /** |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2355 | * nand_do_write_ops - [INTERN] NAND write with ECC |
| 2356 | * @mtd: MTD device structure |
| 2357 | * @to: offset to write to |
| 2358 | * @ops: oob operations description structure |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2359 | * |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2360 | * NAND write with ECC. |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2361 | */ |
| 2362 | static int nand_do_write_ops(struct mtd_info *mtd, loff_t to, |
| 2363 | struct mtd_oob_ops *ops) |
| 2364 | { |
| 2365 | int chipnr, realpage, page, blockmask, column; |
Scott Wood | 17cb4b8 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 2366 | struct nand_chip *chip = mtd_to_nand(mtd); |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2367 | uint32_t writelen = ops->len; |
Christian Hitz | 2a8e0fc | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 2368 | |
| 2369 | uint32_t oobwritelen = ops->ooblen; |
Scott Wood | ceee07b | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 2370 | uint32_t oobmaxlen = mtd_oobavail(mtd, ops); |
Christian Hitz | 2a8e0fc | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 2371 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2372 | uint8_t *oob = ops->oobbuf; |
| 2373 | uint8_t *buf = ops->datbuf; |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2374 | int ret; |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2375 | int oob_required = oob ? 1 : 0; |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2376 | |
| 2377 | ops->retlen = 0; |
| 2378 | if (!writelen) |
| 2379 | return 0; |
| 2380 | |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2381 | /* Reject writes, which are not page aligned */ |
| 2382 | if (NOTALIGNED(to)) { |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2383 | pr_notice("%s: attempt to write non page aligned data\n", |
| 2384 | __func__); |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2385 | return -EINVAL; |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2386 | } |
| 2387 | |
| 2388 | column = to & (mtd->writesize - 1); |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2389 | |
| 2390 | chipnr = (int)(to >> chip->chip_shift); |
| 2391 | chip->select_chip(mtd, chipnr); |
| 2392 | |
| 2393 | /* Check, if it is write protected */ |
| 2394 | if (nand_check_wp(mtd)) { |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2395 | ret = -EIO; |
| 2396 | goto err_out; |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2397 | } |
| 2398 | |
| 2399 | realpage = (int)(to >> chip->page_shift); |
| 2400 | page = realpage & chip->pagemask; |
| 2401 | blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1; |
| 2402 | |
| 2403 | /* Invalidate the page cache, when we write to the cached page */ |
Scott Wood | d396372 | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 2404 | if (to <= ((loff_t)chip->pagebuf << chip->page_shift) && |
| 2405 | ((loff_t)chip->pagebuf << chip->page_shift) < (to + ops->len)) |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2406 | chip->pagebuf = -1; |
| 2407 | |
Christian Hitz | 2a8e0fc | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 2408 | /* Don't allow multipage oob writes with offset */ |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2409 | if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) { |
| 2410 | ret = -EINVAL; |
| 2411 | goto err_out; |
| 2412 | } |
Christian Hitz | 2a8e0fc | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 2413 | |
Christian Hitz | 90e3f39 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 2414 | while (1) { |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2415 | int bytes = mtd->writesize; |
| 2416 | int cached = writelen > bytes && page != blockmask; |
| 2417 | uint8_t *wbuf = buf; |
Scott Wood | d396372 | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 2418 | int use_bufpoi; |
Hector Palacios | ebb7feb | 2016-07-18 09:37:41 +0200 | [diff] [blame] | 2419 | int part_pagewr = (column || writelen < mtd->writesize); |
Scott Wood | d396372 | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 2420 | |
| 2421 | if (part_pagewr) |
| 2422 | use_bufpoi = 1; |
| 2423 | else |
| 2424 | use_bufpoi = 0; |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2425 | |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2426 | WATCHDOG_RESET(); |
Scott Wood | d396372 | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 2427 | /* Partial page write?, or need to use bounce buffer */ |
| 2428 | if (use_bufpoi) { |
| 2429 | pr_debug("%s: using write bounce buffer for buf@%p\n", |
| 2430 | __func__, buf); |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2431 | cached = 0; |
Scott Wood | d396372 | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 2432 | if (part_pagewr) |
| 2433 | bytes = min_t(int, bytes - column, writelen); |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2434 | chip->pagebuf = -1; |
| 2435 | memset(chip->buffers->databuf, 0xff, mtd->writesize); |
| 2436 | memcpy(&chip->buffers->databuf[column], buf, bytes); |
| 2437 | wbuf = chip->buffers->databuf; |
| 2438 | } |
| 2439 | |
Christian Hitz | 2a8e0fc | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 2440 | if (unlikely(oob)) { |
| 2441 | size_t len = min(oobwritelen, oobmaxlen); |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2442 | oob = nand_fill_oob(mtd, oob, len, ops); |
Christian Hitz | 2a8e0fc | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 2443 | oobwritelen -= len; |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2444 | } else { |
| 2445 | /* We still need to erase leftover OOB data */ |
| 2446 | memset(chip->oob_poi, 0xff, mtd->oobsize); |
Christian Hitz | 2a8e0fc | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 2447 | } |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2448 | ret = chip->write_page(mtd, chip, column, bytes, wbuf, |
| 2449 | oob_required, page, cached, |
| 2450 | (ops->mode == MTD_OPS_RAW)); |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2451 | if (ret) |
| 2452 | break; |
| 2453 | |
| 2454 | writelen -= bytes; |
| 2455 | if (!writelen) |
| 2456 | break; |
| 2457 | |
| 2458 | column = 0; |
| 2459 | buf += bytes; |
| 2460 | realpage++; |
| 2461 | |
| 2462 | page = realpage & chip->pagemask; |
| 2463 | /* Check, if we cross a chip boundary */ |
| 2464 | if (!page) { |
| 2465 | chipnr++; |
| 2466 | chip->select_chip(mtd, -1); |
| 2467 | chip->select_chip(mtd, chipnr); |
| 2468 | } |
| 2469 | } |
| 2470 | |
| 2471 | ops->retlen = ops->len - writelen; |
| 2472 | if (unlikely(oob)) |
| 2473 | ops->oobretlen = ops->ooblen; |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2474 | |
| 2475 | err_out: |
| 2476 | chip->select_chip(mtd, -1); |
| 2477 | return ret; |
| 2478 | } |
| 2479 | |
| 2480 | /** |
| 2481 | * panic_nand_write - [MTD Interface] NAND write with ECC |
| 2482 | * @mtd: MTD device structure |
| 2483 | * @to: offset to write to |
| 2484 | * @len: number of bytes to write |
| 2485 | * @retlen: pointer to variable to store the number of written bytes |
| 2486 | * @buf: the data to write |
| 2487 | * |
| 2488 | * NAND write with ECC. Used when performing writes in interrupt context, this |
| 2489 | * may for example be called by mtdoops when writing an oops while in panic. |
| 2490 | */ |
| 2491 | static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len, |
| 2492 | size_t *retlen, const uint8_t *buf) |
| 2493 | { |
Scott Wood | 17cb4b8 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 2494 | struct nand_chip *chip = mtd_to_nand(mtd); |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2495 | struct mtd_oob_ops ops; |
| 2496 | int ret; |
| 2497 | |
| 2498 | /* Wait for the device to get ready */ |
| 2499 | panic_nand_wait(mtd, chip, 400); |
| 2500 | |
| 2501 | /* Grab the device */ |
| 2502 | panic_nand_get_device(chip, mtd, FL_WRITING); |
| 2503 | |
Scott Wood | d396372 | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 2504 | memset(&ops, 0, sizeof(ops)); |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2505 | ops.len = len; |
| 2506 | ops.datbuf = (uint8_t *)buf; |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2507 | ops.mode = MTD_OPS_PLACE_OOB; |
| 2508 | |
| 2509 | ret = nand_do_write_ops(mtd, to, &ops); |
| 2510 | |
| 2511 | *retlen = ops.retlen; |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2512 | return ret; |
| 2513 | } |
| 2514 | |
| 2515 | /** |
| 2516 | * nand_write - [MTD Interface] NAND write with ECC |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2517 | * @mtd: MTD device structure |
| 2518 | * @to: offset to write to |
| 2519 | * @len: number of bytes to write |
| 2520 | * @retlen: pointer to variable to store the number of written bytes |
| 2521 | * @buf: the data to write |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2522 | * |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2523 | * NAND write with ECC. |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2524 | */ |
| 2525 | static int nand_write(struct mtd_info *mtd, loff_t to, size_t len, |
| 2526 | size_t *retlen, const uint8_t *buf) |
| 2527 | { |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2528 | struct mtd_oob_ops ops; |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2529 | int ret; |
| 2530 | |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2531 | nand_get_device(mtd, FL_WRITING); |
Scott Wood | d396372 | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 2532 | memset(&ops, 0, sizeof(ops)); |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2533 | ops.len = len; |
| 2534 | ops.datbuf = (uint8_t *)buf; |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2535 | ops.mode = MTD_OPS_PLACE_OOB; |
| 2536 | ret = nand_do_write_ops(mtd, to, &ops); |
| 2537 | *retlen = ops.retlen; |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2538 | nand_release_device(mtd); |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2539 | return ret; |
| 2540 | } |
| 2541 | |
| 2542 | /** |
| 2543 | * nand_do_write_oob - [MTD Interface] NAND write out-of-band |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2544 | * @mtd: MTD device structure |
| 2545 | * @to: offset to write to |
| 2546 | * @ops: oob operation description structure |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2547 | * |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2548 | * NAND write out-of-band. |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2549 | */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2550 | static int nand_do_write_oob(struct mtd_info *mtd, loff_t to, |
| 2551 | struct mtd_oob_ops *ops) |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2552 | { |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2553 | int chipnr, page, status, len; |
Scott Wood | 17cb4b8 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 2554 | struct nand_chip *chip = mtd_to_nand(mtd); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2555 | |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2556 | pr_debug("%s: to = 0x%08x, len = %i\n", |
Christian Hitz | 90e3f39 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 2557 | __func__, (unsigned int)to, (int)ops->ooblen); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2558 | |
Scott Wood | ceee07b | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 2559 | len = mtd_oobavail(mtd, ops); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2560 | |
| 2561 | /* Do not allow write past end of page */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2562 | if ((ops->ooboffs + ops->ooblen) > len) { |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2563 | pr_debug("%s: attempt to write past end of page\n", |
| 2564 | __func__); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2565 | return -EINVAL; |
| 2566 | } |
| 2567 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2568 | if (unlikely(ops->ooboffs >= len)) { |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2569 | pr_debug("%s: attempt to start write outside oob\n", |
| 2570 | __func__); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2571 | return -EINVAL; |
| 2572 | } |
| 2573 | |
Christian Hitz | 90e3f39 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 2574 | /* Do not allow write past end of device */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2575 | if (unlikely(to >= mtd->size || |
| 2576 | ops->ooboffs + ops->ooblen > |
| 2577 | ((mtd->size >> chip->page_shift) - |
| 2578 | (to >> chip->page_shift)) * len)) { |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2579 | pr_debug("%s: attempt to write beyond end of device\n", |
| 2580 | __func__); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2581 | return -EINVAL; |
| 2582 | } |
| 2583 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2584 | chipnr = (int)(to >> chip->chip_shift); |
| 2585 | chip->select_chip(mtd, chipnr); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2586 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2587 | /* Shift to get page */ |
| 2588 | page = (int)(to >> chip->page_shift); |
| 2589 | |
| 2590 | /* |
| 2591 | * Reset the chip. Some chips (like the Toshiba TC5832DC found in one |
| 2592 | * of my DiskOnChip 2000 test units) will clear the whole data page too |
| 2593 | * if we don't do this. I have no clue why, but I seem to have 'fixed' |
| 2594 | * it in the doc2000 driver in August 1999. dwmw2. |
| 2595 | */ |
| 2596 | chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2597 | |
| 2598 | /* Check, if it is write protected */ |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2599 | if (nand_check_wp(mtd)) { |
| 2600 | chip->select_chip(mtd, -1); |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2601 | return -EROFS; |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2602 | } |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2603 | |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2604 | /* Invalidate the page cache, if we write to the cached page */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2605 | if (page == chip->pagebuf) |
| 2606 | chip->pagebuf = -1; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2607 | |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2608 | nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops); |
| 2609 | |
| 2610 | if (ops->mode == MTD_OPS_RAW) |
| 2611 | status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask); |
| 2612 | else |
| 2613 | status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2614 | |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2615 | chip->select_chip(mtd, -1); |
| 2616 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2617 | if (status) |
| 2618 | return status; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2619 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2620 | ops->oobretlen = ops->ooblen; |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 2621 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2622 | return 0; |
| 2623 | } |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2624 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2625 | /** |
| 2626 | * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2627 | * @mtd: MTD device structure |
| 2628 | * @to: offset to write to |
| 2629 | * @ops: oob operation description structure |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2630 | */ |
| 2631 | static int nand_write_oob(struct mtd_info *mtd, loff_t to, |
| 2632 | struct mtd_oob_ops *ops) |
| 2633 | { |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2634 | int ret = -ENOTSUPP; |
| 2635 | |
| 2636 | ops->retlen = 0; |
| 2637 | |
| 2638 | /* Do not allow writes past end of device */ |
| 2639 | if (ops->datbuf && (to + ops->len) > mtd->size) { |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2640 | pr_debug("%s: attempt to write beyond end of device\n", |
| 2641 | __func__); |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2642 | return -EINVAL; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2643 | } |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2644 | |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2645 | nand_get_device(mtd, FL_WRITING); |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2646 | |
Christian Hitz | 90e3f39 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 2647 | switch (ops->mode) { |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2648 | case MTD_OPS_PLACE_OOB: |
| 2649 | case MTD_OPS_AUTO_OOB: |
| 2650 | case MTD_OPS_RAW: |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2651 | break; |
| 2652 | |
| 2653 | default: |
| 2654 | goto out; |
| 2655 | } |
| 2656 | |
| 2657 | if (!ops->datbuf) |
| 2658 | ret = nand_do_write_oob(mtd, to, ops); |
| 2659 | else |
| 2660 | ret = nand_do_write_ops(mtd, to, ops); |
| 2661 | |
Christian Hitz | 90e3f39 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 2662 | out: |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2663 | nand_release_device(mtd); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2664 | return ret; |
| 2665 | } |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2666 | |
| 2667 | /** |
Scott Wood | d396372 | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 2668 | * single_erase - [GENERIC] NAND standard block erase command function |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2669 | * @mtd: MTD device structure |
| 2670 | * @page: the page address of the block which will be erased |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2671 | * |
Scott Wood | d396372 | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 2672 | * Standard erase command for NAND chips. Returns NAND status. |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2673 | */ |
Scott Wood | d396372 | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 2674 | static int single_erase(struct mtd_info *mtd, int page) |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2675 | { |
Scott Wood | 17cb4b8 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 2676 | struct nand_chip *chip = mtd_to_nand(mtd); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2677 | /* Send commands to erase a block */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2678 | chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page); |
| 2679 | chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1); |
Scott Wood | d396372 | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 2680 | |
| 2681 | return chip->waitfunc(mtd, chip); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2682 | } |
| 2683 | |
| 2684 | /** |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2685 | * nand_erase - [MTD Interface] erase block(s) |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2686 | * @mtd: MTD device structure |
| 2687 | * @instr: erase instruction |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2688 | * |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2689 | * Erase one ore more blocks. |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2690 | */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2691 | static int nand_erase(struct mtd_info *mtd, struct erase_info *instr) |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2692 | { |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2693 | return nand_erase_nand(mtd, instr, 0); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2694 | } |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 2695 | |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2696 | /** |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2697 | * nand_erase_nand - [INTERN] erase block(s) |
| 2698 | * @mtd: MTD device structure |
| 2699 | * @instr: erase instruction |
| 2700 | * @allowbbt: allow erasing the bbt area |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2701 | * |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2702 | * Erase one ore more blocks. |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2703 | */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2704 | int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr, |
| 2705 | int allowbbt) |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2706 | { |
Sandeep Paulraj | aaa8eec | 2009-10-30 13:51:23 -0400 | [diff] [blame] | 2707 | int page, status, pages_per_block, ret, chipnr; |
Scott Wood | 17cb4b8 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 2708 | struct nand_chip *chip = mtd_to_nand(mtd); |
Sandeep Paulraj | aaa8eec | 2009-10-30 13:51:23 -0400 | [diff] [blame] | 2709 | loff_t len; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2710 | |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2711 | pr_debug("%s: start = 0x%012llx, len = %llu\n", |
| 2712 | __func__, (unsigned long long)instr->addr, |
| 2713 | (unsigned long long)instr->len); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2714 | |
Christian Hitz | 2a8e0fc | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 2715 | if (check_offs_len(mtd, instr->addr, instr->len)) |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2716 | return -EINVAL; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2717 | |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2718 | /* Grab the lock and see if the device is available */ |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2719 | nand_get_device(mtd, FL_ERASING); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2720 | |
| 2721 | /* Shift to get first page */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2722 | page = (int)(instr->addr >> chip->page_shift); |
| 2723 | chipnr = (int)(instr->addr >> chip->chip_shift); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2724 | |
| 2725 | /* Calculate pages in each block */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2726 | pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift); |
William Juul | 4cbb651 | 2007-11-08 10:39:53 +0100 | [diff] [blame] | 2727 | |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2728 | /* Select the NAND device */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2729 | chip->select_chip(mtd, chipnr); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2730 | |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2731 | /* Check, if it is write protected */ |
| 2732 | if (nand_check_wp(mtd)) { |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2733 | pr_debug("%s: device is write protected!\n", |
| 2734 | __func__); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2735 | instr->state = MTD_ERASE_FAILED; |
| 2736 | goto erase_exit; |
| 2737 | } |
| 2738 | |
| 2739 | /* Loop through the pages */ |
| 2740 | len = instr->len; |
| 2741 | |
| 2742 | instr->state = MTD_ERASING; |
| 2743 | |
| 2744 | while (len) { |
Scott Wood | 6f2ffc3 | 2011-02-02 18:15:57 -0600 | [diff] [blame] | 2745 | WATCHDOG_RESET(); |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2746 | |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2747 | /* Check if we have a bad block, we do not erase bad blocks! */ |
Masahiro Yamada | 756963d | 2014-12-16 15:36:33 +0900 | [diff] [blame] | 2748 | if (!instr->scrub && nand_block_checkbad(mtd, ((loff_t) page) << |
Scott Wood | ceee07b | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 2749 | chip->page_shift, allowbbt)) { |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2750 | pr_warn("%s: attempt to erase a bad block at page 0x%08x\n", |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2751 | __func__, page); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2752 | instr->state = MTD_ERASE_FAILED; |
| 2753 | goto erase_exit; |
| 2754 | } |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2755 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2756 | /* |
| 2757 | * Invalidate the page cache, if we erase the block which |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2758 | * contains the current cached page. |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2759 | */ |
| 2760 | if (page <= chip->pagebuf && chip->pagebuf < |
| 2761 | (page + pages_per_block)) |
| 2762 | chip->pagebuf = -1; |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 2763 | |
Scott Wood | d396372 | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 2764 | status = chip->erase(mtd, page & chip->pagemask); |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2765 | |
| 2766 | /* |
| 2767 | * See if operation failed and additional status checks are |
| 2768 | * available |
| 2769 | */ |
| 2770 | if ((status & NAND_STATUS_FAIL) && (chip->errstat)) |
| 2771 | status = chip->errstat(mtd, chip, FL_ERASING, |
| 2772 | status, page); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2773 | |
| 2774 | /* See if block erase succeeded */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2775 | if (status & NAND_STATUS_FAIL) { |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2776 | pr_debug("%s: failed erase, page 0x%08x\n", |
| 2777 | __func__, page); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2778 | instr->state = MTD_ERASE_FAILED; |
Christian Hitz | 90e3f39 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 2779 | instr->fail_addr = |
| 2780 | ((loff_t)page << chip->page_shift); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2781 | goto erase_exit; |
| 2782 | } |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 2783 | |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2784 | /* Increment page address and decrement length */ |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2785 | len -= (1ULL << chip->phys_erase_shift); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2786 | page += pages_per_block; |
| 2787 | |
| 2788 | /* Check, if we cross a chip boundary */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2789 | if (len && !(page & chip->pagemask)) { |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2790 | chipnr++; |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2791 | chip->select_chip(mtd, -1); |
| 2792 | chip->select_chip(mtd, chipnr); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2793 | } |
| 2794 | } |
| 2795 | instr->state = MTD_ERASE_DONE; |
| 2796 | |
Christian Hitz | 90e3f39 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 2797 | erase_exit: |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2798 | |
| 2799 | ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2800 | |
| 2801 | /* Deselect and wake up anyone waiting on the device */ |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2802 | chip->select_chip(mtd, -1); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2803 | nand_release_device(mtd); |
| 2804 | |
Scott Wood | c45912d | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 2805 | /* Do call back function */ |
| 2806 | if (!ret) |
| 2807 | mtd_erase_callback(instr); |
| 2808 | |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2809 | /* Return more or less happy */ |
| 2810 | return ret; |
| 2811 | } |
| 2812 | |
| 2813 | /** |
| 2814 | * nand_sync - [MTD Interface] sync |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2815 | * @mtd: MTD device structure |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2816 | * |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2817 | * Sync is actually a wait for chip ready function. |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2818 | */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2819 | static void nand_sync(struct mtd_info *mtd) |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2820 | { |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2821 | pr_debug("%s: called\n", __func__); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2822 | |
| 2823 | /* Grab the lock and see if the device is available */ |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2824 | nand_get_device(mtd, FL_SYNCING); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2825 | /* Release it and go back */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2826 | nand_release_device(mtd); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2827 | } |
| 2828 | |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2829 | /** |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2830 | * nand_block_isbad - [MTD Interface] Check if block at offset is bad |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2831 | * @mtd: MTD device structure |
| 2832 | * @offs: offset relative to mtd start |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2833 | */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2834 | static int nand_block_isbad(struct mtd_info *mtd, loff_t offs) |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2835 | { |
Scott Wood | ceee07b | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 2836 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 2837 | int chipnr = (int)(offs >> chip->chip_shift); |
| 2838 | int ret; |
| 2839 | |
| 2840 | /* Select the NAND device */ |
| 2841 | nand_get_device(mtd, FL_READING); |
| 2842 | chip->select_chip(mtd, chipnr); |
| 2843 | |
| 2844 | ret = nand_block_checkbad(mtd, offs, 0); |
| 2845 | |
| 2846 | chip->select_chip(mtd, -1); |
| 2847 | nand_release_device(mtd); |
| 2848 | |
| 2849 | return ret; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2850 | } |
| 2851 | |
| 2852 | /** |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2853 | * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2854 | * @mtd: MTD device structure |
| 2855 | * @ofs: offset relative to mtd start |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2856 | */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2857 | static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs) |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2858 | { |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2859 | int ret; |
| 2860 | |
Christian Hitz | 2a8e0fc | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 2861 | ret = nand_block_isbad(mtd, ofs); |
| 2862 | if (ret) { |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2863 | /* If it was bad already, return success and do nothing */ |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2864 | if (ret > 0) |
| 2865 | return 0; |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 2866 | return ret; |
| 2867 | } |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2868 | |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2869 | return nand_block_markbad_lowlevel(mtd, ofs); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2870 | } |
| 2871 | |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2872 | /** |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2873 | * nand_onfi_set_features- [REPLACEABLE] set features for ONFI nand |
| 2874 | * @mtd: MTD device structure |
| 2875 | * @chip: nand chip info structure |
| 2876 | * @addr: feature address. |
| 2877 | * @subfeature_param: the subfeature parameters, a four bytes array. |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2878 | */ |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2879 | static int nand_onfi_set_features(struct mtd_info *mtd, struct nand_chip *chip, |
| 2880 | int addr, uint8_t *subfeature_param) |
| 2881 | { |
| 2882 | int status; |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2883 | int i; |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2884 | |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2885 | #ifdef CONFIG_SYS_NAND_ONFI_DETECTION |
| 2886 | if (!chip->onfi_version || |
| 2887 | !(le16_to_cpu(chip->onfi_params.opt_cmd) |
| 2888 | & ONFI_OPT_CMD_SET_GET_FEATURES)) |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2889 | return -EINVAL; |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2890 | #endif |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2891 | |
| 2892 | chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, addr, -1); |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2893 | for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i) |
| 2894 | chip->write_byte(mtd, subfeature_param[i]); |
| 2895 | |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2896 | status = chip->waitfunc(mtd, chip); |
| 2897 | if (status & NAND_STATUS_FAIL) |
| 2898 | return -EIO; |
| 2899 | return 0; |
| 2900 | } |
| 2901 | |
| 2902 | /** |
| 2903 | * nand_onfi_get_features- [REPLACEABLE] get features for ONFI nand |
| 2904 | * @mtd: MTD device structure |
| 2905 | * @chip: nand chip info structure |
| 2906 | * @addr: feature address. |
| 2907 | * @subfeature_param: the subfeature parameters, a four bytes array. |
| 2908 | */ |
| 2909 | static int nand_onfi_get_features(struct mtd_info *mtd, struct nand_chip *chip, |
| 2910 | int addr, uint8_t *subfeature_param) |
| 2911 | { |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2912 | int i; |
| 2913 | |
| 2914 | #ifdef CONFIG_SYS_NAND_ONFI_DETECTION |
| 2915 | if (!chip->onfi_version || |
| 2916 | !(le16_to_cpu(chip->onfi_params.opt_cmd) |
| 2917 | & ONFI_OPT_CMD_SET_GET_FEATURES)) |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2918 | return -EINVAL; |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2919 | #endif |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2920 | |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2921 | chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, addr, -1); |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2922 | for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i) |
| 2923 | *subfeature_param++ = chip->read_byte(mtd); |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2924 | return 0; |
| 2925 | } |
| 2926 | |
| 2927 | /* Set default functions */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2928 | static void nand_set_defaults(struct nand_chip *chip, int busw) |
| 2929 | { |
| 2930 | /* check for proper chip_delay setup, set 20us if not */ |
| 2931 | if (!chip->chip_delay) |
| 2932 | chip->chip_delay = 20; |
| 2933 | |
| 2934 | /* check, if a user supplied command function given */ |
| 2935 | if (chip->cmdfunc == NULL) |
| 2936 | chip->cmdfunc = nand_command; |
| 2937 | |
| 2938 | /* check, if a user supplied wait function given */ |
| 2939 | if (chip->waitfunc == NULL) |
| 2940 | chip->waitfunc = nand_wait; |
| 2941 | |
| 2942 | if (!chip->select_chip) |
| 2943 | chip->select_chip = nand_select_chip; |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2944 | |
| 2945 | /* set for ONFI nand */ |
| 2946 | if (!chip->onfi_set_features) |
| 2947 | chip->onfi_set_features = nand_onfi_set_features; |
| 2948 | if (!chip->onfi_get_features) |
| 2949 | chip->onfi_get_features = nand_onfi_get_features; |
| 2950 | |
| 2951 | /* If called twice, pointers that depend on busw may need to be reset */ |
| 2952 | if (!chip->read_byte || chip->read_byte == nand_read_byte) |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2953 | chip->read_byte = busw ? nand_read_byte16 : nand_read_byte; |
| 2954 | if (!chip->read_word) |
| 2955 | chip->read_word = nand_read_word; |
| 2956 | if (!chip->block_bad) |
| 2957 | chip->block_bad = nand_block_bad; |
| 2958 | if (!chip->block_markbad) |
| 2959 | chip->block_markbad = nand_default_block_markbad; |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2960 | if (!chip->write_buf || chip->write_buf == nand_write_buf) |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2961 | chip->write_buf = busw ? nand_write_buf16 : nand_write_buf; |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2962 | if (!chip->write_byte || chip->write_byte == nand_write_byte) |
| 2963 | chip->write_byte = busw ? nand_write_byte16 : nand_write_byte; |
| 2964 | if (!chip->read_buf || chip->read_buf == nand_read_buf) |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2965 | chip->read_buf = busw ? nand_read_buf16 : nand_read_buf; |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2966 | if (!chip->scan_bbt) |
| 2967 | chip->scan_bbt = nand_default_bbt; |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2968 | |
| 2969 | if (!chip->controller) { |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2970 | chip->controller = &chip->hwcontrol; |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2971 | spin_lock_init(&chip->controller->lock); |
| 2972 | init_waitqueue_head(&chip->controller->wq); |
| 2973 | } |
| 2974 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2975 | } |
| 2976 | |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2977 | /* Sanitize ONFI strings so we can safely print them */ |
Christian Hitz | 5454ddb | 2011-10-12 09:32:05 +0200 | [diff] [blame] | 2978 | static void sanitize_string(char *s, size_t len) |
| 2979 | { |
| 2980 | ssize_t i; |
| 2981 | |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2982 | /* Null terminate */ |
Christian Hitz | 5454ddb | 2011-10-12 09:32:05 +0200 | [diff] [blame] | 2983 | s[len - 1] = 0; |
| 2984 | |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2985 | /* Remove non printable chars */ |
Christian Hitz | 5454ddb | 2011-10-12 09:32:05 +0200 | [diff] [blame] | 2986 | for (i = 0; i < len - 1; i++) { |
| 2987 | if (s[i] < ' ' || s[i] > 127) |
| 2988 | s[i] = '?'; |
| 2989 | } |
| 2990 | |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2991 | /* Remove trailing spaces */ |
Christian Hitz | 5454ddb | 2011-10-12 09:32:05 +0200 | [diff] [blame] | 2992 | strim(s); |
| 2993 | } |
| 2994 | |
Florian Fainelli | 0272c71 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 2995 | static u16 onfi_crc16(u16 crc, u8 const *p, size_t len) |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2996 | { |
Florian Fainelli | 0272c71 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 2997 | int i; |
Florian Fainelli | 0272c71 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 2998 | while (len--) { |
| 2999 | crc ^= *p++ << 8; |
| 3000 | for (i = 0; i < 8; i++) |
| 3001 | crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0); |
Scott Wood | c45912d | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 3002 | } |
| 3003 | |
Florian Fainelli | 0272c71 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3004 | return crc; |
| 3005 | } |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3006 | |
Heiko Schocher | 4e67c57 | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 3007 | #ifdef CONFIG_SYS_NAND_ONFI_DETECTION |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3008 | /* Parse the Extended Parameter Page. */ |
| 3009 | static int nand_flash_detect_ext_param_page(struct mtd_info *mtd, |
| 3010 | struct nand_chip *chip, struct nand_onfi_params *p) |
| 3011 | { |
| 3012 | struct onfi_ext_param_page *ep; |
| 3013 | struct onfi_ext_section *s; |
| 3014 | struct onfi_ext_ecc_info *ecc; |
| 3015 | uint8_t *cursor; |
| 3016 | int ret = -EINVAL; |
| 3017 | int len; |
| 3018 | int i; |
| 3019 | |
| 3020 | len = le16_to_cpu(p->ext_param_page_length) * 16; |
| 3021 | ep = kmalloc(len, GFP_KERNEL); |
| 3022 | if (!ep) |
| 3023 | return -ENOMEM; |
| 3024 | |
| 3025 | /* Send our own NAND_CMD_PARAM. */ |
| 3026 | chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1); |
| 3027 | |
| 3028 | /* Use the Change Read Column command to skip the ONFI param pages. */ |
| 3029 | chip->cmdfunc(mtd, NAND_CMD_RNDOUT, |
| 3030 | sizeof(*p) * p->num_of_param_pages , -1); |
| 3031 | |
| 3032 | /* Read out the Extended Parameter Page. */ |
| 3033 | chip->read_buf(mtd, (uint8_t *)ep, len); |
| 3034 | if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2) |
| 3035 | != le16_to_cpu(ep->crc))) { |
| 3036 | pr_debug("fail in the CRC.\n"); |
| 3037 | goto ext_out; |
| 3038 | } |
| 3039 | |
| 3040 | /* |
| 3041 | * Check the signature. |
| 3042 | * Do not strictly follow the ONFI spec, maybe changed in future. |
| 3043 | */ |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3044 | if (strncmp((char *)ep->sig, "EPPS", 4)) { |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3045 | pr_debug("The signature is invalid.\n"); |
| 3046 | goto ext_out; |
| 3047 | } |
| 3048 | |
| 3049 | /* find the ECC section. */ |
| 3050 | cursor = (uint8_t *)(ep + 1); |
| 3051 | for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) { |
| 3052 | s = ep->sections + i; |
| 3053 | if (s->type == ONFI_SECTION_TYPE_2) |
| 3054 | break; |
| 3055 | cursor += s->length * 16; |
| 3056 | } |
| 3057 | if (i == ONFI_EXT_SECTION_MAX) { |
| 3058 | pr_debug("We can not find the ECC section.\n"); |
| 3059 | goto ext_out; |
| 3060 | } |
| 3061 | |
| 3062 | /* get the info we want. */ |
| 3063 | ecc = (struct onfi_ext_ecc_info *)cursor; |
| 3064 | |
| 3065 | if (!ecc->codeword_size) { |
| 3066 | pr_debug("Invalid codeword size\n"); |
| 3067 | goto ext_out; |
| 3068 | } |
| 3069 | |
| 3070 | chip->ecc_strength_ds = ecc->ecc_bits; |
| 3071 | chip->ecc_step_ds = 1 << ecc->codeword_size; |
| 3072 | ret = 0; |
| 3073 | |
| 3074 | ext_out: |
| 3075 | kfree(ep); |
| 3076 | return ret; |
| 3077 | } |
| 3078 | |
| 3079 | static int nand_setup_read_retry_micron(struct mtd_info *mtd, int retry_mode) |
| 3080 | { |
Scott Wood | 17cb4b8 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 3081 | struct nand_chip *chip = mtd_to_nand(mtd); |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3082 | uint8_t feature[ONFI_SUBFEATURE_PARAM_LEN] = {retry_mode}; |
| 3083 | |
| 3084 | return chip->onfi_set_features(mtd, chip, ONFI_FEATURE_ADDR_READ_RETRY, |
| 3085 | feature); |
| 3086 | } |
| 3087 | |
| 3088 | /* |
| 3089 | * Configure chip properties from Micron vendor-specific ONFI table |
| 3090 | */ |
| 3091 | static void nand_onfi_detect_micron(struct nand_chip *chip, |
| 3092 | struct nand_onfi_params *p) |
| 3093 | { |
| 3094 | struct nand_onfi_vendor_micron *micron = (void *)p->vendor; |
| 3095 | |
| 3096 | if (le16_to_cpu(p->vendor_revision) < 1) |
| 3097 | return; |
| 3098 | |
| 3099 | chip->read_retries = micron->read_retry_options; |
| 3100 | chip->setup_read_retry = nand_setup_read_retry_micron; |
| 3101 | } |
| 3102 | |
Florian Fainelli | 0272c71 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3103 | /* |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3104 | * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise. |
Florian Fainelli | 0272c71 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3105 | */ |
Christian Hitz | 90e3f39 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 3106 | static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip, |
Florian Fainelli | 0272c71 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3107 | int *busw) |
| 3108 | { |
| 3109 | struct nand_onfi_params *p = &chip->onfi_params; |
Brian Norris | b9ae609 | 2014-05-06 00:46:16 +0530 | [diff] [blame] | 3110 | int i, j; |
Florian Fainelli | 0272c71 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3111 | int val; |
| 3112 | |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3113 | /* Try ONFI for unknown chip or LP */ |
Florian Fainelli | 0272c71 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3114 | chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1); |
| 3115 | if (chip->read_byte(mtd) != 'O' || chip->read_byte(mtd) != 'N' || |
| 3116 | chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I') |
| 3117 | return 0; |
| 3118 | |
Florian Fainelli | 0272c71 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3119 | chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1); |
| 3120 | for (i = 0; i < 3; i++) { |
Brian Norris | b9ae609 | 2014-05-06 00:46:16 +0530 | [diff] [blame] | 3121 | for (j = 0; j < sizeof(*p); j++) |
| 3122 | ((uint8_t *)p)[j] = chip->read_byte(mtd); |
Florian Fainelli | 0272c71 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3123 | if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) == |
Christian Hitz | 90e3f39 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 3124 | le16_to_cpu(p->crc)) { |
Wolfgang Denk | d1a24f0 | 2011-02-02 22:36:10 +0100 | [diff] [blame] | 3125 | break; |
Florian Fainelli | 0272c71 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3126 | } |
Florian Fainelli | 3e9b349 | 2010-06-12 20:59:25 +0200 | [diff] [blame] | 3127 | } |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3128 | |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3129 | if (i == 3) { |
| 3130 | pr_err("Could not find valid ONFI parameter page; aborting\n"); |
Florian Fainelli | 0272c71 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3131 | return 0; |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3132 | } |
Florian Fainelli | 0272c71 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3133 | |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3134 | /* Check version */ |
Florian Fainelli | 0272c71 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3135 | val = le16_to_cpu(p->revision); |
Florian Fainelli | aad99bb | 2011-04-03 18:23:56 +0200 | [diff] [blame] | 3136 | if (val & (1 << 5)) |
| 3137 | chip->onfi_version = 23; |
| 3138 | else if (val & (1 << 4)) |
Florian Fainelli | 0272c71 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3139 | chip->onfi_version = 22; |
| 3140 | else if (val & (1 << 3)) |
| 3141 | chip->onfi_version = 21; |
| 3142 | else if (val & (1 << 2)) |
| 3143 | chip->onfi_version = 20; |
Florian Fainelli | aad99bb | 2011-04-03 18:23:56 +0200 | [diff] [blame] | 3144 | else if (val & (1 << 1)) |
Florian Fainelli | 0272c71 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3145 | chip->onfi_version = 10; |
Florian Fainelli | aad99bb | 2011-04-03 18:23:56 +0200 | [diff] [blame] | 3146 | |
| 3147 | if (!chip->onfi_version) { |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3148 | pr_info("unsupported ONFI version: %d\n", val); |
Florian Fainelli | aad99bb | 2011-04-03 18:23:56 +0200 | [diff] [blame] | 3149 | return 0; |
| 3150 | } |
Florian Fainelli | 0272c71 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3151 | |
Christian Hitz | 5454ddb | 2011-10-12 09:32:05 +0200 | [diff] [blame] | 3152 | sanitize_string(p->manufacturer, sizeof(p->manufacturer)); |
| 3153 | sanitize_string(p->model, sizeof(p->model)); |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3154 | if (!mtd->name) |
Florian Fainelli | 0272c71 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3155 | mtd->name = p->model; |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3156 | |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3157 | mtd->writesize = le32_to_cpu(p->byte_per_page); |
| 3158 | |
| 3159 | /* |
| 3160 | * pages_per_block and blocks_per_lun may not be a power-of-2 size |
| 3161 | * (don't ask me who thought of this...). MTD assumes that these |
| 3162 | * dimensions will be power-of-2, so just truncate the remaining area. |
| 3163 | */ |
| 3164 | mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1); |
| 3165 | mtd->erasesize *= mtd->writesize; |
| 3166 | |
| 3167 | mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page); |
| 3168 | |
| 3169 | /* See erasesize comment */ |
| 3170 | chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1); |
| 3171 | chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count; |
| 3172 | chip->bits_per_cell = p->bits_per_cell; |
| 3173 | |
| 3174 | if (onfi_feature(chip) & ONFI_FEATURE_16_BIT_BUS) |
| 3175 | *busw = NAND_BUSWIDTH_16; |
| 3176 | else |
| 3177 | *busw = 0; |
| 3178 | |
| 3179 | if (p->ecc_bits != 0xff) { |
| 3180 | chip->ecc_strength_ds = p->ecc_bits; |
| 3181 | chip->ecc_step_ds = 512; |
| 3182 | } else if (chip->onfi_version >= 21 && |
| 3183 | (onfi_feature(chip) & ONFI_FEATURE_EXT_PARAM_PAGE)) { |
| 3184 | |
| 3185 | /* |
| 3186 | * The nand_flash_detect_ext_param_page() uses the |
| 3187 | * Change Read Column command which maybe not supported |
| 3188 | * by the chip->cmdfunc. So try to update the chip->cmdfunc |
| 3189 | * now. We do not replace user supplied command function. |
| 3190 | */ |
| 3191 | if (mtd->writesize > 512 && chip->cmdfunc == nand_command) |
| 3192 | chip->cmdfunc = nand_command_lp; |
| 3193 | |
| 3194 | /* The Extended Parameter Page is supported since ONFI 2.1. */ |
| 3195 | if (nand_flash_detect_ext_param_page(mtd, chip, p)) |
| 3196 | pr_warn("Failed to detect ONFI extended param page\n"); |
| 3197 | } else { |
| 3198 | pr_warn("Could not retrieve ONFI ECC requirements\n"); |
| 3199 | } |
| 3200 | |
| 3201 | if (p->jedec_id == NAND_MFR_MICRON) |
| 3202 | nand_onfi_detect_micron(chip, p); |
| 3203 | |
Florian Fainelli | 0272c71 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3204 | return 1; |
| 3205 | } |
| 3206 | #else |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3207 | static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip, |
Florian Fainelli | 0272c71 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3208 | int *busw) |
| 3209 | { |
| 3210 | return 0; |
| 3211 | } |
| 3212 | #endif |
| 3213 | |
Florian Fainelli | 0272c71 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3214 | /* |
Heiko Schocher | 4e67c57 | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 3215 | * Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise. |
| 3216 | */ |
| 3217 | static int nand_flash_detect_jedec(struct mtd_info *mtd, struct nand_chip *chip, |
| 3218 | int *busw) |
| 3219 | { |
| 3220 | struct nand_jedec_params *p = &chip->jedec_params; |
| 3221 | struct jedec_ecc_info *ecc; |
| 3222 | int val; |
| 3223 | int i, j; |
| 3224 | |
| 3225 | /* Try JEDEC for unknown chip or LP */ |
| 3226 | chip->cmdfunc(mtd, NAND_CMD_READID, 0x40, -1); |
| 3227 | if (chip->read_byte(mtd) != 'J' || chip->read_byte(mtd) != 'E' || |
| 3228 | chip->read_byte(mtd) != 'D' || chip->read_byte(mtd) != 'E' || |
| 3229 | chip->read_byte(mtd) != 'C') |
| 3230 | return 0; |
| 3231 | |
| 3232 | chip->cmdfunc(mtd, NAND_CMD_PARAM, 0x40, -1); |
| 3233 | for (i = 0; i < 3; i++) { |
| 3234 | for (j = 0; j < sizeof(*p); j++) |
| 3235 | ((uint8_t *)p)[j] = chip->read_byte(mtd); |
| 3236 | |
| 3237 | if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 510) == |
| 3238 | le16_to_cpu(p->crc)) |
| 3239 | break; |
| 3240 | } |
| 3241 | |
| 3242 | if (i == 3) { |
| 3243 | pr_err("Could not find valid JEDEC parameter page; aborting\n"); |
| 3244 | return 0; |
| 3245 | } |
| 3246 | |
| 3247 | /* Check version */ |
| 3248 | val = le16_to_cpu(p->revision); |
| 3249 | if (val & (1 << 2)) |
| 3250 | chip->jedec_version = 10; |
| 3251 | else if (val & (1 << 1)) |
| 3252 | chip->jedec_version = 1; /* vendor specific version */ |
| 3253 | |
| 3254 | if (!chip->jedec_version) { |
| 3255 | pr_info("unsupported JEDEC version: %d\n", val); |
| 3256 | return 0; |
| 3257 | } |
| 3258 | |
| 3259 | sanitize_string(p->manufacturer, sizeof(p->manufacturer)); |
| 3260 | sanitize_string(p->model, sizeof(p->model)); |
| 3261 | if (!mtd->name) |
| 3262 | mtd->name = p->model; |
| 3263 | |
| 3264 | mtd->writesize = le32_to_cpu(p->byte_per_page); |
| 3265 | |
| 3266 | /* Please reference to the comment for nand_flash_detect_onfi. */ |
| 3267 | mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1); |
| 3268 | mtd->erasesize *= mtd->writesize; |
| 3269 | |
| 3270 | mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page); |
| 3271 | |
| 3272 | /* Please reference to the comment for nand_flash_detect_onfi. */ |
| 3273 | chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1); |
| 3274 | chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count; |
| 3275 | chip->bits_per_cell = p->bits_per_cell; |
| 3276 | |
| 3277 | if (jedec_feature(chip) & JEDEC_FEATURE_16_BIT_BUS) |
| 3278 | *busw = NAND_BUSWIDTH_16; |
| 3279 | else |
| 3280 | *busw = 0; |
| 3281 | |
| 3282 | /* ECC info */ |
| 3283 | ecc = &p->ecc_info[0]; |
| 3284 | |
| 3285 | if (ecc->codeword_size >= 9) { |
| 3286 | chip->ecc_strength_ds = ecc->ecc_bits; |
| 3287 | chip->ecc_step_ds = 1 << ecc->codeword_size; |
| 3288 | } else { |
| 3289 | pr_warn("Invalid codeword size\n"); |
| 3290 | } |
| 3291 | |
| 3292 | return 1; |
| 3293 | } |
| 3294 | |
| 3295 | /* |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3296 | * nand_id_has_period - Check if an ID string has a given wraparound period |
| 3297 | * @id_data: the ID string |
| 3298 | * @arrlen: the length of the @id_data array |
| 3299 | * @period: the period of repitition |
| 3300 | * |
| 3301 | * Check if an ID string is repeated within a given sequence of bytes at |
| 3302 | * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3303 | * period of 3). This is a helper function for nand_id_len(). Returns non-zero |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3304 | * if the repetition has a period of @period; otherwise, returns zero. |
| 3305 | */ |
| 3306 | static int nand_id_has_period(u8 *id_data, int arrlen, int period) |
| 3307 | { |
| 3308 | int i, j; |
| 3309 | for (i = 0; i < period; i++) |
| 3310 | for (j = i + period; j < arrlen; j += period) |
| 3311 | if (id_data[i] != id_data[j]) |
| 3312 | return 0; |
| 3313 | return 1; |
| 3314 | } |
| 3315 | |
| 3316 | /* |
| 3317 | * nand_id_len - Get the length of an ID string returned by CMD_READID |
| 3318 | * @id_data: the ID string |
| 3319 | * @arrlen: the length of the @id_data array |
| 3320 | |
| 3321 | * Returns the length of the ID string, according to known wraparound/trailing |
| 3322 | * zero patterns. If no pattern exists, returns the length of the array. |
| 3323 | */ |
| 3324 | static int nand_id_len(u8 *id_data, int arrlen) |
| 3325 | { |
| 3326 | int last_nonzero, period; |
| 3327 | |
| 3328 | /* Find last non-zero byte */ |
| 3329 | for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--) |
| 3330 | if (id_data[last_nonzero]) |
| 3331 | break; |
| 3332 | |
| 3333 | /* All zeros */ |
| 3334 | if (last_nonzero < 0) |
| 3335 | return 0; |
| 3336 | |
| 3337 | /* Calculate wraparound period */ |
| 3338 | for (period = 1; period < arrlen; period++) |
| 3339 | if (nand_id_has_period(id_data, arrlen, period)) |
| 3340 | break; |
| 3341 | |
| 3342 | /* There's a repeated pattern */ |
| 3343 | if (period < arrlen) |
| 3344 | return period; |
| 3345 | |
| 3346 | /* There are trailing zeros */ |
| 3347 | if (last_nonzero < arrlen - 1) |
| 3348 | return last_nonzero + 1; |
| 3349 | |
| 3350 | /* No pattern detected */ |
| 3351 | return arrlen; |
| 3352 | } |
| 3353 | |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3354 | /* Extract the bits of per cell from the 3rd byte of the extended ID */ |
| 3355 | static int nand_get_bits_per_cell(u8 cellinfo) |
| 3356 | { |
| 3357 | int bits; |
| 3358 | |
| 3359 | bits = cellinfo & NAND_CI_CELLTYPE_MSK; |
| 3360 | bits >>= NAND_CI_CELLTYPE_SHIFT; |
| 3361 | return bits + 1; |
| 3362 | } |
| 3363 | |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3364 | /* |
| 3365 | * Many new NAND share similar device ID codes, which represent the size of the |
| 3366 | * chip. The rest of the parameters must be decoded according to generic or |
| 3367 | * manufacturer-specific "extended ID" decoding patterns. |
| 3368 | */ |
| 3369 | static void nand_decode_ext_id(struct mtd_info *mtd, struct nand_chip *chip, |
| 3370 | u8 id_data[8], int *busw) |
| 3371 | { |
| 3372 | int extid, id_len; |
| 3373 | /* The 3rd id byte holds MLC / multichip data */ |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3374 | chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]); |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3375 | /* The 4th id byte is the important one */ |
| 3376 | extid = id_data[3]; |
| 3377 | |
| 3378 | id_len = nand_id_len(id_data, 8); |
| 3379 | |
| 3380 | /* |
| 3381 | * Field definitions are in the following datasheets: |
| 3382 | * Old style (4,5 byte ID): Samsung K9GAG08U0M (p.32) |
| 3383 | * New Samsung (6 byte ID): Samsung K9GAG08U0F (p.44) |
| 3384 | * Hynix MLC (6 byte ID): Hynix H27UBG8T2B (p.22) |
| 3385 | * |
| 3386 | * Check for ID length, non-zero 6th byte, cell type, and Hynix/Samsung |
| 3387 | * ID to decide what to do. |
| 3388 | */ |
| 3389 | if (id_len == 6 && id_data[0] == NAND_MFR_SAMSUNG && |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3390 | !nand_is_slc(chip) && id_data[5] != 0x00) { |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3391 | /* Calc pagesize */ |
| 3392 | mtd->writesize = 2048 << (extid & 0x03); |
| 3393 | extid >>= 2; |
| 3394 | /* Calc oobsize */ |
| 3395 | switch (((extid >> 2) & 0x04) | (extid & 0x03)) { |
| 3396 | case 1: |
| 3397 | mtd->oobsize = 128; |
| 3398 | break; |
| 3399 | case 2: |
| 3400 | mtd->oobsize = 218; |
| 3401 | break; |
| 3402 | case 3: |
| 3403 | mtd->oobsize = 400; |
| 3404 | break; |
| 3405 | case 4: |
| 3406 | mtd->oobsize = 436; |
| 3407 | break; |
| 3408 | case 5: |
| 3409 | mtd->oobsize = 512; |
| 3410 | break; |
| 3411 | case 6: |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3412 | mtd->oobsize = 640; |
| 3413 | break; |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3414 | case 7: |
| 3415 | default: /* Other cases are "reserved" (unknown) */ |
| 3416 | mtd->oobsize = 1024; |
| 3417 | break; |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3418 | } |
| 3419 | extid >>= 2; |
| 3420 | /* Calc blocksize */ |
| 3421 | mtd->erasesize = (128 * 1024) << |
| 3422 | (((extid >> 1) & 0x04) | (extid & 0x03)); |
| 3423 | *busw = 0; |
| 3424 | } else if (id_len == 6 && id_data[0] == NAND_MFR_HYNIX && |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3425 | !nand_is_slc(chip)) { |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3426 | unsigned int tmp; |
| 3427 | |
| 3428 | /* Calc pagesize */ |
| 3429 | mtd->writesize = 2048 << (extid & 0x03); |
| 3430 | extid >>= 2; |
| 3431 | /* Calc oobsize */ |
| 3432 | switch (((extid >> 2) & 0x04) | (extid & 0x03)) { |
| 3433 | case 0: |
| 3434 | mtd->oobsize = 128; |
| 3435 | break; |
| 3436 | case 1: |
| 3437 | mtd->oobsize = 224; |
| 3438 | break; |
| 3439 | case 2: |
| 3440 | mtd->oobsize = 448; |
| 3441 | break; |
| 3442 | case 3: |
| 3443 | mtd->oobsize = 64; |
| 3444 | break; |
| 3445 | case 4: |
| 3446 | mtd->oobsize = 32; |
| 3447 | break; |
| 3448 | case 5: |
| 3449 | mtd->oobsize = 16; |
| 3450 | break; |
| 3451 | default: |
| 3452 | mtd->oobsize = 640; |
| 3453 | break; |
| 3454 | } |
| 3455 | extid >>= 2; |
| 3456 | /* Calc blocksize */ |
| 3457 | tmp = ((extid >> 1) & 0x04) | (extid & 0x03); |
| 3458 | if (tmp < 0x03) |
| 3459 | mtd->erasesize = (128 * 1024) << tmp; |
| 3460 | else if (tmp == 0x03) |
| 3461 | mtd->erasesize = 768 * 1024; |
| 3462 | else |
| 3463 | mtd->erasesize = (64 * 1024) << tmp; |
| 3464 | *busw = 0; |
| 3465 | } else { |
| 3466 | /* Calc pagesize */ |
| 3467 | mtd->writesize = 1024 << (extid & 0x03); |
| 3468 | extid >>= 2; |
| 3469 | /* Calc oobsize */ |
| 3470 | mtd->oobsize = (8 << (extid & 0x01)) * |
| 3471 | (mtd->writesize >> 9); |
| 3472 | extid >>= 2; |
| 3473 | /* Calc blocksize. Blocksize is multiples of 64KiB */ |
| 3474 | mtd->erasesize = (64 * 1024) << (extid & 0x03); |
| 3475 | extid >>= 2; |
| 3476 | /* Get buswidth information */ |
| 3477 | *busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0; |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3478 | |
| 3479 | /* |
| 3480 | * Toshiba 24nm raw SLC (i.e., not BENAND) have 32B OOB per |
| 3481 | * 512B page. For Toshiba SLC, we decode the 5th/6th byte as |
| 3482 | * follows: |
| 3483 | * - ID byte 6, bits[2:0]: 100b -> 43nm, 101b -> 32nm, |
| 3484 | * 110b -> 24nm |
| 3485 | * - ID byte 5, bit[7]: 1 -> BENAND, 0 -> raw SLC |
| 3486 | */ |
| 3487 | if (id_len >= 6 && id_data[0] == NAND_MFR_TOSHIBA && |
| 3488 | nand_is_slc(chip) && |
| 3489 | (id_data[5] & 0x7) == 0x6 /* 24nm */ && |
| 3490 | !(id_data[4] & 0x80) /* !BENAND */) { |
| 3491 | mtd->oobsize = 32 * mtd->writesize >> 9; |
| 3492 | } |
| 3493 | |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3494 | } |
| 3495 | } |
| 3496 | |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3497 | /* |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3498 | * Old devices have chip data hardcoded in the device ID table. nand_decode_id |
| 3499 | * decodes a matching ID table entry and assigns the MTD size parameters for |
| 3500 | * the chip. |
| 3501 | */ |
| 3502 | static void nand_decode_id(struct mtd_info *mtd, struct nand_chip *chip, |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3503 | struct nand_flash_dev *type, u8 id_data[8], |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3504 | int *busw) |
| 3505 | { |
| 3506 | int maf_id = id_data[0]; |
| 3507 | |
| 3508 | mtd->erasesize = type->erasesize; |
| 3509 | mtd->writesize = type->pagesize; |
| 3510 | mtd->oobsize = mtd->writesize / 32; |
| 3511 | *busw = type->options & NAND_BUSWIDTH_16; |
| 3512 | |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3513 | /* All legacy ID NAND are small-page, SLC */ |
| 3514 | chip->bits_per_cell = 1; |
| 3515 | |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3516 | /* |
| 3517 | * Check for Spansion/AMD ID + repeating 5th, 6th byte since |
| 3518 | * some Spansion chips have erasesize that conflicts with size |
| 3519 | * listed in nand_ids table. |
| 3520 | * Data sheet (5 byte ID): Spansion S30ML-P ORNAND (p.39) |
| 3521 | */ |
| 3522 | if (maf_id == NAND_MFR_AMD && id_data[4] != 0x00 && id_data[5] == 0x00 |
| 3523 | && id_data[6] == 0x00 && id_data[7] == 0x00 |
| 3524 | && mtd->writesize == 512) { |
| 3525 | mtd->erasesize = 128 * 1024; |
| 3526 | mtd->erasesize <<= ((id_data[3] & 0x03) << 1); |
| 3527 | } |
| 3528 | } |
| 3529 | |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3530 | /* |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3531 | * Set the bad block marker/indicator (BBM/BBI) patterns according to some |
| 3532 | * heuristic patterns using various detected parameters (e.g., manufacturer, |
| 3533 | * page size, cell-type information). |
| 3534 | */ |
| 3535 | static void nand_decode_bbm_options(struct mtd_info *mtd, |
| 3536 | struct nand_chip *chip, u8 id_data[8]) |
| 3537 | { |
| 3538 | int maf_id = id_data[0]; |
| 3539 | |
| 3540 | /* Set the bad block position */ |
| 3541 | if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16)) |
| 3542 | chip->badblockpos = NAND_LARGE_BADBLOCK_POS; |
| 3543 | else |
| 3544 | chip->badblockpos = NAND_SMALL_BADBLOCK_POS; |
| 3545 | |
| 3546 | /* |
| 3547 | * Bad block marker is stored in the last page of each block on Samsung |
| 3548 | * and Hynix MLC devices; stored in first two pages of each block on |
| 3549 | * Micron devices with 2KiB pages and on SLC Samsung, Hynix, Toshiba, |
| 3550 | * AMD/Spansion, and Macronix. All others scan only the first page. |
| 3551 | */ |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3552 | if (!nand_is_slc(chip) && |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3553 | (maf_id == NAND_MFR_SAMSUNG || |
| 3554 | maf_id == NAND_MFR_HYNIX)) |
| 3555 | chip->bbt_options |= NAND_BBT_SCANLASTPAGE; |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3556 | else if ((nand_is_slc(chip) && |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3557 | (maf_id == NAND_MFR_SAMSUNG || |
| 3558 | maf_id == NAND_MFR_HYNIX || |
| 3559 | maf_id == NAND_MFR_TOSHIBA || |
| 3560 | maf_id == NAND_MFR_AMD || |
| 3561 | maf_id == NAND_MFR_MACRONIX)) || |
| 3562 | (mtd->writesize == 2048 && |
| 3563 | maf_id == NAND_MFR_MICRON)) |
| 3564 | chip->bbt_options |= NAND_BBT_SCAN2NDPAGE; |
| 3565 | } |
| 3566 | |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3567 | static inline bool is_full_id_nand(struct nand_flash_dev *type) |
| 3568 | { |
| 3569 | return type->id_len; |
| 3570 | } |
| 3571 | |
| 3572 | static bool find_full_id_nand(struct mtd_info *mtd, struct nand_chip *chip, |
| 3573 | struct nand_flash_dev *type, u8 *id_data, int *busw) |
| 3574 | { |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3575 | if (!strncmp((char *)type->id, (char *)id_data, type->id_len)) { |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3576 | mtd->writesize = type->pagesize; |
| 3577 | mtd->erasesize = type->erasesize; |
| 3578 | mtd->oobsize = type->oobsize; |
| 3579 | |
| 3580 | chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]); |
| 3581 | chip->chipsize = (uint64_t)type->chipsize << 20; |
| 3582 | chip->options |= type->options; |
| 3583 | chip->ecc_strength_ds = NAND_ECC_STRENGTH(type); |
| 3584 | chip->ecc_step_ds = NAND_ECC_STEP(type); |
Scott Wood | d396372 | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 3585 | chip->onfi_timing_mode_default = |
| 3586 | type->onfi_timing_mode_default; |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3587 | |
| 3588 | *busw = type->options & NAND_BUSWIDTH_16; |
| 3589 | |
| 3590 | if (!mtd->name) |
| 3591 | mtd->name = type->name; |
| 3592 | |
| 3593 | return true; |
| 3594 | } |
| 3595 | return false; |
| 3596 | } |
| 3597 | |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3598 | /* |
| 3599 | * Get the flash and manufacturer id and lookup if the type is supported. |
Florian Fainelli | 0272c71 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3600 | */ |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3601 | static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd, |
Florian Fainelli | 0272c71 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3602 | struct nand_chip *chip, |
Florian Fainelli | 0272c71 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3603 | int *maf_id, int *dev_id, |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3604 | struct nand_flash_dev *type) |
Florian Fainelli | 0272c71 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3605 | { |
Heiko Schocher | 4e67c57 | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 3606 | int busw; |
Christian Hitz | 2a8e0fc | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 3607 | int i, maf_idx; |
| 3608 | u8 id_data[8]; |
Florian Fainelli | 0272c71 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3609 | |
| 3610 | /* Select the device */ |
| 3611 | chip->select_chip(mtd, 0); |
| 3612 | |
| 3613 | /* |
| 3614 | * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx) |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3615 | * after power-up. |
Florian Fainelli | 0272c71 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3616 | */ |
| 3617 | chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1); |
| 3618 | |
| 3619 | /* Send the command for reading device ID */ |
| 3620 | chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1); |
| 3621 | |
| 3622 | /* Read manufacturer and device IDs */ |
| 3623 | *maf_id = chip->read_byte(mtd); |
| 3624 | *dev_id = chip->read_byte(mtd); |
| 3625 | |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3626 | /* |
| 3627 | * Try again to make sure, as some systems the bus-hold or other |
Florian Fainelli | 0272c71 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3628 | * interface concerns can cause random data which looks like a |
| 3629 | * possibly credible NAND flash to appear. If the two results do |
| 3630 | * not match, ignore the device completely. |
| 3631 | */ |
| 3632 | |
| 3633 | chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1); |
| 3634 | |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3635 | /* Read entire ID string */ |
| 3636 | for (i = 0; i < 8; i++) |
Christian Hitz | 2a8e0fc | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 3637 | id_data[i] = chip->read_byte(mtd); |
Florian Fainelli | 0272c71 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3638 | |
Christian Hitz | 2a8e0fc | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 3639 | if (id_data[0] != *maf_id || id_data[1] != *dev_id) { |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3640 | pr_info("second ID read did not match %02x,%02x against %02x,%02x\n", |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3641 | *maf_id, *dev_id, id_data[0], id_data[1]); |
Florian Fainelli | 0272c71 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3642 | return ERR_PTR(-ENODEV); |
| 3643 | } |
| 3644 | |
| 3645 | if (!type) |
| 3646 | type = nand_flash_ids; |
| 3647 | |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3648 | for (; type->name != NULL; type++) { |
| 3649 | if (is_full_id_nand(type)) { |
| 3650 | if (find_full_id_nand(mtd, chip, type, id_data, &busw)) |
| 3651 | goto ident_done; |
| 3652 | } else if (*dev_id == type->dev_id) { |
Scott Wood | ceee07b | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 3653 | break; |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3654 | } |
| 3655 | } |
Florian Fainelli | 0272c71 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3656 | |
Christian Hitz | 2a8e0fc | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 3657 | chip->onfi_version = 0; |
| 3658 | if (!type->name || !type->pagesize) { |
Scott Wood | d396372 | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 3659 | /* Check if the chip is ONFI compliant */ |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3660 | if (nand_flash_detect_onfi(mtd, chip, &busw)) |
Christian Hitz | 2a8e0fc | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 3661 | goto ident_done; |
Heiko Schocher | 4e67c57 | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 3662 | |
| 3663 | /* Check if the chip is JEDEC compliant */ |
| 3664 | if (nand_flash_detect_jedec(mtd, chip, &busw)) |
| 3665 | goto ident_done; |
Florian Fainelli | 0272c71 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3666 | } |
| 3667 | |
Christian Hitz | 2a8e0fc | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 3668 | if (!type->name) |
| 3669 | return ERR_PTR(-ENODEV); |
| 3670 | |
Florian Fainelli | 0272c71 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3671 | if (!mtd->name) |
| 3672 | mtd->name = type->name; |
| 3673 | |
| 3674 | chip->chipsize = (uint64_t)type->chipsize << 20; |
Florian Fainelli | 0272c71 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3675 | |
Scott Wood | ceee07b | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 3676 | if (!type->pagesize) { |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3677 | /* Decode parameters from extended ID */ |
| 3678 | nand_decode_ext_id(mtd, chip, id_data, &busw); |
Christian Hitz | 2a8e0fc | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 3679 | } else { |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3680 | nand_decode_id(mtd, chip, type, id_data, &busw); |
Christian Hitz | 2a8e0fc | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 3681 | } |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3682 | /* Get chip options */ |
Marek Vasut | 9c790a7 | 2012-08-30 13:39:38 +0000 | [diff] [blame] | 3683 | chip->options |= type->options; |
Florian Fainelli | 0272c71 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3684 | |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3685 | /* |
| 3686 | * Check if chip is not a Samsung device. Do not clear the |
| 3687 | * options for chips which do not have an extended id. |
Christian Hitz | 2a8e0fc | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 3688 | */ |
| 3689 | if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize) |
| 3690 | chip->options &= ~NAND_SAMSUNG_LP_OPTIONS; |
| 3691 | ident_done: |
| 3692 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3693 | /* Try to identify manufacturer */ |
| 3694 | for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) { |
| 3695 | if (nand_manuf_ids[maf_idx].id == *maf_id) |
| 3696 | break; |
| 3697 | } |
| 3698 | |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3699 | if (chip->options & NAND_BUSWIDTH_AUTO) { |
| 3700 | WARN_ON(chip->options & NAND_BUSWIDTH_16); |
| 3701 | chip->options |= busw; |
| 3702 | nand_set_defaults(chip, busw); |
| 3703 | } else if (busw != (chip->options & NAND_BUSWIDTH_16)) { |
| 3704 | /* |
| 3705 | * Check, if buswidth is correct. Hardware drivers should set |
| 3706 | * chip correct! |
| 3707 | */ |
| 3708 | pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n", |
| 3709 | *maf_id, *dev_id); |
| 3710 | pr_info("%s %s\n", nand_manuf_ids[maf_idx].name, mtd->name); |
| 3711 | pr_warn("bus width %d instead %d bit\n", |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3712 | (chip->options & NAND_BUSWIDTH_16) ? 16 : 8, |
| 3713 | busw ? 16 : 8); |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3714 | return ERR_PTR(-EINVAL); |
| 3715 | } |
| 3716 | |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3717 | nand_decode_bbm_options(mtd, chip, id_data); |
| 3718 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3719 | /* Calculate the address shift from the page size */ |
| 3720 | chip->page_shift = ffs(mtd->writesize) - 1; |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3721 | /* Convert chipsize to number of pages per chip -1 */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3722 | chip->pagemask = (chip->chipsize >> chip->page_shift) - 1; |
| 3723 | |
| 3724 | chip->bbt_erase_shift = chip->phys_erase_shift = |
| 3725 | ffs(mtd->erasesize) - 1; |
Sandeep Paulraj | aaa8eec | 2009-10-30 13:51:23 -0400 | [diff] [blame] | 3726 | if (chip->chipsize & 0xffffffff) |
Sandeep Paulraj | 4f41e7e | 2009-11-07 14:24:06 -0500 | [diff] [blame] | 3727 | chip->chip_shift = ffs((unsigned)chip->chipsize) - 1; |
Christian Hitz | 2a8e0fc | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 3728 | else { |
| 3729 | chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32)); |
| 3730 | chip->chip_shift += 32 - 1; |
| 3731 | } |
| 3732 | |
| 3733 | chip->badblockbits = 8; |
Scott Wood | d396372 | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 3734 | chip->erase = single_erase; |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3735 | |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3736 | /* Do not replace user supplied command function! */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3737 | if (mtd->writesize > 512 && chip->cmdfunc == nand_command) |
| 3738 | chip->cmdfunc = nand_command_lp; |
| 3739 | |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3740 | pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n", |
| 3741 | *maf_id, *dev_id); |
Heiko Schocher | 4e67c57 | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 3742 | |
Christian Hitz | 2a8e0fc | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 3743 | #ifdef CONFIG_SYS_NAND_ONFI_DETECTION |
Heiko Schocher | 4e67c57 | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 3744 | if (chip->onfi_version) |
| 3745 | pr_info("%s %s\n", nand_manuf_ids[maf_idx].name, |
| 3746 | chip->onfi_params.model); |
| 3747 | else if (chip->jedec_version) |
| 3748 | pr_info("%s %s\n", nand_manuf_ids[maf_idx].name, |
| 3749 | chip->jedec_params.model); |
| 3750 | else |
| 3751 | pr_info("%s %s\n", nand_manuf_ids[maf_idx].name, |
| 3752 | type->name); |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3753 | #else |
Heiko Schocher | 4e67c57 | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 3754 | if (chip->jedec_version) |
| 3755 | pr_info("%s %s\n", nand_manuf_ids[maf_idx].name, |
| 3756 | chip->jedec_params.model); |
| 3757 | else |
| 3758 | pr_info("%s %s\n", nand_manuf_ids[maf_idx].name, |
| 3759 | type->name); |
| 3760 | |
| 3761 | pr_info("%s %s\n", nand_manuf_ids[maf_idx].name, |
| 3762 | type->name); |
Christian Hitz | 2a8e0fc | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 3763 | #endif |
Heiko Schocher | 4e67c57 | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 3764 | |
Scott Wood | d396372 | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 3765 | pr_info("%d MiB, %s, erase size: %d KiB, page size: %d, OOB size: %d\n", |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3766 | (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC", |
Scott Wood | d396372 | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 3767 | mtd->erasesize >> 10, mtd->writesize, mtd->oobsize); |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3768 | return type; |
| 3769 | } |
| 3770 | |
Brian Norris | 42bd19c | 2016-06-15 21:09:22 +0200 | [diff] [blame] | 3771 | #if CONFIG_IS_ENABLED(OF_CONTROL) |
| 3772 | DECLARE_GLOBAL_DATA_PTR; |
| 3773 | |
| 3774 | static int nand_dt_init(struct mtd_info *mtd, struct nand_chip *chip, int node) |
| 3775 | { |
| 3776 | int ret, ecc_mode = -1, ecc_strength, ecc_step; |
| 3777 | const void *blob = gd->fdt_blob; |
| 3778 | const char *str; |
| 3779 | |
| 3780 | ret = fdtdec_get_int(blob, node, "nand-bus-width", -1); |
| 3781 | if (ret == 16) |
| 3782 | chip->options |= NAND_BUSWIDTH_16; |
| 3783 | |
| 3784 | if (fdtdec_get_bool(blob, node, "nand-on-flash-bbt")) |
| 3785 | chip->bbt_options |= NAND_BBT_USE_FLASH; |
| 3786 | |
| 3787 | str = fdt_getprop(blob, node, "nand-ecc-mode", NULL); |
| 3788 | if (str) { |
| 3789 | if (!strcmp(str, "none")) |
| 3790 | ecc_mode = NAND_ECC_NONE; |
| 3791 | else if (!strcmp(str, "soft")) |
| 3792 | ecc_mode = NAND_ECC_SOFT; |
| 3793 | else if (!strcmp(str, "hw")) |
| 3794 | ecc_mode = NAND_ECC_HW; |
| 3795 | else if (!strcmp(str, "hw_syndrome")) |
| 3796 | ecc_mode = NAND_ECC_HW_SYNDROME; |
| 3797 | else if (!strcmp(str, "hw_oob_first")) |
| 3798 | ecc_mode = NAND_ECC_HW_OOB_FIRST; |
| 3799 | else if (!strcmp(str, "soft_bch")) |
| 3800 | ecc_mode = NAND_ECC_SOFT_BCH; |
| 3801 | } |
| 3802 | |
| 3803 | |
| 3804 | ecc_strength = fdtdec_get_int(blob, node, "nand-ecc-strength", -1); |
| 3805 | ecc_step = fdtdec_get_int(blob, node, "nand-ecc-step-size", -1); |
| 3806 | |
| 3807 | if ((ecc_step >= 0 && !(ecc_strength >= 0)) || |
| 3808 | (!(ecc_step >= 0) && ecc_strength >= 0)) { |
| 3809 | pr_err("must set both strength and step size in DT\n"); |
| 3810 | return -EINVAL; |
| 3811 | } |
| 3812 | |
| 3813 | if (ecc_mode >= 0) |
| 3814 | chip->ecc.mode = ecc_mode; |
| 3815 | |
| 3816 | if (ecc_strength >= 0) |
| 3817 | chip->ecc.strength = ecc_strength; |
| 3818 | |
| 3819 | if (ecc_step > 0) |
| 3820 | chip->ecc.size = ecc_step; |
| 3821 | |
| 3822 | return 0; |
| 3823 | } |
| 3824 | #else |
| 3825 | static int nand_dt_init(struct mtd_info *mtd, struct nand_chip *chip, int node) |
| 3826 | { |
| 3827 | return 0; |
| 3828 | } |
| 3829 | #endif /* CONFIG_IS_ENABLED(OF_CONTROL) */ |
| 3830 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3831 | /** |
| 3832 | * nand_scan_ident - [NAND Interface] Scan for the NAND device |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3833 | * @mtd: MTD device structure |
| 3834 | * @maxchips: number of chips to scan for |
| 3835 | * @table: alternative NAND ID table |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3836 | * |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3837 | * This is the first phase of the normal nand_scan() function. It reads the |
| 3838 | * flash ID and sets up MTD fields accordingly. |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3839 | * |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3840 | */ |
Lei Wen | 245eb90 | 2011-01-06 09:48:18 +0800 | [diff] [blame] | 3841 | int nand_scan_ident(struct mtd_info *mtd, int maxchips, |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3842 | struct nand_flash_dev *table) |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3843 | { |
Heiko Schocher | 4e67c57 | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 3844 | int i, nand_maf_id, nand_dev_id; |
Scott Wood | 17cb4b8 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 3845 | struct nand_chip *chip = mtd_to_nand(mtd); |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3846 | struct nand_flash_dev *type; |
Brian Norris | 42bd19c | 2016-06-15 21:09:22 +0200 | [diff] [blame] | 3847 | int ret; |
| 3848 | |
| 3849 | if (chip->flash_node) { |
| 3850 | ret = nand_dt_init(mtd, chip, chip->flash_node); |
| 3851 | if (ret) |
| 3852 | return ret; |
| 3853 | } |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3854 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3855 | /* Set the default functions */ |
Heiko Schocher | 4e67c57 | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 3856 | nand_set_defaults(chip, chip->options & NAND_BUSWIDTH_16); |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3857 | |
| 3858 | /* Read the flash type */ |
Heiko Schocher | 4e67c57 | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 3859 | type = nand_get_flash_type(mtd, chip, &nand_maf_id, |
| 3860 | &nand_dev_id, table); |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3861 | |
| 3862 | if (IS_ERR(type)) { |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3863 | if (!(chip->options & NAND_SCAN_SILENT_NODEV)) |
| 3864 | pr_warn("No NAND device found\n"); |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3865 | chip->select_chip(mtd, -1); |
| 3866 | return PTR_ERR(type); |
| 3867 | } |
| 3868 | |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3869 | chip->select_chip(mtd, -1); |
| 3870 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3871 | /* Check for a chip array */ |
| 3872 | for (i = 1; i < maxchips; i++) { |
| 3873 | chip->select_chip(mtd, i); |
Karl Beldan | 33efde5 | 2008-09-15 16:08:03 +0200 | [diff] [blame] | 3874 | /* See comment in nand_get_flash_type for reset */ |
| 3875 | chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1); |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3876 | /* Send the command for reading device ID */ |
| 3877 | chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1); |
| 3878 | /* Read manufacturer and device IDs */ |
| 3879 | if (nand_maf_id != chip->read_byte(mtd) || |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3880 | nand_dev_id != chip->read_byte(mtd)) { |
| 3881 | chip->select_chip(mtd, -1); |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3882 | break; |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3883 | } |
| 3884 | chip->select_chip(mtd, -1); |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3885 | } |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3886 | |
Wolfgang Grandegger | 672ed2a | 2009-02-11 18:38:20 +0100 | [diff] [blame] | 3887 | #ifdef DEBUG |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3888 | if (i > 1) |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3889 | pr_info("%d chips detected\n", i); |
Wolfgang Grandegger | 672ed2a | 2009-02-11 18:38:20 +0100 | [diff] [blame] | 3890 | #endif |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3891 | |
| 3892 | /* Store the number of chips and calc total size for mtd */ |
| 3893 | chip->numchips = i; |
| 3894 | mtd->size = i * chip->chipsize; |
| 3895 | |
| 3896 | return 0; |
| 3897 | } |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3898 | EXPORT_SYMBOL(nand_scan_ident); |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3899 | |
Scott Wood | d396372 | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 3900 | /* |
| 3901 | * Check if the chip configuration meet the datasheet requirements. |
| 3902 | |
| 3903 | * If our configuration corrects A bits per B bytes and the minimum |
| 3904 | * required correction level is X bits per Y bytes, then we must ensure |
| 3905 | * both of the following are true: |
| 3906 | * |
| 3907 | * (1) A / B >= X / Y |
| 3908 | * (2) A >= X |
| 3909 | * |
| 3910 | * Requirement (1) ensures we can correct for the required bitflip density. |
| 3911 | * Requirement (2) ensures we can correct even when all bitflips are clumped |
| 3912 | * in the same sector. |
| 3913 | */ |
| 3914 | static bool nand_ecc_strength_good(struct mtd_info *mtd) |
| 3915 | { |
Scott Wood | 17cb4b8 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 3916 | struct nand_chip *chip = mtd_to_nand(mtd); |
Scott Wood | d396372 | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 3917 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 3918 | int corr, ds_corr; |
| 3919 | |
| 3920 | if (ecc->size == 0 || chip->ecc_step_ds == 0) |
| 3921 | /* Not enough information */ |
| 3922 | return true; |
| 3923 | |
| 3924 | /* |
| 3925 | * We get the number of corrected bits per page to compare |
| 3926 | * the correction density. |
| 3927 | */ |
| 3928 | corr = (mtd->writesize * ecc->strength) / ecc->size; |
| 3929 | ds_corr = (mtd->writesize * chip->ecc_strength_ds) / chip->ecc_step_ds; |
| 3930 | |
| 3931 | return corr >= ds_corr && ecc->strength >= chip->ecc_strength_ds; |
| 3932 | } |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3933 | |
| 3934 | /** |
| 3935 | * nand_scan_tail - [NAND Interface] Scan for the NAND device |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3936 | * @mtd: MTD device structure |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3937 | * |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3938 | * This is the second phase of the normal nand_scan() function. It fills out |
| 3939 | * all the uninitialized function pointers with the defaults and scans for a |
| 3940 | * bad block table if appropriate. |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3941 | */ |
| 3942 | int nand_scan_tail(struct mtd_info *mtd) |
| 3943 | { |
| 3944 | int i; |
Scott Wood | 17cb4b8 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 3945 | struct nand_chip *chip = mtd_to_nand(mtd); |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3946 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
Heiko Schocher | 4e67c57 | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 3947 | struct nand_buffers *nbuf; |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3948 | |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3949 | /* New bad blocks should be marked in OOB, flash-based BBT, or both */ |
| 3950 | BUG_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) && |
| 3951 | !(chip->bbt_options & NAND_BBT_USE_FLASH)); |
| 3952 | |
Heiko Schocher | 4e67c57 | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 3953 | if (!(chip->options & NAND_OWN_BUFFERS)) { |
Heiko Schocher | 4e67c57 | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 3954 | nbuf = kzalloc(sizeof(struct nand_buffers), GFP_KERNEL); |
Heiko Schocher | 4e67c57 | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 3955 | chip->buffers = nbuf; |
| 3956 | } else { |
| 3957 | if (!chip->buffers) |
| 3958 | return -ENOMEM; |
| 3959 | } |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3960 | |
| 3961 | /* Set the internal oob buffer location, just after the page data */ |
| 3962 | chip->oob_poi = chip->buffers->databuf + mtd->writesize; |
| 3963 | |
| 3964 | /* |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3965 | * If no default placement scheme is given, select an appropriate one. |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3966 | */ |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3967 | if (!ecc->layout && (ecc->mode != NAND_ECC_SOFT_BCH)) { |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3968 | switch (mtd->oobsize) { |
| 3969 | case 8: |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3970 | ecc->layout = &nand_oob_8; |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3971 | break; |
| 3972 | case 16: |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3973 | ecc->layout = &nand_oob_16; |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3974 | break; |
| 3975 | case 64: |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3976 | ecc->layout = &nand_oob_64; |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3977 | break; |
| 3978 | case 128: |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3979 | ecc->layout = &nand_oob_128; |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3980 | break; |
| 3981 | default: |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3982 | pr_warn("No oob scheme defined for oobsize %d\n", |
| 3983 | mtd->oobsize); |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3984 | BUG(); |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3985 | } |
| 3986 | } |
| 3987 | |
| 3988 | if (!chip->write_page) |
| 3989 | chip->write_page = nand_write_page; |
| 3990 | |
| 3991 | /* |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3992 | * Check ECC mode, default to software if 3byte/512byte hardware ECC is |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3993 | * selected and we have 256 byte pagesize fallback to software ECC |
| 3994 | */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3995 | |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3996 | switch (ecc->mode) { |
Sandeep Paulraj | f83b7f9 | 2009-08-10 13:27:56 -0400 | [diff] [blame] | 3997 | case NAND_ECC_HW_OOB_FIRST: |
| 3998 | /* Similar to NAND_ECC_HW, but a separate read_page handle */ |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3999 | if (!ecc->calculate || !ecc->correct || !ecc->hwctl) { |
Scott Wood | d396372 | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 4000 | pr_warn("No ECC functions supplied; hardware ECC not possible\n"); |
Sandeep Paulraj | f83b7f9 | 2009-08-10 13:27:56 -0400 | [diff] [blame] | 4001 | BUG(); |
| 4002 | } |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4003 | if (!ecc->read_page) |
| 4004 | ecc->read_page = nand_read_page_hwecc_oob_first; |
Sandeep Paulraj | f83b7f9 | 2009-08-10 13:27:56 -0400 | [diff] [blame] | 4005 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4006 | case NAND_ECC_HW: |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4007 | /* Use standard hwecc read page function? */ |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4008 | if (!ecc->read_page) |
| 4009 | ecc->read_page = nand_read_page_hwecc; |
| 4010 | if (!ecc->write_page) |
| 4011 | ecc->write_page = nand_write_page_hwecc; |
| 4012 | if (!ecc->read_page_raw) |
| 4013 | ecc->read_page_raw = nand_read_page_raw; |
| 4014 | if (!ecc->write_page_raw) |
| 4015 | ecc->write_page_raw = nand_write_page_raw; |
| 4016 | if (!ecc->read_oob) |
| 4017 | ecc->read_oob = nand_read_oob_std; |
| 4018 | if (!ecc->write_oob) |
| 4019 | ecc->write_oob = nand_write_oob_std; |
| 4020 | if (!ecc->read_subpage) |
| 4021 | ecc->read_subpage = nand_read_subpage; |
Scott Wood | ceee07b | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 4022 | if (!ecc->write_subpage && ecc->hwctl && ecc->calculate) |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4023 | ecc->write_subpage = nand_write_subpage_hwecc; |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4024 | |
| 4025 | case NAND_ECC_HW_SYNDROME: |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4026 | if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) && |
| 4027 | (!ecc->read_page || |
| 4028 | ecc->read_page == nand_read_page_hwecc || |
| 4029 | !ecc->write_page || |
| 4030 | ecc->write_page == nand_write_page_hwecc)) { |
Scott Wood | d396372 | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 4031 | pr_warn("No ECC functions supplied; hardware ECC not possible\n"); |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4032 | BUG(); |
| 4033 | } |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4034 | /* Use standard syndrome read/write page function? */ |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4035 | if (!ecc->read_page) |
| 4036 | ecc->read_page = nand_read_page_syndrome; |
| 4037 | if (!ecc->write_page) |
| 4038 | ecc->write_page = nand_write_page_syndrome; |
| 4039 | if (!ecc->read_page_raw) |
| 4040 | ecc->read_page_raw = nand_read_page_raw_syndrome; |
| 4041 | if (!ecc->write_page_raw) |
| 4042 | ecc->write_page_raw = nand_write_page_raw_syndrome; |
| 4043 | if (!ecc->read_oob) |
| 4044 | ecc->read_oob = nand_read_oob_syndrome; |
| 4045 | if (!ecc->write_oob) |
| 4046 | ecc->write_oob = nand_write_oob_syndrome; |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4047 | |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4048 | if (mtd->writesize >= ecc->size) { |
| 4049 | if (!ecc->strength) { |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4050 | pr_warn("Driver must set ecc.strength when using hardware ECC\n"); |
| 4051 | BUG(); |
| 4052 | } |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4053 | break; |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4054 | } |
Scott Wood | d396372 | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 4055 | pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n", |
| 4056 | ecc->size, mtd->writesize); |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4057 | ecc->mode = NAND_ECC_SOFT; |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4058 | |
| 4059 | case NAND_ECC_SOFT: |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4060 | ecc->calculate = nand_calculate_ecc; |
| 4061 | ecc->correct = nand_correct_data; |
| 4062 | ecc->read_page = nand_read_page_swecc; |
| 4063 | ecc->read_subpage = nand_read_subpage; |
| 4064 | ecc->write_page = nand_write_page_swecc; |
| 4065 | ecc->read_page_raw = nand_read_page_raw; |
| 4066 | ecc->write_page_raw = nand_write_page_raw; |
| 4067 | ecc->read_oob = nand_read_oob_std; |
| 4068 | ecc->write_oob = nand_write_oob_std; |
| 4069 | if (!ecc->size) |
| 4070 | ecc->size = 256; |
| 4071 | ecc->bytes = 3; |
| 4072 | ecc->strength = 1; |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4073 | break; |
| 4074 | |
Christian Hitz | 4c6de85 | 2011-10-12 09:31:59 +0200 | [diff] [blame] | 4075 | case NAND_ECC_SOFT_BCH: |
| 4076 | if (!mtd_nand_has_bch()) { |
Heiko Schocher | 4e67c57 | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 4077 | pr_warn("CONFIG_MTD_NAND_ECC_BCH not enabled\n"); |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4078 | BUG(); |
Christian Hitz | 4c6de85 | 2011-10-12 09:31:59 +0200 | [diff] [blame] | 4079 | } |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4080 | ecc->calculate = nand_bch_calculate_ecc; |
| 4081 | ecc->correct = nand_bch_correct_data; |
| 4082 | ecc->read_page = nand_read_page_swecc; |
| 4083 | ecc->read_subpage = nand_read_subpage; |
| 4084 | ecc->write_page = nand_write_page_swecc; |
| 4085 | ecc->read_page_raw = nand_read_page_raw; |
| 4086 | ecc->write_page_raw = nand_write_page_raw; |
| 4087 | ecc->read_oob = nand_read_oob_std; |
| 4088 | ecc->write_oob = nand_write_oob_std; |
Christian Hitz | 4c6de85 | 2011-10-12 09:31:59 +0200 | [diff] [blame] | 4089 | /* |
Scott Wood | d396372 | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 4090 | * Board driver should supply ecc.size and ecc.strength values |
| 4091 | * to select how many bits are correctable. Otherwise, default |
| 4092 | * to 4 bits for large page devices. |
Christian Hitz | 4c6de85 | 2011-10-12 09:31:59 +0200 | [diff] [blame] | 4093 | */ |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4094 | if (!ecc->size && (mtd->oobsize >= 64)) { |
| 4095 | ecc->size = 512; |
Scott Wood | d396372 | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 4096 | ecc->strength = 4; |
Christian Hitz | 4c6de85 | 2011-10-12 09:31:59 +0200 | [diff] [blame] | 4097 | } |
Scott Wood | d396372 | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 4098 | |
| 4099 | /* See nand_bch_init() for details. */ |
Scott Wood | ceee07b | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 4100 | ecc->bytes = 0; |
| 4101 | ecc->priv = nand_bch_init(mtd); |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4102 | if (!ecc->priv) { |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4103 | pr_warn("BCH ECC initialization failed!\n"); |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4104 | BUG(); |
| 4105 | } |
Christian Hitz | 4c6de85 | 2011-10-12 09:31:59 +0200 | [diff] [blame] | 4106 | break; |
| 4107 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4108 | case NAND_ECC_NONE: |
Scott Wood | d396372 | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 4109 | pr_warn("NAND_ECC_NONE selected by board driver. This is not recommended!\n"); |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4110 | ecc->read_page = nand_read_page_raw; |
| 4111 | ecc->write_page = nand_write_page_raw; |
| 4112 | ecc->read_oob = nand_read_oob_std; |
| 4113 | ecc->read_page_raw = nand_read_page_raw; |
| 4114 | ecc->write_page_raw = nand_write_page_raw; |
| 4115 | ecc->write_oob = nand_write_oob_std; |
| 4116 | ecc->size = mtd->writesize; |
| 4117 | ecc->bytes = 0; |
| 4118 | ecc->strength = 0; |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4119 | break; |
| 4120 | |
| 4121 | default: |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4122 | pr_warn("Invalid NAND_ECC_MODE %d\n", ecc->mode); |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4123 | BUG(); |
| 4124 | } |
| 4125 | |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4126 | /* For many systems, the standard OOB write also works for raw */ |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4127 | if (!ecc->read_oob_raw) |
| 4128 | ecc->read_oob_raw = ecc->read_oob; |
| 4129 | if (!ecc->write_oob_raw) |
| 4130 | ecc->write_oob_raw = ecc->write_oob; |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4131 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4132 | /* |
| 4133 | * The number of bytes available for a client to place data into |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4134 | * the out of band area. |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4135 | */ |
Scott Wood | ceee07b | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 4136 | mtd->oobavail = 0; |
| 4137 | if (ecc->layout) { |
| 4138 | for (i = 0; ecc->layout->oobfree[i].length; i++) |
| 4139 | mtd->oobavail += ecc->layout->oobfree[i].length; |
| 4140 | } |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4141 | |
Scott Wood | d396372 | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 4142 | /* ECC sanity check: warn if it's too weak */ |
| 4143 | if (!nand_ecc_strength_good(mtd)) |
| 4144 | pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n", |
| 4145 | mtd->name); |
| 4146 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4147 | /* |
| 4148 | * Set the number of read / write steps for one page depending on ECC |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4149 | * mode. |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4150 | */ |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4151 | ecc->steps = mtd->writesize / ecc->size; |
| 4152 | if (ecc->steps * ecc->size != mtd->writesize) { |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4153 | pr_warn("Invalid ECC parameters\n"); |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4154 | BUG(); |
| 4155 | } |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4156 | ecc->total = ecc->steps * ecc->bytes; |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4157 | |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4158 | /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */ |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4159 | if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) { |
| 4160 | switch (ecc->steps) { |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4161 | case 2: |
| 4162 | mtd->subpage_sft = 1; |
| 4163 | break; |
| 4164 | case 4: |
| 4165 | case 8: |
Sandeep Paulraj | aad4a28 | 2009-11-07 14:24:34 -0500 | [diff] [blame] | 4166 | case 16: |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4167 | mtd->subpage_sft = 2; |
| 4168 | break; |
| 4169 | } |
| 4170 | } |
| 4171 | chip->subpagesize = mtd->writesize >> mtd->subpage_sft; |
| 4172 | |
| 4173 | /* Initialize state */ |
| 4174 | chip->state = FL_READY; |
| 4175 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4176 | /* Invalidate the pagebuffer reference */ |
| 4177 | chip->pagebuf = -1; |
| 4178 | |
Joe Hershberger | c788ecf | 2012-11-05 06:46:31 +0000 | [diff] [blame] | 4179 | /* Large page NAND with SOFT_ECC should support subpage reads */ |
Scott Wood | d396372 | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 4180 | switch (ecc->mode) { |
| 4181 | case NAND_ECC_SOFT: |
| 4182 | case NAND_ECC_SOFT_BCH: |
| 4183 | if (chip->page_shift > 9) |
| 4184 | chip->options |= NAND_SUBPAGE_READ; |
| 4185 | break; |
| 4186 | |
| 4187 | default: |
| 4188 | break; |
| 4189 | } |
Joe Hershberger | c788ecf | 2012-11-05 06:46:31 +0000 | [diff] [blame] | 4190 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4191 | /* Fill in remaining MTD driver data */ |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4192 | mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH; |
Christian Hitz | 2a8e0fc | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 4193 | mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM : |
| 4194 | MTD_CAP_NANDFLASH; |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4195 | mtd->_erase = nand_erase; |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4196 | mtd->_read = nand_read; |
| 4197 | mtd->_write = nand_write; |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4198 | mtd->_panic_write = panic_nand_write; |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4199 | mtd->_read_oob = nand_read_oob; |
| 4200 | mtd->_write_oob = nand_write_oob; |
| 4201 | mtd->_sync = nand_sync; |
| 4202 | mtd->_lock = NULL; |
| 4203 | mtd->_unlock = NULL; |
Ezequiel Garcia | 86a720a | 2014-05-21 19:06:12 -0300 | [diff] [blame] | 4204 | mtd->_block_isreserved = nand_block_isreserved; |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4205 | mtd->_block_isbad = nand_block_isbad; |
| 4206 | mtd->_block_markbad = nand_block_markbad; |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4207 | mtd->writebufsize = mtd->writesize; |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4208 | |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4209 | /* propagate ecc info to mtd_info */ |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4210 | mtd->ecclayout = ecc->layout; |
| 4211 | mtd->ecc_strength = ecc->strength; |
| 4212 | mtd->ecc_step_size = ecc->size; |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4213 | /* |
| 4214 | * Initialize bitflip_threshold to its default prior scan_bbt() call. |
| 4215 | * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be |
| 4216 | * properly set. |
| 4217 | */ |
| 4218 | if (!mtd->bitflip_threshold) |
Scott Wood | d396372 | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 4219 | mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4); |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4220 | |
Rostislav Lisovy | 35c204d | 2014-10-22 13:40:44 +0200 | [diff] [blame] | 4221 | return 0; |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4222 | } |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4223 | EXPORT_SYMBOL(nand_scan_tail); |
| 4224 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4225 | /** |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 4226 | * nand_scan - [NAND Interface] Scan for the NAND device |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4227 | * @mtd: MTD device structure |
| 4228 | * @maxchips: number of chips to scan for |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 4229 | * |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4230 | * This fills out all the uninitialized function pointers with the defaults. |
| 4231 | * The flash ID is read and the mtd/chip structures are filled with the |
Scott Wood | ceee07b | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 4232 | * appropriate values. |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 4233 | */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4234 | int nand_scan(struct mtd_info *mtd, int maxchips) |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 4235 | { |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4236 | int ret; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 4237 | |
Lei Wen | 245eb90 | 2011-01-06 09:48:18 +0800 | [diff] [blame] | 4238 | ret = nand_scan_ident(mtd, maxchips, NULL); |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4239 | if (!ret) |
| 4240 | ret = nand_scan_tail(mtd); |
| 4241 | return ret; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 4242 | } |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4243 | EXPORT_SYMBOL(nand_scan); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 4244 | |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4245 | MODULE_LICENSE("GPL"); |
| 4246 | MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>"); |
| 4247 | MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>"); |
| 4248 | MODULE_DESCRIPTION("Generic NAND flash driver code"); |