Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1 | /* |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2 | * Overview: |
| 3 | * This is the generic MTD driver for NAND flash devices. It should be |
| 4 | * capable of working with almost all NAND chips currently available. |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 5 | * |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 6 | * Additional technical information is available on |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 7 | * http://www.linux-mtd.infradead.org/doc/nand.html |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 8 | * |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 9 | * Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com) |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 10 | * 2002-2006 Thomas Gleixner (tglx@linutronix.de) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 11 | * |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 12 | * Credits: |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 13 | * David Woodhouse for adding multichip support |
| 14 | * |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 15 | * Aleph One Ltd. and Toby Churchill Ltd. for supporting the |
| 16 | * rework for 2K page size chips |
| 17 | * |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 18 | * TODO: |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 19 | * Enable cached programming for 2k page size chips |
| 20 | * Check, if mtd->ecctype should be set to MTD_ECC_HW |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 21 | * if we have HW ECC support. |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 22 | * BBT table is not serialized, has to be fixed |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 23 | * |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 24 | * This program is free software; you can redistribute it and/or modify |
| 25 | * it under the terms of the GNU General Public License version 2 as |
| 26 | * published by the Free Software Foundation. |
| 27 | * |
| 28 | */ |
| 29 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 30 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 31 | #include <common.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 32 | #include <log.h> |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 33 | #include <malloc.h> |
| 34 | #include <watchdog.h> |
Simon Glass | d66c5f7 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 35 | #include <dm/devres.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 36 | #include <linux/bitops.h> |
Simon Glass | c06c1be | 2020-05-10 11:40:08 -0600 | [diff] [blame] | 37 | #include <linux/bug.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 38 | #include <linux/delay.h> |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 39 | #include <linux/err.h> |
Mike Frysinger | 11d1a09 | 2012-04-09 13:39:55 +0000 | [diff] [blame] | 40 | #include <linux/compat.h> |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 41 | #include <linux/mtd/mtd.h> |
Masahiro Yamada | 2b7a873 | 2017-11-30 13:45:24 +0900 | [diff] [blame] | 42 | #include <linux/mtd/rawnand.h> |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 43 | #include <linux/mtd/nand_ecc.h> |
Christian Hitz | 55f7bca | 2011-10-12 09:31:59 +0200 | [diff] [blame] | 44 | #include <linux/mtd/nand_bch.h> |
Stefan Roese | fa252ea | 2009-04-24 15:58:33 +0200 | [diff] [blame] | 45 | #ifdef CONFIG_MTD_PARTITIONS |
| 46 | #include <linux/mtd/partitions.h> |
| 47 | #endif |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 48 | #include <asm/io.h> |
Masahiro Yamada | 56a931c | 2016-09-21 11:28:55 +0900 | [diff] [blame] | 49 | #include <linux/errno.h> |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 50 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 51 | /* Define default oob placement schemes for large and small page devices */ |
Gregory CLEMENT | e5b9631 | 2019-04-17 11:22:05 +0200 | [diff] [blame] | 52 | #ifndef CONFIG_SYS_NAND_DRIVER_ECC_LAYOUT |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 53 | static struct nand_ecclayout nand_oob_8 = { |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 54 | .eccbytes = 3, |
| 55 | .eccpos = {0, 1, 2}, |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 56 | .oobfree = { |
| 57 | {.offset = 3, |
| 58 | .length = 2}, |
| 59 | {.offset = 6, |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 60 | .length = 2} } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 61 | }; |
| 62 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 63 | static struct nand_ecclayout nand_oob_16 = { |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 64 | .eccbytes = 6, |
| 65 | .eccpos = {0, 1, 2, 3, 6, 7}, |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 66 | .oobfree = { |
| 67 | {.offset = 8, |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 68 | . length = 8} } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 69 | }; |
| 70 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 71 | static struct nand_ecclayout nand_oob_64 = { |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 72 | .eccbytes = 24, |
| 73 | .eccpos = { |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 74 | 40, 41, 42, 43, 44, 45, 46, 47, |
| 75 | 48, 49, 50, 51, 52, 53, 54, 55, |
| 76 | 56, 57, 58, 59, 60, 61, 62, 63}, |
| 77 | .oobfree = { |
| 78 | {.offset = 2, |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 79 | .length = 38} } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 80 | }; |
| 81 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 82 | static struct nand_ecclayout nand_oob_128 = { |
Sergei Poselenov | 04fbaa0 | 2008-06-06 15:42:43 +0200 | [diff] [blame] | 83 | .eccbytes = 48, |
| 84 | .eccpos = { |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 85 | 80, 81, 82, 83, 84, 85, 86, 87, |
| 86 | 88, 89, 90, 91, 92, 93, 94, 95, |
| 87 | 96, 97, 98, 99, 100, 101, 102, 103, |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 88 | 104, 105, 106, 107, 108, 109, 110, 111, |
| 89 | 112, 113, 114, 115, 116, 117, 118, 119, |
| 90 | 120, 121, 122, 123, 124, 125, 126, 127}, |
| 91 | .oobfree = { |
| 92 | {.offset = 2, |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 93 | .length = 78} } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 94 | }; |
Stefan Agner | bd18614 | 2018-12-06 14:57:09 +0100 | [diff] [blame] | 95 | #endif |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 96 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 97 | static int nand_get_device(struct mtd_info *mtd, int new_state); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 98 | |
| 99 | static int nand_do_write_oob(struct mtd_info *mtd, loff_t to, |
| 100 | struct mtd_oob_ops *ops); |
| 101 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 102 | /* |
| 103 | * For devices which display every fart in the system on a separate LED. Is |
| 104 | * compiled away when LED support is disabled. |
| 105 | */ |
| 106 | DEFINE_LED_TRIGGER(nand_led_trigger); |
Sergei Poselenov | 04fbaa0 | 2008-06-06 15:42:43 +0200 | [diff] [blame] | 107 | |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 108 | static int check_offs_len(struct mtd_info *mtd, |
| 109 | loff_t ofs, uint64_t len) |
| 110 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 111 | struct nand_chip *chip = mtd_to_nand(mtd); |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 112 | int ret = 0; |
| 113 | |
| 114 | /* Start address must align on block boundary */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 115 | if (ofs & ((1ULL << chip->phys_erase_shift) - 1)) { |
| 116 | pr_debug("%s: unaligned address\n", __func__); |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 117 | ret = -EINVAL; |
| 118 | } |
| 119 | |
| 120 | /* Length must align on block boundary */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 121 | if (len & ((1ULL << chip->phys_erase_shift) - 1)) { |
| 122 | pr_debug("%s: length not block aligned\n", __func__); |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 123 | ret = -EINVAL; |
| 124 | } |
| 125 | |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 126 | return ret; |
| 127 | } |
| 128 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 129 | /** |
| 130 | * nand_release_device - [GENERIC] release chip |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 131 | * @mtd: MTD device structure |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 132 | * |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 133 | * Release chip lock and wake up anyone waiting on the device. |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 134 | */ |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 135 | static void nand_release_device(struct mtd_info *mtd) |
Wolfgang Denk | 9a08dd1 | 2005-11-02 14:29:12 +0100 | [diff] [blame] | 136 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 137 | struct nand_chip *chip = mtd_to_nand(mtd); |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 138 | |
| 139 | /* De-select the NAND device */ |
| 140 | chip->select_chip(mtd, -1); |
Wolfgang Denk | 9a08dd1 | 2005-11-02 14:29:12 +0100 | [diff] [blame] | 141 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 142 | |
| 143 | /** |
| 144 | * nand_read_byte - [DEFAULT] read one byte from the chip |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 145 | * @mtd: MTD device structure |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 146 | * |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 147 | * Default read function for 8bit buswidth |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 148 | */ |
Simon Schwarz | 5a9fc19 | 2011-10-31 06:34:44 +0000 | [diff] [blame] | 149 | uint8_t nand_read_byte(struct mtd_info *mtd) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 150 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 151 | struct nand_chip *chip = mtd_to_nand(mtd); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 152 | return readb(chip->IO_ADDR_R); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | /** |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 156 | * nand_read_byte16 - [DEFAULT] read one byte endianness aware from the chip |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 157 | * @mtd: MTD device structure |
| 158 | * |
| 159 | * Default read function for 16bit buswidth with endianness conversion. |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 160 | * |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 161 | */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 162 | static uint8_t nand_read_byte16(struct mtd_info *mtd) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 163 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 164 | struct nand_chip *chip = mtd_to_nand(mtd); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 165 | return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R)); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | /** |
| 169 | * nand_read_word - [DEFAULT] read one word from the chip |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 170 | * @mtd: MTD device structure |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 171 | * |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 172 | * Default read function for 16bit buswidth without endianness conversion. |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 173 | */ |
| 174 | static u16 nand_read_word(struct mtd_info *mtd) |
| 175 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 176 | struct nand_chip *chip = mtd_to_nand(mtd); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 177 | return readw(chip->IO_ADDR_R); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | /** |
| 181 | * nand_select_chip - [DEFAULT] control CE line |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 182 | * @mtd: MTD device structure |
| 183 | * @chipnr: chipnumber to select, -1 for deselect |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 184 | * |
| 185 | * Default select function for 1 chip devices. |
| 186 | */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 187 | static void nand_select_chip(struct mtd_info *mtd, int chipnr) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 188 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 189 | struct nand_chip *chip = mtd_to_nand(mtd); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 190 | |
| 191 | switch (chipnr) { |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 192 | case -1: |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 193 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 194 | break; |
| 195 | case 0: |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 196 | break; |
| 197 | |
| 198 | default: |
| 199 | BUG(); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | /** |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 204 | * nand_write_byte - [DEFAULT] write single byte to chip |
| 205 | * @mtd: MTD device structure |
| 206 | * @byte: value to write |
| 207 | * |
| 208 | * Default function to write a byte to I/O[7:0] |
| 209 | */ |
| 210 | static void nand_write_byte(struct mtd_info *mtd, uint8_t byte) |
| 211 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 212 | struct nand_chip *chip = mtd_to_nand(mtd); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 213 | |
| 214 | chip->write_buf(mtd, &byte, 1); |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * nand_write_byte16 - [DEFAULT] write single byte to a chip with width 16 |
| 219 | * @mtd: MTD device structure |
| 220 | * @byte: value to write |
| 221 | * |
| 222 | * Default function to write a byte to I/O[7:0] on a 16-bit wide chip. |
| 223 | */ |
| 224 | static void nand_write_byte16(struct mtd_info *mtd, uint8_t byte) |
| 225 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 226 | struct nand_chip *chip = mtd_to_nand(mtd); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 227 | uint16_t word = byte; |
| 228 | |
| 229 | /* |
| 230 | * It's not entirely clear what should happen to I/O[15:8] when writing |
| 231 | * a byte. The ONFi spec (Revision 3.1; 2012-09-19, Section 2.16) reads: |
| 232 | * |
| 233 | * When the host supports a 16-bit bus width, only data is |
| 234 | * transferred at the 16-bit width. All address and command line |
| 235 | * transfers shall use only the lower 8-bits of the data bus. During |
| 236 | * command transfers, the host may place any value on the upper |
| 237 | * 8-bits of the data bus. During address transfers, the host shall |
| 238 | * set the upper 8-bits of the data bus to 00h. |
| 239 | * |
| 240 | * One user of the write_byte callback is nand_onfi_set_features. The |
| 241 | * four parameters are specified to be written to I/O[7:0], but this is |
| 242 | * neither an address nor a command transfer. Let's assume a 0 on the |
| 243 | * upper I/O lines is OK. |
| 244 | */ |
| 245 | chip->write_buf(mtd, (uint8_t *)&word, 2); |
| 246 | } |
| 247 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 248 | static void iowrite8_rep(void *addr, const uint8_t *buf, int len) |
| 249 | { |
| 250 | int i; |
| 251 | |
| 252 | for (i = 0; i < len; i++) |
| 253 | writeb(buf[i], addr); |
| 254 | } |
| 255 | static void ioread8_rep(void *addr, uint8_t *buf, int len) |
| 256 | { |
| 257 | int i; |
| 258 | |
| 259 | for (i = 0; i < len; i++) |
| 260 | buf[i] = readb(addr); |
| 261 | } |
| 262 | |
| 263 | static void ioread16_rep(void *addr, void *buf, int len) |
| 264 | { |
| 265 | int i; |
Wolfgang Denk | 62fb2b4 | 2021-09-27 17:42:39 +0200 | [diff] [blame] | 266 | u16 *p = (u16 *) buf; |
Stefan Roese | a9e9954 | 2014-09-05 09:57:01 +0200 | [diff] [blame] | 267 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 268 | for (i = 0; i < len; i++) |
| 269 | p[i] = readw(addr); |
| 270 | } |
| 271 | |
| 272 | static void iowrite16_rep(void *addr, void *buf, int len) |
| 273 | { |
| 274 | int i; |
| 275 | u16 *p = (u16 *) buf; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 276 | |
| 277 | for (i = 0; i < len; i++) |
| 278 | writew(p[i], addr); |
| 279 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 280 | |
| 281 | /** |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 282 | * nand_write_buf - [DEFAULT] write buffer to chip |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 283 | * @mtd: MTD device structure |
| 284 | * @buf: data buffer |
| 285 | * @len: number of bytes to write |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 286 | * |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 287 | * Default write function for 8bit buswidth. |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 288 | */ |
Simon Schwarz | 5a9fc19 | 2011-10-31 06:34:44 +0000 | [diff] [blame] | 289 | void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 290 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 291 | struct nand_chip *chip = mtd_to_nand(mtd); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 292 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 293 | iowrite8_rep(chip->IO_ADDR_W, buf, len); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | /** |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 297 | * nand_read_buf - [DEFAULT] read chip data into buffer |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 298 | * @mtd: MTD device structure |
| 299 | * @buf: buffer to store date |
| 300 | * @len: number of bytes to read |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 301 | * |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 302 | * Default read function for 8bit buswidth. |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 303 | */ |
Simon Schwarz | 4f62e98 | 2011-09-14 15:30:16 -0400 | [diff] [blame] | 304 | void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 305 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 306 | struct nand_chip *chip = mtd_to_nand(mtd); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 307 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 308 | ioread8_rep(chip->IO_ADDR_R, buf, len); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 309 | } |
| 310 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 311 | /** |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 312 | * nand_write_buf16 - [DEFAULT] write buffer to chip |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 313 | * @mtd: MTD device structure |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 314 | * @buf: data buffer |
| 315 | * @len: number of bytes to write |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 316 | * |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 317 | * Default write function for 16bit buswidth. |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 318 | */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 319 | void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 320 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 321 | struct nand_chip *chip = mtd_to_nand(mtd); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 322 | u16 *p = (u16 *) buf; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 323 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 324 | iowrite16_rep(chip->IO_ADDR_W, p, len >> 1); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | /** |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 328 | * nand_read_buf16 - [DEFAULT] read chip data into buffer |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 329 | * @mtd: MTD device structure |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 330 | * @buf: buffer to store date |
| 331 | * @len: number of bytes to read |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 332 | * |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 333 | * Default read function for 16bit buswidth. |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 334 | */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 335 | void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 336 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 337 | struct nand_chip *chip = mtd_to_nand(mtd); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 338 | u16 *p = (u16 *) buf; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 339 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 340 | ioread16_rep(chip->IO_ADDR_R, p, len >> 1); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | /** |
| 344 | * nand_block_bad - [DEFAULT] Read bad block marker from the chip |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 345 | * @mtd: MTD device structure |
| 346 | * @ofs: offset from device start |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 347 | * |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 348 | * Check, if the block is bad. |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 349 | */ |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 350 | static int nand_block_bad(struct mtd_info *mtd, loff_t ofs) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 351 | { |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 352 | int page, res = 0, i = 0; |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 353 | struct nand_chip *chip = mtd_to_nand(mtd); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 354 | u16 bad; |
| 355 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 356 | if (chip->bbt_options & NAND_BBT_SCANLASTPAGE) |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 357 | ofs += mtd->erasesize - mtd->writesize; |
| 358 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 359 | page = (int)(ofs >> chip->page_shift) & chip->pagemask; |
Thomas Knobloch | 9e2aeaf | 2007-05-05 07:04:42 +0200 | [diff] [blame] | 360 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 361 | do { |
| 362 | if (chip->options & NAND_BUSWIDTH_16) { |
| 363 | chip->cmdfunc(mtd, NAND_CMD_READOOB, |
| 364 | chip->badblockpos & 0xFE, page); |
| 365 | bad = cpu_to_le16(chip->read_word(mtd)); |
| 366 | if (chip->badblockpos & 0x1) |
| 367 | bad >>= 8; |
| 368 | else |
| 369 | bad &= 0xFF; |
| 370 | } else { |
| 371 | chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos, |
| 372 | page); |
| 373 | bad = chip->read_byte(mtd); |
| 374 | } |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 375 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 376 | if (likely(chip->badblockbits == 8)) |
| 377 | res = bad != 0xFF; |
| 378 | else |
| 379 | res = hweight8(bad) < chip->badblockbits; |
| 380 | ofs += mtd->writesize; |
| 381 | page = (int)(ofs >> chip->page_shift) & chip->pagemask; |
| 382 | i++; |
| 383 | } while (!res && i < 2 && (chip->bbt_options & NAND_BBT_SCAN2NDPAGE)); |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 384 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 385 | return res; |
| 386 | } |
| 387 | |
| 388 | /** |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 389 | * nand_default_block_markbad - [DEFAULT] mark a block bad via bad block marker |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 390 | * @mtd: MTD device structure |
| 391 | * @ofs: offset from device start |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 392 | * |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 393 | * This is the default implementation, which can be overridden by a hardware |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 394 | * specific driver. It provides the details for writing a bad block marker to a |
| 395 | * block. |
| 396 | */ |
| 397 | static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs) |
| 398 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 399 | struct nand_chip *chip = mtd_to_nand(mtd); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 400 | struct mtd_oob_ops ops; |
| 401 | uint8_t buf[2] = { 0, 0 }; |
| 402 | int ret = 0, res, i = 0; |
| 403 | |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 404 | memset(&ops, 0, sizeof(ops)); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 405 | ops.oobbuf = buf; |
| 406 | ops.ooboffs = chip->badblockpos; |
| 407 | if (chip->options & NAND_BUSWIDTH_16) { |
| 408 | ops.ooboffs &= ~0x01; |
| 409 | ops.len = ops.ooblen = 2; |
| 410 | } else { |
| 411 | ops.len = ops.ooblen = 1; |
| 412 | } |
| 413 | ops.mode = MTD_OPS_PLACE_OOB; |
| 414 | |
| 415 | /* Write to first/last page(s) if necessary */ |
| 416 | if (chip->bbt_options & NAND_BBT_SCANLASTPAGE) |
| 417 | ofs += mtd->erasesize - mtd->writesize; |
| 418 | do { |
| 419 | res = nand_do_write_oob(mtd, ofs, &ops); |
| 420 | if (!ret) |
| 421 | ret = res; |
| 422 | |
| 423 | i++; |
| 424 | ofs += mtd->writesize; |
| 425 | } while ((chip->bbt_options & NAND_BBT_SCAN2NDPAGE) && i < 2); |
| 426 | |
| 427 | return ret; |
| 428 | } |
| 429 | |
| 430 | /** |
| 431 | * nand_block_markbad_lowlevel - mark a block bad |
| 432 | * @mtd: MTD device structure |
| 433 | * @ofs: offset from device start |
| 434 | * |
| 435 | * This function performs the generic NAND bad block marking steps (i.e., bad |
| 436 | * block table(s) and/or marker(s)). We only allow the hardware driver to |
| 437 | * specify how to write bad block markers to OOB (chip->block_markbad). |
| 438 | * |
| 439 | * We try operations in the following order: |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 440 | * (1) erase the affected block, to allow OOB marker to be written cleanly |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 441 | * (2) write bad block marker to OOB area of affected block (unless flag |
| 442 | * NAND_BBT_NO_OOB_BBM is present) |
| 443 | * (3) update the BBT |
| 444 | * Note that we retain the first error encountered in (2) or (3), finish the |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 445 | * procedures, and dump the error in the end. |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 446 | */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 447 | static int nand_block_markbad_lowlevel(struct mtd_info *mtd, loff_t ofs) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 448 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 449 | struct nand_chip *chip = mtd_to_nand(mtd); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 450 | int res, ret = 0; |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 451 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 452 | if (!(chip->bbt_options & NAND_BBT_NO_OOB_BBM)) { |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 453 | struct erase_info einfo; |
| 454 | |
| 455 | /* Attempt erase before marking OOB */ |
| 456 | memset(&einfo, 0, sizeof(einfo)); |
| 457 | einfo.mtd = mtd; |
| 458 | einfo.addr = ofs; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 459 | einfo.len = 1ULL << chip->phys_erase_shift; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 460 | nand_erase_nand(mtd, &einfo, 0); |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 461 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 462 | /* Write bad block marker to OOB */ |
| 463 | nand_get_device(mtd, FL_WRITING); |
| 464 | ret = chip->block_markbad(mtd, ofs); |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 465 | nand_release_device(mtd); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 466 | } |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 467 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 468 | /* Mark block bad in BBT */ |
| 469 | if (chip->bbt) { |
| 470 | res = nand_markbad_bbt(mtd, ofs); |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 471 | if (!ret) |
| 472 | ret = res; |
| 473 | } |
| 474 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 475 | if (!ret) |
| 476 | mtd->ecc_stats.badblocks++; |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 477 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 478 | return ret; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 479 | } |
| 480 | |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 481 | /** |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 482 | * nand_check_wp - [GENERIC] check if the chip is write protected |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 483 | * @mtd: MTD device structure |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 484 | * |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 485 | * Check, if the device is write protected. The function expects, that the |
| 486 | * device is already selected. |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 487 | */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 488 | static int nand_check_wp(struct mtd_info *mtd) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 489 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 490 | struct nand_chip *chip = mtd_to_nand(mtd); |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 491 | u8 status; |
| 492 | int ret; |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 493 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 494 | /* Broken xD cards report WP despite being writable */ |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 495 | if (chip->options & NAND_BROKEN_XD) |
| 496 | return 0; |
| 497 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 498 | /* Check the WP bit */ |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 499 | ret = nand_status_op(chip, &status); |
| 500 | if (ret) |
| 501 | return ret; |
| 502 | |
| 503 | return status & NAND_STATUS_WP ? 0 : 1; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 504 | } |
Markus Klotzbücher | 27eba14 | 2006-03-06 15:04:25 +0100 | [diff] [blame] | 505 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 506 | /** |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 507 | * nand_block_isreserved - [GENERIC] Check if a block is marked reserved. |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 508 | * @mtd: MTD device structure |
| 509 | * @ofs: offset from device start |
Ezequiel Garcia | fc9d57c | 2014-05-21 19:06:12 -0300 | [diff] [blame] | 510 | * |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 511 | * Check if the block is marked as reserved. |
Ezequiel Garcia | fc9d57c | 2014-05-21 19:06:12 -0300 | [diff] [blame] | 512 | */ |
| 513 | static int nand_block_isreserved(struct mtd_info *mtd, loff_t ofs) |
| 514 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 515 | struct nand_chip *chip = mtd_to_nand(mtd); |
Ezequiel Garcia | fc9d57c | 2014-05-21 19:06:12 -0300 | [diff] [blame] | 516 | |
| 517 | if (!chip->bbt) |
| 518 | return 0; |
| 519 | /* Return info from the table */ |
| 520 | return nand_isreserved_bbt(mtd, ofs); |
| 521 | } |
| 522 | |
| 523 | /** |
| 524 | * nand_block_checkbad - [GENERIC] Check if a block is marked bad |
| 525 | * @mtd: MTD device structure |
| 526 | * @ofs: offset from device start |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 527 | * @allowbbt: 1, if its allowed to access the bbt area |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 528 | * |
| 529 | * Check, if the block is bad. Either by reading the bad block table or |
| 530 | * calling of the scan function. |
| 531 | */ |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 532 | static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int allowbbt) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 533 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 534 | struct nand_chip *chip = mtd_to_nand(mtd); |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 535 | |
Masahiro Yamada | 8d10054 | 2014-12-26 22:20:58 +0900 | [diff] [blame] | 536 | if (!(chip->options & NAND_SKIP_BBTSCAN) && |
| 537 | !(chip->options & NAND_BBT_SCANNED)) { |
Rostislav Lisovy | dc17bdc | 2014-10-22 13:40:44 +0200 | [diff] [blame] | 538 | chip->options |= NAND_BBT_SCANNED; |
Masahiro Yamada | 8c6c14a | 2014-12-26 22:20:57 +0900 | [diff] [blame] | 539 | chip->scan_bbt(mtd); |
Rostislav Lisovy | dc17bdc | 2014-10-22 13:40:44 +0200 | [diff] [blame] | 540 | } |
| 541 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 542 | if (!chip->bbt) |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 543 | return chip->block_bad(mtd, ofs); |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 544 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 545 | /* Return info from the table */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 546 | return nand_isbad_bbt(mtd, ofs, allowbbt); |
| 547 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 548 | |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 549 | /** |
| 550 | * nand_wait_ready - [GENERIC] Wait for the ready pin after commands. |
| 551 | * @mtd: MTD device structure |
| 552 | * |
| 553 | * Wait for the ready pin after a command, and warn if a timeout occurs. |
| 554 | */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 555 | void nand_wait_ready(struct mtd_info *mtd) |
| 556 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 557 | struct nand_chip *chip = mtd_to_nand(mtd); |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 558 | u32 timeo = (CONFIG_SYS_HZ * 400) / 1000; |
Reinhard Meyer | 4d1fb18 | 2010-11-18 03:14:26 +0000 | [diff] [blame] | 559 | u32 time_start; |
Stefan Roese | a5c312c | 2008-01-05 16:43:25 +0100 | [diff] [blame] | 560 | |
Reinhard Meyer | 4d1fb18 | 2010-11-18 03:14:26 +0000 | [diff] [blame] | 561 | time_start = get_timer(0); |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 562 | /* Wait until command is processed or timeout occurs */ |
Reinhard Meyer | 4d1fb18 | 2010-11-18 03:14:26 +0000 | [diff] [blame] | 563 | while (get_timer(time_start) < timeo) { |
Stefan Roese | a5c312c | 2008-01-05 16:43:25 +0100 | [diff] [blame] | 564 | if (chip->dev_ready) |
| 565 | if (chip->dev_ready(mtd)) |
| 566 | break; |
| 567 | } |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 568 | |
| 569 | if (!chip->dev_ready(mtd)) |
| 570 | pr_warn("timeout while waiting for chip to become ready\n"); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 571 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 572 | EXPORT_SYMBOL_GPL(nand_wait_ready); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 573 | |
| 574 | /** |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 575 | * nand_wait_status_ready - [GENERIC] Wait for the ready status after commands. |
| 576 | * @mtd: MTD device structure |
| 577 | * @timeo: Timeout in ms |
| 578 | * |
| 579 | * Wait for status ready (i.e. command done) or timeout. |
| 580 | */ |
| 581 | static void nand_wait_status_ready(struct mtd_info *mtd, unsigned long timeo) |
| 582 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 583 | register struct nand_chip *chip = mtd_to_nand(mtd); |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 584 | u32 time_start; |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 585 | int ret; |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 586 | |
| 587 | timeo = (CONFIG_SYS_HZ * timeo) / 1000; |
| 588 | time_start = get_timer(0); |
| 589 | while (get_timer(time_start) < timeo) { |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 590 | u8 status; |
| 591 | |
| 592 | ret = nand_read_data_op(chip, &status, sizeof(status), true); |
| 593 | if (ret) |
| 594 | return; |
| 595 | |
| 596 | if (status & NAND_STATUS_READY) |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 597 | break; |
| 598 | WATCHDOG_RESET(); |
| 599 | } |
| 600 | }; |
| 601 | |
| 602 | /** |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 603 | * nand_command - [DEFAULT] Send command to NAND device |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 604 | * @mtd: MTD device structure |
| 605 | * @command: the command to be sent |
| 606 | * @column: the column address for this command, -1 if none |
| 607 | * @page_addr: the page address for this command, -1 if none |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 608 | * |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 609 | * Send command to NAND device. This function is used for small page devices |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 610 | * (512 Bytes per page). |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 611 | */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 612 | static void nand_command(struct mtd_info *mtd, unsigned int command, |
| 613 | int column, int page_addr) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 614 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 615 | register struct nand_chip *chip = mtd_to_nand(mtd); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 616 | int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 617 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 618 | /* Write out the command to the device */ |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 619 | if (command == NAND_CMD_SEQIN) { |
| 620 | int readcmd; |
| 621 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 622 | if (column >= mtd->writesize) { |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 623 | /* OOB area */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 624 | column -= mtd->writesize; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 625 | readcmd = NAND_CMD_READOOB; |
| 626 | } else if (column < 256) { |
| 627 | /* First 256 bytes --> READ0 */ |
| 628 | readcmd = NAND_CMD_READ0; |
| 629 | } else { |
| 630 | column -= 256; |
| 631 | readcmd = NAND_CMD_READ1; |
| 632 | } |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 633 | chip->cmd_ctrl(mtd, readcmd, ctrl); |
| 634 | ctrl &= ~NAND_CTRL_CHANGE; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 635 | } |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 636 | chip->cmd_ctrl(mtd, command, ctrl); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 637 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 638 | /* Address cycle, when necessary */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 639 | ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE; |
| 640 | /* Serially input address */ |
| 641 | if (column != -1) { |
| 642 | /* Adjust columns for 16 bit buswidth */ |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 643 | if (chip->options & NAND_BUSWIDTH_16 && |
Brian Norris | 6767522 | 2014-05-06 00:46:17 +0530 | [diff] [blame] | 644 | !nand_opcode_8bits(command)) |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 645 | column >>= 1; |
| 646 | chip->cmd_ctrl(mtd, column, ctrl); |
| 647 | ctrl &= ~NAND_CTRL_CHANGE; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 648 | } |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 649 | if (page_addr != -1) { |
| 650 | chip->cmd_ctrl(mtd, page_addr, ctrl); |
| 651 | ctrl &= ~NAND_CTRL_CHANGE; |
| 652 | chip->cmd_ctrl(mtd, page_addr >> 8, ctrl); |
Masahiro Yamada | 984926b | 2017-11-22 02:38:31 +0900 | [diff] [blame] | 653 | if (chip->options & NAND_ROW_ADDR_3) |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 654 | chip->cmd_ctrl(mtd, page_addr >> 16, ctrl); |
| 655 | } |
| 656 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE); |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 657 | |
| 658 | /* |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 659 | * Program and erase have their own busy handlers status and sequential |
| 660 | * in needs no delay |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 661 | */ |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 662 | switch (command) { |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 663 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 664 | case NAND_CMD_PAGEPROG: |
| 665 | case NAND_CMD_ERASE1: |
| 666 | case NAND_CMD_ERASE2: |
| 667 | case NAND_CMD_SEQIN: |
| 668 | case NAND_CMD_STATUS: |
Masahiro Yamada | 7f9baa1 | 2017-09-15 21:44:58 +0900 | [diff] [blame] | 669 | case NAND_CMD_READID: |
Masahiro Yamada | 0cd1018 | 2017-09-15 21:44:59 +0900 | [diff] [blame] | 670 | case NAND_CMD_SET_FEATURES: |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 671 | return; |
| 672 | |
| 673 | case NAND_CMD_RESET: |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 674 | if (chip->dev_ready) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 675 | break; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 676 | udelay(chip->chip_delay); |
| 677 | chip->cmd_ctrl(mtd, NAND_CMD_STATUS, |
| 678 | NAND_CTRL_CLE | NAND_CTRL_CHANGE); |
| 679 | chip->cmd_ctrl(mtd, |
| 680 | NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE); |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 681 | /* EZ-NAND can take upto 250ms as per ONFi v4.0 */ |
| 682 | nand_wait_status_ready(mtd, 250); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 683 | return; |
| 684 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 685 | /* This applies to read commands */ |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 686 | default: |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 687 | /* |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 688 | * If we don't have access to the busy pin, we apply the given |
| 689 | * command delay |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 690 | */ |
| 691 | if (!chip->dev_ready) { |
| 692 | udelay(chip->chip_delay); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 693 | return; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 694 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 695 | } |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 696 | /* |
| 697 | * Apply this short delay always to ensure that we do wait tWB in |
| 698 | * any case on any machine. |
| 699 | */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 700 | ndelay(100); |
| 701 | |
| 702 | nand_wait_ready(mtd); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 703 | } |
| 704 | |
| 705 | /** |
| 706 | * nand_command_lp - [DEFAULT] Send command to NAND large page device |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 707 | * @mtd: MTD device structure |
| 708 | * @command: the command to be sent |
| 709 | * @column: the column address for this command, -1 if none |
| 710 | * @page_addr: the page address for this command, -1 if none |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 711 | * |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 712 | * Send command to NAND device. This is the version for the new large page |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 713 | * devices. We don't have the separate regions as we have in the small page |
| 714 | * devices. We must emulate NAND_CMD_READOOB to keep the code compatible. |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 715 | */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 716 | static void nand_command_lp(struct mtd_info *mtd, unsigned int command, |
| 717 | int column, int page_addr) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 718 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 719 | register struct nand_chip *chip = mtd_to_nand(mtd); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 720 | |
| 721 | /* Emulate NAND_CMD_READOOB */ |
| 722 | if (command == NAND_CMD_READOOB) { |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 723 | column += mtd->writesize; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 724 | command = NAND_CMD_READ0; |
| 725 | } |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 726 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 727 | /* Command latch cycle */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 728 | chip->cmd_ctrl(mtd, command, NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 729 | |
| 730 | if (column != -1 || page_addr != -1) { |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 731 | int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 732 | |
| 733 | /* Serially input address */ |
| 734 | if (column != -1) { |
| 735 | /* Adjust columns for 16 bit buswidth */ |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 736 | if (chip->options & NAND_BUSWIDTH_16 && |
Brian Norris | 6767522 | 2014-05-06 00:46:17 +0530 | [diff] [blame] | 737 | !nand_opcode_8bits(command)) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 738 | column >>= 1; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 739 | chip->cmd_ctrl(mtd, column, ctrl); |
| 740 | ctrl &= ~NAND_CTRL_CHANGE; |
| 741 | chip->cmd_ctrl(mtd, column >> 8, ctrl); |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 742 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 743 | if (page_addr != -1) { |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 744 | chip->cmd_ctrl(mtd, page_addr, ctrl); |
| 745 | chip->cmd_ctrl(mtd, page_addr >> 8, |
| 746 | NAND_NCE | NAND_ALE); |
Masahiro Yamada | 984926b | 2017-11-22 02:38:31 +0900 | [diff] [blame] | 747 | if (chip->options & NAND_ROW_ADDR_3) |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 748 | chip->cmd_ctrl(mtd, page_addr >> 16, |
| 749 | NAND_NCE | NAND_ALE); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 750 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 751 | } |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 752 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE); |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 753 | |
| 754 | /* |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 755 | * Program and erase have their own busy handlers status, sequential |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 756 | * in and status need no delay. |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 757 | */ |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 758 | switch (command) { |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 759 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 760 | case NAND_CMD_CACHEDPROG: |
| 761 | case NAND_CMD_PAGEPROG: |
| 762 | case NAND_CMD_ERASE1: |
| 763 | case NAND_CMD_ERASE2: |
| 764 | case NAND_CMD_SEQIN: |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 765 | case NAND_CMD_RNDIN: |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 766 | case NAND_CMD_STATUS: |
Masahiro Yamada | 7f9baa1 | 2017-09-15 21:44:58 +0900 | [diff] [blame] | 767 | case NAND_CMD_READID: |
Masahiro Yamada | 0cd1018 | 2017-09-15 21:44:59 +0900 | [diff] [blame] | 768 | case NAND_CMD_SET_FEATURES: |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 769 | return; |
| 770 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 771 | case NAND_CMD_RESET: |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 772 | if (chip->dev_ready) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 773 | break; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 774 | udelay(chip->chip_delay); |
| 775 | chip->cmd_ctrl(mtd, NAND_CMD_STATUS, |
| 776 | NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE); |
| 777 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, |
| 778 | NAND_NCE | NAND_CTRL_CHANGE); |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 779 | /* EZ-NAND can take upto 250ms as per ONFi v4.0 */ |
| 780 | nand_wait_status_ready(mtd, 250); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 781 | return; |
| 782 | |
| 783 | case NAND_CMD_RNDOUT: |
| 784 | /* No ready / busy check necessary */ |
| 785 | chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART, |
| 786 | NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE); |
| 787 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, |
| 788 | NAND_NCE | NAND_CTRL_CHANGE); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 789 | return; |
| 790 | |
| 791 | case NAND_CMD_READ0: |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 792 | chip->cmd_ctrl(mtd, NAND_CMD_READSTART, |
| 793 | NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE); |
| 794 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, |
| 795 | NAND_NCE | NAND_CTRL_CHANGE); |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 796 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 797 | /* This applies to read commands */ |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 798 | default: |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 799 | /* |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 800 | * If we don't have access to the busy pin, we apply the given |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 801 | * command delay. |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 802 | */ |
| 803 | if (!chip->dev_ready) { |
| 804 | udelay(chip->chip_delay); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 805 | return; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 806 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 807 | } |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 808 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 809 | /* |
| 810 | * Apply this short delay always to ensure that we do wait tWB in |
| 811 | * any case on any machine. |
| 812 | */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 813 | ndelay(100); |
| 814 | |
| 815 | nand_wait_ready(mtd); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 816 | } |
| 817 | |
| 818 | /** |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 819 | * panic_nand_get_device - [GENERIC] Get chip for selected access |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 820 | * @chip: the nand chip descriptor |
| 821 | * @mtd: MTD device structure |
| 822 | * @new_state: the state which is requested |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 823 | * |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 824 | * Used when in panic, no locks are taken. |
| 825 | */ |
| 826 | static void panic_nand_get_device(struct nand_chip *chip, |
| 827 | struct mtd_info *mtd, int new_state) |
| 828 | { |
| 829 | /* Hardware controller shared among independent devices */ |
| 830 | chip->controller->active = chip; |
| 831 | chip->state = new_state; |
| 832 | } |
| 833 | |
| 834 | /** |
| 835 | * nand_get_device - [GENERIC] Get chip for selected access |
| 836 | * @mtd: MTD device structure |
| 837 | * @new_state: the state which is requested |
| 838 | * |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 839 | * Get the device and lock it for exclusive access |
| 840 | */ |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 841 | static int |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 842 | nand_get_device(struct mtd_info *mtd, int new_state) |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 843 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 844 | struct nand_chip *chip = mtd_to_nand(mtd); |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 845 | chip->state = new_state; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 846 | return 0; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 847 | } |
| 848 | |
| 849 | /** |
| 850 | * panic_nand_wait - [GENERIC] wait until the command is done |
| 851 | * @mtd: MTD device structure |
| 852 | * @chip: NAND chip structure |
| 853 | * @timeo: timeout |
| 854 | * |
| 855 | * Wait for command done. This is a helper function for nand_wait used when |
| 856 | * we are in interrupt context. May happen when in panic and trying to write |
| 857 | * an oops through mtdoops. |
| 858 | */ |
| 859 | static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip, |
| 860 | unsigned long timeo) |
| 861 | { |
| 862 | int i; |
| 863 | for (i = 0; i < timeo; i++) { |
| 864 | if (chip->dev_ready) { |
| 865 | if (chip->dev_ready(mtd)) |
| 866 | break; |
| 867 | } else { |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 868 | int ret; |
| 869 | u8 status; |
| 870 | |
| 871 | ret = nand_read_data_op(chip, &status, sizeof(status), |
| 872 | true); |
| 873 | if (ret) |
| 874 | return; |
| 875 | |
| 876 | if (status & NAND_STATUS_READY) |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 877 | break; |
| 878 | } |
| 879 | mdelay(1); |
| 880 | } |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 881 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 882 | |
| 883 | /** |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 884 | * nand_wait - [DEFAULT] wait until the command is done |
| 885 | * @mtd: MTD device structure |
| 886 | * @chip: NAND chip structure |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 887 | * |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 888 | * Wait for command done. This applies to erase and program only. |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 889 | */ |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 890 | static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 891 | { |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 892 | unsigned long timeo = 400; |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 893 | u8 status; |
| 894 | int ret; |
Wolfgang Denk | 9a08dd1 | 2005-11-02 14:29:12 +0100 | [diff] [blame] | 895 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 896 | led_trigger_event(nand_led_trigger, LED_FULL); |
Wolfgang Denk | 9a08dd1 | 2005-11-02 14:29:12 +0100 | [diff] [blame] | 897 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 898 | /* |
| 899 | * Apply this short delay always to ensure that we do wait tWB in any |
| 900 | * case on any machine. |
| 901 | */ |
| 902 | ndelay(100); |
Wolfgang Denk | 9a08dd1 | 2005-11-02 14:29:12 +0100 | [diff] [blame] | 903 | |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 904 | ret = nand_status_op(chip, NULL); |
| 905 | if (ret) |
| 906 | return ret; |
Wolfgang Denk | 9a08dd1 | 2005-11-02 14:29:12 +0100 | [diff] [blame] | 907 | |
Wolfgang Denk | 62fb2b4 | 2021-09-27 17:42:39 +0200 | [diff] [blame] | 908 | u32 timer = (CONFIG_SYS_HZ * timeo) / 1000; |
| 909 | u32 time_start; |
Wolfgang Denk | 9d328a6 | 2021-09-27 17:42:38 +0200 | [diff] [blame] | 910 | |
Wolfgang Denk | 62fb2b4 | 2021-09-27 17:42:39 +0200 | [diff] [blame] | 911 | time_start = get_timer(0); |
| 912 | while (get_timer(time_start) < timer) { |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 913 | if (chip->dev_ready) { |
| 914 | if (chip->dev_ready(mtd)) |
Wolfgang Denk | 9a08dd1 | 2005-11-02 14:29:12 +0100 | [diff] [blame] | 915 | break; |
| 916 | } else { |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 917 | ret = nand_read_data_op(chip, &status, |
| 918 | sizeof(status), true); |
| 919 | if (ret) |
| 920 | return ret; |
| 921 | |
| 922 | if (status & NAND_STATUS_READY) |
Wolfgang Denk | 9a08dd1 | 2005-11-02 14:29:12 +0100 | [diff] [blame] | 923 | break; |
| 924 | } |
| 925 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 926 | led_trigger_event(nand_led_trigger, LED_OFF); |
Bartlomiej Sieka | 6eed2ab | 2006-02-24 09:37:22 +0100 | [diff] [blame] | 927 | |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 928 | ret = nand_read_data_op(chip, &status, sizeof(status), true); |
| 929 | if (ret) |
| 930 | return ret; |
| 931 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 932 | /* This can happen if in case of timeout or buggy dev_ready */ |
| 933 | WARN_ON(!(status & NAND_STATUS_READY)); |
| 934 | return status; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 935 | } |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 936 | |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 937 | /** |
Boris Brezillon | e509cba | 2017-11-22 02:38:19 +0900 | [diff] [blame] | 938 | * nand_reset_data_interface - Reset data interface and timings |
| 939 | * @chip: The NAND chip |
Boris Brezillon | 32935f4 | 2017-11-22 02:38:28 +0900 | [diff] [blame] | 940 | * @chipnr: Internal die id |
Boris Brezillon | e509cba | 2017-11-22 02:38:19 +0900 | [diff] [blame] | 941 | * |
| 942 | * Reset the Data interface and timings to ONFI mode 0. |
| 943 | * |
| 944 | * Returns 0 for success or negative error code otherwise. |
| 945 | */ |
Boris Brezillon | 32935f4 | 2017-11-22 02:38:28 +0900 | [diff] [blame] | 946 | static int nand_reset_data_interface(struct nand_chip *chip, int chipnr) |
Boris Brezillon | e509cba | 2017-11-22 02:38:19 +0900 | [diff] [blame] | 947 | { |
| 948 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 949 | const struct nand_data_interface *conf; |
| 950 | int ret; |
| 951 | |
| 952 | if (!chip->setup_data_interface) |
| 953 | return 0; |
| 954 | |
| 955 | /* |
| 956 | * The ONFI specification says: |
| 957 | * " |
| 958 | * To transition from NV-DDR or NV-DDR2 to the SDR data |
| 959 | * interface, the host shall use the Reset (FFh) command |
| 960 | * using SDR timing mode 0. A device in any timing mode is |
| 961 | * required to recognize Reset (FFh) command issued in SDR |
| 962 | * timing mode 0. |
| 963 | * " |
| 964 | * |
| 965 | * Configure the data interface in SDR mode and set the |
| 966 | * timings to timing mode 0. |
| 967 | */ |
| 968 | |
| 969 | conf = nand_get_default_data_interface(); |
Boris Brezillon | 32935f4 | 2017-11-22 02:38:28 +0900 | [diff] [blame] | 970 | ret = chip->setup_data_interface(mtd, chipnr, conf); |
Boris Brezillon | e509cba | 2017-11-22 02:38:19 +0900 | [diff] [blame] | 971 | if (ret) |
| 972 | pr_err("Failed to configure data interface to SDR timing mode 0\n"); |
| 973 | |
| 974 | return ret; |
| 975 | } |
| 976 | |
Kory Maincent | 0cda0cb | 2022-06-22 11:11:45 +0200 | [diff] [blame] | 977 | static int nand_onfi_set_timings(struct mtd_info *mtd, struct nand_chip *chip) |
| 978 | { |
| 979 | if (!chip->onfi_version || |
| 980 | !(le16_to_cpu(chip->onfi_params.opt_cmd) |
| 981 | & ONFI_OPT_CMD_SET_GET_FEATURES)) |
| 982 | return 0; |
| 983 | |
| 984 | u8 tmode_param[ONFI_SUBFEATURE_PARAM_LEN] = { |
| 985 | chip->onfi_timing_mode_default, |
| 986 | }; |
| 987 | |
| 988 | return chip->onfi_set_features(mtd, chip, |
| 989 | ONFI_FEATURE_ADDR_TIMING_MODE, |
| 990 | tmode_param); |
| 991 | } |
| 992 | |
Boris Brezillon | e509cba | 2017-11-22 02:38:19 +0900 | [diff] [blame] | 993 | /** |
| 994 | * nand_setup_data_interface - Setup the best data interface and timings |
| 995 | * @chip: The NAND chip |
Boris Brezillon | 32935f4 | 2017-11-22 02:38:28 +0900 | [diff] [blame] | 996 | * @chipnr: Internal die id |
Boris Brezillon | e509cba | 2017-11-22 02:38:19 +0900 | [diff] [blame] | 997 | * |
| 998 | * Find and configure the best data interface and NAND timings supported by |
| 999 | * the chip and the driver. |
| 1000 | * First tries to retrieve supported timing modes from ONFI information, |
| 1001 | * and if the NAND chip does not support ONFI, relies on the |
| 1002 | * ->onfi_timing_mode_default specified in the nand_ids table. |
| 1003 | * |
| 1004 | * Returns 0 for success or negative error code otherwise. |
| 1005 | */ |
Boris Brezillon | 32935f4 | 2017-11-22 02:38:28 +0900 | [diff] [blame] | 1006 | static int nand_setup_data_interface(struct nand_chip *chip, int chipnr) |
Boris Brezillon | e509cba | 2017-11-22 02:38:19 +0900 | [diff] [blame] | 1007 | { |
| 1008 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1009 | int ret; |
| 1010 | |
| 1011 | if (!chip->setup_data_interface || !chip->data_interface) |
| 1012 | return 0; |
| 1013 | |
| 1014 | /* |
| 1015 | * Ensure the timing mode has been changed on the chip side |
| 1016 | * before changing timings on the controller side. |
| 1017 | */ |
Kory Maincent | 0cda0cb | 2022-06-22 11:11:45 +0200 | [diff] [blame] | 1018 | ret = nand_onfi_set_timings(mtd, chip); |
| 1019 | if (ret) |
| 1020 | goto err; |
Boris Brezillon | e509cba | 2017-11-22 02:38:19 +0900 | [diff] [blame] | 1021 | |
Boris Brezillon | 32935f4 | 2017-11-22 02:38:28 +0900 | [diff] [blame] | 1022 | ret = chip->setup_data_interface(mtd, chipnr, chip->data_interface); |
Boris Brezillon | e509cba | 2017-11-22 02:38:19 +0900 | [diff] [blame] | 1023 | err: |
| 1024 | return ret; |
| 1025 | } |
| 1026 | |
| 1027 | /** |
| 1028 | * nand_init_data_interface - find the best data interface and timings |
| 1029 | * @chip: The NAND chip |
| 1030 | * |
| 1031 | * Find the best data interface and NAND timings supported by the chip |
| 1032 | * and the driver. |
| 1033 | * First tries to retrieve supported timing modes from ONFI information, |
| 1034 | * and if the NAND chip does not support ONFI, relies on the |
| 1035 | * ->onfi_timing_mode_default specified in the nand_ids table. After this |
| 1036 | * function nand_chip->data_interface is initialized with the best timing mode |
| 1037 | * available. |
| 1038 | * |
| 1039 | * Returns 0 for success or negative error code otherwise. |
| 1040 | */ |
| 1041 | static int nand_init_data_interface(struct nand_chip *chip) |
| 1042 | { |
| 1043 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1044 | int modes, mode, ret; |
| 1045 | |
| 1046 | if (!chip->setup_data_interface) |
| 1047 | return 0; |
| 1048 | |
| 1049 | /* |
| 1050 | * First try to identify the best timings from ONFI parameters and |
| 1051 | * if the NAND does not support ONFI, fallback to the default ONFI |
| 1052 | * timing mode. |
| 1053 | */ |
| 1054 | modes = onfi_get_async_timing_mode(chip); |
| 1055 | if (modes == ONFI_TIMING_MODE_UNKNOWN) { |
| 1056 | if (!chip->onfi_timing_mode_default) |
| 1057 | return 0; |
| 1058 | |
| 1059 | modes = GENMASK(chip->onfi_timing_mode_default, 0); |
| 1060 | } |
| 1061 | |
| 1062 | chip->data_interface = kzalloc(sizeof(*chip->data_interface), |
| 1063 | GFP_KERNEL); |
| 1064 | if (!chip->data_interface) |
| 1065 | return -ENOMEM; |
| 1066 | |
| 1067 | for (mode = fls(modes) - 1; mode >= 0; mode--) { |
| 1068 | ret = onfi_init_data_interface(chip, chip->data_interface, |
| 1069 | NAND_SDR_IFACE, mode); |
| 1070 | if (ret) |
| 1071 | continue; |
| 1072 | |
Boris Brezillon | 32935f4 | 2017-11-22 02:38:28 +0900 | [diff] [blame] | 1073 | /* Pass -1 to only */ |
| 1074 | ret = chip->setup_data_interface(mtd, |
| 1075 | NAND_DATA_IFACE_CHECK_ONLY, |
| 1076 | chip->data_interface); |
Boris Brezillon | e509cba | 2017-11-22 02:38:19 +0900 | [diff] [blame] | 1077 | if (!ret) { |
| 1078 | chip->onfi_timing_mode_default = mode; |
| 1079 | break; |
| 1080 | } |
| 1081 | } |
| 1082 | |
| 1083 | return 0; |
| 1084 | } |
| 1085 | |
| 1086 | static void __maybe_unused nand_release_data_interface(struct nand_chip *chip) |
| 1087 | { |
| 1088 | kfree(chip->data_interface); |
| 1089 | } |
| 1090 | |
| 1091 | /** |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 1092 | * nand_read_page_op - Do a READ PAGE operation |
| 1093 | * @chip: The NAND chip |
| 1094 | * @page: page to read |
| 1095 | * @offset_in_page: offset within the page |
| 1096 | * @buf: buffer used to store the data |
| 1097 | * @len: length of the buffer |
| 1098 | * |
| 1099 | * This function issues a READ PAGE operation. |
| 1100 | * This function does not select/unselect the CS line. |
| 1101 | * |
| 1102 | * Returns 0 on success, a negative error code otherwise. |
| 1103 | */ |
| 1104 | int nand_read_page_op(struct nand_chip *chip, unsigned int page, |
| 1105 | unsigned int offset_in_page, void *buf, unsigned int len) |
| 1106 | { |
| 1107 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1108 | |
| 1109 | if (len && !buf) |
| 1110 | return -EINVAL; |
| 1111 | |
| 1112 | if (offset_in_page + len > mtd->writesize + mtd->oobsize) |
| 1113 | return -EINVAL; |
| 1114 | |
| 1115 | chip->cmdfunc(mtd, NAND_CMD_READ0, offset_in_page, page); |
| 1116 | if (len) |
| 1117 | chip->read_buf(mtd, buf, len); |
| 1118 | |
| 1119 | return 0; |
| 1120 | } |
| 1121 | EXPORT_SYMBOL_GPL(nand_read_page_op); |
| 1122 | |
| 1123 | /** |
| 1124 | * nand_read_param_page_op - Do a READ PARAMETER PAGE operation |
| 1125 | * @chip: The NAND chip |
| 1126 | * @page: parameter page to read |
| 1127 | * @buf: buffer used to store the data |
| 1128 | * @len: length of the buffer |
| 1129 | * |
| 1130 | * This function issues a READ PARAMETER PAGE operation. |
| 1131 | * This function does not select/unselect the CS line. |
| 1132 | * |
| 1133 | * Returns 0 on success, a negative error code otherwise. |
| 1134 | */ |
| 1135 | static int nand_read_param_page_op(struct nand_chip *chip, u8 page, void *buf, |
| 1136 | unsigned int len) |
| 1137 | { |
| 1138 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1139 | unsigned int i; |
| 1140 | u8 *p = buf; |
| 1141 | |
| 1142 | if (len && !buf) |
| 1143 | return -EINVAL; |
| 1144 | |
| 1145 | chip->cmdfunc(mtd, NAND_CMD_PARAM, page, -1); |
| 1146 | for (i = 0; i < len; i++) |
| 1147 | p[i] = chip->read_byte(mtd); |
| 1148 | |
| 1149 | return 0; |
| 1150 | } |
| 1151 | |
| 1152 | /** |
| 1153 | * nand_change_read_column_op - Do a CHANGE READ COLUMN operation |
| 1154 | * @chip: The NAND chip |
| 1155 | * @offset_in_page: offset within the page |
| 1156 | * @buf: buffer used to store the data |
| 1157 | * @len: length of the buffer |
| 1158 | * @force_8bit: force 8-bit bus access |
| 1159 | * |
| 1160 | * This function issues a CHANGE READ COLUMN operation. |
| 1161 | * This function does not select/unselect the CS line. |
| 1162 | * |
| 1163 | * Returns 0 on success, a negative error code otherwise. |
| 1164 | */ |
| 1165 | int nand_change_read_column_op(struct nand_chip *chip, |
| 1166 | unsigned int offset_in_page, void *buf, |
| 1167 | unsigned int len, bool force_8bit) |
| 1168 | { |
| 1169 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1170 | |
| 1171 | if (len && !buf) |
| 1172 | return -EINVAL; |
| 1173 | |
| 1174 | if (offset_in_page + len > mtd->writesize + mtd->oobsize) |
| 1175 | return -EINVAL; |
| 1176 | |
| 1177 | chip->cmdfunc(mtd, NAND_CMD_RNDOUT, offset_in_page, -1); |
| 1178 | if (len) |
| 1179 | chip->read_buf(mtd, buf, len); |
| 1180 | |
| 1181 | return 0; |
| 1182 | } |
| 1183 | EXPORT_SYMBOL_GPL(nand_change_read_column_op); |
| 1184 | |
| 1185 | /** |
| 1186 | * nand_read_oob_op - Do a READ OOB operation |
| 1187 | * @chip: The NAND chip |
| 1188 | * @page: page to read |
| 1189 | * @offset_in_oob: offset within the OOB area |
| 1190 | * @buf: buffer used to store the data |
| 1191 | * @len: length of the buffer |
| 1192 | * |
| 1193 | * This function issues a READ OOB operation. |
| 1194 | * This function does not select/unselect the CS line. |
| 1195 | * |
| 1196 | * Returns 0 on success, a negative error code otherwise. |
| 1197 | */ |
| 1198 | int nand_read_oob_op(struct nand_chip *chip, unsigned int page, |
| 1199 | unsigned int offset_in_oob, void *buf, unsigned int len) |
| 1200 | { |
| 1201 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1202 | |
| 1203 | if (len && !buf) |
| 1204 | return -EINVAL; |
| 1205 | |
| 1206 | if (offset_in_oob + len > mtd->oobsize) |
| 1207 | return -EINVAL; |
| 1208 | |
| 1209 | chip->cmdfunc(mtd, NAND_CMD_READOOB, offset_in_oob, page); |
| 1210 | if (len) |
| 1211 | chip->read_buf(mtd, buf, len); |
| 1212 | |
| 1213 | return 0; |
| 1214 | } |
| 1215 | EXPORT_SYMBOL_GPL(nand_read_oob_op); |
| 1216 | |
| 1217 | /** |
| 1218 | * nand_prog_page_begin_op - starts a PROG PAGE operation |
| 1219 | * @chip: The NAND chip |
| 1220 | * @page: page to write |
| 1221 | * @offset_in_page: offset within the page |
| 1222 | * @buf: buffer containing the data to write to the page |
| 1223 | * @len: length of the buffer |
| 1224 | * |
| 1225 | * This function issues the first half of a PROG PAGE operation. |
| 1226 | * This function does not select/unselect the CS line. |
| 1227 | * |
| 1228 | * Returns 0 on success, a negative error code otherwise. |
| 1229 | */ |
| 1230 | int nand_prog_page_begin_op(struct nand_chip *chip, unsigned int page, |
| 1231 | unsigned int offset_in_page, const void *buf, |
| 1232 | unsigned int len) |
| 1233 | { |
| 1234 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1235 | |
| 1236 | if (len && !buf) |
| 1237 | return -EINVAL; |
| 1238 | |
| 1239 | if (offset_in_page + len > mtd->writesize + mtd->oobsize) |
| 1240 | return -EINVAL; |
| 1241 | |
| 1242 | chip->cmdfunc(mtd, NAND_CMD_SEQIN, offset_in_page, page); |
| 1243 | |
| 1244 | if (buf) |
| 1245 | chip->write_buf(mtd, buf, len); |
| 1246 | |
| 1247 | return 0; |
| 1248 | } |
| 1249 | EXPORT_SYMBOL_GPL(nand_prog_page_begin_op); |
| 1250 | |
| 1251 | /** |
| 1252 | * nand_prog_page_end_op - ends a PROG PAGE operation |
| 1253 | * @chip: The NAND chip |
| 1254 | * |
| 1255 | * This function issues the second half of a PROG PAGE operation. |
| 1256 | * This function does not select/unselect the CS line. |
| 1257 | * |
| 1258 | * Returns 0 on success, a negative error code otherwise. |
| 1259 | */ |
| 1260 | int nand_prog_page_end_op(struct nand_chip *chip) |
| 1261 | { |
| 1262 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1263 | int status; |
| 1264 | |
| 1265 | chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1); |
| 1266 | |
| 1267 | status = chip->waitfunc(mtd, chip); |
| 1268 | if (status & NAND_STATUS_FAIL) |
| 1269 | return -EIO; |
| 1270 | |
| 1271 | return 0; |
| 1272 | } |
| 1273 | EXPORT_SYMBOL_GPL(nand_prog_page_end_op); |
| 1274 | |
| 1275 | /** |
| 1276 | * nand_prog_page_op - Do a full PROG PAGE operation |
| 1277 | * @chip: The NAND chip |
| 1278 | * @page: page to write |
| 1279 | * @offset_in_page: offset within the page |
| 1280 | * @buf: buffer containing the data to write to the page |
| 1281 | * @len: length of the buffer |
| 1282 | * |
| 1283 | * This function issues a full PROG PAGE operation. |
| 1284 | * This function does not select/unselect the CS line. |
| 1285 | * |
| 1286 | * Returns 0 on success, a negative error code otherwise. |
| 1287 | */ |
| 1288 | int nand_prog_page_op(struct nand_chip *chip, unsigned int page, |
| 1289 | unsigned int offset_in_page, const void *buf, |
| 1290 | unsigned int len) |
| 1291 | { |
| 1292 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1293 | int status; |
| 1294 | |
| 1295 | if (!len || !buf) |
| 1296 | return -EINVAL; |
| 1297 | |
| 1298 | if (offset_in_page + len > mtd->writesize + mtd->oobsize) |
| 1299 | return -EINVAL; |
| 1300 | |
| 1301 | chip->cmdfunc(mtd, NAND_CMD_SEQIN, offset_in_page, page); |
| 1302 | chip->write_buf(mtd, buf, len); |
| 1303 | chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1); |
| 1304 | |
| 1305 | status = chip->waitfunc(mtd, chip); |
| 1306 | if (status & NAND_STATUS_FAIL) |
| 1307 | return -EIO; |
| 1308 | |
| 1309 | return 0; |
| 1310 | } |
| 1311 | EXPORT_SYMBOL_GPL(nand_prog_page_op); |
| 1312 | |
| 1313 | /** |
| 1314 | * nand_change_write_column_op - Do a CHANGE WRITE COLUMN operation |
| 1315 | * @chip: The NAND chip |
| 1316 | * @offset_in_page: offset within the page |
| 1317 | * @buf: buffer containing the data to send to the NAND |
| 1318 | * @len: length of the buffer |
| 1319 | * @force_8bit: force 8-bit bus access |
| 1320 | * |
| 1321 | * This function issues a CHANGE WRITE COLUMN operation. |
| 1322 | * This function does not select/unselect the CS line. |
| 1323 | * |
| 1324 | * Returns 0 on success, a negative error code otherwise. |
| 1325 | */ |
| 1326 | int nand_change_write_column_op(struct nand_chip *chip, |
| 1327 | unsigned int offset_in_page, |
| 1328 | const void *buf, unsigned int len, |
| 1329 | bool force_8bit) |
| 1330 | { |
| 1331 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1332 | |
| 1333 | if (len && !buf) |
| 1334 | return -EINVAL; |
| 1335 | |
| 1336 | if (offset_in_page + len > mtd->writesize + mtd->oobsize) |
| 1337 | return -EINVAL; |
| 1338 | |
| 1339 | chip->cmdfunc(mtd, NAND_CMD_RNDIN, offset_in_page, -1); |
| 1340 | if (len) |
| 1341 | chip->write_buf(mtd, buf, len); |
| 1342 | |
| 1343 | return 0; |
| 1344 | } |
| 1345 | EXPORT_SYMBOL_GPL(nand_change_write_column_op); |
| 1346 | |
| 1347 | /** |
| 1348 | * nand_readid_op - Do a READID operation |
| 1349 | * @chip: The NAND chip |
| 1350 | * @addr: address cycle to pass after the READID command |
| 1351 | * @buf: buffer used to store the ID |
| 1352 | * @len: length of the buffer |
| 1353 | * |
| 1354 | * This function sends a READID command and reads back the ID returned by the |
| 1355 | * NAND. |
| 1356 | * This function does not select/unselect the CS line. |
| 1357 | * |
| 1358 | * Returns 0 on success, a negative error code otherwise. |
| 1359 | */ |
| 1360 | int nand_readid_op(struct nand_chip *chip, u8 addr, void *buf, |
| 1361 | unsigned int len) |
| 1362 | { |
| 1363 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1364 | unsigned int i; |
| 1365 | u8 *id = buf; |
| 1366 | |
| 1367 | if (len && !buf) |
| 1368 | return -EINVAL; |
| 1369 | |
| 1370 | chip->cmdfunc(mtd, NAND_CMD_READID, addr, -1); |
| 1371 | |
| 1372 | for (i = 0; i < len; i++) |
| 1373 | id[i] = chip->read_byte(mtd); |
| 1374 | |
| 1375 | return 0; |
| 1376 | } |
| 1377 | EXPORT_SYMBOL_GPL(nand_readid_op); |
| 1378 | |
| 1379 | /** |
| 1380 | * nand_status_op - Do a STATUS operation |
| 1381 | * @chip: The NAND chip |
| 1382 | * @status: out variable to store the NAND status |
| 1383 | * |
| 1384 | * This function sends a STATUS command and reads back the status returned by |
| 1385 | * the NAND. |
| 1386 | * This function does not select/unselect the CS line. |
| 1387 | * |
| 1388 | * Returns 0 on success, a negative error code otherwise. |
| 1389 | */ |
| 1390 | int nand_status_op(struct nand_chip *chip, u8 *status) |
| 1391 | { |
| 1392 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1393 | |
| 1394 | chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1); |
| 1395 | if (status) |
| 1396 | *status = chip->read_byte(mtd); |
| 1397 | |
| 1398 | return 0; |
| 1399 | } |
| 1400 | EXPORT_SYMBOL_GPL(nand_status_op); |
| 1401 | |
| 1402 | /** |
| 1403 | * nand_exit_status_op - Exit a STATUS operation |
| 1404 | * @chip: The NAND chip |
| 1405 | * |
| 1406 | * This function sends a READ0 command to cancel the effect of the STATUS |
| 1407 | * command to avoid reading only the status until a new read command is sent. |
| 1408 | * |
| 1409 | * This function does not select/unselect the CS line. |
| 1410 | * |
| 1411 | * Returns 0 on success, a negative error code otherwise. |
| 1412 | */ |
| 1413 | int nand_exit_status_op(struct nand_chip *chip) |
| 1414 | { |
| 1415 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1416 | |
| 1417 | chip->cmdfunc(mtd, NAND_CMD_READ0, -1, -1); |
| 1418 | |
| 1419 | return 0; |
| 1420 | } |
| 1421 | EXPORT_SYMBOL_GPL(nand_exit_status_op); |
| 1422 | |
| 1423 | /** |
| 1424 | * nand_erase_op - Do an erase operation |
| 1425 | * @chip: The NAND chip |
| 1426 | * @eraseblock: block to erase |
| 1427 | * |
| 1428 | * This function sends an ERASE command and waits for the NAND to be ready |
| 1429 | * before returning. |
| 1430 | * This function does not select/unselect the CS line. |
| 1431 | * |
| 1432 | * Returns 0 on success, a negative error code otherwise. |
| 1433 | */ |
| 1434 | int nand_erase_op(struct nand_chip *chip, unsigned int eraseblock) |
| 1435 | { |
| 1436 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1437 | unsigned int page = eraseblock << |
| 1438 | (chip->phys_erase_shift - chip->page_shift); |
| 1439 | int status; |
| 1440 | |
| 1441 | chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page); |
| 1442 | chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1); |
| 1443 | |
| 1444 | status = chip->waitfunc(mtd, chip); |
| 1445 | if (status < 0) |
| 1446 | return status; |
| 1447 | |
| 1448 | if (status & NAND_STATUS_FAIL) |
| 1449 | return -EIO; |
| 1450 | |
| 1451 | return 0; |
| 1452 | } |
| 1453 | EXPORT_SYMBOL_GPL(nand_erase_op); |
| 1454 | |
| 1455 | /** |
| 1456 | * nand_set_features_op - Do a SET FEATURES operation |
| 1457 | * @chip: The NAND chip |
| 1458 | * @feature: feature id |
| 1459 | * @data: 4 bytes of data |
| 1460 | * |
| 1461 | * This function sends a SET FEATURES command and waits for the NAND to be |
| 1462 | * ready before returning. |
| 1463 | * This function does not select/unselect the CS line. |
| 1464 | * |
| 1465 | * Returns 0 on success, a negative error code otherwise. |
| 1466 | */ |
| 1467 | static int nand_set_features_op(struct nand_chip *chip, u8 feature, |
| 1468 | const void *data) |
| 1469 | { |
| 1470 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1471 | const u8 *params = data; |
| 1472 | int i, status; |
| 1473 | |
| 1474 | chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, feature, -1); |
| 1475 | for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i) |
| 1476 | chip->write_byte(mtd, params[i]); |
| 1477 | |
| 1478 | status = chip->waitfunc(mtd, chip); |
| 1479 | if (status & NAND_STATUS_FAIL) |
| 1480 | return -EIO; |
| 1481 | |
| 1482 | return 0; |
| 1483 | } |
| 1484 | |
| 1485 | /** |
| 1486 | * nand_get_features_op - Do a GET FEATURES operation |
| 1487 | * @chip: The NAND chip |
| 1488 | * @feature: feature id |
| 1489 | * @data: 4 bytes of data |
| 1490 | * |
| 1491 | * This function sends a GET FEATURES command and waits for the NAND to be |
| 1492 | * ready before returning. |
| 1493 | * This function does not select/unselect the CS line. |
| 1494 | * |
| 1495 | * Returns 0 on success, a negative error code otherwise. |
| 1496 | */ |
| 1497 | static int nand_get_features_op(struct nand_chip *chip, u8 feature, |
| 1498 | void *data) |
| 1499 | { |
| 1500 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1501 | u8 *params = data; |
| 1502 | int i; |
| 1503 | |
| 1504 | chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, feature, -1); |
| 1505 | for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i) |
| 1506 | params[i] = chip->read_byte(mtd); |
| 1507 | |
| 1508 | return 0; |
| 1509 | } |
| 1510 | |
| 1511 | /** |
| 1512 | * nand_reset_op - Do a reset operation |
| 1513 | * @chip: The NAND chip |
| 1514 | * |
| 1515 | * This function sends a RESET command and waits for the NAND to be ready |
| 1516 | * before returning. |
| 1517 | * This function does not select/unselect the CS line. |
| 1518 | * |
| 1519 | * Returns 0 on success, a negative error code otherwise. |
| 1520 | */ |
| 1521 | int nand_reset_op(struct nand_chip *chip) |
| 1522 | { |
| 1523 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1524 | |
| 1525 | chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1); |
| 1526 | |
| 1527 | return 0; |
| 1528 | } |
| 1529 | EXPORT_SYMBOL_GPL(nand_reset_op); |
| 1530 | |
| 1531 | /** |
| 1532 | * nand_read_data_op - Read data from the NAND |
| 1533 | * @chip: The NAND chip |
| 1534 | * @buf: buffer used to store the data |
| 1535 | * @len: length of the buffer |
| 1536 | * @force_8bit: force 8-bit bus access |
| 1537 | * |
| 1538 | * This function does a raw data read on the bus. Usually used after launching |
| 1539 | * another NAND operation like nand_read_page_op(). |
| 1540 | * This function does not select/unselect the CS line. |
| 1541 | * |
| 1542 | * Returns 0 on success, a negative error code otherwise. |
| 1543 | */ |
| 1544 | int nand_read_data_op(struct nand_chip *chip, void *buf, unsigned int len, |
| 1545 | bool force_8bit) |
| 1546 | { |
| 1547 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1548 | |
| 1549 | if (!len || !buf) |
| 1550 | return -EINVAL; |
| 1551 | |
| 1552 | if (force_8bit) { |
| 1553 | u8 *p = buf; |
| 1554 | unsigned int i; |
| 1555 | |
| 1556 | for (i = 0; i < len; i++) |
| 1557 | p[i] = chip->read_byte(mtd); |
| 1558 | } else { |
| 1559 | chip->read_buf(mtd, buf, len); |
| 1560 | } |
| 1561 | |
| 1562 | return 0; |
| 1563 | } |
| 1564 | EXPORT_SYMBOL_GPL(nand_read_data_op); |
| 1565 | |
| 1566 | /** |
| 1567 | * nand_write_data_op - Write data from the NAND |
| 1568 | * @chip: The NAND chip |
| 1569 | * @buf: buffer containing the data to send on the bus |
| 1570 | * @len: length of the buffer |
| 1571 | * @force_8bit: force 8-bit bus access |
| 1572 | * |
| 1573 | * This function does a raw data write on the bus. Usually used after launching |
| 1574 | * another NAND operation like nand_write_page_begin_op(). |
| 1575 | * This function does not select/unselect the CS line. |
| 1576 | * |
| 1577 | * Returns 0 on success, a negative error code otherwise. |
| 1578 | */ |
| 1579 | int nand_write_data_op(struct nand_chip *chip, const void *buf, |
| 1580 | unsigned int len, bool force_8bit) |
| 1581 | { |
| 1582 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1583 | |
| 1584 | if (!len || !buf) |
| 1585 | return -EINVAL; |
| 1586 | |
| 1587 | if (force_8bit) { |
| 1588 | const u8 *p = buf; |
| 1589 | unsigned int i; |
| 1590 | |
| 1591 | for (i = 0; i < len; i++) |
| 1592 | chip->write_byte(mtd, p[i]); |
| 1593 | } else { |
| 1594 | chip->write_buf(mtd, buf, len); |
| 1595 | } |
| 1596 | |
| 1597 | return 0; |
| 1598 | } |
| 1599 | EXPORT_SYMBOL_GPL(nand_write_data_op); |
| 1600 | |
| 1601 | /** |
Sascha Hauer | 44ad3b9 | 2017-11-22 02:38:15 +0900 | [diff] [blame] | 1602 | * nand_reset - Reset and initialize a NAND device |
| 1603 | * @chip: The NAND chip |
Boris Brezillon | 7ec6dc5 | 2017-11-22 02:38:20 +0900 | [diff] [blame] | 1604 | * @chipnr: Internal die id |
Sascha Hauer | 44ad3b9 | 2017-11-22 02:38:15 +0900 | [diff] [blame] | 1605 | * |
| 1606 | * Returns 0 for success or negative error code otherwise |
| 1607 | */ |
Boris Brezillon | 7ec6dc5 | 2017-11-22 02:38:20 +0900 | [diff] [blame] | 1608 | int nand_reset(struct nand_chip *chip, int chipnr) |
Sascha Hauer | 44ad3b9 | 2017-11-22 02:38:15 +0900 | [diff] [blame] | 1609 | { |
| 1610 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | e509cba | 2017-11-22 02:38:19 +0900 | [diff] [blame] | 1611 | int ret; |
| 1612 | |
Boris Brezillon | 32935f4 | 2017-11-22 02:38:28 +0900 | [diff] [blame] | 1613 | ret = nand_reset_data_interface(chip, chipnr); |
Boris Brezillon | e509cba | 2017-11-22 02:38:19 +0900 | [diff] [blame] | 1614 | if (ret) |
| 1615 | return ret; |
Sascha Hauer | 44ad3b9 | 2017-11-22 02:38:15 +0900 | [diff] [blame] | 1616 | |
Boris Brezillon | 7ec6dc5 | 2017-11-22 02:38:20 +0900 | [diff] [blame] | 1617 | /* |
| 1618 | * The CS line has to be released before we can apply the new NAND |
| 1619 | * interface settings, hence this weird ->select_chip() dance. |
| 1620 | */ |
| 1621 | chip->select_chip(mtd, chipnr); |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 1622 | ret = nand_reset_op(chip); |
Boris Brezillon | 7ec6dc5 | 2017-11-22 02:38:20 +0900 | [diff] [blame] | 1623 | chip->select_chip(mtd, -1); |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 1624 | if (ret) |
| 1625 | return ret; |
Sascha Hauer | 44ad3b9 | 2017-11-22 02:38:15 +0900 | [diff] [blame] | 1626 | |
Boris Brezillon | 7ec6dc5 | 2017-11-22 02:38:20 +0900 | [diff] [blame] | 1627 | chip->select_chip(mtd, chipnr); |
Boris Brezillon | 32935f4 | 2017-11-22 02:38:28 +0900 | [diff] [blame] | 1628 | ret = nand_setup_data_interface(chip, chipnr); |
Boris Brezillon | 7ec6dc5 | 2017-11-22 02:38:20 +0900 | [diff] [blame] | 1629 | chip->select_chip(mtd, -1); |
Boris Brezillon | e509cba | 2017-11-22 02:38:19 +0900 | [diff] [blame] | 1630 | if (ret) |
| 1631 | return ret; |
| 1632 | |
Sascha Hauer | 44ad3b9 | 2017-11-22 02:38:15 +0900 | [diff] [blame] | 1633 | return 0; |
| 1634 | } |
| 1635 | |
| 1636 | /** |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 1637 | * nand_check_erased_buf - check if a buffer contains (almost) only 0xff data |
| 1638 | * @buf: buffer to test |
| 1639 | * @len: buffer length |
| 1640 | * @bitflips_threshold: maximum number of bitflips |
| 1641 | * |
| 1642 | * Check if a buffer contains only 0xff, which means the underlying region |
| 1643 | * has been erased and is ready to be programmed. |
| 1644 | * The bitflips_threshold specify the maximum number of bitflips before |
| 1645 | * considering the region is not erased. |
| 1646 | * Note: The logic of this function has been extracted from the memweight |
| 1647 | * implementation, except that nand_check_erased_buf function exit before |
| 1648 | * testing the whole buffer if the number of bitflips exceed the |
| 1649 | * bitflips_threshold value. |
| 1650 | * |
| 1651 | * Returns a positive number of bitflips less than or equal to |
| 1652 | * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the |
| 1653 | * threshold. |
| 1654 | */ |
| 1655 | static int nand_check_erased_buf(void *buf, int len, int bitflips_threshold) |
| 1656 | { |
| 1657 | const unsigned char *bitmap = buf; |
| 1658 | int bitflips = 0; |
| 1659 | int weight; |
| 1660 | |
| 1661 | for (; len && ((uintptr_t)bitmap) % sizeof(long); |
| 1662 | len--, bitmap++) { |
| 1663 | weight = hweight8(*bitmap); |
| 1664 | bitflips += BITS_PER_BYTE - weight; |
| 1665 | if (unlikely(bitflips > bitflips_threshold)) |
| 1666 | return -EBADMSG; |
| 1667 | } |
| 1668 | |
| 1669 | for (; len >= 4; len -= 4, bitmap += 4) { |
| 1670 | weight = hweight32(*((u32 *)bitmap)); |
| 1671 | bitflips += 32 - weight; |
| 1672 | if (unlikely(bitflips > bitflips_threshold)) |
| 1673 | return -EBADMSG; |
| 1674 | } |
| 1675 | |
| 1676 | for (; len > 0; len--, bitmap++) { |
| 1677 | weight = hweight8(*bitmap); |
| 1678 | bitflips += BITS_PER_BYTE - weight; |
| 1679 | if (unlikely(bitflips > bitflips_threshold)) |
| 1680 | return -EBADMSG; |
| 1681 | } |
| 1682 | |
| 1683 | return bitflips; |
| 1684 | } |
| 1685 | |
| 1686 | /** |
| 1687 | * nand_check_erased_ecc_chunk - check if an ECC chunk contains (almost) only |
| 1688 | * 0xff data |
| 1689 | * @data: data buffer to test |
| 1690 | * @datalen: data length |
| 1691 | * @ecc: ECC buffer |
| 1692 | * @ecclen: ECC length |
| 1693 | * @extraoob: extra OOB buffer |
| 1694 | * @extraooblen: extra OOB length |
| 1695 | * @bitflips_threshold: maximum number of bitflips |
| 1696 | * |
| 1697 | * Check if a data buffer and its associated ECC and OOB data contains only |
| 1698 | * 0xff pattern, which means the underlying region has been erased and is |
| 1699 | * ready to be programmed. |
| 1700 | * The bitflips_threshold specify the maximum number of bitflips before |
| 1701 | * considering the region as not erased. |
| 1702 | * |
| 1703 | * Note: |
| 1704 | * 1/ ECC algorithms are working on pre-defined block sizes which are usually |
| 1705 | * different from the NAND page size. When fixing bitflips, ECC engines will |
| 1706 | * report the number of errors per chunk, and the NAND core infrastructure |
| 1707 | * expect you to return the maximum number of bitflips for the whole page. |
| 1708 | * This is why you should always use this function on a single chunk and |
| 1709 | * not on the whole page. After checking each chunk you should update your |
| 1710 | * max_bitflips value accordingly. |
| 1711 | * 2/ When checking for bitflips in erased pages you should not only check |
| 1712 | * the payload data but also their associated ECC data, because a user might |
| 1713 | * have programmed almost all bits to 1 but a few. In this case, we |
| 1714 | * shouldn't consider the chunk as erased, and checking ECC bytes prevent |
| 1715 | * this case. |
| 1716 | * 3/ The extraoob argument is optional, and should be used if some of your OOB |
| 1717 | * data are protected by the ECC engine. |
| 1718 | * It could also be used if you support subpages and want to attach some |
| 1719 | * extra OOB data to an ECC chunk. |
| 1720 | * |
| 1721 | * Returns a positive number of bitflips less than or equal to |
| 1722 | * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the |
| 1723 | * threshold. In case of success, the passed buffers are filled with 0xff. |
| 1724 | */ |
| 1725 | int nand_check_erased_ecc_chunk(void *data, int datalen, |
| 1726 | void *ecc, int ecclen, |
| 1727 | void *extraoob, int extraooblen, |
| 1728 | int bitflips_threshold) |
| 1729 | { |
| 1730 | int data_bitflips = 0, ecc_bitflips = 0, extraoob_bitflips = 0; |
| 1731 | |
| 1732 | data_bitflips = nand_check_erased_buf(data, datalen, |
| 1733 | bitflips_threshold); |
| 1734 | if (data_bitflips < 0) |
| 1735 | return data_bitflips; |
| 1736 | |
| 1737 | bitflips_threshold -= data_bitflips; |
| 1738 | |
| 1739 | ecc_bitflips = nand_check_erased_buf(ecc, ecclen, bitflips_threshold); |
| 1740 | if (ecc_bitflips < 0) |
| 1741 | return ecc_bitflips; |
| 1742 | |
| 1743 | bitflips_threshold -= ecc_bitflips; |
| 1744 | |
| 1745 | extraoob_bitflips = nand_check_erased_buf(extraoob, extraooblen, |
| 1746 | bitflips_threshold); |
| 1747 | if (extraoob_bitflips < 0) |
| 1748 | return extraoob_bitflips; |
| 1749 | |
| 1750 | if (data_bitflips) |
| 1751 | memset(data, 0xff, datalen); |
| 1752 | |
| 1753 | if (ecc_bitflips) |
| 1754 | memset(ecc, 0xff, ecclen); |
| 1755 | |
| 1756 | if (extraoob_bitflips) |
| 1757 | memset(extraoob, 0xff, extraooblen); |
| 1758 | |
| 1759 | return data_bitflips + ecc_bitflips + extraoob_bitflips; |
| 1760 | } |
| 1761 | EXPORT_SYMBOL(nand_check_erased_ecc_chunk); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1762 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1763 | /** |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1764 | * nand_read_page_raw - [INTERN] read raw page data without ecc |
| 1765 | * @mtd: mtd info structure |
| 1766 | * @chip: nand chip info structure |
| 1767 | * @buf: buffer to store read data |
| 1768 | * @oob_required: caller requires OOB data read to chip->oob_poi |
| 1769 | * @page: page number to read |
David Brownell | ee86b8d | 2009-11-07 16:27:01 -0500 | [diff] [blame] | 1770 | * |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1771 | * Not for syndrome calculating ECC controllers, which use a special oob layout. |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1772 | */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1773 | static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip, |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1774 | uint8_t *buf, int oob_required, int page) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1775 | { |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 1776 | int ret; |
| 1777 | |
| 1778 | ret = nand_read_data_op(chip, buf, mtd->writesize, false); |
| 1779 | if (ret) |
| 1780 | return ret; |
| 1781 | |
| 1782 | if (oob_required) { |
| 1783 | ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize, |
| 1784 | false); |
| 1785 | if (ret) |
| 1786 | return ret; |
| 1787 | } |
| 1788 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1789 | return 0; |
| 1790 | } |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 1791 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1792 | /** |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1793 | * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc |
| 1794 | * @mtd: mtd info structure |
| 1795 | * @chip: nand chip info structure |
| 1796 | * @buf: buffer to store read data |
| 1797 | * @oob_required: caller requires OOB data read to chip->oob_poi |
| 1798 | * @page: page number to read |
David Brownell | ee86b8d | 2009-11-07 16:27:01 -0500 | [diff] [blame] | 1799 | * |
| 1800 | * We need a special oob layout and handling even when OOB isn't used. |
| 1801 | */ |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 1802 | static int nand_read_page_raw_syndrome(struct mtd_info *mtd, |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1803 | struct nand_chip *chip, uint8_t *buf, |
| 1804 | int oob_required, int page) |
David Brownell | ee86b8d | 2009-11-07 16:27:01 -0500 | [diff] [blame] | 1805 | { |
| 1806 | int eccsize = chip->ecc.size; |
| 1807 | int eccbytes = chip->ecc.bytes; |
| 1808 | uint8_t *oob = chip->oob_poi; |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 1809 | int steps, size, ret; |
David Brownell | ee86b8d | 2009-11-07 16:27:01 -0500 | [diff] [blame] | 1810 | |
| 1811 | for (steps = chip->ecc.steps; steps > 0; steps--) { |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 1812 | ret = nand_read_data_op(chip, buf, eccsize, false); |
| 1813 | if (ret) |
| 1814 | return ret; |
| 1815 | |
David Brownell | ee86b8d | 2009-11-07 16:27:01 -0500 | [diff] [blame] | 1816 | buf += eccsize; |
| 1817 | |
| 1818 | if (chip->ecc.prepad) { |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 1819 | ret = nand_read_data_op(chip, oob, chip->ecc.prepad, |
| 1820 | false); |
| 1821 | if (ret) |
| 1822 | return ret; |
| 1823 | |
David Brownell | ee86b8d | 2009-11-07 16:27:01 -0500 | [diff] [blame] | 1824 | oob += chip->ecc.prepad; |
| 1825 | } |
| 1826 | |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 1827 | ret = nand_read_data_op(chip, oob, eccbytes, false); |
| 1828 | if (ret) |
| 1829 | return ret; |
| 1830 | |
David Brownell | ee86b8d | 2009-11-07 16:27:01 -0500 | [diff] [blame] | 1831 | oob += eccbytes; |
| 1832 | |
| 1833 | if (chip->ecc.postpad) { |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 1834 | ret = nand_read_data_op(chip, oob, chip->ecc.postpad, |
| 1835 | false); |
| 1836 | if (ret) |
| 1837 | return ret; |
| 1838 | |
David Brownell | ee86b8d | 2009-11-07 16:27:01 -0500 | [diff] [blame] | 1839 | oob += chip->ecc.postpad; |
| 1840 | } |
| 1841 | } |
| 1842 | |
| 1843 | size = mtd->oobsize - (oob - chip->oob_poi); |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 1844 | if (size) { |
| 1845 | ret = nand_read_data_op(chip, oob, size, false); |
| 1846 | if (ret) |
| 1847 | return ret; |
| 1848 | } |
David Brownell | ee86b8d | 2009-11-07 16:27:01 -0500 | [diff] [blame] | 1849 | |
| 1850 | return 0; |
| 1851 | } |
| 1852 | |
| 1853 | /** |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1854 | * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function |
| 1855 | * @mtd: mtd info structure |
| 1856 | * @chip: nand chip info structure |
| 1857 | * @buf: buffer to store read data |
| 1858 | * @oob_required: caller requires OOB data read to chip->oob_poi |
| 1859 | * @page: page number to read |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1860 | */ |
| 1861 | static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip, |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1862 | uint8_t *buf, int oob_required, int page) |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1863 | { |
| 1864 | int i, eccsize = chip->ecc.size; |
| 1865 | int eccbytes = chip->ecc.bytes; |
| 1866 | int eccsteps = chip->ecc.steps; |
| 1867 | uint8_t *p = buf; |
| 1868 | uint8_t *ecc_calc = chip->buffers->ecccalc; |
| 1869 | uint8_t *ecc_code = chip->buffers->ecccode; |
| 1870 | uint32_t *eccpos = chip->ecc.layout->eccpos; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1871 | unsigned int max_bitflips = 0; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 1872 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1873 | chip->ecc.read_page_raw(mtd, chip, buf, 1, page); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1874 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1875 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) |
| 1876 | chip->ecc.calculate(mtd, p, &ecc_calc[i]); |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 1877 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1878 | for (i = 0; i < chip->ecc.total; i++) |
| 1879 | ecc_code[i] = chip->oob_poi[eccpos[i]]; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 1880 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1881 | eccsteps = chip->ecc.steps; |
| 1882 | p = buf; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1883 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1884 | for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 1885 | int stat; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1886 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1887 | stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1888 | if (stat < 0) { |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1889 | mtd->ecc_stats.failed++; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1890 | } else { |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 1891 | mtd->ecc_stats.corrected += stat; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1892 | max_bitflips = max_t(unsigned int, max_bitflips, stat); |
| 1893 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1894 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1895 | return max_bitflips; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1896 | } |
| 1897 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1898 | /** |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1899 | * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1900 | * @mtd: mtd info structure |
| 1901 | * @chip: nand chip info structure |
| 1902 | * @data_offs: offset of requested data within the page |
| 1903 | * @readlen: data length |
| 1904 | * @bufpoi: buffer to store read data |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 1905 | * @page: page number to read |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 1906 | */ |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 1907 | static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip, |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 1908 | uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi, |
| 1909 | int page) |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 1910 | { |
| 1911 | int start_step, end_step, num_steps; |
| 1912 | uint32_t *eccpos = chip->ecc.layout->eccpos; |
| 1913 | uint8_t *p; |
| 1914 | int data_col_addr, i, gaps = 0; |
| 1915 | int datafrag_len, eccfrag_len, aligned_len, aligned_pos; |
| 1916 | int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1; |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 1917 | int index; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1918 | unsigned int max_bitflips = 0; |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 1919 | int ret; |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 1920 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1921 | /* Column address within the page aligned to ECC size (256bytes) */ |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 1922 | start_step = data_offs / chip->ecc.size; |
| 1923 | end_step = (data_offs + readlen - 1) / chip->ecc.size; |
| 1924 | num_steps = end_step - start_step + 1; |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 1925 | index = start_step * chip->ecc.bytes; |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 1926 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1927 | /* Data size aligned to ECC ecc.size */ |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 1928 | datafrag_len = num_steps * chip->ecc.size; |
| 1929 | eccfrag_len = num_steps * chip->ecc.bytes; |
| 1930 | |
| 1931 | data_col_addr = start_step * chip->ecc.size; |
| 1932 | /* If we read not a page aligned data */ |
| 1933 | if (data_col_addr != 0) |
| 1934 | chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1); |
| 1935 | |
| 1936 | p = bufpoi + data_col_addr; |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 1937 | ret = nand_read_data_op(chip, p, datafrag_len, false); |
| 1938 | if (ret) |
| 1939 | return ret; |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 1940 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1941 | /* Calculate ECC */ |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 1942 | for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) |
| 1943 | chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]); |
| 1944 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1945 | /* |
| 1946 | * The performance is faster if we position offsets according to |
| 1947 | * ecc.pos. Let's make sure that there are no gaps in ECC positions. |
| 1948 | */ |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 1949 | for (i = 0; i < eccfrag_len - 1; i++) { |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 1950 | if (eccpos[i + index] + 1 != eccpos[i + index + 1]) { |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 1951 | gaps = 1; |
| 1952 | break; |
| 1953 | } |
| 1954 | } |
| 1955 | if (gaps) { |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 1956 | ret = nand_change_read_column_op(chip, mtd->writesize, |
| 1957 | chip->oob_poi, mtd->oobsize, |
| 1958 | false); |
| 1959 | if (ret) |
| 1960 | return ret; |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 1961 | } else { |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1962 | /* |
| 1963 | * Send the command to read the particular ECC bytes take care |
| 1964 | * about buswidth alignment in read_buf. |
| 1965 | */ |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 1966 | aligned_pos = eccpos[index] & ~(busw - 1); |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 1967 | aligned_len = eccfrag_len; |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 1968 | if (eccpos[index] & (busw - 1)) |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 1969 | aligned_len++; |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 1970 | if (eccpos[index + (num_steps * chip->ecc.bytes)] & (busw - 1)) |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 1971 | aligned_len++; |
| 1972 | |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 1973 | ret = nand_change_read_column_op(chip, |
| 1974 | mtd->writesize + aligned_pos, |
| 1975 | &chip->oob_poi[aligned_pos], |
| 1976 | aligned_len, false); |
| 1977 | if (ret) |
| 1978 | return ret; |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 1979 | } |
| 1980 | |
| 1981 | for (i = 0; i < eccfrag_len; i++) |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 1982 | chip->buffers->ecccode[i] = chip->oob_poi[eccpos[i + index]]; |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 1983 | |
| 1984 | p = bufpoi + data_col_addr; |
| 1985 | for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) { |
| 1986 | int stat; |
| 1987 | |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 1988 | stat = chip->ecc.correct(mtd, p, |
| 1989 | &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]); |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 1990 | if (stat == -EBADMSG && |
| 1991 | (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) { |
| 1992 | /* check for empty pages with bitflips */ |
| 1993 | stat = nand_check_erased_ecc_chunk(p, chip->ecc.size, |
| 1994 | &chip->buffers->ecccode[i], |
| 1995 | chip->ecc.bytes, |
| 1996 | NULL, 0, |
| 1997 | chip->ecc.strength); |
| 1998 | } |
| 1999 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2000 | if (stat < 0) { |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 2001 | mtd->ecc_stats.failed++; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2002 | } else { |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 2003 | mtd->ecc_stats.corrected += stat; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2004 | max_bitflips = max_t(unsigned int, max_bitflips, stat); |
| 2005 | } |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 2006 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2007 | return max_bitflips; |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 2008 | } |
| 2009 | |
| 2010 | /** |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2011 | * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function |
| 2012 | * @mtd: mtd info structure |
| 2013 | * @chip: nand chip info structure |
| 2014 | * @buf: buffer to store read data |
| 2015 | * @oob_required: caller requires OOB data read to chip->oob_poi |
| 2016 | * @page: page number to read |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2017 | * |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2018 | * Not for syndrome calculating ECC controllers which need a special oob layout. |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2019 | */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2020 | static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip, |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2021 | uint8_t *buf, int oob_required, int page) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2022 | { |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2023 | int i, eccsize = chip->ecc.size; |
| 2024 | int eccbytes = chip->ecc.bytes; |
| 2025 | int eccsteps = chip->ecc.steps; |
| 2026 | uint8_t *p = buf; |
| 2027 | uint8_t *ecc_calc = chip->buffers->ecccalc; |
| 2028 | uint8_t *ecc_code = chip->buffers->ecccode; |
| 2029 | uint32_t *eccpos = chip->ecc.layout->eccpos; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2030 | unsigned int max_bitflips = 0; |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2031 | int ret; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2032 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2033 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 2034 | chip->ecc.hwctl(mtd, NAND_ECC_READ); |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2035 | |
| 2036 | ret = nand_read_data_op(chip, p, eccsize, false); |
| 2037 | if (ret) |
| 2038 | return ret; |
| 2039 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2040 | chip->ecc.calculate(mtd, p, &ecc_calc[i]); |
| 2041 | } |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2042 | |
| 2043 | ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize, false); |
| 2044 | if (ret) |
| 2045 | return ret; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2046 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2047 | for (i = 0; i < chip->ecc.total; i++) |
| 2048 | ecc_code[i] = chip->oob_poi[eccpos[i]]; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2049 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2050 | eccsteps = chip->ecc.steps; |
| 2051 | p = buf; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2052 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2053 | for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 2054 | int stat; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2055 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2056 | stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]); |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 2057 | if (stat == -EBADMSG && |
| 2058 | (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) { |
| 2059 | /* check for empty pages with bitflips */ |
| 2060 | stat = nand_check_erased_ecc_chunk(p, eccsize, |
| 2061 | &ecc_code[i], eccbytes, |
| 2062 | NULL, 0, |
| 2063 | chip->ecc.strength); |
| 2064 | } |
| 2065 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2066 | if (stat < 0) { |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2067 | mtd->ecc_stats.failed++; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2068 | } else { |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2069 | mtd->ecc_stats.corrected += stat; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2070 | max_bitflips = max_t(unsigned int, max_bitflips, stat); |
| 2071 | } |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2072 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2073 | return max_bitflips; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2074 | } |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 2075 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2076 | /** |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2077 | * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first |
| 2078 | * @mtd: mtd info structure |
| 2079 | * @chip: nand chip info structure |
| 2080 | * @buf: buffer to store read data |
| 2081 | * @oob_required: caller requires OOB data read to chip->oob_poi |
| 2082 | * @page: page number to read |
Sandeep Paulraj | dea4070 | 2009-08-10 13:27:56 -0400 | [diff] [blame] | 2083 | * |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2084 | * Hardware ECC for large page chips, require OOB to be read first. For this |
| 2085 | * ECC mode, the write_page method is re-used from ECC_HW. These methods |
| 2086 | * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with |
| 2087 | * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from |
| 2088 | * the data area, by overwriting the NAND manufacturer bad block markings. |
Sandeep Paulraj | dea4070 | 2009-08-10 13:27:56 -0400 | [diff] [blame] | 2089 | */ |
| 2090 | static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd, |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2091 | struct nand_chip *chip, uint8_t *buf, int oob_required, int page) |
Sandeep Paulraj | dea4070 | 2009-08-10 13:27:56 -0400 | [diff] [blame] | 2092 | { |
| 2093 | int i, eccsize = chip->ecc.size; |
| 2094 | int eccbytes = chip->ecc.bytes; |
| 2095 | int eccsteps = chip->ecc.steps; |
| 2096 | uint8_t *p = buf; |
| 2097 | uint8_t *ecc_code = chip->buffers->ecccode; |
| 2098 | uint32_t *eccpos = chip->ecc.layout->eccpos; |
| 2099 | uint8_t *ecc_calc = chip->buffers->ecccalc; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2100 | unsigned int max_bitflips = 0; |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2101 | int ret; |
Sandeep Paulraj | dea4070 | 2009-08-10 13:27:56 -0400 | [diff] [blame] | 2102 | |
| 2103 | /* Read the OOB area first */ |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2104 | ret = nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize); |
| 2105 | if (ret) |
| 2106 | return ret; |
| 2107 | |
| 2108 | ret = nand_read_page_op(chip, page, 0, NULL, 0); |
| 2109 | if (ret) |
| 2110 | return ret; |
Sandeep Paulraj | dea4070 | 2009-08-10 13:27:56 -0400 | [diff] [blame] | 2111 | |
| 2112 | for (i = 0; i < chip->ecc.total; i++) |
| 2113 | ecc_code[i] = chip->oob_poi[eccpos[i]]; |
| 2114 | |
| 2115 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 2116 | int stat; |
| 2117 | |
| 2118 | chip->ecc.hwctl(mtd, NAND_ECC_READ); |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2119 | |
| 2120 | ret = nand_read_data_op(chip, p, eccsize, false); |
| 2121 | if (ret) |
| 2122 | return ret; |
| 2123 | |
Sandeep Paulraj | dea4070 | 2009-08-10 13:27:56 -0400 | [diff] [blame] | 2124 | chip->ecc.calculate(mtd, p, &ecc_calc[i]); |
| 2125 | |
| 2126 | stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL); |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 2127 | if (stat == -EBADMSG && |
| 2128 | (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) { |
| 2129 | /* check for empty pages with bitflips */ |
| 2130 | stat = nand_check_erased_ecc_chunk(p, eccsize, |
| 2131 | &ecc_code[i], eccbytes, |
| 2132 | NULL, 0, |
| 2133 | chip->ecc.strength); |
| 2134 | } |
| 2135 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2136 | if (stat < 0) { |
Sandeep Paulraj | dea4070 | 2009-08-10 13:27:56 -0400 | [diff] [blame] | 2137 | mtd->ecc_stats.failed++; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2138 | } else { |
Sandeep Paulraj | dea4070 | 2009-08-10 13:27:56 -0400 | [diff] [blame] | 2139 | mtd->ecc_stats.corrected += stat; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2140 | max_bitflips = max_t(unsigned int, max_bitflips, stat); |
| 2141 | } |
Sandeep Paulraj | dea4070 | 2009-08-10 13:27:56 -0400 | [diff] [blame] | 2142 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2143 | return max_bitflips; |
Sandeep Paulraj | dea4070 | 2009-08-10 13:27:56 -0400 | [diff] [blame] | 2144 | } |
| 2145 | |
| 2146 | /** |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2147 | * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read |
| 2148 | * @mtd: mtd info structure |
| 2149 | * @chip: nand chip info structure |
| 2150 | * @buf: buffer to store read data |
| 2151 | * @oob_required: caller requires OOB data read to chip->oob_poi |
| 2152 | * @page: page number to read |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2153 | * |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2154 | * The hw generator calculates the error syndrome automatically. Therefore we |
| 2155 | * need a special oob layout and handling. |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2156 | */ |
| 2157 | static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip, |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2158 | uint8_t *buf, int oob_required, int page) |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2159 | { |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2160 | int ret, i, eccsize = chip->ecc.size; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2161 | int eccbytes = chip->ecc.bytes; |
| 2162 | int eccsteps = chip->ecc.steps; |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 2163 | int eccpadbytes = eccbytes + chip->ecc.prepad + chip->ecc.postpad; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2164 | uint8_t *p = buf; |
| 2165 | uint8_t *oob = chip->oob_poi; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2166 | unsigned int max_bitflips = 0; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2167 | |
| 2168 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 2169 | int stat; |
| 2170 | |
| 2171 | chip->ecc.hwctl(mtd, NAND_ECC_READ); |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2172 | |
| 2173 | ret = nand_read_data_op(chip, p, eccsize, false); |
| 2174 | if (ret) |
| 2175 | return ret; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2176 | |
| 2177 | if (chip->ecc.prepad) { |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2178 | ret = nand_read_data_op(chip, oob, chip->ecc.prepad, |
| 2179 | false); |
| 2180 | if (ret) |
| 2181 | return ret; |
| 2182 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2183 | oob += chip->ecc.prepad; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2184 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2185 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2186 | chip->ecc.hwctl(mtd, NAND_ECC_READSYN); |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2187 | |
| 2188 | ret = nand_read_data_op(chip, oob, eccbytes, false); |
| 2189 | if (ret) |
| 2190 | return ret; |
| 2191 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2192 | stat = chip->ecc.correct(mtd, p, oob, NULL); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2193 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2194 | oob += eccbytes; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 2195 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2196 | if (chip->ecc.postpad) { |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2197 | ret = nand_read_data_op(chip, oob, chip->ecc.postpad, |
| 2198 | false); |
| 2199 | if (ret) |
| 2200 | return ret; |
| 2201 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2202 | oob += chip->ecc.postpad; |
| 2203 | } |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 2204 | |
| 2205 | if (stat == -EBADMSG && |
| 2206 | (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) { |
| 2207 | /* check for empty pages with bitflips */ |
| 2208 | stat = nand_check_erased_ecc_chunk(p, chip->ecc.size, |
| 2209 | oob - eccpadbytes, |
| 2210 | eccpadbytes, |
| 2211 | NULL, 0, |
| 2212 | chip->ecc.strength); |
| 2213 | } |
| 2214 | |
| 2215 | if (stat < 0) { |
| 2216 | mtd->ecc_stats.failed++; |
| 2217 | } else { |
| 2218 | mtd->ecc_stats.corrected += stat; |
| 2219 | max_bitflips = max_t(unsigned int, max_bitflips, stat); |
| 2220 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2221 | } |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2222 | |
| 2223 | /* Calculate remaining oob bytes */ |
| 2224 | i = mtd->oobsize - (oob - chip->oob_poi); |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2225 | if (i) { |
| 2226 | ret = nand_read_data_op(chip, oob, i, false); |
| 2227 | if (ret) |
| 2228 | return ret; |
| 2229 | } |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2230 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2231 | return max_bitflips; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2232 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2233 | |
| 2234 | /** |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2235 | * nand_transfer_oob - [INTERN] Transfer oob to client buffer |
| 2236 | * @chip: nand chip structure |
| 2237 | * @oob: oob destination address |
| 2238 | * @ops: oob ops structure |
| 2239 | * @len: size of oob to transfer |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2240 | */ |
| 2241 | static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob, |
| 2242 | struct mtd_oob_ops *ops, size_t len) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2243 | { |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 2244 | switch (ops->mode) { |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2245 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2246 | case MTD_OPS_PLACE_OOB: |
| 2247 | case MTD_OPS_RAW: |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2248 | memcpy(oob, chip->oob_poi + ops->ooboffs, len); |
| 2249 | return oob + len; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2250 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2251 | case MTD_OPS_AUTO_OOB: { |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2252 | struct nand_oobfree *free = chip->ecc.layout->oobfree; |
| 2253 | uint32_t boffs = 0, roffs = ops->ooboffs; |
| 2254 | size_t bytes = 0; |
| 2255 | |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 2256 | for (; free->length && len; free++, len -= bytes) { |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2257 | /* Read request not from offset 0? */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2258 | if (unlikely(roffs)) { |
| 2259 | if (roffs >= free->length) { |
| 2260 | roffs -= free->length; |
| 2261 | continue; |
| 2262 | } |
| 2263 | boffs = free->offset + roffs; |
| 2264 | bytes = min_t(size_t, len, |
| 2265 | (free->length - roffs)); |
| 2266 | roffs = 0; |
| 2267 | } else { |
| 2268 | bytes = min_t(size_t, len, free->length); |
| 2269 | boffs = free->offset; |
| 2270 | } |
| 2271 | memcpy(oob, chip->oob_poi + boffs, bytes); |
| 2272 | oob += bytes; |
| 2273 | } |
| 2274 | return oob; |
| 2275 | } |
| 2276 | default: |
| 2277 | BUG(); |
| 2278 | } |
| 2279 | return NULL; |
| 2280 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2281 | |
| 2282 | /** |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2283 | * nand_setup_read_retry - [INTERN] Set the READ RETRY mode |
| 2284 | * @mtd: MTD device structure |
| 2285 | * @retry_mode: the retry mode to use |
| 2286 | * |
| 2287 | * Some vendors supply a special command to shift the Vt threshold, to be used |
| 2288 | * when there are too many bitflips in a page (i.e., ECC error). After setting |
| 2289 | * a new threshold, the host should retry reading the page. |
| 2290 | */ |
| 2291 | static int nand_setup_read_retry(struct mtd_info *mtd, int retry_mode) |
| 2292 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 2293 | struct nand_chip *chip = mtd_to_nand(mtd); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2294 | |
| 2295 | pr_debug("setting READ RETRY mode %d\n", retry_mode); |
| 2296 | |
| 2297 | if (retry_mode >= chip->read_retries) |
| 2298 | return -EINVAL; |
| 2299 | |
| 2300 | if (!chip->setup_read_retry) |
| 2301 | return -EOPNOTSUPP; |
| 2302 | |
| 2303 | return chip->setup_read_retry(mtd, retry_mode); |
| 2304 | } |
| 2305 | |
| 2306 | /** |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2307 | * nand_do_read_ops - [INTERN] Read data with ECC |
| 2308 | * @mtd: MTD device structure |
| 2309 | * @from: offset to read from |
| 2310 | * @ops: oob ops structure |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2311 | * |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2312 | * Internal function. Called with chip held. |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2313 | */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2314 | static int nand_do_read_ops(struct mtd_info *mtd, loff_t from, |
| 2315 | struct mtd_oob_ops *ops) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2316 | { |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2317 | int chipnr, page, realpage, col, bytes, aligned, oob_required; |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 2318 | struct nand_chip *chip = mtd_to_nand(mtd); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2319 | int ret = 0; |
| 2320 | uint32_t readlen = ops->len; |
| 2321 | uint32_t oobreadlen = ops->ooblen; |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 2322 | uint32_t max_oobsize = mtd_oobavail(mtd, ops); |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 2323 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2324 | uint8_t *bufpoi, *oob, *buf; |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 2325 | int use_bufpoi; |
Paul Burton | 700a76c | 2013-09-04 15:16:56 +0100 | [diff] [blame] | 2326 | unsigned int max_bitflips = 0; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2327 | int retry_mode = 0; |
| 2328 | bool ecc_fail = false; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2329 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2330 | chipnr = (int)(from >> chip->chip_shift); |
| 2331 | chip->select_chip(mtd, chipnr); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2332 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2333 | realpage = (int)(from >> chip->page_shift); |
| 2334 | page = realpage & chip->pagemask; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2335 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2336 | col = (int)(from & (mtd->writesize - 1)); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2337 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2338 | buf = ops->datbuf; |
| 2339 | oob = ops->oobbuf; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2340 | oob_required = oob ? 1 : 0; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 2341 | |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 2342 | while (1) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2343 | unsigned int ecc_failures = mtd->ecc_stats.failed; |
Scott Wood | ea95b64 | 2011-02-02 18:15:57 -0600 | [diff] [blame] | 2344 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2345 | WATCHDOG_RESET(); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2346 | bytes = min(mtd->writesize - col, readlen); |
| 2347 | aligned = (bytes == mtd->writesize); |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 2348 | |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 2349 | if (!aligned) |
| 2350 | use_bufpoi = 1; |
Masahiro Yamada | b9c07b6 | 2017-11-22 02:38:27 +0900 | [diff] [blame] | 2351 | else if (chip->options & NAND_USE_BOUNCE_BUFFER) |
| 2352 | use_bufpoi = !IS_ALIGNED((unsigned long)buf, |
| 2353 | chip->buf_align); |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 2354 | else |
| 2355 | use_bufpoi = 0; |
| 2356 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2357 | /* Is the current page in the buffer? */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2358 | if (realpage != chip->pagebuf || oob) { |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 2359 | bufpoi = use_bufpoi ? chip->buffers->databuf : buf; |
| 2360 | |
| 2361 | if (use_bufpoi && aligned) |
| 2362 | pr_debug("%s: using read bounce buffer for buf@%p\n", |
| 2363 | __func__, buf); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2364 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2365 | read_retry: |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2366 | if (nand_standard_page_accessors(&chip->ecc)) { |
| 2367 | ret = nand_read_page_op(chip, page, 0, NULL, 0); |
| 2368 | if (ret) |
| 2369 | break; |
| 2370 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2371 | |
Paul Burton | 700a76c | 2013-09-04 15:16:56 +0100 | [diff] [blame] | 2372 | /* |
| 2373 | * Now read the page into the buffer. Absent an error, |
| 2374 | * the read methods return max bitflips per ecc step. |
| 2375 | */ |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2376 | if (unlikely(ops->mode == MTD_OPS_RAW)) |
| 2377 | ret = chip->ecc.read_page_raw(mtd, chip, bufpoi, |
| 2378 | oob_required, |
| 2379 | page); |
Joe Hershberger | 7a38ffa | 2012-11-05 06:46:31 +0000 | [diff] [blame] | 2380 | else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) && |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2381 | !oob) |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 2382 | ret = chip->ecc.read_subpage(mtd, chip, |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 2383 | col, bytes, bufpoi, |
| 2384 | page); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2385 | else |
Sandeep Paulraj | 883189e | 2009-08-10 13:27:46 -0400 | [diff] [blame] | 2386 | ret = chip->ecc.read_page(mtd, chip, bufpoi, |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2387 | oob_required, page); |
| 2388 | if (ret < 0) { |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 2389 | if (use_bufpoi) |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2390 | /* Invalidate page cache */ |
| 2391 | chip->pagebuf = -1; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2392 | break; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2393 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2394 | |
Paul Burton | 700a76c | 2013-09-04 15:16:56 +0100 | [diff] [blame] | 2395 | max_bitflips = max_t(unsigned int, max_bitflips, ret); |
| 2396 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2397 | /* Transfer not aligned data */ |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 2398 | if (use_bufpoi) { |
Joe Hershberger | 7a38ffa | 2012-11-05 06:46:31 +0000 | [diff] [blame] | 2399 | if (!NAND_HAS_SUBPAGE_READ(chip) && !oob && |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2400 | !(mtd->ecc_stats.failed - ecc_failures) && |
Paul Burton | 700a76c | 2013-09-04 15:16:56 +0100 | [diff] [blame] | 2401 | (ops->mode != MTD_OPS_RAW)) { |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 2402 | chip->pagebuf = realpage; |
Paul Burton | 700a76c | 2013-09-04 15:16:56 +0100 | [diff] [blame] | 2403 | chip->pagebuf_bitflips = ret; |
| 2404 | } else { |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2405 | /* Invalidate page cache */ |
| 2406 | chip->pagebuf = -1; |
Paul Burton | 700a76c | 2013-09-04 15:16:56 +0100 | [diff] [blame] | 2407 | } |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2408 | memcpy(buf, chip->buffers->databuf + col, bytes); |
| 2409 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2410 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2411 | if (unlikely(oob)) { |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 2412 | int toread = min(oobreadlen, max_oobsize); |
| 2413 | |
| 2414 | if (toread) { |
| 2415 | oob = nand_transfer_oob(chip, |
| 2416 | oob, ops, toread); |
| 2417 | oobreadlen -= toread; |
| 2418 | } |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2419 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2420 | |
| 2421 | if (chip->options & NAND_NEED_READRDY) { |
| 2422 | /* Apply delay or wait for ready/busy pin */ |
| 2423 | if (!chip->dev_ready) |
| 2424 | udelay(chip->chip_delay); |
| 2425 | else |
| 2426 | nand_wait_ready(mtd); |
| 2427 | } |
| 2428 | |
| 2429 | if (mtd->ecc_stats.failed - ecc_failures) { |
| 2430 | if (retry_mode + 1 < chip->read_retries) { |
| 2431 | retry_mode++; |
| 2432 | ret = nand_setup_read_retry(mtd, |
| 2433 | retry_mode); |
| 2434 | if (ret < 0) |
| 2435 | break; |
| 2436 | |
| 2437 | /* Reset failures; retry */ |
| 2438 | mtd->ecc_stats.failed = ecc_failures; |
| 2439 | goto read_retry; |
| 2440 | } else { |
| 2441 | /* No more retry modes; real failure */ |
| 2442 | ecc_fail = true; |
| 2443 | } |
| 2444 | } |
| 2445 | |
| 2446 | buf += bytes; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2447 | } else { |
| 2448 | memcpy(buf, chip->buffers->databuf + col, bytes); |
| 2449 | buf += bytes; |
Paul Burton | 700a76c | 2013-09-04 15:16:56 +0100 | [diff] [blame] | 2450 | max_bitflips = max_t(unsigned int, max_bitflips, |
| 2451 | chip->pagebuf_bitflips); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2452 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2453 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2454 | readlen -= bytes; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 2455 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2456 | /* Reset to retry mode 0 */ |
| 2457 | if (retry_mode) { |
| 2458 | ret = nand_setup_read_retry(mtd, 0); |
| 2459 | if (ret < 0) |
| 2460 | break; |
| 2461 | retry_mode = 0; |
| 2462 | } |
| 2463 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2464 | if (!readlen) |
| 2465 | break; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 2466 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2467 | /* For subsequent reads align to page boundary */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2468 | col = 0; |
| 2469 | /* Increment page address */ |
| 2470 | realpage++; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2471 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2472 | page = realpage & chip->pagemask; |
| 2473 | /* Check, if we cross a chip boundary */ |
| 2474 | if (!page) { |
| 2475 | chipnr++; |
| 2476 | chip->select_chip(mtd, -1); |
| 2477 | chip->select_chip(mtd, chipnr); |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 2478 | } |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2479 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2480 | chip->select_chip(mtd, -1); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2481 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2482 | ops->retlen = ops->len - (size_t) readlen; |
| 2483 | if (oob) |
| 2484 | ops->oobretlen = ops->ooblen - oobreadlen; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 2485 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2486 | if (ret < 0) |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2487 | return ret; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 2488 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2489 | if (ecc_fail) |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2490 | return -EBADMSG; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2491 | |
Paul Burton | 700a76c | 2013-09-04 15:16:56 +0100 | [diff] [blame] | 2492 | return max_bitflips; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2493 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2494 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2495 | /** |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2496 | * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function |
| 2497 | * @mtd: mtd info structure |
| 2498 | * @chip: nand chip info structure |
| 2499 | * @page: page number to read |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2500 | */ |
| 2501 | static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip, |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2502 | int page) |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2503 | { |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2504 | return nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2505 | } |
| 2506 | |
| 2507 | /** |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2508 | * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2509 | * with syndromes |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2510 | * @mtd: mtd info structure |
| 2511 | * @chip: nand chip info structure |
| 2512 | * @page: page number to read |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2513 | */ |
| 2514 | static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip, |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2515 | int page) |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2516 | { |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2517 | int length = mtd->oobsize; |
| 2518 | int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad; |
| 2519 | int eccsize = chip->ecc.size; |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 2520 | uint8_t *bufpoi = chip->oob_poi; |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2521 | int i, toread, sndrnd = 0, pos, ret; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2522 | |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2523 | ret = nand_read_page_op(chip, page, chip->ecc.size, NULL, 0); |
| 2524 | if (ret) |
| 2525 | return ret; |
| 2526 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2527 | for (i = 0; i < chip->ecc.steps; i++) { |
| 2528 | if (sndrnd) { |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2529 | int ret; |
| 2530 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2531 | pos = eccsize + i * (eccsize + chunk); |
| 2532 | if (mtd->writesize > 512) |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2533 | ret = nand_change_read_column_op(chip, pos, |
| 2534 | NULL, 0, |
| 2535 | false); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2536 | else |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2537 | ret = nand_read_page_op(chip, page, pos, NULL, |
| 2538 | 0); |
| 2539 | |
| 2540 | if (ret) |
| 2541 | return ret; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 2542 | } else |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2543 | sndrnd = 1; |
| 2544 | toread = min_t(int, length, chunk); |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2545 | |
| 2546 | ret = nand_read_data_op(chip, bufpoi, toread, false); |
| 2547 | if (ret) |
| 2548 | return ret; |
| 2549 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2550 | bufpoi += toread; |
| 2551 | length -= toread; |
| 2552 | } |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2553 | if (length > 0) { |
| 2554 | ret = nand_read_data_op(chip, bufpoi, length, false); |
| 2555 | if (ret) |
| 2556 | return ret; |
| 2557 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2558 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2559 | return 0; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2560 | } |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 2561 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2562 | /** |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2563 | * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function |
| 2564 | * @mtd: mtd info structure |
| 2565 | * @chip: nand chip info structure |
| 2566 | * @page: page number to write |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2567 | */ |
| 2568 | static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip, |
| 2569 | int page) |
| 2570 | { |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2571 | return nand_prog_page_op(chip, page, mtd->writesize, chip->oob_poi, |
| 2572 | mtd->oobsize); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2573 | } |
| 2574 | |
| 2575 | /** |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2576 | * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC |
| 2577 | * with syndrome - only for large page flash |
| 2578 | * @mtd: mtd info structure |
| 2579 | * @chip: nand chip info structure |
| 2580 | * @page: page number to write |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2581 | */ |
| 2582 | static int nand_write_oob_syndrome(struct mtd_info *mtd, |
| 2583 | struct nand_chip *chip, int page) |
| 2584 | { |
| 2585 | int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad; |
| 2586 | int eccsize = chip->ecc.size, length = mtd->oobsize; |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2587 | int ret, i, len, pos, sndcmd = 0, steps = chip->ecc.steps; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2588 | const uint8_t *bufpoi = chip->oob_poi; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2589 | |
| 2590 | /* |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2591 | * data-ecc-data-ecc ... ecc-oob |
| 2592 | * or |
| 2593 | * data-pad-ecc-pad-data-pad .... ecc-pad-oob |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2594 | */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2595 | if (!chip->ecc.prepad && !chip->ecc.postpad) { |
| 2596 | pos = steps * (eccsize + chunk); |
| 2597 | steps = 0; |
| 2598 | } else |
| 2599 | pos = eccsize; |
| 2600 | |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2601 | ret = nand_prog_page_begin_op(chip, page, pos, NULL, 0); |
| 2602 | if (ret) |
| 2603 | return ret; |
| 2604 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2605 | for (i = 0; i < steps; i++) { |
| 2606 | if (sndcmd) { |
| 2607 | if (mtd->writesize <= 512) { |
| 2608 | uint32_t fill = 0xFFFFFFFF; |
| 2609 | |
| 2610 | len = eccsize; |
| 2611 | while (len > 0) { |
| 2612 | int num = min_t(int, len, 4); |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2613 | |
| 2614 | ret = nand_write_data_op(chip, &fill, |
| 2615 | num, false); |
| 2616 | if (ret) |
| 2617 | return ret; |
| 2618 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2619 | len -= num; |
| 2620 | } |
| 2621 | } else { |
| 2622 | pos = eccsize + i * (eccsize + chunk); |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2623 | ret = nand_change_write_column_op(chip, pos, |
| 2624 | NULL, 0, |
| 2625 | false); |
| 2626 | if (ret) |
| 2627 | return ret; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2628 | } |
| 2629 | } else |
| 2630 | sndcmd = 1; |
| 2631 | len = min_t(int, length, chunk); |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2632 | |
| 2633 | ret = nand_write_data_op(chip, bufpoi, len, false); |
| 2634 | if (ret) |
| 2635 | return ret; |
| 2636 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2637 | bufpoi += len; |
| 2638 | length -= len; |
| 2639 | } |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2640 | if (length > 0) { |
| 2641 | ret = nand_write_data_op(chip, bufpoi, length, false); |
| 2642 | if (ret) |
| 2643 | return ret; |
| 2644 | } |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2645 | |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2646 | return nand_prog_page_end_op(chip); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2647 | } |
| 2648 | |
| 2649 | /** |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2650 | * nand_do_read_oob - [INTERN] NAND read out-of-band |
| 2651 | * @mtd: MTD device structure |
| 2652 | * @from: offset to read from |
| 2653 | * @ops: oob operations description structure |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2654 | * |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2655 | * NAND read out-of-band data from the spare area. |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2656 | */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2657 | static int nand_do_read_oob(struct mtd_info *mtd, loff_t from, |
| 2658 | struct mtd_oob_ops *ops) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2659 | { |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2660 | int page, realpage, chipnr; |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 2661 | struct nand_chip *chip = mtd_to_nand(mtd); |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2662 | struct mtd_ecc_stats stats; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2663 | int readlen = ops->ooblen; |
| 2664 | int len; |
| 2665 | uint8_t *buf = ops->oobbuf; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2666 | int ret = 0; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2667 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2668 | pr_debug("%s: from = 0x%08Lx, len = %i\n", |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 2669 | __func__, (unsigned long long)from, readlen); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2670 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2671 | stats = mtd->ecc_stats; |
| 2672 | |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 2673 | len = mtd_oobavail(mtd, ops); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2674 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2675 | if (unlikely(ops->ooboffs >= len)) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2676 | pr_debug("%s: attempt to start read outside oob\n", |
| 2677 | __func__); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2678 | return -EINVAL; |
| 2679 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2680 | |
| 2681 | /* Do not allow reads past end of device */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2682 | if (unlikely(from >= mtd->size || |
| 2683 | ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) - |
| 2684 | (from >> chip->page_shift)) * len)) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2685 | pr_debug("%s: attempt to read beyond end of device\n", |
| 2686 | __func__); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2687 | return -EINVAL; |
| 2688 | } |
| 2689 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2690 | chipnr = (int)(from >> chip->chip_shift); |
| 2691 | chip->select_chip(mtd, chipnr); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2692 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2693 | /* Shift to get page */ |
| 2694 | realpage = (int)(from >> chip->page_shift); |
| 2695 | page = realpage & chip->pagemask; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2696 | |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 2697 | while (1) { |
Scott Wood | ea95b64 | 2011-02-02 18:15:57 -0600 | [diff] [blame] | 2698 | WATCHDOG_RESET(); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2699 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2700 | if (ops->mode == MTD_OPS_RAW) |
| 2701 | ret = chip->ecc.read_oob_raw(mtd, chip, page); |
| 2702 | else |
| 2703 | ret = chip->ecc.read_oob(mtd, chip, page); |
| 2704 | |
| 2705 | if (ret < 0) |
| 2706 | break; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 2707 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2708 | len = min(len, readlen); |
| 2709 | buf = nand_transfer_oob(chip, buf, ops, len); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2710 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2711 | if (chip->options & NAND_NEED_READRDY) { |
| 2712 | /* Apply delay or wait for ready/busy pin */ |
| 2713 | if (!chip->dev_ready) |
| 2714 | udelay(chip->chip_delay); |
| 2715 | else |
| 2716 | nand_wait_ready(mtd); |
| 2717 | } |
| 2718 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2719 | readlen -= len; |
| 2720 | if (!readlen) |
| 2721 | break; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 2722 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2723 | /* Increment page address */ |
| 2724 | realpage++; |
| 2725 | |
| 2726 | page = realpage & chip->pagemask; |
| 2727 | /* Check, if we cross a chip boundary */ |
| 2728 | if (!page) { |
| 2729 | chipnr++; |
| 2730 | chip->select_chip(mtd, -1); |
| 2731 | chip->select_chip(mtd, chipnr); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2732 | } |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2733 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2734 | chip->select_chip(mtd, -1); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2735 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2736 | ops->oobretlen = ops->ooblen - readlen; |
| 2737 | |
| 2738 | if (ret < 0) |
| 2739 | return ret; |
| 2740 | |
| 2741 | if (mtd->ecc_stats.failed - stats.failed) |
| 2742 | return -EBADMSG; |
| 2743 | |
| 2744 | return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2745 | } |
| 2746 | |
| 2747 | /** |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2748 | * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2749 | * @mtd: MTD device structure |
| 2750 | * @from: offset to read from |
| 2751 | * @ops: oob operation description structure |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2752 | * |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2753 | * NAND read data and/or out-of-band data. |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2754 | */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2755 | static int nand_read_oob(struct mtd_info *mtd, loff_t from, |
| 2756 | struct mtd_oob_ops *ops) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2757 | { |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2758 | int ret = -ENOTSUPP; |
| 2759 | |
| 2760 | ops->retlen = 0; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2761 | |
| 2762 | /* Do not allow reads past end of device */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2763 | if (ops->datbuf && (from + ops->len) > mtd->size) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2764 | pr_debug("%s: attempt to read beyond end of device\n", |
| 2765 | __func__); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2766 | return -EINVAL; |
| 2767 | } |
| 2768 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2769 | nand_get_device(mtd, FL_READING); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2770 | |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 2771 | switch (ops->mode) { |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2772 | case MTD_OPS_PLACE_OOB: |
| 2773 | case MTD_OPS_AUTO_OOB: |
| 2774 | case MTD_OPS_RAW: |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2775 | break; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 2776 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2777 | default: |
| 2778 | goto out; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2779 | } |
| 2780 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2781 | if (!ops->datbuf) |
| 2782 | ret = nand_do_read_oob(mtd, from, ops); |
| 2783 | else |
| 2784 | ret = nand_do_read_ops(mtd, from, ops); |
| 2785 | |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 2786 | out: |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2787 | nand_release_device(mtd); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2788 | return ret; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2789 | } |
| 2790 | |
| 2791 | |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 2792 | /** |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2793 | * nand_write_page_raw - [INTERN] raw page write function |
| 2794 | * @mtd: mtd info structure |
| 2795 | * @chip: nand chip info structure |
| 2796 | * @buf: data buffer |
| 2797 | * @oob_required: must write chip->oob_poi to OOB |
Scott Wood | 46e1310 | 2016-05-30 13:57:57 -0500 | [diff] [blame] | 2798 | * @page: page number to write |
David Brownell | ee86b8d | 2009-11-07 16:27:01 -0500 | [diff] [blame] | 2799 | * |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2800 | * Not for syndrome calculating ECC controllers, which use a special oob layout. |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2801 | */ |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2802 | static int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip, |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 2803 | const uint8_t *buf, int oob_required, int page) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2804 | { |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2805 | int ret; |
| 2806 | |
| 2807 | ret = nand_write_data_op(chip, buf, mtd->writesize, false); |
| 2808 | if (ret) |
| 2809 | return ret; |
| 2810 | |
| 2811 | if (oob_required) { |
| 2812 | ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, |
| 2813 | false); |
| 2814 | if (ret) |
| 2815 | return ret; |
| 2816 | } |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2817 | |
| 2818 | return 0; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2819 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2820 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2821 | /** |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2822 | * nand_write_page_raw_syndrome - [INTERN] raw page write function |
| 2823 | * @mtd: mtd info structure |
| 2824 | * @chip: nand chip info structure |
| 2825 | * @buf: data buffer |
| 2826 | * @oob_required: must write chip->oob_poi to OOB |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 2827 | * @page: page number to write |
David Brownell | ee86b8d | 2009-11-07 16:27:01 -0500 | [diff] [blame] | 2828 | * |
| 2829 | * We need a special oob layout and handling even when ECC isn't checked. |
| 2830 | */ |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2831 | static int nand_write_page_raw_syndrome(struct mtd_info *mtd, |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 2832 | struct nand_chip *chip, |
Scott Wood | 46e1310 | 2016-05-30 13:57:57 -0500 | [diff] [blame] | 2833 | const uint8_t *buf, int oob_required, |
| 2834 | int page) |
David Brownell | ee86b8d | 2009-11-07 16:27:01 -0500 | [diff] [blame] | 2835 | { |
| 2836 | int eccsize = chip->ecc.size; |
| 2837 | int eccbytes = chip->ecc.bytes; |
| 2838 | uint8_t *oob = chip->oob_poi; |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2839 | int steps, size, ret; |
David Brownell | ee86b8d | 2009-11-07 16:27:01 -0500 | [diff] [blame] | 2840 | |
| 2841 | for (steps = chip->ecc.steps; steps > 0; steps--) { |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2842 | ret = nand_write_data_op(chip, buf, eccsize, false); |
| 2843 | if (ret) |
| 2844 | return ret; |
| 2845 | |
David Brownell | ee86b8d | 2009-11-07 16:27:01 -0500 | [diff] [blame] | 2846 | buf += eccsize; |
| 2847 | |
| 2848 | if (chip->ecc.prepad) { |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2849 | ret = nand_write_data_op(chip, oob, chip->ecc.prepad, |
| 2850 | false); |
| 2851 | if (ret) |
| 2852 | return ret; |
| 2853 | |
David Brownell | ee86b8d | 2009-11-07 16:27:01 -0500 | [diff] [blame] | 2854 | oob += chip->ecc.prepad; |
| 2855 | } |
| 2856 | |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2857 | ret = nand_write_data_op(chip, oob, eccbytes, false); |
| 2858 | if (ret) |
| 2859 | return ret; |
| 2860 | |
David Brownell | ee86b8d | 2009-11-07 16:27:01 -0500 | [diff] [blame] | 2861 | oob += eccbytes; |
| 2862 | |
| 2863 | if (chip->ecc.postpad) { |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2864 | ret = nand_write_data_op(chip, oob, chip->ecc.postpad, |
| 2865 | false); |
| 2866 | if (ret) |
| 2867 | return ret; |
| 2868 | |
David Brownell | ee86b8d | 2009-11-07 16:27:01 -0500 | [diff] [blame] | 2869 | oob += chip->ecc.postpad; |
| 2870 | } |
| 2871 | } |
| 2872 | |
| 2873 | size = mtd->oobsize - (oob - chip->oob_poi); |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2874 | if (size) { |
| 2875 | ret = nand_write_data_op(chip, oob, size, false); |
| 2876 | if (ret) |
| 2877 | return ret; |
| 2878 | } |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2879 | |
| 2880 | return 0; |
David Brownell | ee86b8d | 2009-11-07 16:27:01 -0500 | [diff] [blame] | 2881 | } |
| 2882 | /** |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2883 | * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function |
| 2884 | * @mtd: mtd info structure |
| 2885 | * @chip: nand chip info structure |
| 2886 | * @buf: data buffer |
| 2887 | * @oob_required: must write chip->oob_poi to OOB |
Scott Wood | 46e1310 | 2016-05-30 13:57:57 -0500 | [diff] [blame] | 2888 | * @page: page number to write |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2889 | */ |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2890 | static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip, |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 2891 | const uint8_t *buf, int oob_required, |
| 2892 | int page) |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2893 | { |
| 2894 | int i, eccsize = chip->ecc.size; |
| 2895 | int eccbytes = chip->ecc.bytes; |
| 2896 | int eccsteps = chip->ecc.steps; |
| 2897 | uint8_t *ecc_calc = chip->buffers->ecccalc; |
| 2898 | const uint8_t *p = buf; |
| 2899 | uint32_t *eccpos = chip->ecc.layout->eccpos; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2900 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2901 | /* Software ECC calculation */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2902 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) |
| 2903 | chip->ecc.calculate(mtd, p, &ecc_calc[i]); |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 2904 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2905 | for (i = 0; i < chip->ecc.total; i++) |
| 2906 | chip->oob_poi[eccpos[i]] = ecc_calc[i]; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 2907 | |
Scott Wood | 46e1310 | 2016-05-30 13:57:57 -0500 | [diff] [blame] | 2908 | return chip->ecc.write_page_raw(mtd, chip, buf, 1, page); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2909 | } |
| 2910 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2911 | /** |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2912 | * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function |
| 2913 | * @mtd: mtd info structure |
| 2914 | * @chip: nand chip info structure |
| 2915 | * @buf: data buffer |
| 2916 | * @oob_required: must write chip->oob_poi to OOB |
Scott Wood | 46e1310 | 2016-05-30 13:57:57 -0500 | [diff] [blame] | 2917 | * @page: page number to write |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2918 | */ |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2919 | static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip, |
Scott Wood | 46e1310 | 2016-05-30 13:57:57 -0500 | [diff] [blame] | 2920 | const uint8_t *buf, int oob_required, |
| 2921 | int page) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2922 | { |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2923 | int i, eccsize = chip->ecc.size; |
| 2924 | int eccbytes = chip->ecc.bytes; |
| 2925 | int eccsteps = chip->ecc.steps; |
| 2926 | uint8_t *ecc_calc = chip->buffers->ecccalc; |
| 2927 | const uint8_t *p = buf; |
| 2928 | uint32_t *eccpos = chip->ecc.layout->eccpos; |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2929 | int ret; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2930 | |
| 2931 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 2932 | chip->ecc.hwctl(mtd, NAND_ECC_WRITE); |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2933 | |
| 2934 | ret = nand_write_data_op(chip, p, eccsize, false); |
| 2935 | if (ret) |
| 2936 | return ret; |
| 2937 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 2938 | chip->ecc.calculate(mtd, p, &ecc_calc[i]); |
| 2939 | } |
| 2940 | |
| 2941 | for (i = 0; i < chip->ecc.total; i++) |
| 2942 | chip->oob_poi[eccpos[i]] = ecc_calc[i]; |
| 2943 | |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2944 | ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false); |
| 2945 | if (ret) |
| 2946 | return ret; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 2947 | |
| 2948 | return 0; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2949 | } |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 2950 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2951 | |
| 2952 | /** |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 2953 | * nand_write_subpage_hwecc - [REPLACEABLE] hardware ECC based subpage write |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2954 | * @mtd: mtd info structure |
| 2955 | * @chip: nand chip info structure |
| 2956 | * @offset: column address of subpage within the page |
| 2957 | * @data_len: data length |
| 2958 | * @buf: data buffer |
| 2959 | * @oob_required: must write chip->oob_poi to OOB |
Scott Wood | 46e1310 | 2016-05-30 13:57:57 -0500 | [diff] [blame] | 2960 | * @page: page number to write |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2961 | */ |
| 2962 | static int nand_write_subpage_hwecc(struct mtd_info *mtd, |
| 2963 | struct nand_chip *chip, uint32_t offset, |
| 2964 | uint32_t data_len, const uint8_t *buf, |
Scott Wood | 46e1310 | 2016-05-30 13:57:57 -0500 | [diff] [blame] | 2965 | int oob_required, int page) |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2966 | { |
| 2967 | uint8_t *oob_buf = chip->oob_poi; |
| 2968 | uint8_t *ecc_calc = chip->buffers->ecccalc; |
| 2969 | int ecc_size = chip->ecc.size; |
| 2970 | int ecc_bytes = chip->ecc.bytes; |
| 2971 | int ecc_steps = chip->ecc.steps; |
| 2972 | uint32_t *eccpos = chip->ecc.layout->eccpos; |
| 2973 | uint32_t start_step = offset / ecc_size; |
| 2974 | uint32_t end_step = (offset + data_len - 1) / ecc_size; |
| 2975 | int oob_bytes = mtd->oobsize / ecc_steps; |
| 2976 | int step, i; |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2977 | int ret; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2978 | |
| 2979 | for (step = 0; step < ecc_steps; step++) { |
| 2980 | /* configure controller for WRITE access */ |
| 2981 | chip->ecc.hwctl(mtd, NAND_ECC_WRITE); |
| 2982 | |
| 2983 | /* write data (untouched subpages already masked by 0xFF) */ |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 2984 | ret = nand_write_data_op(chip, buf, ecc_size, false); |
| 2985 | if (ret) |
| 2986 | return ret; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2987 | |
| 2988 | /* mask ECC of un-touched subpages by padding 0xFF */ |
| 2989 | if ((step < start_step) || (step > end_step)) |
| 2990 | memset(ecc_calc, 0xff, ecc_bytes); |
| 2991 | else |
| 2992 | chip->ecc.calculate(mtd, buf, ecc_calc); |
| 2993 | |
| 2994 | /* mask OOB of un-touched subpages by padding 0xFF */ |
| 2995 | /* if oob_required, preserve OOB metadata of written subpage */ |
| 2996 | if (!oob_required || (step < start_step) || (step > end_step)) |
| 2997 | memset(oob_buf, 0xff, oob_bytes); |
| 2998 | |
| 2999 | buf += ecc_size; |
| 3000 | ecc_calc += ecc_bytes; |
| 3001 | oob_buf += oob_bytes; |
| 3002 | } |
| 3003 | |
| 3004 | /* copy calculated ECC for whole page to chip->buffer->oob */ |
| 3005 | /* this include masked-value(0xFF) for unwritten subpages */ |
| 3006 | ecc_calc = chip->buffers->ecccalc; |
| 3007 | for (i = 0; i < chip->ecc.total; i++) |
| 3008 | chip->oob_poi[eccpos[i]] = ecc_calc[i]; |
| 3009 | |
| 3010 | /* write OOB buffer to NAND device */ |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 3011 | ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false); |
| 3012 | if (ret) |
| 3013 | return ret; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3014 | |
| 3015 | return 0; |
| 3016 | } |
| 3017 | |
| 3018 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3019 | /** |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3020 | * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write |
| 3021 | * @mtd: mtd info structure |
| 3022 | * @chip: nand chip info structure |
| 3023 | * @buf: data buffer |
| 3024 | * @oob_required: must write chip->oob_poi to OOB |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 3025 | * @page: page number to write |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3026 | * |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3027 | * The hw generator calculates the error syndrome automatically. Therefore we |
| 3028 | * need a special oob layout and handling. |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3029 | */ |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3030 | static int nand_write_page_syndrome(struct mtd_info *mtd, |
| 3031 | struct nand_chip *chip, |
Scott Wood | 46e1310 | 2016-05-30 13:57:57 -0500 | [diff] [blame] | 3032 | const uint8_t *buf, int oob_required, |
| 3033 | int page) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3034 | { |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3035 | int i, eccsize = chip->ecc.size; |
| 3036 | int eccbytes = chip->ecc.bytes; |
| 3037 | int eccsteps = chip->ecc.steps; |
| 3038 | const uint8_t *p = buf; |
| 3039 | uint8_t *oob = chip->oob_poi; |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 3040 | int ret; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3041 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3042 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3043 | chip->ecc.hwctl(mtd, NAND_ECC_WRITE); |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 3044 | |
| 3045 | ret = nand_write_data_op(chip, p, eccsize, false); |
| 3046 | if (ret) |
| 3047 | return ret; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3048 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3049 | if (chip->ecc.prepad) { |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 3050 | ret = nand_write_data_op(chip, oob, chip->ecc.prepad, |
| 3051 | false); |
| 3052 | if (ret) |
| 3053 | return ret; |
| 3054 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3055 | oob += chip->ecc.prepad; |
| 3056 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3057 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3058 | chip->ecc.calculate(mtd, p, oob); |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 3059 | |
| 3060 | ret = nand_write_data_op(chip, oob, eccbytes, false); |
| 3061 | if (ret) |
| 3062 | return ret; |
| 3063 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3064 | oob += eccbytes; |
| 3065 | |
| 3066 | if (chip->ecc.postpad) { |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 3067 | ret = nand_write_data_op(chip, oob, chip->ecc.postpad, |
| 3068 | false); |
| 3069 | if (ret) |
| 3070 | return ret; |
| 3071 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3072 | oob += chip->ecc.postpad; |
| 3073 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3074 | } |
| 3075 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3076 | /* Calculate remaining oob bytes */ |
| 3077 | i = mtd->oobsize - (oob - chip->oob_poi); |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 3078 | if (i) { |
| 3079 | ret = nand_write_data_op(chip, oob, i, false); |
| 3080 | if (ret) |
| 3081 | return ret; |
| 3082 | } |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3083 | |
| 3084 | return 0; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3085 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3086 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3087 | /** |
| 3088 | * nand_write_page - [REPLACEABLE] write one page |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3089 | * @mtd: MTD device structure |
| 3090 | * @chip: NAND chip descriptor |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3091 | * @offset: address offset within the page |
| 3092 | * @data_len: length of actual data to be written |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3093 | * @buf: the data to write |
| 3094 | * @oob_required: must write chip->oob_poi to OOB |
| 3095 | * @page: page number to write |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3096 | * @raw: use _raw version of write_page |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3097 | */ |
| 3098 | static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip, |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3099 | uint32_t offset, int data_len, const uint8_t *buf, |
Boris Brezillon | b9bf43c | 2017-11-22 02:38:24 +0900 | [diff] [blame] | 3100 | int oob_required, int page, int raw) |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3101 | { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3102 | int status, subpage; |
| 3103 | |
| 3104 | if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && |
| 3105 | chip->ecc.write_subpage) |
| 3106 | subpage = offset || (data_len < mtd->writesize); |
| 3107 | else |
| 3108 | subpage = 0; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3109 | |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 3110 | if (nand_standard_page_accessors(&chip->ecc)) { |
| 3111 | status = nand_prog_page_begin_op(chip, page, 0, NULL, 0); |
| 3112 | if (status) |
| 3113 | return status; |
| 3114 | } |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3115 | |
| 3116 | if (unlikely(raw)) |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3117 | status = chip->ecc.write_page_raw(mtd, chip, buf, |
Scott Wood | 46e1310 | 2016-05-30 13:57:57 -0500 | [diff] [blame] | 3118 | oob_required, page); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3119 | else if (subpage) |
| 3120 | status = chip->ecc.write_subpage(mtd, chip, offset, data_len, |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 3121 | buf, oob_required, page); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3122 | else |
Scott Wood | 46e1310 | 2016-05-30 13:57:57 -0500 | [diff] [blame] | 3123 | status = chip->ecc.write_page(mtd, chip, buf, oob_required, |
| 3124 | page); |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3125 | |
| 3126 | if (status < 0) |
| 3127 | return status; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3128 | |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 3129 | if (nand_standard_page_accessors(&chip->ecc)) |
| 3130 | return nand_prog_page_end_op(chip); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3131 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3132 | return 0; |
| 3133 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3134 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3135 | /** |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3136 | * nand_fill_oob - [INTERN] Transfer client buffer to oob |
| 3137 | * @mtd: MTD device structure |
| 3138 | * @oob: oob data buffer |
| 3139 | * @len: oob data write length |
| 3140 | * @ops: oob ops structure |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3141 | */ |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3142 | static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len, |
| 3143 | struct mtd_oob_ops *ops) |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3144 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 3145 | struct nand_chip *chip = mtd_to_nand(mtd); |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3146 | |
| 3147 | /* |
| 3148 | * Initialise to all 0xFF, to avoid the possibility of left over OOB |
| 3149 | * data from a previous OOB read. |
| 3150 | */ |
| 3151 | memset(chip->oob_poi, 0xff, mtd->oobsize); |
| 3152 | |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 3153 | switch (ops->mode) { |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 3154 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3155 | case MTD_OPS_PLACE_OOB: |
| 3156 | case MTD_OPS_RAW: |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3157 | memcpy(chip->oob_poi + ops->ooboffs, oob, len); |
| 3158 | return oob + len; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3159 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3160 | case MTD_OPS_AUTO_OOB: { |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3161 | struct nand_oobfree *free = chip->ecc.layout->oobfree; |
| 3162 | uint32_t boffs = 0, woffs = ops->ooboffs; |
| 3163 | size_t bytes = 0; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3164 | |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 3165 | for (; free->length && len; free++, len -= bytes) { |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3166 | /* Write request not from offset 0? */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3167 | if (unlikely(woffs)) { |
| 3168 | if (woffs >= free->length) { |
| 3169 | woffs -= free->length; |
| 3170 | continue; |
| 3171 | } |
| 3172 | boffs = free->offset + woffs; |
| 3173 | bytes = min_t(size_t, len, |
| 3174 | (free->length - woffs)); |
| 3175 | woffs = 0; |
| 3176 | } else { |
| 3177 | bytes = min_t(size_t, len, free->length); |
| 3178 | boffs = free->offset; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3179 | } |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3180 | memcpy(chip->oob_poi + boffs, oob, bytes); |
| 3181 | oob += bytes; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3182 | } |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3183 | return oob; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3184 | } |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3185 | default: |
| 3186 | BUG(); |
| 3187 | } |
| 3188 | return NULL; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3189 | } |
| 3190 | |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 3191 | #define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3192 | |
| 3193 | /** |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3194 | * nand_do_write_ops - [INTERN] NAND write with ECC |
| 3195 | * @mtd: MTD device structure |
| 3196 | * @to: offset to write to |
| 3197 | * @ops: oob operations description structure |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3198 | * |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3199 | * NAND write with ECC. |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3200 | */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3201 | static int nand_do_write_ops(struct mtd_info *mtd, loff_t to, |
| 3202 | struct mtd_oob_ops *ops) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3203 | { |
Boris Brezillon | b9bf43c | 2017-11-22 02:38:24 +0900 | [diff] [blame] | 3204 | int chipnr, realpage, page, column; |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 3205 | struct nand_chip *chip = mtd_to_nand(mtd); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3206 | uint32_t writelen = ops->len; |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 3207 | |
| 3208 | uint32_t oobwritelen = ops->ooblen; |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 3209 | uint32_t oobmaxlen = mtd_oobavail(mtd, ops); |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 3210 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3211 | uint8_t *oob = ops->oobbuf; |
| 3212 | uint8_t *buf = ops->datbuf; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3213 | int ret; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3214 | int oob_required = oob ? 1 : 0; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3215 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3216 | ops->retlen = 0; |
| 3217 | if (!writelen) |
| 3218 | return 0; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3219 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3220 | /* Reject writes, which are not page aligned */ |
| 3221 | if (NOTALIGNED(to)) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3222 | pr_notice("%s: attempt to write non page aligned data\n", |
| 3223 | __func__); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3224 | return -EINVAL; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3225 | } |
| 3226 | |
| 3227 | column = to & (mtd->writesize - 1); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3228 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3229 | chipnr = (int)(to >> chip->chip_shift); |
| 3230 | chip->select_chip(mtd, chipnr); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3231 | |
| 3232 | /* Check, if it is write protected */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3233 | if (nand_check_wp(mtd)) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3234 | ret = -EIO; |
| 3235 | goto err_out; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3236 | } |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 3237 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3238 | realpage = (int)(to >> chip->page_shift); |
| 3239 | page = realpage & chip->pagemask; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3240 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3241 | /* Invalidate the page cache, when we write to the cached page */ |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 3242 | if (to <= ((loff_t)chip->pagebuf << chip->page_shift) && |
| 3243 | ((loff_t)chip->pagebuf << chip->page_shift) < (to + ops->len)) |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3244 | chip->pagebuf = -1; |
| 3245 | |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 3246 | /* Don't allow multipage oob writes with offset */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3247 | if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) { |
| 3248 | ret = -EINVAL; |
| 3249 | goto err_out; |
| 3250 | } |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 3251 | |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 3252 | while (1) { |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3253 | int bytes = mtd->writesize; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3254 | uint8_t *wbuf = buf; |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 3255 | int use_bufpoi; |
Hector Palacios | e4fcdbb | 2016-07-18 09:37:41 +0200 | [diff] [blame] | 3256 | int part_pagewr = (column || writelen < mtd->writesize); |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 3257 | |
| 3258 | if (part_pagewr) |
| 3259 | use_bufpoi = 1; |
Masahiro Yamada | b9c07b6 | 2017-11-22 02:38:27 +0900 | [diff] [blame] | 3260 | else if (chip->options & NAND_USE_BOUNCE_BUFFER) |
| 3261 | use_bufpoi = !IS_ALIGNED((unsigned long)buf, |
| 3262 | chip->buf_align); |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 3263 | else |
| 3264 | use_bufpoi = 0; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3265 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3266 | WATCHDOG_RESET(); |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 3267 | /* Partial page write?, or need to use bounce buffer */ |
| 3268 | if (use_bufpoi) { |
| 3269 | pr_debug("%s: using write bounce buffer for buf@%p\n", |
| 3270 | __func__, buf); |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 3271 | if (part_pagewr) |
| 3272 | bytes = min_t(int, bytes - column, writelen); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3273 | chip->pagebuf = -1; |
| 3274 | memset(chip->buffers->databuf, 0xff, mtd->writesize); |
| 3275 | memcpy(&chip->buffers->databuf[column], buf, bytes); |
| 3276 | wbuf = chip->buffers->databuf; |
Sergei Poselenov | 04fbaa0 | 2008-06-06 15:42:43 +0200 | [diff] [blame] | 3277 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3278 | |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 3279 | if (unlikely(oob)) { |
| 3280 | size_t len = min(oobwritelen, oobmaxlen); |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3281 | oob = nand_fill_oob(mtd, oob, len, ops); |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 3282 | oobwritelen -= len; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3283 | } else { |
| 3284 | /* We still need to erase leftover OOB data */ |
| 3285 | memset(chip->oob_poi, 0xff, mtd->oobsize); |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 3286 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3287 | ret = chip->write_page(mtd, chip, column, bytes, wbuf, |
Boris Brezillon | b9bf43c | 2017-11-22 02:38:24 +0900 | [diff] [blame] | 3288 | oob_required, page, |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3289 | (ops->mode == MTD_OPS_RAW)); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3290 | if (ret) |
| 3291 | break; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3292 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3293 | writelen -= bytes; |
| 3294 | if (!writelen) |
| 3295 | break; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3296 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3297 | column = 0; |
| 3298 | buf += bytes; |
| 3299 | realpage++; |
| 3300 | |
| 3301 | page = realpage & chip->pagemask; |
| 3302 | /* Check, if we cross a chip boundary */ |
| 3303 | if (!page) { |
| 3304 | chipnr++; |
| 3305 | chip->select_chip(mtd, -1); |
| 3306 | chip->select_chip(mtd, chipnr); |
| 3307 | } |
| 3308 | } |
| 3309 | |
| 3310 | ops->retlen = ops->len - writelen; |
| 3311 | if (unlikely(oob)) |
| 3312 | ops->oobretlen = ops->ooblen; |
| 3313 | |
| 3314 | err_out: |
| 3315 | chip->select_chip(mtd, -1); |
| 3316 | return ret; |
| 3317 | } |
| 3318 | |
| 3319 | /** |
| 3320 | * panic_nand_write - [MTD Interface] NAND write with ECC |
| 3321 | * @mtd: MTD device structure |
| 3322 | * @to: offset to write to |
| 3323 | * @len: number of bytes to write |
| 3324 | * @retlen: pointer to variable to store the number of written bytes |
| 3325 | * @buf: the data to write |
| 3326 | * |
| 3327 | * NAND write with ECC. Used when performing writes in interrupt context, this |
| 3328 | * may for example be called by mtdoops when writing an oops while in panic. |
| 3329 | */ |
| 3330 | static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len, |
| 3331 | size_t *retlen, const uint8_t *buf) |
| 3332 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 3333 | struct nand_chip *chip = mtd_to_nand(mtd); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3334 | struct mtd_oob_ops ops; |
| 3335 | int ret; |
| 3336 | |
| 3337 | /* Wait for the device to get ready */ |
| 3338 | panic_nand_wait(mtd, chip, 400); |
| 3339 | |
| 3340 | /* Grab the device */ |
| 3341 | panic_nand_get_device(chip, mtd, FL_WRITING); |
| 3342 | |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 3343 | memset(&ops, 0, sizeof(ops)); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3344 | ops.len = len; |
| 3345 | ops.datbuf = (uint8_t *)buf; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3346 | ops.mode = MTD_OPS_PLACE_OOB; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3347 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3348 | ret = nand_do_write_ops(mtd, to, &ops); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3349 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3350 | *retlen = ops.retlen; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3351 | return ret; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3352 | } |
| 3353 | |
| 3354 | /** |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3355 | * nand_do_write_oob - [MTD Interface] NAND write out-of-band |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3356 | * @mtd: MTD device structure |
| 3357 | * @to: offset to write to |
| 3358 | * @ops: oob operation description structure |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3359 | * |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3360 | * NAND write out-of-band. |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3361 | */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3362 | static int nand_do_write_oob(struct mtd_info *mtd, loff_t to, |
| 3363 | struct mtd_oob_ops *ops) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3364 | { |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3365 | int chipnr, page, status, len; |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 3366 | struct nand_chip *chip = mtd_to_nand(mtd); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3367 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3368 | pr_debug("%s: to = 0x%08x, len = %i\n", |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 3369 | __func__, (unsigned int)to, (int)ops->ooblen); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3370 | |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 3371 | len = mtd_oobavail(mtd, ops); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3372 | |
| 3373 | /* Do not allow write past end of page */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3374 | if ((ops->ooboffs + ops->ooblen) > len) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3375 | pr_debug("%s: attempt to write past end of page\n", |
| 3376 | __func__); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3377 | return -EINVAL; |
| 3378 | } |
| 3379 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3380 | if (unlikely(ops->ooboffs >= len)) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3381 | pr_debug("%s: attempt to start write outside oob\n", |
| 3382 | __func__); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3383 | return -EINVAL; |
| 3384 | } |
| 3385 | |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 3386 | /* Do not allow write past end of device */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3387 | if (unlikely(to >= mtd->size || |
| 3388 | ops->ooboffs + ops->ooblen > |
| 3389 | ((mtd->size >> chip->page_shift) - |
| 3390 | (to >> chip->page_shift)) * len)) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3391 | pr_debug("%s: attempt to write beyond end of device\n", |
| 3392 | __func__); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3393 | return -EINVAL; |
| 3394 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3395 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3396 | chipnr = (int)(to >> chip->chip_shift); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3397 | |
| 3398 | /* |
| 3399 | * Reset the chip. Some chips (like the Toshiba TC5832DC found in one |
| 3400 | * of my DiskOnChip 2000 test units) will clear the whole data page too |
| 3401 | * if we don't do this. I have no clue why, but I seem to have 'fixed' |
| 3402 | * it in the doc2000 driver in August 1999. dwmw2. |
| 3403 | */ |
Boris Brezillon | 7ec6dc5 | 2017-11-22 02:38:20 +0900 | [diff] [blame] | 3404 | nand_reset(chip, chipnr); |
| 3405 | |
| 3406 | chip->select_chip(mtd, chipnr); |
| 3407 | |
| 3408 | /* Shift to get page */ |
| 3409 | page = (int)(to >> chip->page_shift); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3410 | |
| 3411 | /* Check, if it is write protected */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3412 | if (nand_check_wp(mtd)) { |
| 3413 | chip->select_chip(mtd, -1); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3414 | return -EROFS; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3415 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3416 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3417 | /* Invalidate the page cache, if we write to the cached page */ |
| 3418 | if (page == chip->pagebuf) |
| 3419 | chip->pagebuf = -1; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3420 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3421 | nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops); |
| 3422 | |
| 3423 | if (ops->mode == MTD_OPS_RAW) |
| 3424 | status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask); |
| 3425 | else |
| 3426 | status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3427 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3428 | chip->select_chip(mtd, -1); |
| 3429 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3430 | if (status) |
| 3431 | return status; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3432 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3433 | ops->oobretlen = ops->ooblen; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3434 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3435 | return 0; |
| 3436 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3437 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3438 | /** |
| 3439 | * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3440 | * @mtd: MTD device structure |
| 3441 | * @to: offset to write to |
| 3442 | * @ops: oob operation description structure |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3443 | */ |
| 3444 | static int nand_write_oob(struct mtd_info *mtd, loff_t to, |
| 3445 | struct mtd_oob_ops *ops) |
| 3446 | { |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3447 | int ret = -ENOTSUPP; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 3448 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3449 | ops->retlen = 0; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3450 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3451 | /* Do not allow writes past end of device */ |
| 3452 | if (ops->datbuf && (to + ops->len) > mtd->size) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3453 | pr_debug("%s: attempt to write beyond end of device\n", |
| 3454 | __func__); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3455 | return -EINVAL; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3456 | } |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3457 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3458 | nand_get_device(mtd, FL_WRITING); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3459 | |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 3460 | switch (ops->mode) { |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3461 | case MTD_OPS_PLACE_OOB: |
| 3462 | case MTD_OPS_AUTO_OOB: |
| 3463 | case MTD_OPS_RAW: |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3464 | break; |
| 3465 | |
| 3466 | default: |
| 3467 | goto out; |
| 3468 | } |
| 3469 | |
| 3470 | if (!ops->datbuf) |
| 3471 | ret = nand_do_write_oob(mtd, to, ops); |
| 3472 | else |
| 3473 | ret = nand_do_write_ops(mtd, to, ops); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3474 | |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 3475 | out: |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3476 | nand_release_device(mtd); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3477 | return ret; |
| 3478 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3479 | |
| 3480 | /** |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 3481 | * single_erase - [GENERIC] NAND standard block erase command function |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3482 | * @mtd: MTD device structure |
| 3483 | * @page: the page address of the block which will be erased |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3484 | * |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 3485 | * Standard erase command for NAND chips. Returns NAND status. |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3486 | */ |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 3487 | static int single_erase(struct mtd_info *mtd, int page) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3488 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 3489 | struct nand_chip *chip = mtd_to_nand(mtd); |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 3490 | unsigned int eraseblock; |
| 3491 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3492 | /* Send commands to erase a block */ |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 3493 | eraseblock = page >> (chip->phys_erase_shift - chip->page_shift); |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 3494 | |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 3495 | return nand_erase_op(chip, eraseblock); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3496 | } |
| 3497 | |
| 3498 | /** |
| 3499 | * nand_erase - [MTD Interface] erase block(s) |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3500 | * @mtd: MTD device structure |
| 3501 | * @instr: erase instruction |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3502 | * |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3503 | * Erase one ore more blocks. |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3504 | */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3505 | static int nand_erase(struct mtd_info *mtd, struct erase_info *instr) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3506 | { |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3507 | return nand_erase_nand(mtd, instr, 0); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3508 | } |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 3509 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3510 | /** |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3511 | * nand_erase_nand - [INTERN] erase block(s) |
| 3512 | * @mtd: MTD device structure |
| 3513 | * @instr: erase instruction |
| 3514 | * @allowbbt: allow erasing the bbt area |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3515 | * |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3516 | * Erase one ore more blocks. |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3517 | */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3518 | int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr, |
| 3519 | int allowbbt) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3520 | { |
Sandeep Paulraj | eab580c | 2009-10-30 13:51:23 -0400 | [diff] [blame] | 3521 | int page, status, pages_per_block, ret, chipnr; |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 3522 | struct nand_chip *chip = mtd_to_nand(mtd); |
Sandeep Paulraj | eab580c | 2009-10-30 13:51:23 -0400 | [diff] [blame] | 3523 | loff_t len; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3524 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3525 | pr_debug("%s: start = 0x%012llx, len = %llu\n", |
| 3526 | __func__, (unsigned long long)instr->addr, |
| 3527 | (unsigned long long)instr->len); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3528 | |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 3529 | if (check_offs_len(mtd, instr->addr, instr->len)) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3530 | return -EINVAL; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3531 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3532 | /* Grab the lock and see if the device is available */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3533 | nand_get_device(mtd, FL_ERASING); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3534 | |
| 3535 | /* Shift to get first page */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3536 | page = (int)(instr->addr >> chip->page_shift); |
| 3537 | chipnr = (int)(instr->addr >> chip->chip_shift); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3538 | |
| 3539 | /* Calculate pages in each block */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3540 | pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift); |
William Juul | b76ec38 | 2007-11-08 10:39:53 +0100 | [diff] [blame] | 3541 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3542 | /* Select the NAND device */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3543 | chip->select_chip(mtd, chipnr); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3544 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3545 | /* Check, if it is write protected */ |
| 3546 | if (nand_check_wp(mtd)) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3547 | pr_debug("%s: device is write protected!\n", |
| 3548 | __func__); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3549 | instr->state = MTD_ERASE_FAILED; |
| 3550 | goto erase_exit; |
| 3551 | } |
| 3552 | |
| 3553 | /* Loop through the pages */ |
| 3554 | len = instr->len; |
| 3555 | |
| 3556 | instr->state = MTD_ERASING; |
| 3557 | |
| 3558 | while (len) { |
Scott Wood | ea95b64 | 2011-02-02 18:15:57 -0600 | [diff] [blame] | 3559 | WATCHDOG_RESET(); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3560 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3561 | /* Check if we have a bad block, we do not erase bad blocks! */ |
Masahiro Yamada | f5a1902 | 2014-12-16 15:36:33 +0900 | [diff] [blame] | 3562 | if (!instr->scrub && nand_block_checkbad(mtd, ((loff_t) page) << |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 3563 | chip->page_shift, allowbbt)) { |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3564 | pr_warn("%s: attempt to erase a bad block at page 0x%08x\n", |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3565 | __func__, page); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3566 | instr->state = MTD_ERASE_FAILED; |
Farhan Ali | 7c05319 | 2021-02-24 15:25:53 -0800 | [diff] [blame] | 3567 | instr->fail_addr = |
| 3568 | ((loff_t)page << chip->page_shift); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3569 | goto erase_exit; |
| 3570 | } |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3571 | |
| 3572 | /* |
| 3573 | * Invalidate the page cache, if we erase the block which |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3574 | * contains the current cached page. |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3575 | */ |
| 3576 | if (page <= chip->pagebuf && chip->pagebuf < |
| 3577 | (page + pages_per_block)) |
| 3578 | chip->pagebuf = -1; |
| 3579 | |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 3580 | status = chip->erase(mtd, page & chip->pagemask); |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 3581 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3582 | /* See if block erase succeeded */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3583 | if (status & NAND_STATUS_FAIL) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3584 | pr_debug("%s: failed erase, page 0x%08x\n", |
| 3585 | __func__, page); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3586 | instr->state = MTD_ERASE_FAILED; |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 3587 | instr->fail_addr = |
| 3588 | ((loff_t)page << chip->page_shift); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3589 | goto erase_exit; |
| 3590 | } |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 3591 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3592 | /* Increment page address and decrement length */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3593 | len -= (1ULL << chip->phys_erase_shift); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3594 | page += pages_per_block; |
| 3595 | |
| 3596 | /* Check, if we cross a chip boundary */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3597 | if (len && !(page & chip->pagemask)) { |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3598 | chipnr++; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3599 | chip->select_chip(mtd, -1); |
| 3600 | chip->select_chip(mtd, chipnr); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3601 | } |
| 3602 | } |
| 3603 | instr->state = MTD_ERASE_DONE; |
| 3604 | |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 3605 | erase_exit: |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3606 | |
| 3607 | ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3608 | |
| 3609 | /* Deselect and wake up anyone waiting on the device */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3610 | chip->select_chip(mtd, -1); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3611 | nand_release_device(mtd); |
| 3612 | |
| 3613 | /* Return more or less happy */ |
| 3614 | return ret; |
| 3615 | } |
| 3616 | |
| 3617 | /** |
| 3618 | * nand_sync - [MTD Interface] sync |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3619 | * @mtd: MTD device structure |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3620 | * |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3621 | * Sync is actually a wait for chip ready function. |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3622 | */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3623 | static void nand_sync(struct mtd_info *mtd) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3624 | { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3625 | pr_debug("%s: called\n", __func__); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3626 | |
| 3627 | /* Grab the lock and see if the device is available */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3628 | nand_get_device(mtd, FL_SYNCING); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3629 | /* Release it and go back */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3630 | nand_release_device(mtd); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3631 | } |
| 3632 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3633 | /** |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3634 | * nand_block_isbad - [MTD Interface] Check if block at offset is bad |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3635 | * @mtd: MTD device structure |
| 3636 | * @offs: offset relative to mtd start |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3637 | */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3638 | static int nand_block_isbad(struct mtd_info *mtd, loff_t offs) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3639 | { |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 3640 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 3641 | int chipnr = (int)(offs >> chip->chip_shift); |
| 3642 | int ret; |
| 3643 | |
| 3644 | /* Select the NAND device */ |
| 3645 | nand_get_device(mtd, FL_READING); |
| 3646 | chip->select_chip(mtd, chipnr); |
| 3647 | |
| 3648 | ret = nand_block_checkbad(mtd, offs, 0); |
| 3649 | |
| 3650 | chip->select_chip(mtd, -1); |
| 3651 | nand_release_device(mtd); |
| 3652 | |
| 3653 | return ret; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3654 | } |
| 3655 | |
| 3656 | /** |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3657 | * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3658 | * @mtd: MTD device structure |
| 3659 | * @ofs: offset relative to mtd start |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3660 | */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3661 | static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3662 | { |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3663 | int ret; |
| 3664 | |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 3665 | ret = nand_block_isbad(mtd, ofs); |
| 3666 | if (ret) { |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3667 | /* If it was bad already, return success and do nothing */ |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3668 | if (ret > 0) |
| 3669 | return 0; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 3670 | return ret; |
| 3671 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3672 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3673 | return nand_block_markbad_lowlevel(mtd, ofs); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3674 | } |
| 3675 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3676 | /** |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3677 | * nand_onfi_set_features- [REPLACEABLE] set features for ONFI nand |
| 3678 | * @mtd: MTD device structure |
| 3679 | * @chip: nand chip info structure |
| 3680 | * @addr: feature address. |
| 3681 | * @subfeature_param: the subfeature parameters, a four bytes array. |
| 3682 | */ |
| 3683 | static int nand_onfi_set_features(struct mtd_info *mtd, struct nand_chip *chip, |
| 3684 | int addr, uint8_t *subfeature_param) |
| 3685 | { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3686 | #ifdef CONFIG_SYS_NAND_ONFI_DETECTION |
| 3687 | if (!chip->onfi_version || |
| 3688 | !(le16_to_cpu(chip->onfi_params.opt_cmd) |
| 3689 | & ONFI_OPT_CMD_SET_GET_FEATURES)) |
Mylène Josserand | c21946b | 2018-07-13 18:10:23 +0200 | [diff] [blame] | 3690 | return -ENOTSUPP; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3691 | #endif |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3692 | |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 3693 | return nand_set_features_op(chip, addr, subfeature_param); |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3694 | } |
| 3695 | |
| 3696 | /** |
| 3697 | * nand_onfi_get_features- [REPLACEABLE] get features for ONFI nand |
| 3698 | * @mtd: MTD device structure |
| 3699 | * @chip: nand chip info structure |
| 3700 | * @addr: feature address. |
| 3701 | * @subfeature_param: the subfeature parameters, a four bytes array. |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3702 | */ |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3703 | static int nand_onfi_get_features(struct mtd_info *mtd, struct nand_chip *chip, |
| 3704 | int addr, uint8_t *subfeature_param) |
| 3705 | { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3706 | #ifdef CONFIG_SYS_NAND_ONFI_DETECTION |
| 3707 | if (!chip->onfi_version || |
| 3708 | !(le16_to_cpu(chip->onfi_params.opt_cmd) |
| 3709 | & ONFI_OPT_CMD_SET_GET_FEATURES)) |
Mylène Josserand | c21946b | 2018-07-13 18:10:23 +0200 | [diff] [blame] | 3710 | return -ENOTSUPP; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3711 | #endif |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3712 | |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 3713 | return nand_get_features_op(chip, addr, subfeature_param); |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3714 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3715 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3716 | /* Set default functions */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3717 | static void nand_set_defaults(struct nand_chip *chip, int busw) |
| 3718 | { |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3719 | /* check for proper chip_delay setup, set 20us if not */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3720 | if (!chip->chip_delay) |
| 3721 | chip->chip_delay = 20; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3722 | |
| 3723 | /* check, if a user supplied command function given */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3724 | if (chip->cmdfunc == NULL) |
| 3725 | chip->cmdfunc = nand_command; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3726 | |
| 3727 | /* check, if a user supplied wait function given */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3728 | if (chip->waitfunc == NULL) |
| 3729 | chip->waitfunc = nand_wait; |
| 3730 | |
| 3731 | if (!chip->select_chip) |
| 3732 | chip->select_chip = nand_select_chip; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3733 | |
| 3734 | /* set for ONFI nand */ |
| 3735 | if (!chip->onfi_set_features) |
| 3736 | chip->onfi_set_features = nand_onfi_set_features; |
| 3737 | if (!chip->onfi_get_features) |
| 3738 | chip->onfi_get_features = nand_onfi_get_features; |
| 3739 | |
| 3740 | /* If called twice, pointers that depend on busw may need to be reset */ |
| 3741 | if (!chip->read_byte || chip->read_byte == nand_read_byte) |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3742 | chip->read_byte = busw ? nand_read_byte16 : nand_read_byte; |
| 3743 | if (!chip->read_word) |
| 3744 | chip->read_word = nand_read_word; |
| 3745 | if (!chip->block_bad) |
| 3746 | chip->block_bad = nand_block_bad; |
| 3747 | if (!chip->block_markbad) |
| 3748 | chip->block_markbad = nand_default_block_markbad; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3749 | if (!chip->write_buf || chip->write_buf == nand_write_buf) |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3750 | chip->write_buf = busw ? nand_write_buf16 : nand_write_buf; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3751 | if (!chip->write_byte || chip->write_byte == nand_write_byte) |
| 3752 | chip->write_byte = busw ? nand_write_byte16 : nand_write_byte; |
| 3753 | if (!chip->read_buf || chip->read_buf == nand_read_buf) |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3754 | chip->read_buf = busw ? nand_read_buf16 : nand_read_buf; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3755 | if (!chip->scan_bbt) |
| 3756 | chip->scan_bbt = nand_default_bbt; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3757 | |
| 3758 | if (!chip->controller) { |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3759 | chip->controller = &chip->hwcontrol; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3760 | spin_lock_init(&chip->controller->lock); |
| 3761 | init_waitqueue_head(&chip->controller->wq); |
| 3762 | } |
| 3763 | |
Masahiro Yamada | b9c07b6 | 2017-11-22 02:38:27 +0900 | [diff] [blame] | 3764 | if (!chip->buf_align) |
| 3765 | chip->buf_align = 1; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 3766 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 3767 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3768 | /* Sanitize ONFI strings so we can safely print them */ |
Christian Hitz | 6f1c9e0 | 2011-10-12 09:32:05 +0200 | [diff] [blame] | 3769 | static void sanitize_string(char *s, size_t len) |
| 3770 | { |
| 3771 | ssize_t i; |
| 3772 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3773 | /* Null terminate */ |
Christian Hitz | 6f1c9e0 | 2011-10-12 09:32:05 +0200 | [diff] [blame] | 3774 | s[len - 1] = 0; |
| 3775 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3776 | /* Remove non printable chars */ |
Christian Hitz | 6f1c9e0 | 2011-10-12 09:32:05 +0200 | [diff] [blame] | 3777 | for (i = 0; i < len - 1; i++) { |
| 3778 | if (s[i] < ' ' || s[i] > 127) |
| 3779 | s[i] = '?'; |
| 3780 | } |
| 3781 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3782 | /* Remove trailing spaces */ |
Christian Hitz | 6f1c9e0 | 2011-10-12 09:32:05 +0200 | [diff] [blame] | 3783 | strim(s); |
| 3784 | } |
| 3785 | |
Florian Fainelli | c98a935 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3786 | static u16 onfi_crc16(u16 crc, u8 const *p, size_t len) |
| 3787 | { |
| 3788 | int i; |
Florian Fainelli | c98a935 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3789 | while (len--) { |
| 3790 | crc ^= *p++ << 8; |
| 3791 | for (i = 0; i < 8; i++) |
| 3792 | crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0); |
| 3793 | } |
| 3794 | |
| 3795 | return crc; |
| 3796 | } |
| 3797 | |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 3798 | #ifdef CONFIG_SYS_NAND_ONFI_DETECTION |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3799 | /* Parse the Extended Parameter Page. */ |
| 3800 | static int nand_flash_detect_ext_param_page(struct mtd_info *mtd, |
| 3801 | struct nand_chip *chip, struct nand_onfi_params *p) |
| 3802 | { |
| 3803 | struct onfi_ext_param_page *ep; |
| 3804 | struct onfi_ext_section *s; |
| 3805 | struct onfi_ext_ecc_info *ecc; |
| 3806 | uint8_t *cursor; |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 3807 | int ret; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3808 | int len; |
| 3809 | int i; |
| 3810 | |
| 3811 | len = le16_to_cpu(p->ext_param_page_length) * 16; |
| 3812 | ep = kmalloc(len, GFP_KERNEL); |
| 3813 | if (!ep) |
| 3814 | return -ENOMEM; |
| 3815 | |
| 3816 | /* Send our own NAND_CMD_PARAM. */ |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 3817 | ret = nand_read_param_page_op(chip, 0, NULL, 0); |
| 3818 | if (ret) |
| 3819 | goto ext_out; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3820 | |
| 3821 | /* Use the Change Read Column command to skip the ONFI param pages. */ |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 3822 | ret = nand_change_read_column_op(chip, |
| 3823 | sizeof(*p) * p->num_of_param_pages, |
| 3824 | ep, len, true); |
| 3825 | if (ret) |
| 3826 | goto ext_out; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3827 | |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 3828 | ret = -EINVAL; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3829 | if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2) |
| 3830 | != le16_to_cpu(ep->crc))) { |
| 3831 | pr_debug("fail in the CRC.\n"); |
| 3832 | goto ext_out; |
| 3833 | } |
| 3834 | |
| 3835 | /* |
| 3836 | * Check the signature. |
| 3837 | * Do not strictly follow the ONFI spec, maybe changed in future. |
| 3838 | */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3839 | if (strncmp((char *)ep->sig, "EPPS", 4)) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3840 | pr_debug("The signature is invalid.\n"); |
| 3841 | goto ext_out; |
| 3842 | } |
| 3843 | |
| 3844 | /* find the ECC section. */ |
| 3845 | cursor = (uint8_t *)(ep + 1); |
| 3846 | for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) { |
| 3847 | s = ep->sections + i; |
| 3848 | if (s->type == ONFI_SECTION_TYPE_2) |
| 3849 | break; |
| 3850 | cursor += s->length * 16; |
| 3851 | } |
| 3852 | if (i == ONFI_EXT_SECTION_MAX) { |
| 3853 | pr_debug("We can not find the ECC section.\n"); |
| 3854 | goto ext_out; |
| 3855 | } |
| 3856 | |
| 3857 | /* get the info we want. */ |
| 3858 | ecc = (struct onfi_ext_ecc_info *)cursor; |
| 3859 | |
| 3860 | if (!ecc->codeword_size) { |
| 3861 | pr_debug("Invalid codeword size\n"); |
| 3862 | goto ext_out; |
| 3863 | } |
| 3864 | |
| 3865 | chip->ecc_strength_ds = ecc->ecc_bits; |
| 3866 | chip->ecc_step_ds = 1 << ecc->codeword_size; |
| 3867 | ret = 0; |
| 3868 | |
| 3869 | ext_out: |
| 3870 | kfree(ep); |
| 3871 | return ret; |
| 3872 | } |
| 3873 | |
| 3874 | static int nand_setup_read_retry_micron(struct mtd_info *mtd, int retry_mode) |
| 3875 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 3876 | struct nand_chip *chip = mtd_to_nand(mtd); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3877 | uint8_t feature[ONFI_SUBFEATURE_PARAM_LEN] = {retry_mode}; |
| 3878 | |
| 3879 | return chip->onfi_set_features(mtd, chip, ONFI_FEATURE_ADDR_READ_RETRY, |
| 3880 | feature); |
| 3881 | } |
| 3882 | |
| 3883 | /* |
| 3884 | * Configure chip properties from Micron vendor-specific ONFI table |
| 3885 | */ |
| 3886 | static void nand_onfi_detect_micron(struct nand_chip *chip, |
| 3887 | struct nand_onfi_params *p) |
| 3888 | { |
| 3889 | struct nand_onfi_vendor_micron *micron = (void *)p->vendor; |
| 3890 | |
| 3891 | if (le16_to_cpu(p->vendor_revision) < 1) |
| 3892 | return; |
| 3893 | |
| 3894 | chip->read_retries = micron->read_retry_options; |
| 3895 | chip->setup_read_retry = nand_setup_read_retry_micron; |
| 3896 | } |
| 3897 | |
Florian Fainelli | c98a935 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3898 | /* |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3899 | * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise. |
Florian Fainelli | c98a935 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3900 | */ |
Michael Trimarchi | dc7fdd6 | 2022-07-20 18:22:04 +0200 | [diff] [blame] | 3901 | static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip) |
Florian Fainelli | c98a935 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3902 | { |
| 3903 | struct nand_onfi_params *p = &chip->onfi_params; |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 3904 | char id[4]; |
| 3905 | int i, ret, val; |
Florian Fainelli | c98a935 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3906 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3907 | /* Try ONFI for unknown chip or LP */ |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 3908 | ret = nand_readid_op(chip, 0x20, id, sizeof(id)); |
| 3909 | if (ret || strncmp(id, "ONFI", 4)) |
| 3910 | return 0; |
| 3911 | |
| 3912 | ret = nand_read_param_page_op(chip, 0, NULL, 0); |
| 3913 | if (ret) |
Florian Fainelli | c98a935 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3914 | return 0; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3915 | |
Florian Fainelli | c98a935 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3916 | for (i = 0; i < 3; i++) { |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 3917 | ret = nand_read_data_op(chip, p, sizeof(*p), true); |
| 3918 | if (ret) |
| 3919 | return 0; |
| 3920 | |
Florian Fainelli | c98a935 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3921 | if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) == |
Christian Hitz | 13fc0e2 | 2011-10-12 09:32:01 +0200 | [diff] [blame] | 3922 | le16_to_cpu(p->crc)) { |
Florian Fainelli | c98a935 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3923 | break; |
| 3924 | } |
| 3925 | } |
| 3926 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3927 | if (i == 3) { |
| 3928 | pr_err("Could not find valid ONFI parameter page; aborting\n"); |
Florian Fainelli | c98a935 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3929 | return 0; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3930 | } |
Florian Fainelli | c98a935 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3931 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 3932 | /* Check version */ |
Florian Fainelli | c98a935 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3933 | val = le16_to_cpu(p->revision); |
Florian Fainelli | 3eb4fc6 | 2011-04-03 18:23:56 +0200 | [diff] [blame] | 3934 | if (val & (1 << 5)) |
| 3935 | chip->onfi_version = 23; |
| 3936 | else if (val & (1 << 4)) |
Florian Fainelli | c98a935 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3937 | chip->onfi_version = 22; |
| 3938 | else if (val & (1 << 3)) |
| 3939 | chip->onfi_version = 21; |
| 3940 | else if (val & (1 << 2)) |
| 3941 | chip->onfi_version = 20; |
Florian Fainelli | 3eb4fc6 | 2011-04-03 18:23:56 +0200 | [diff] [blame] | 3942 | else if (val & (1 << 1)) |
Florian Fainelli | c98a935 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3943 | chip->onfi_version = 10; |
Florian Fainelli | 3eb4fc6 | 2011-04-03 18:23:56 +0200 | [diff] [blame] | 3944 | |
| 3945 | if (!chip->onfi_version) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3946 | pr_info("unsupported ONFI version: %d\n", val); |
Florian Fainelli | 3eb4fc6 | 2011-04-03 18:23:56 +0200 | [diff] [blame] | 3947 | return 0; |
| 3948 | } |
Florian Fainelli | c98a935 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3949 | |
Christian Hitz | 6f1c9e0 | 2011-10-12 09:32:05 +0200 | [diff] [blame] | 3950 | sanitize_string(p->manufacturer, sizeof(p->manufacturer)); |
| 3951 | sanitize_string(p->model, sizeof(p->model)); |
Florian Fainelli | c98a935 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3952 | if (!mtd->name) |
| 3953 | mtd->name = p->model; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3954 | |
Florian Fainelli | c98a935 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3955 | mtd->writesize = le32_to_cpu(p->byte_per_page); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3956 | |
| 3957 | /* |
| 3958 | * pages_per_block and blocks_per_lun may not be a power-of-2 size |
| 3959 | * (don't ask me who thought of this...). MTD assumes that these |
| 3960 | * dimensions will be power-of-2, so just truncate the remaining area. |
| 3961 | */ |
| 3962 | mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1); |
| 3963 | mtd->erasesize *= mtd->writesize; |
| 3964 | |
Florian Fainelli | c98a935 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3965 | mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3966 | |
| 3967 | /* See erasesize comment */ |
| 3968 | chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1); |
Matthieu CASTET | b20b6f2 | 2012-03-19 15:35:25 +0100 | [diff] [blame] | 3969 | chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3970 | chip->bits_per_cell = p->bits_per_cell; |
| 3971 | |
| 3972 | if (onfi_feature(chip) & ONFI_FEATURE_16_BIT_BUS) |
Michael Trimarchi | dc7fdd6 | 2022-07-20 18:22:04 +0200 | [diff] [blame] | 3973 | chip->options |= NAND_BUSWIDTH_16; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 3974 | |
| 3975 | if (p->ecc_bits != 0xff) { |
| 3976 | chip->ecc_strength_ds = p->ecc_bits; |
| 3977 | chip->ecc_step_ds = 512; |
| 3978 | } else if (chip->onfi_version >= 21 && |
| 3979 | (onfi_feature(chip) & ONFI_FEATURE_EXT_PARAM_PAGE)) { |
| 3980 | |
| 3981 | /* |
| 3982 | * The nand_flash_detect_ext_param_page() uses the |
| 3983 | * Change Read Column command which maybe not supported |
| 3984 | * by the chip->cmdfunc. So try to update the chip->cmdfunc |
| 3985 | * now. We do not replace user supplied command function. |
| 3986 | */ |
| 3987 | if (mtd->writesize > 512 && chip->cmdfunc == nand_command) |
| 3988 | chip->cmdfunc = nand_command_lp; |
| 3989 | |
| 3990 | /* The Extended Parameter Page is supported since ONFI 2.1. */ |
| 3991 | if (nand_flash_detect_ext_param_page(mtd, chip, p)) |
| 3992 | pr_warn("Failed to detect ONFI extended param page\n"); |
| 3993 | } else { |
| 3994 | pr_warn("Could not retrieve ONFI ECC requirements\n"); |
| 3995 | } |
| 3996 | |
| 3997 | if (p->jedec_id == NAND_MFR_MICRON) |
| 3998 | nand_onfi_detect_micron(chip, p); |
Florian Fainelli | c98a935 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 3999 | |
| 4000 | return 1; |
| 4001 | } |
| 4002 | #else |
Michael Trimarchi | dc7fdd6 | 2022-07-20 18:22:04 +0200 | [diff] [blame] | 4003 | static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip) |
Florian Fainelli | c98a935 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 4004 | { |
| 4005 | return 0; |
| 4006 | } |
| 4007 | #endif |
| 4008 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4009 | /* |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 4010 | * Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise. |
| 4011 | */ |
Michael Trimarchi | dc7fdd6 | 2022-07-20 18:22:04 +0200 | [diff] [blame] | 4012 | static int nand_flash_detect_jedec(struct mtd_info *mtd, struct nand_chip *chip) |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 4013 | { |
| 4014 | struct nand_jedec_params *p = &chip->jedec_params; |
| 4015 | struct jedec_ecc_info *ecc; |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 4016 | char id[5]; |
| 4017 | int i, val, ret; |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 4018 | |
| 4019 | /* Try JEDEC for unknown chip or LP */ |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 4020 | ret = nand_readid_op(chip, 0x40, id, sizeof(id)); |
| 4021 | if (ret || strncmp(id, "JEDEC", sizeof(id))) |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 4022 | return 0; |
| 4023 | |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 4024 | ret = nand_read_param_page_op(chip, 0x40, NULL, 0); |
| 4025 | if (ret) |
| 4026 | return 0; |
| 4027 | |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 4028 | for (i = 0; i < 3; i++) { |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 4029 | ret = nand_read_data_op(chip, p, sizeof(*p), true); |
| 4030 | if (ret) |
| 4031 | return 0; |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 4032 | |
| 4033 | if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 510) == |
| 4034 | le16_to_cpu(p->crc)) |
| 4035 | break; |
| 4036 | } |
| 4037 | |
| 4038 | if (i == 3) { |
| 4039 | pr_err("Could not find valid JEDEC parameter page; aborting\n"); |
| 4040 | return 0; |
| 4041 | } |
| 4042 | |
| 4043 | /* Check version */ |
| 4044 | val = le16_to_cpu(p->revision); |
| 4045 | if (val & (1 << 2)) |
| 4046 | chip->jedec_version = 10; |
| 4047 | else if (val & (1 << 1)) |
| 4048 | chip->jedec_version = 1; /* vendor specific version */ |
| 4049 | |
| 4050 | if (!chip->jedec_version) { |
| 4051 | pr_info("unsupported JEDEC version: %d\n", val); |
| 4052 | return 0; |
| 4053 | } |
| 4054 | |
| 4055 | sanitize_string(p->manufacturer, sizeof(p->manufacturer)); |
| 4056 | sanitize_string(p->model, sizeof(p->model)); |
| 4057 | if (!mtd->name) |
| 4058 | mtd->name = p->model; |
| 4059 | |
| 4060 | mtd->writesize = le32_to_cpu(p->byte_per_page); |
| 4061 | |
| 4062 | /* Please reference to the comment for nand_flash_detect_onfi. */ |
| 4063 | mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1); |
| 4064 | mtd->erasesize *= mtd->writesize; |
| 4065 | |
| 4066 | mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page); |
| 4067 | |
| 4068 | /* Please reference to the comment for nand_flash_detect_onfi. */ |
| 4069 | chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1); |
| 4070 | chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count; |
| 4071 | chip->bits_per_cell = p->bits_per_cell; |
| 4072 | |
| 4073 | if (jedec_feature(chip) & JEDEC_FEATURE_16_BIT_BUS) |
Michael Trimarchi | dc7fdd6 | 2022-07-20 18:22:04 +0200 | [diff] [blame] | 4074 | chip->options |= NAND_BUSWIDTH_16; |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 4075 | |
| 4076 | /* ECC info */ |
| 4077 | ecc = &p->ecc_info[0]; |
| 4078 | |
| 4079 | if (ecc->codeword_size >= 9) { |
| 4080 | chip->ecc_strength_ds = ecc->ecc_bits; |
| 4081 | chip->ecc_step_ds = 1 << ecc->codeword_size; |
| 4082 | } else { |
| 4083 | pr_warn("Invalid codeword size\n"); |
| 4084 | } |
| 4085 | |
| 4086 | return 1; |
| 4087 | } |
| 4088 | |
| 4089 | /* |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4090 | * nand_id_has_period - Check if an ID string has a given wraparound period |
| 4091 | * @id_data: the ID string |
| 4092 | * @arrlen: the length of the @id_data array |
| 4093 | * @period: the period of repitition |
| 4094 | * |
| 4095 | * Check if an ID string is repeated within a given sequence of bytes at |
| 4096 | * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4097 | * period of 3). This is a helper function for nand_id_len(). Returns non-zero |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4098 | * if the repetition has a period of @period; otherwise, returns zero. |
| 4099 | */ |
| 4100 | static int nand_id_has_period(u8 *id_data, int arrlen, int period) |
| 4101 | { |
| 4102 | int i, j; |
| 4103 | for (i = 0; i < period; i++) |
| 4104 | for (j = i + period; j < arrlen; j += period) |
| 4105 | if (id_data[i] != id_data[j]) |
| 4106 | return 0; |
| 4107 | return 1; |
| 4108 | } |
| 4109 | |
| 4110 | /* |
| 4111 | * nand_id_len - Get the length of an ID string returned by CMD_READID |
| 4112 | * @id_data: the ID string |
| 4113 | * @arrlen: the length of the @id_data array |
| 4114 | |
| 4115 | * Returns the length of the ID string, according to known wraparound/trailing |
| 4116 | * zero patterns. If no pattern exists, returns the length of the array. |
| 4117 | */ |
| 4118 | static int nand_id_len(u8 *id_data, int arrlen) |
| 4119 | { |
| 4120 | int last_nonzero, period; |
| 4121 | |
| 4122 | /* Find last non-zero byte */ |
| 4123 | for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--) |
| 4124 | if (id_data[last_nonzero]) |
| 4125 | break; |
| 4126 | |
| 4127 | /* All zeros */ |
| 4128 | if (last_nonzero < 0) |
| 4129 | return 0; |
| 4130 | |
| 4131 | /* Calculate wraparound period */ |
| 4132 | for (period = 1; period < arrlen; period++) |
| 4133 | if (nand_id_has_period(id_data, arrlen, period)) |
| 4134 | break; |
| 4135 | |
| 4136 | /* There's a repeated pattern */ |
| 4137 | if (period < arrlen) |
| 4138 | return period; |
| 4139 | |
| 4140 | /* There are trailing zeros */ |
| 4141 | if (last_nonzero < arrlen - 1) |
| 4142 | return last_nonzero + 1; |
| 4143 | |
| 4144 | /* No pattern detected */ |
| 4145 | return arrlen; |
| 4146 | } |
| 4147 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4148 | /* Extract the bits of per cell from the 3rd byte of the extended ID */ |
| 4149 | static int nand_get_bits_per_cell(u8 cellinfo) |
| 4150 | { |
| 4151 | int bits; |
| 4152 | |
| 4153 | bits = cellinfo & NAND_CI_CELLTYPE_MSK; |
| 4154 | bits >>= NAND_CI_CELLTYPE_SHIFT; |
| 4155 | return bits + 1; |
| 4156 | } |
| 4157 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4158 | /* |
| 4159 | * Many new NAND share similar device ID codes, which represent the size of the |
| 4160 | * chip. The rest of the parameters must be decoded according to generic or |
| 4161 | * manufacturer-specific "extended ID" decoding patterns. |
| 4162 | */ |
Michael Trimarchi | 60f26dc | 2022-07-20 18:22:08 +0200 | [diff] [blame] | 4163 | void nand_decode_ext_id(struct nand_chip *chip) |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4164 | { |
Michael Trimarchi | 270c153 | 2022-07-20 18:22:07 +0200 | [diff] [blame] | 4165 | struct mtd_info *mtd = &chip->mtd; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4166 | int extid, id_len; |
| 4167 | /* The 3rd id byte holds MLC / multichip data */ |
Michael Trimarchi | cd4d904 | 2022-07-20 18:22:05 +0200 | [diff] [blame] | 4168 | chip->bits_per_cell = nand_get_bits_per_cell(chip->id.data[2]); |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4169 | /* The 4th id byte is the important one */ |
Michael Trimarchi | cd4d904 | 2022-07-20 18:22:05 +0200 | [diff] [blame] | 4170 | extid = chip->id.data[3]; |
| 4171 | id_len = chip->id.len; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4172 | |
Michael Trimarchi | 3dc9060 | 2022-07-20 18:22:10 +0200 | [diff] [blame^] | 4173 | /* Calc pagesize */ |
| 4174 | mtd->writesize = 1024 << (extid & 0x03); |
| 4175 | extid >>= 2; |
| 4176 | /* Calc oobsize */ |
| 4177 | mtd->oobsize = (8 << (extid & 0x01)) * |
| 4178 | (mtd->writesize >> 9); |
| 4179 | extid >>= 2; |
| 4180 | /* Calc blocksize. Blocksize is multiples of 64KiB */ |
| 4181 | mtd->erasesize = (64 * 1024) << (extid & 0x03); |
| 4182 | extid >>= 2; |
| 4183 | /* Get buswidth information */ |
| 4184 | /* Get buswidth information */ |
| 4185 | if (extid & 0x1) |
| 4186 | chip->options |= NAND_BUSWIDTH_16; |
| 4187 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4188 | /* |
Michael Trimarchi | 3dc9060 | 2022-07-20 18:22:10 +0200 | [diff] [blame^] | 4189 | * Toshiba 24nm raw SLC (i.e., not BENAND) have 32B OOB per |
| 4190 | * 512B page. For Toshiba SLC, we decode the 5th/6th byte as |
| 4191 | * follows: |
| 4192 | * - ID byte 6, bits[2:0]: 100b -> 43nm, 101b -> 32nm, |
| 4193 | * 110b -> 24nm |
| 4194 | * - ID byte 5, bit[7]: 1 -> BENAND, 0 -> raw SLC |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4195 | */ |
Michael Trimarchi | 3dc9060 | 2022-07-20 18:22:10 +0200 | [diff] [blame^] | 4196 | if (id_len >= 6 && chip->id.data[0] == NAND_MFR_TOSHIBA && |
| 4197 | nand_is_slc(chip) && |
| 4198 | (chip->id.data[5] & 0x7) == 0x6 /* 24nm */ && |
| 4199 | !(chip->id.data[4] & 0x80) /* !BENAND */) { |
| 4200 | mtd->oobsize = 32 * mtd->writesize >> 9; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4201 | } |
| 4202 | } |
Michael Trimarchi | 60f26dc | 2022-07-20 18:22:08 +0200 | [diff] [blame] | 4203 | EXPORT_SYMBOL_GPL(nand_decode_ext_id); |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4204 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4205 | /* |
Michael Trimarchi | 4a26e1d | 2022-07-20 18:22:06 +0200 | [diff] [blame] | 4206 | * Manufacturer detection. Only used when the NAND is not ONFI or JEDEC |
| 4207 | * compliant and does not have a full-id or legacy-id entry in the nand_ids |
| 4208 | * table. |
| 4209 | */ |
Michael Trimarchi | 270c153 | 2022-07-20 18:22:07 +0200 | [diff] [blame] | 4210 | static void nand_manufacturer_detect(struct nand_chip *chip) |
Michael Trimarchi | 4a26e1d | 2022-07-20 18:22:06 +0200 | [diff] [blame] | 4211 | { |
| 4212 | /* |
| 4213 | * Try manufacturer detection if available and use |
| 4214 | * nand_decode_ext_id() otherwise. |
| 4215 | */ |
| 4216 | if (chip->manufacturer.desc && chip->manufacturer.desc->ops && |
| 4217 | chip->manufacturer.desc->ops->detect) |
| 4218 | chip->manufacturer.desc->ops->detect(chip); |
| 4219 | else |
Michael Trimarchi | 270c153 | 2022-07-20 18:22:07 +0200 | [diff] [blame] | 4220 | nand_decode_ext_id(chip); |
Michael Trimarchi | 4a26e1d | 2022-07-20 18:22:06 +0200 | [diff] [blame] | 4221 | } |
| 4222 | |
| 4223 | /* |
| 4224 | * Manufacturer initialization. This function is called for all NANDs including |
| 4225 | * ONFI and JEDEC compliant ones. |
| 4226 | * Manufacturer drivers should put all their specific initialization code in |
| 4227 | * their ->init() hook. |
| 4228 | */ |
| 4229 | static int nand_manufacturer_init(struct nand_chip *chip) |
| 4230 | { |
| 4231 | if (!chip->manufacturer.desc || !chip->manufacturer.desc->ops || |
| 4232 | !chip->manufacturer.desc->ops->init) |
| 4233 | return 0; |
| 4234 | |
| 4235 | return chip->manufacturer.desc->ops->init(chip); |
| 4236 | } |
| 4237 | |
| 4238 | /* |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4239 | * Old devices have chip data hardcoded in the device ID table. nand_decode_id |
| 4240 | * decodes a matching ID table entry and assigns the MTD size parameters for |
| 4241 | * the chip. |
| 4242 | */ |
Michael Trimarchi | 270c153 | 2022-07-20 18:22:07 +0200 | [diff] [blame] | 4243 | static void nand_decode_id(struct nand_chip *chip, struct nand_flash_dev *type) |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4244 | { |
Michael Trimarchi | 270c153 | 2022-07-20 18:22:07 +0200 | [diff] [blame] | 4245 | struct mtd_info *mtd = &chip->mtd; |
Michael Trimarchi | cd4d904 | 2022-07-20 18:22:05 +0200 | [diff] [blame] | 4246 | int maf_id = chip->id.data[0]; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4247 | |
| 4248 | mtd->erasesize = type->erasesize; |
| 4249 | mtd->writesize = type->pagesize; |
| 4250 | mtd->oobsize = mtd->writesize / 32; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4251 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4252 | /* All legacy ID NAND are small-page, SLC */ |
| 4253 | chip->bits_per_cell = 1; |
| 4254 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4255 | /* |
| 4256 | * Check for Spansion/AMD ID + repeating 5th, 6th byte since |
| 4257 | * some Spansion chips have erasesize that conflicts with size |
| 4258 | * listed in nand_ids table. |
| 4259 | * Data sheet (5 byte ID): Spansion S30ML-P ORNAND (p.39) |
| 4260 | */ |
Michael Trimarchi | cd4d904 | 2022-07-20 18:22:05 +0200 | [diff] [blame] | 4261 | if (maf_id == NAND_MFR_AMD && chip->id.data[4] != 0x00 && |
| 4262 | chip->id.data[5] == 0x00 && chip->id.data[6] == 0x00 && |
| 4263 | chip->id.data[7] == 0x00 && mtd->writesize == 512) { |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4264 | mtd->erasesize = 128 * 1024; |
Michael Trimarchi | cd4d904 | 2022-07-20 18:22:05 +0200 | [diff] [blame] | 4265 | mtd->erasesize <<= ((chip->id.data[3] & 0x03) << 1); |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4266 | } |
| 4267 | } |
| 4268 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4269 | /* |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4270 | * Set the bad block marker/indicator (BBM/BBI) patterns according to some |
| 4271 | * heuristic patterns using various detected parameters (e.g., manufacturer, |
| 4272 | * page size, cell-type information). |
| 4273 | */ |
| 4274 | static void nand_decode_bbm_options(struct mtd_info *mtd, |
Michael Trimarchi | cd4d904 | 2022-07-20 18:22:05 +0200 | [diff] [blame] | 4275 | struct nand_chip *chip) |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4276 | { |
Michael Trimarchi | cd4d904 | 2022-07-20 18:22:05 +0200 | [diff] [blame] | 4277 | int maf_id = chip->id.data[0]; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4278 | |
| 4279 | /* Set the bad block position */ |
| 4280 | if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16)) |
| 4281 | chip->badblockpos = NAND_LARGE_BADBLOCK_POS; |
| 4282 | else |
| 4283 | chip->badblockpos = NAND_SMALL_BADBLOCK_POS; |
| 4284 | |
| 4285 | /* |
| 4286 | * Bad block marker is stored in the last page of each block on Samsung |
| 4287 | * and Hynix MLC devices; stored in first two pages of each block on |
| 4288 | * Micron devices with 2KiB pages and on SLC Samsung, Hynix, Toshiba, |
| 4289 | * AMD/Spansion, and Macronix. All others scan only the first page. |
| 4290 | */ |
Michael Trimarchi | 3dc9060 | 2022-07-20 18:22:10 +0200 | [diff] [blame^] | 4291 | if ((nand_is_slc(chip) && |
| 4292 | (maf_id == NAND_MFR_TOSHIBA || |
| 4293 | maf_id == NAND_MFR_AMD || |
| 4294 | maf_id == NAND_MFR_MACRONIX)) || |
| 4295 | (mtd->writesize == 2048 && maf_id == NAND_MFR_MICRON)) |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4296 | chip->bbt_options |= NAND_BBT_SCAN2NDPAGE; |
| 4297 | } |
| 4298 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4299 | static inline bool is_full_id_nand(struct nand_flash_dev *type) |
| 4300 | { |
| 4301 | return type->id_len; |
| 4302 | } |
| 4303 | |
| 4304 | static bool find_full_id_nand(struct mtd_info *mtd, struct nand_chip *chip, |
Michael Trimarchi | cd4d904 | 2022-07-20 18:22:05 +0200 | [diff] [blame] | 4305 | struct nand_flash_dev *type) |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4306 | { |
Michael Trimarchi | cd4d904 | 2022-07-20 18:22:05 +0200 | [diff] [blame] | 4307 | if (!strncmp((char *)type->id, (char *)chip->id.data, type->id_len)) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4308 | mtd->writesize = type->pagesize; |
| 4309 | mtd->erasesize = type->erasesize; |
| 4310 | mtd->oobsize = type->oobsize; |
| 4311 | |
Michael Trimarchi | cd4d904 | 2022-07-20 18:22:05 +0200 | [diff] [blame] | 4312 | chip->bits_per_cell = nand_get_bits_per_cell(chip->id.data[2]); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4313 | chip->chipsize = (uint64_t)type->chipsize << 20; |
| 4314 | chip->options |= type->options; |
| 4315 | chip->ecc_strength_ds = NAND_ECC_STRENGTH(type); |
| 4316 | chip->ecc_step_ds = NAND_ECC_STEP(type); |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 4317 | chip->onfi_timing_mode_default = |
| 4318 | type->onfi_timing_mode_default; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4319 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4320 | if (!mtd->name) |
| 4321 | mtd->name = type->name; |
| 4322 | |
| 4323 | return true; |
| 4324 | } |
| 4325 | return false; |
| 4326 | } |
| 4327 | |
Michael Trimarchi | 4a26e1d | 2022-07-20 18:22:06 +0200 | [diff] [blame] | 4328 | /** |
| 4329 | * nand_get_manufacturer_desc - Get manufacturer information from the |
| 4330 | * manufacturer ID |
| 4331 | * @id: manufacturer ID |
| 4332 | * |
| 4333 | * Returns a nand_manufacturer_desc object if the manufacturer is defined |
| 4334 | * in the NAND manufacturers database, NULL otherwise. |
| 4335 | */ |
| 4336 | static const struct nand_manufacturers *nand_get_manufacturer_desc(u8 id) |
| 4337 | { |
| 4338 | int i; |
| 4339 | |
| 4340 | for (i = 0; nand_manuf_ids[i].id != 0x0; i++) { |
| 4341 | if (nand_manuf_ids[i].id == id) |
| 4342 | return &nand_manuf_ids[i]; |
| 4343 | } |
| 4344 | |
| 4345 | return NULL; |
| 4346 | } |
| 4347 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4348 | /* |
| 4349 | * Get the flash and manufacturer id and lookup if the type is supported. |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4350 | */ |
Michael Trimarchi | 270c153 | 2022-07-20 18:22:07 +0200 | [diff] [blame] | 4351 | struct nand_flash_dev *nand_get_flash_type(struct nand_chip *chip, int *maf_id, |
| 4352 | int *dev_id, |
| 4353 | struct nand_flash_dev *type) |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4354 | { |
Michael Trimarchi | 270c153 | 2022-07-20 18:22:07 +0200 | [diff] [blame] | 4355 | struct mtd_info *mtd = &chip->mtd; |
Michael Trimarchi | 4a26e1d | 2022-07-20 18:22:06 +0200 | [diff] [blame] | 4356 | const struct nand_manufacturers *manufacturer_desc; |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 4357 | int busw, ret; |
Michael Trimarchi | cd4d904 | 2022-07-20 18:22:05 +0200 | [diff] [blame] | 4358 | u8 *id_data = chip->id.data; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 4359 | |
Karl Beldan | b6322fc | 2008-09-15 16:08:03 +0200 | [diff] [blame] | 4360 | /* |
| 4361 | * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx) |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4362 | * after power-up. |
Karl Beldan | b6322fc | 2008-09-15 16:08:03 +0200 | [diff] [blame] | 4363 | */ |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 4364 | ret = nand_reset(chip, 0); |
| 4365 | if (ret) |
| 4366 | return ERR_PTR(ret); |
Boris Brezillon | 7ec6dc5 | 2017-11-22 02:38:20 +0900 | [diff] [blame] | 4367 | |
| 4368 | /* Select the device */ |
| 4369 | chip->select_chip(mtd, 0); |
Karl Beldan | b6322fc | 2008-09-15 16:08:03 +0200 | [diff] [blame] | 4370 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 4371 | /* Send the command for reading device ID */ |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 4372 | ret = nand_readid_op(chip, 0, id_data, 2); |
| 4373 | if (ret) |
| 4374 | return ERR_PTR(ret); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 4375 | |
| 4376 | /* Read manufacturer and device IDs */ |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 4377 | *maf_id = id_data[0]; |
| 4378 | *dev_id = id_data[1]; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 4379 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4380 | /* |
| 4381 | * Try again to make sure, as some systems the bus-hold or other |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 4382 | * interface concerns can cause random data which looks like a |
| 4383 | * possibly credible NAND flash to appear. If the two results do |
| 4384 | * not match, ignore the device completely. |
| 4385 | */ |
| 4386 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4387 | /* Read entire ID string */ |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 4388 | ret = nand_readid_op(chip, 0, id_data, 8); |
| 4389 | if (ret) |
| 4390 | return ERR_PTR(ret); |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 4391 | |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 4392 | if (id_data[0] != *maf_id || id_data[1] != *dev_id) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4393 | pr_info("second ID read did not match %02x,%02x against %02x,%02x\n", |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4394 | *maf_id, *dev_id, id_data[0], id_data[1]); |
Scott Wood | 3628f00 | 2008-10-24 16:20:43 -0500 | [diff] [blame] | 4395 | return ERR_PTR(-ENODEV); |
| 4396 | } |
| 4397 | |
Michael Trimarchi | 4a26e1d | 2022-07-20 18:22:06 +0200 | [diff] [blame] | 4398 | chip->id.len = nand_id_len(id_data, ARRAY_SIZE(chip->id.data)); |
| 4399 | |
| 4400 | /* Try to identify manufacturer */ |
| 4401 | manufacturer_desc = nand_get_manufacturer_desc(*maf_id); |
| 4402 | chip->manufacturer.desc = manufacturer_desc; |
| 4403 | |
Lei Wen | 75bde94 | 2011-01-06 09:48:18 +0800 | [diff] [blame] | 4404 | if (!type) |
| 4405 | type = nand_flash_ids; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 4406 | |
Michael Trimarchi | dc7fdd6 | 2022-07-20 18:22:04 +0200 | [diff] [blame] | 4407 | /* |
| 4408 | * Save the NAND_BUSWIDTH_16 flag before letting auto-detection logic |
| 4409 | * override it. |
| 4410 | * This is required to make sure initial NAND bus width set by the |
| 4411 | * NAND controller driver is coherent with the real NAND bus width |
| 4412 | * (extracted by auto-detection code). |
| 4413 | */ |
| 4414 | busw = chip->options & NAND_BUSWIDTH_16; |
| 4415 | |
| 4416 | /* |
| 4417 | * The flag is only set (never cleared), reset it to its default value |
| 4418 | * before starting auto-detection. |
| 4419 | */ |
| 4420 | chip->options &= ~NAND_BUSWIDTH_16; |
| 4421 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4422 | for (; type->name != NULL; type++) { |
| 4423 | if (is_full_id_nand(type)) { |
Michael Trimarchi | cd4d904 | 2022-07-20 18:22:05 +0200 | [diff] [blame] | 4424 | if (find_full_id_nand(mtd, chip, type)) |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4425 | goto ident_done; |
| 4426 | } else if (*dev_id == type->dev_id) { |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 4427 | break; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4428 | } |
| 4429 | } |
Lei Wen | 75bde94 | 2011-01-06 09:48:18 +0800 | [diff] [blame] | 4430 | |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 4431 | chip->onfi_version = 0; |
| 4432 | if (!type->name || !type->pagesize) { |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 4433 | /* Check if the chip is ONFI compliant */ |
Michael Trimarchi | dc7fdd6 | 2022-07-20 18:22:04 +0200 | [diff] [blame] | 4434 | if (nand_flash_detect_onfi(mtd, chip)) |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 4435 | goto ident_done; |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 4436 | |
| 4437 | /* Check if the chip is JEDEC compliant */ |
Michael Trimarchi | dc7fdd6 | 2022-07-20 18:22:04 +0200 | [diff] [blame] | 4438 | if (nand_flash_detect_jedec(mtd, chip)) |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 4439 | goto ident_done; |
Florian Fainelli | d619189 | 2010-06-12 20:59:25 +0200 | [diff] [blame] | 4440 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 4441 | |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 4442 | if (!type->name) |
| 4443 | return ERR_PTR(-ENODEV); |
| 4444 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4445 | if (!mtd->name) |
| 4446 | mtd->name = type->name; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 4447 | |
Sandeep Paulraj | eab580c | 2009-10-30 13:51:23 -0400 | [diff] [blame] | 4448 | chip->chipsize = (uint64_t)type->chipsize << 20; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 4449 | |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 4450 | if (!type->pagesize) { |
Michael Trimarchi | 270c153 | 2022-07-20 18:22:07 +0200 | [diff] [blame] | 4451 | nand_manufacturer_detect(chip); |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 4452 | } else { |
Michael Trimarchi | 270c153 | 2022-07-20 18:22:07 +0200 | [diff] [blame] | 4453 | nand_decode_id(chip, type); |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 4454 | } |
Michael Trimarchi | dc7fdd6 | 2022-07-20 18:22:04 +0200 | [diff] [blame] | 4455 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4456 | /* Get chip options */ |
Marek Vasut | fc41719 | 2012-08-30 13:39:38 +0000 | [diff] [blame] | 4457 | chip->options |= type->options; |
Florian Fainelli | c98a935 | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 4458 | |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 4459 | ident_done: |
| 4460 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4461 | if (chip->options & NAND_BUSWIDTH_AUTO) { |
| 4462 | WARN_ON(chip->options & NAND_BUSWIDTH_16); |
| 4463 | chip->options |= busw; |
| 4464 | nand_set_defaults(chip, busw); |
| 4465 | } else if (busw != (chip->options & NAND_BUSWIDTH_16)) { |
| 4466 | /* |
| 4467 | * Check, if buswidth is correct. Hardware drivers should set |
| 4468 | * chip correct! |
| 4469 | */ |
| 4470 | pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n", |
| 4471 | *maf_id, *dev_id); |
Michael Trimarchi | 4a26e1d | 2022-07-20 18:22:06 +0200 | [diff] [blame] | 4472 | pr_info("%s %s\n", manufacturer_desc->name, mtd->name); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4473 | pr_warn("bus width %d instead %d bit\n", |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4474 | (chip->options & NAND_BUSWIDTH_16) ? 16 : 8, |
| 4475 | busw ? 16 : 8); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4476 | return ERR_PTR(-EINVAL); |
| 4477 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 4478 | |
Michael Trimarchi | cd4d904 | 2022-07-20 18:22:05 +0200 | [diff] [blame] | 4479 | nand_decode_bbm_options(mtd, chip); |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4480 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4481 | /* Calculate the address shift from the page size */ |
| 4482 | chip->page_shift = ffs(mtd->writesize) - 1; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4483 | /* Convert chipsize to number of pages per chip -1 */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4484 | chip->pagemask = (chip->chipsize >> chip->page_shift) - 1; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 4485 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4486 | chip->bbt_erase_shift = chip->phys_erase_shift = |
| 4487 | ffs(mtd->erasesize) - 1; |
Sandeep Paulraj | eab580c | 2009-10-30 13:51:23 -0400 | [diff] [blame] | 4488 | if (chip->chipsize & 0xffffffff) |
Sandeep Paulraj | 1bc877c | 2009-11-07 14:24:06 -0500 | [diff] [blame] | 4489 | chip->chip_shift = ffs((unsigned)chip->chipsize) - 1; |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 4490 | else { |
| 4491 | chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32)); |
| 4492 | chip->chip_shift += 32 - 1; |
| 4493 | } |
| 4494 | |
Masahiro Yamada | 984926b | 2017-11-22 02:38:31 +0900 | [diff] [blame] | 4495 | if (chip->chip_shift - chip->page_shift > 16) |
| 4496 | chip->options |= NAND_ROW_ADDR_3; |
| 4497 | |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 4498 | chip->badblockbits = 8; |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 4499 | chip->erase = single_erase; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 4500 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4501 | /* Do not replace user supplied command function! */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4502 | if (mtd->writesize > 512 && chip->cmdfunc == nand_command) |
| 4503 | chip->cmdfunc = nand_command_lp; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 4504 | |
Michael Trimarchi | 4a26e1d | 2022-07-20 18:22:06 +0200 | [diff] [blame] | 4505 | ret = nand_manufacturer_init(chip); |
| 4506 | if (ret) |
| 4507 | return ERR_PTR(ret); |
| 4508 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4509 | pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n", |
| 4510 | *maf_id, *dev_id); |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 4511 | |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 4512 | #ifdef CONFIG_SYS_NAND_ONFI_DETECTION |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 4513 | if (chip->onfi_version) |
Michael Trimarchi | 4a26e1d | 2022-07-20 18:22:06 +0200 | [diff] [blame] | 4514 | pr_info("%s %s\n", manufacturer_desc->name, |
| 4515 | chip->onfi_params.model); |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 4516 | else if (chip->jedec_version) |
Michael Trimarchi | 4a26e1d | 2022-07-20 18:22:06 +0200 | [diff] [blame] | 4517 | pr_info("%s %s\n", manufacturer_desc->name, |
| 4518 | chip->jedec_params.model); |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 4519 | else |
Michael Trimarchi | 4a26e1d | 2022-07-20 18:22:06 +0200 | [diff] [blame] | 4520 | pr_info("%s %s\n", manufacturer_desc->name, type->name); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4521 | #else |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 4522 | if (chip->jedec_version) |
Michael Trimarchi | 4a26e1d | 2022-07-20 18:22:06 +0200 | [diff] [blame] | 4523 | pr_info("%s %s\n", manufacturer_desc->name, |
| 4524 | chip->jedec_params.model); |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 4525 | else |
Michael Trimarchi | 4a26e1d | 2022-07-20 18:22:06 +0200 | [diff] [blame] | 4526 | pr_info("%s %s\n", manufacturer_desc->name, type->name); |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 4527 | |
Michael Trimarchi | 4a26e1d | 2022-07-20 18:22:06 +0200 | [diff] [blame] | 4528 | pr_info("%s %s\n", manufacturer_desc->name, |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 4529 | type->name); |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 4530 | #endif |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 4531 | |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 4532 | pr_info("%d MiB, %s, erase size: %d KiB, page size: %d, OOB size: %d\n", |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4533 | (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC", |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 4534 | mtd->erasesize >> 10, mtd->writesize, mtd->oobsize); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4535 | return type; |
| 4536 | } |
Jörg Krause | 929fb44 | 2018-01-14 19:26:37 +0100 | [diff] [blame] | 4537 | EXPORT_SYMBOL(nand_get_flash_type); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4538 | |
Brian Norris | ba6463d | 2016-06-15 21:09:22 +0200 | [diff] [blame] | 4539 | #if CONFIG_IS_ENABLED(OF_CONTROL) |
Brian Norris | ba6463d | 2016-06-15 21:09:22 +0200 | [diff] [blame] | 4540 | |
Patrice Chotard | bc77af5 | 2021-09-13 16:25:53 +0200 | [diff] [blame] | 4541 | static int nand_dt_init(struct mtd_info *mtd, struct nand_chip *chip, ofnode node) |
Brian Norris | ba6463d | 2016-06-15 21:09:22 +0200 | [diff] [blame] | 4542 | { |
| 4543 | int ret, ecc_mode = -1, ecc_strength, ecc_step; |
Brian Norris | ba6463d | 2016-06-15 21:09:22 +0200 | [diff] [blame] | 4544 | const char *str; |
| 4545 | |
Patrice Chotard | bc77af5 | 2021-09-13 16:25:53 +0200 | [diff] [blame] | 4546 | ret = ofnode_read_s32_default(node, "nand-bus-width", -1); |
Brian Norris | ba6463d | 2016-06-15 21:09:22 +0200 | [diff] [blame] | 4547 | if (ret == 16) |
| 4548 | chip->options |= NAND_BUSWIDTH_16; |
| 4549 | |
Patrice Chotard | bc77af5 | 2021-09-13 16:25:53 +0200 | [diff] [blame] | 4550 | if (ofnode_read_bool(node, "nand-on-flash-bbt")) |
Brian Norris | ba6463d | 2016-06-15 21:09:22 +0200 | [diff] [blame] | 4551 | chip->bbt_options |= NAND_BBT_USE_FLASH; |
| 4552 | |
Patrice Chotard | bc77af5 | 2021-09-13 16:25:53 +0200 | [diff] [blame] | 4553 | str = ofnode_read_string(node, "nand-ecc-mode"); |
Brian Norris | ba6463d | 2016-06-15 21:09:22 +0200 | [diff] [blame] | 4554 | if (str) { |
| 4555 | if (!strcmp(str, "none")) |
| 4556 | ecc_mode = NAND_ECC_NONE; |
| 4557 | else if (!strcmp(str, "soft")) |
| 4558 | ecc_mode = NAND_ECC_SOFT; |
| 4559 | else if (!strcmp(str, "hw")) |
| 4560 | ecc_mode = NAND_ECC_HW; |
| 4561 | else if (!strcmp(str, "hw_syndrome")) |
| 4562 | ecc_mode = NAND_ECC_HW_SYNDROME; |
| 4563 | else if (!strcmp(str, "hw_oob_first")) |
| 4564 | ecc_mode = NAND_ECC_HW_OOB_FIRST; |
| 4565 | else if (!strcmp(str, "soft_bch")) |
| 4566 | ecc_mode = NAND_ECC_SOFT_BCH; |
| 4567 | } |
| 4568 | |
Pali Rohár | 44acf8a | 2022-04-04 18:17:21 +0200 | [diff] [blame] | 4569 | if (ecc_mode == NAND_ECC_SOFT) { |
| 4570 | str = ofnode_read_string(node, "nand-ecc-algo"); |
| 4571 | if (str && !strcmp(str, "bch")) |
| 4572 | ecc_mode = NAND_ECC_SOFT_BCH; |
| 4573 | } |
| 4574 | |
Patrice Chotard | bc77af5 | 2021-09-13 16:25:53 +0200 | [diff] [blame] | 4575 | ecc_strength = ofnode_read_s32_default(node, |
| 4576 | "nand-ecc-strength", -1); |
| 4577 | ecc_step = ofnode_read_s32_default(node, |
| 4578 | "nand-ecc-step-size", -1); |
Brian Norris | ba6463d | 2016-06-15 21:09:22 +0200 | [diff] [blame] | 4579 | |
| 4580 | if ((ecc_step >= 0 && !(ecc_strength >= 0)) || |
| 4581 | (!(ecc_step >= 0) && ecc_strength >= 0)) { |
| 4582 | pr_err("must set both strength and step size in DT\n"); |
| 4583 | return -EINVAL; |
| 4584 | } |
| 4585 | |
| 4586 | if (ecc_mode >= 0) |
| 4587 | chip->ecc.mode = ecc_mode; |
| 4588 | |
| 4589 | if (ecc_strength >= 0) |
| 4590 | chip->ecc.strength = ecc_strength; |
| 4591 | |
| 4592 | if (ecc_step > 0) |
| 4593 | chip->ecc.size = ecc_step; |
| 4594 | |
Patrice Chotard | bc77af5 | 2021-09-13 16:25:53 +0200 | [diff] [blame] | 4595 | if (ofnode_read_bool(node, "nand-ecc-maximize")) |
Boris Brezillon | f1a54b0 | 2017-11-22 02:38:13 +0900 | [diff] [blame] | 4596 | chip->ecc.options |= NAND_ECC_MAXIMIZE; |
| 4597 | |
Brian Norris | ba6463d | 2016-06-15 21:09:22 +0200 | [diff] [blame] | 4598 | return 0; |
| 4599 | } |
| 4600 | #else |
Patrice Chotard | bc77af5 | 2021-09-13 16:25:53 +0200 | [diff] [blame] | 4601 | static int nand_dt_init(struct mtd_info *mtd, struct nand_chip *chip, ofnode node) |
Brian Norris | ba6463d | 2016-06-15 21:09:22 +0200 | [diff] [blame] | 4602 | { |
| 4603 | return 0; |
| 4604 | } |
| 4605 | #endif /* CONFIG_IS_ENABLED(OF_CONTROL) */ |
| 4606 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4607 | /** |
| 4608 | * nand_scan_ident - [NAND Interface] Scan for the NAND device |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4609 | * @mtd: MTD device structure |
| 4610 | * @maxchips: number of chips to scan for |
| 4611 | * @table: alternative NAND ID table |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4612 | * |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4613 | * This is the first phase of the normal nand_scan() function. It reads the |
| 4614 | * flash ID and sets up MTD fields accordingly. |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4615 | * |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4616 | */ |
Lei Wen | 75bde94 | 2011-01-06 09:48:18 +0800 | [diff] [blame] | 4617 | int nand_scan_ident(struct mtd_info *mtd, int maxchips, |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4618 | struct nand_flash_dev *table) |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4619 | { |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 4620 | int i, nand_maf_id, nand_dev_id; |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 4621 | struct nand_chip *chip = mtd_to_nand(mtd); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4622 | struct nand_flash_dev *type; |
Brian Norris | ba6463d | 2016-06-15 21:09:22 +0200 | [diff] [blame] | 4623 | int ret; |
| 4624 | |
Patrice Chotard | bc77af5 | 2021-09-13 16:25:53 +0200 | [diff] [blame] | 4625 | if (ofnode_valid(chip->flash_node)) { |
Brian Norris | ba6463d | 2016-06-15 21:09:22 +0200 | [diff] [blame] | 4626 | ret = nand_dt_init(mtd, chip, chip->flash_node); |
| 4627 | if (ret) |
| 4628 | return ret; |
| 4629 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 4630 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4631 | /* Set the default functions */ |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 4632 | nand_set_defaults(chip, chip->options & NAND_BUSWIDTH_16); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4633 | |
| 4634 | /* Read the flash type */ |
Michael Trimarchi | 270c153 | 2022-07-20 18:22:07 +0200 | [diff] [blame] | 4635 | type = nand_get_flash_type(chip, &nand_maf_id, |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 4636 | &nand_dev_id, table); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4637 | |
| 4638 | if (IS_ERR(type)) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4639 | if (!(chip->options & NAND_SCAN_SILENT_NODEV)) |
| 4640 | pr_warn("No NAND device found\n"); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4641 | chip->select_chip(mtd, -1); |
| 4642 | return PTR_ERR(type); |
| 4643 | } |
| 4644 | |
Boris Brezillon | 7ec6dc5 | 2017-11-22 02:38:20 +0900 | [diff] [blame] | 4645 | /* Initialize the ->data_interface field. */ |
Boris Brezillon | e509cba | 2017-11-22 02:38:19 +0900 | [diff] [blame] | 4646 | ret = nand_init_data_interface(chip); |
| 4647 | if (ret) |
| 4648 | return ret; |
| 4649 | |
Boris Brezillon | 7ec6dc5 | 2017-11-22 02:38:20 +0900 | [diff] [blame] | 4650 | /* |
| 4651 | * Setup the data interface correctly on the chip and controller side. |
| 4652 | * This explicit call to nand_setup_data_interface() is only required |
| 4653 | * for the first die, because nand_reset() has been called before |
| 4654 | * ->data_interface and ->default_onfi_timing_mode were set. |
| 4655 | * For the other dies, nand_reset() will automatically switch to the |
| 4656 | * best mode for us. |
| 4657 | */ |
Boris Brezillon | 32935f4 | 2017-11-22 02:38:28 +0900 | [diff] [blame] | 4658 | ret = nand_setup_data_interface(chip, 0); |
Boris Brezillon | 7ec6dc5 | 2017-11-22 02:38:20 +0900 | [diff] [blame] | 4659 | if (ret) |
| 4660 | return ret; |
| 4661 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4662 | chip->select_chip(mtd, -1); |
| 4663 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4664 | /* Check for a chip array */ |
| 4665 | for (i = 1; i < maxchips; i++) { |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 4666 | u8 id[2]; |
| 4667 | |
Karl Beldan | b6322fc | 2008-09-15 16:08:03 +0200 | [diff] [blame] | 4668 | /* See comment in nand_get_flash_type for reset */ |
Boris Brezillon | 7ec6dc5 | 2017-11-22 02:38:20 +0900 | [diff] [blame] | 4669 | nand_reset(chip, i); |
| 4670 | |
| 4671 | chip->select_chip(mtd, i); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4672 | /* Send the command for reading device ID */ |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 4673 | nand_readid_op(chip, 0, id, sizeof(id)); |
| 4674 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 4675 | /* Read manufacturer and device IDs */ |
Boris Brezillon | 16ee8f6 | 2019-03-15 15:14:32 +0100 | [diff] [blame] | 4676 | if (nand_maf_id != id[0] || nand_dev_id != id[1]) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4677 | chip->select_chip(mtd, -1); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 4678 | break; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4679 | } |
| 4680 | chip->select_chip(mtd, -1); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 4681 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4682 | |
Wolfgang Grandegger | b325d7e | 2009-02-11 18:38:20 +0100 | [diff] [blame] | 4683 | #ifdef DEBUG |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 4684 | if (i > 1) |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4685 | pr_info("%d chips detected\n", i); |
Wolfgang Grandegger | b325d7e | 2009-02-11 18:38:20 +0100 | [diff] [blame] | 4686 | #endif |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 4687 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4688 | /* Store the number of chips and calc total size for mtd */ |
| 4689 | chip->numchips = i; |
| 4690 | mtd->size = i * chip->chipsize; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 4691 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4692 | return 0; |
| 4693 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4694 | EXPORT_SYMBOL(nand_scan_ident); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 4695 | |
Masahiro Yamada | 820eb48 | 2017-11-22 02:38:29 +0900 | [diff] [blame] | 4696 | /** |
| 4697 | * nand_check_ecc_caps - check the sanity of preset ECC settings |
| 4698 | * @chip: nand chip info structure |
| 4699 | * @caps: ECC caps info structure |
| 4700 | * @oobavail: OOB size that the ECC engine can use |
| 4701 | * |
| 4702 | * When ECC step size and strength are already set, check if they are supported |
| 4703 | * by the controller and the calculated ECC bytes fit within the chip's OOB. |
| 4704 | * On success, the calculated ECC bytes is set. |
| 4705 | */ |
| 4706 | int nand_check_ecc_caps(struct nand_chip *chip, |
| 4707 | const struct nand_ecc_caps *caps, int oobavail) |
| 4708 | { |
| 4709 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 4710 | const struct nand_ecc_step_info *stepinfo; |
| 4711 | int preset_step = chip->ecc.size; |
| 4712 | int preset_strength = chip->ecc.strength; |
| 4713 | int nsteps, ecc_bytes; |
| 4714 | int i, j; |
| 4715 | |
| 4716 | if (WARN_ON(oobavail < 0)) |
| 4717 | return -EINVAL; |
| 4718 | |
| 4719 | if (!preset_step || !preset_strength) |
| 4720 | return -ENODATA; |
| 4721 | |
| 4722 | nsteps = mtd->writesize / preset_step; |
| 4723 | |
| 4724 | for (i = 0; i < caps->nstepinfos; i++) { |
| 4725 | stepinfo = &caps->stepinfos[i]; |
| 4726 | |
| 4727 | if (stepinfo->stepsize != preset_step) |
| 4728 | continue; |
| 4729 | |
| 4730 | for (j = 0; j < stepinfo->nstrengths; j++) { |
| 4731 | if (stepinfo->strengths[j] != preset_strength) |
| 4732 | continue; |
| 4733 | |
| 4734 | ecc_bytes = caps->calc_ecc_bytes(preset_step, |
| 4735 | preset_strength); |
| 4736 | if (WARN_ON_ONCE(ecc_bytes < 0)) |
| 4737 | return ecc_bytes; |
| 4738 | |
| 4739 | if (ecc_bytes * nsteps > oobavail) { |
| 4740 | pr_err("ECC (step, strength) = (%d, %d) does not fit in OOB", |
| 4741 | preset_step, preset_strength); |
| 4742 | return -ENOSPC; |
| 4743 | } |
| 4744 | |
| 4745 | chip->ecc.bytes = ecc_bytes; |
| 4746 | |
| 4747 | return 0; |
| 4748 | } |
| 4749 | } |
| 4750 | |
| 4751 | pr_err("ECC (step, strength) = (%d, %d) not supported on this controller", |
| 4752 | preset_step, preset_strength); |
| 4753 | |
| 4754 | return -ENOTSUPP; |
| 4755 | } |
| 4756 | EXPORT_SYMBOL_GPL(nand_check_ecc_caps); |
| 4757 | |
| 4758 | /** |
| 4759 | * nand_match_ecc_req - meet the chip's requirement with least ECC bytes |
| 4760 | * @chip: nand chip info structure |
| 4761 | * @caps: ECC engine caps info structure |
| 4762 | * @oobavail: OOB size that the ECC engine can use |
| 4763 | * |
| 4764 | * If a chip's ECC requirement is provided, try to meet it with the least |
| 4765 | * number of ECC bytes (i.e. with the largest number of OOB-free bytes). |
| 4766 | * On success, the chosen ECC settings are set. |
| 4767 | */ |
| 4768 | int nand_match_ecc_req(struct nand_chip *chip, |
| 4769 | const struct nand_ecc_caps *caps, int oobavail) |
| 4770 | { |
| 4771 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 4772 | const struct nand_ecc_step_info *stepinfo; |
| 4773 | int req_step = chip->ecc_step_ds; |
| 4774 | int req_strength = chip->ecc_strength_ds; |
| 4775 | int req_corr, step_size, strength, nsteps, ecc_bytes, ecc_bytes_total; |
| 4776 | int best_step, best_strength, best_ecc_bytes; |
| 4777 | int best_ecc_bytes_total = INT_MAX; |
| 4778 | int i, j; |
| 4779 | |
| 4780 | if (WARN_ON(oobavail < 0)) |
| 4781 | return -EINVAL; |
| 4782 | |
| 4783 | /* No information provided by the NAND chip */ |
| 4784 | if (!req_step || !req_strength) |
| 4785 | return -ENOTSUPP; |
| 4786 | |
| 4787 | /* number of correctable bits the chip requires in a page */ |
| 4788 | req_corr = mtd->writesize / req_step * req_strength; |
| 4789 | |
| 4790 | for (i = 0; i < caps->nstepinfos; i++) { |
| 4791 | stepinfo = &caps->stepinfos[i]; |
| 4792 | step_size = stepinfo->stepsize; |
| 4793 | |
| 4794 | for (j = 0; j < stepinfo->nstrengths; j++) { |
| 4795 | strength = stepinfo->strengths[j]; |
| 4796 | |
| 4797 | /* |
| 4798 | * If both step size and strength are smaller than the |
| 4799 | * chip's requirement, it is not easy to compare the |
| 4800 | * resulted reliability. |
| 4801 | */ |
| 4802 | if (step_size < req_step && strength < req_strength) |
| 4803 | continue; |
| 4804 | |
| 4805 | if (mtd->writesize % step_size) |
| 4806 | continue; |
| 4807 | |
| 4808 | nsteps = mtd->writesize / step_size; |
| 4809 | |
| 4810 | ecc_bytes = caps->calc_ecc_bytes(step_size, strength); |
| 4811 | if (WARN_ON_ONCE(ecc_bytes < 0)) |
| 4812 | continue; |
| 4813 | ecc_bytes_total = ecc_bytes * nsteps; |
| 4814 | |
| 4815 | if (ecc_bytes_total > oobavail || |
| 4816 | strength * nsteps < req_corr) |
| 4817 | continue; |
| 4818 | |
| 4819 | /* |
| 4820 | * We assume the best is to meet the chip's requrement |
| 4821 | * with the least number of ECC bytes. |
| 4822 | */ |
| 4823 | if (ecc_bytes_total < best_ecc_bytes_total) { |
| 4824 | best_ecc_bytes_total = ecc_bytes_total; |
| 4825 | best_step = step_size; |
| 4826 | best_strength = strength; |
| 4827 | best_ecc_bytes = ecc_bytes; |
| 4828 | } |
| 4829 | } |
| 4830 | } |
| 4831 | |
| 4832 | if (best_ecc_bytes_total == INT_MAX) |
| 4833 | return -ENOTSUPP; |
| 4834 | |
| 4835 | chip->ecc.size = best_step; |
| 4836 | chip->ecc.strength = best_strength; |
| 4837 | chip->ecc.bytes = best_ecc_bytes; |
| 4838 | |
| 4839 | return 0; |
| 4840 | } |
| 4841 | EXPORT_SYMBOL_GPL(nand_match_ecc_req); |
| 4842 | |
| 4843 | /** |
| 4844 | * nand_maximize_ecc - choose the max ECC strength available |
| 4845 | * @chip: nand chip info structure |
| 4846 | * @caps: ECC engine caps info structure |
| 4847 | * @oobavail: OOB size that the ECC engine can use |
| 4848 | * |
| 4849 | * Choose the max ECC strength that is supported on the controller, and can fit |
| 4850 | * within the chip's OOB. On success, the chosen ECC settings are set. |
| 4851 | */ |
| 4852 | int nand_maximize_ecc(struct nand_chip *chip, |
| 4853 | const struct nand_ecc_caps *caps, int oobavail) |
| 4854 | { |
| 4855 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 4856 | const struct nand_ecc_step_info *stepinfo; |
| 4857 | int step_size, strength, nsteps, ecc_bytes, corr; |
| 4858 | int best_corr = 0; |
| 4859 | int best_step = 0; |
| 4860 | int best_strength, best_ecc_bytes; |
| 4861 | int i, j; |
| 4862 | |
| 4863 | if (WARN_ON(oobavail < 0)) |
| 4864 | return -EINVAL; |
| 4865 | |
| 4866 | for (i = 0; i < caps->nstepinfos; i++) { |
| 4867 | stepinfo = &caps->stepinfos[i]; |
| 4868 | step_size = stepinfo->stepsize; |
| 4869 | |
| 4870 | /* If chip->ecc.size is already set, respect it */ |
| 4871 | if (chip->ecc.size && step_size != chip->ecc.size) |
| 4872 | continue; |
| 4873 | |
| 4874 | for (j = 0; j < stepinfo->nstrengths; j++) { |
| 4875 | strength = stepinfo->strengths[j]; |
| 4876 | |
| 4877 | if (mtd->writesize % step_size) |
| 4878 | continue; |
| 4879 | |
| 4880 | nsteps = mtd->writesize / step_size; |
| 4881 | |
| 4882 | ecc_bytes = caps->calc_ecc_bytes(step_size, strength); |
| 4883 | if (WARN_ON_ONCE(ecc_bytes < 0)) |
| 4884 | continue; |
| 4885 | |
| 4886 | if (ecc_bytes * nsteps > oobavail) |
| 4887 | continue; |
| 4888 | |
| 4889 | corr = strength * nsteps; |
| 4890 | |
| 4891 | /* |
| 4892 | * If the number of correctable bits is the same, |
| 4893 | * bigger step_size has more reliability. |
| 4894 | */ |
| 4895 | if (corr > best_corr || |
| 4896 | (corr == best_corr && step_size > best_step)) { |
| 4897 | best_corr = corr; |
| 4898 | best_step = step_size; |
| 4899 | best_strength = strength; |
| 4900 | best_ecc_bytes = ecc_bytes; |
| 4901 | } |
| 4902 | } |
| 4903 | } |
| 4904 | |
| 4905 | if (!best_corr) |
| 4906 | return -ENOTSUPP; |
| 4907 | |
| 4908 | chip->ecc.size = best_step; |
| 4909 | chip->ecc.strength = best_strength; |
| 4910 | chip->ecc.bytes = best_ecc_bytes; |
| 4911 | |
| 4912 | return 0; |
| 4913 | } |
| 4914 | EXPORT_SYMBOL_GPL(nand_maximize_ecc); |
| 4915 | |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 4916 | /* |
| 4917 | * Check if the chip configuration meet the datasheet requirements. |
| 4918 | |
| 4919 | * If our configuration corrects A bits per B bytes and the minimum |
| 4920 | * required correction level is X bits per Y bytes, then we must ensure |
| 4921 | * both of the following are true: |
| 4922 | * |
| 4923 | * (1) A / B >= X / Y |
| 4924 | * (2) A >= X |
| 4925 | * |
| 4926 | * Requirement (1) ensures we can correct for the required bitflip density. |
| 4927 | * Requirement (2) ensures we can correct even when all bitflips are clumped |
| 4928 | * in the same sector. |
| 4929 | */ |
| 4930 | static bool nand_ecc_strength_good(struct mtd_info *mtd) |
| 4931 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 4932 | struct nand_chip *chip = mtd_to_nand(mtd); |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 4933 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 4934 | int corr, ds_corr; |
| 4935 | |
| 4936 | if (ecc->size == 0 || chip->ecc_step_ds == 0) |
| 4937 | /* Not enough information */ |
| 4938 | return true; |
| 4939 | |
| 4940 | /* |
| 4941 | * We get the number of corrected bits per page to compare |
| 4942 | * the correction density. |
| 4943 | */ |
| 4944 | corr = (mtd->writesize * ecc->strength) / ecc->size; |
| 4945 | ds_corr = (mtd->writesize * chip->ecc_strength_ds) / chip->ecc_step_ds; |
| 4946 | |
| 4947 | return corr >= ds_corr && ecc->strength >= chip->ecc_strength_ds; |
| 4948 | } |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4949 | |
Marc Gonzalez | c3a2985 | 2017-11-22 02:38:22 +0900 | [diff] [blame] | 4950 | static bool invalid_ecc_page_accessors(struct nand_chip *chip) |
| 4951 | { |
| 4952 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 4953 | |
| 4954 | if (nand_standard_page_accessors(ecc)) |
| 4955 | return false; |
| 4956 | |
| 4957 | /* |
| 4958 | * NAND_ECC_CUSTOM_PAGE_ACCESS flag is set, make sure the NAND |
| 4959 | * controller driver implements all the page accessors because |
| 4960 | * default helpers are not suitable when the core does not |
| 4961 | * send the READ0/PAGEPROG commands. |
| 4962 | */ |
| 4963 | return (!ecc->read_page || !ecc->write_page || |
| 4964 | !ecc->read_page_raw || !ecc->write_page_raw || |
| 4965 | (NAND_HAS_SUBPAGE_READ(chip) && !ecc->read_subpage) || |
| 4966 | (NAND_HAS_SUBPAGE_WRITE(chip) && !ecc->write_subpage && |
| 4967 | ecc->hwctl && ecc->calculate)); |
| 4968 | } |
| 4969 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4970 | /** |
| 4971 | * nand_scan_tail - [NAND Interface] Scan for the NAND device |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4972 | * @mtd: MTD device structure |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4973 | * |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4974 | * This is the second phase of the normal nand_scan() function. It fills out |
| 4975 | * all the uninitialized function pointers with the defaults and scans for a |
| 4976 | * bad block table if appropriate. |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 4977 | */ |
| 4978 | int nand_scan_tail(struct mtd_info *mtd) |
| 4979 | { |
| 4980 | int i; |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 4981 | struct nand_chip *chip = mtd_to_nand(mtd); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 4982 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 4983 | struct nand_buffers *nbuf; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 4984 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 4985 | /* New bad blocks should be marked in OOB, flash-based BBT, or both */ |
| 4986 | BUG_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) && |
| 4987 | !(chip->bbt_options & NAND_BBT_USE_FLASH)); |
| 4988 | |
Marc Gonzalez | c3a2985 | 2017-11-22 02:38:22 +0900 | [diff] [blame] | 4989 | if (invalid_ecc_page_accessors(chip)) { |
| 4990 | pr_err("Invalid ECC page accessors setup\n"); |
| 4991 | return -EINVAL; |
| 4992 | } |
| 4993 | |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 4994 | if (!(chip->options & NAND_OWN_BUFFERS)) { |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 4995 | nbuf = kzalloc(sizeof(struct nand_buffers), GFP_KERNEL); |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 4996 | chip->buffers = nbuf; |
| 4997 | } else { |
| 4998 | if (!chip->buffers) |
| 4999 | return -ENOMEM; |
| 5000 | } |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5001 | |
| 5002 | /* Set the internal oob buffer location, just after the page data */ |
| 5003 | chip->oob_poi = chip->buffers->databuf + mtd->writesize; |
| 5004 | |
| 5005 | /* |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 5006 | * If no default placement scheme is given, select an appropriate one. |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5007 | */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5008 | if (!ecc->layout && (ecc->mode != NAND_ECC_SOFT_BCH)) { |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 5009 | switch (mtd->oobsize) { |
Gregory CLEMENT | e5b9631 | 2019-04-17 11:22:05 +0200 | [diff] [blame] | 5010 | #ifndef CONFIG_SYS_NAND_DRIVER_ECC_LAYOUT |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 5011 | case 8: |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5012 | ecc->layout = &nand_oob_8; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 5013 | break; |
| 5014 | case 16: |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5015 | ecc->layout = &nand_oob_16; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 5016 | break; |
| 5017 | case 64: |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5018 | ecc->layout = &nand_oob_64; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 5019 | break; |
Sergei Poselenov | 04fbaa0 | 2008-06-06 15:42:43 +0200 | [diff] [blame] | 5020 | case 128: |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5021 | ecc->layout = &nand_oob_128; |
Sergei Poselenov | 04fbaa0 | 2008-06-06 15:42:43 +0200 | [diff] [blame] | 5022 | break; |
Stefan Agner | bd18614 | 2018-12-06 14:57:09 +0100 | [diff] [blame] | 5023 | #endif |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 5024 | default: |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 5025 | pr_warn("No oob scheme defined for oobsize %d\n", |
| 5026 | mtd->oobsize); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5027 | BUG(); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 5028 | } |
| 5029 | } |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 5030 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5031 | if (!chip->write_page) |
| 5032 | chip->write_page = nand_write_page; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 5033 | |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 5034 | /* |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 5035 | * Check ECC mode, default to software if 3byte/512byte hardware ECC is |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5036 | * selected and we have 256 byte pagesize fallback to software ECC |
| 5037 | */ |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 5038 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5039 | switch (ecc->mode) { |
Sandeep Paulraj | dea4070 | 2009-08-10 13:27:56 -0400 | [diff] [blame] | 5040 | case NAND_ECC_HW_OOB_FIRST: |
| 5041 | /* Similar to NAND_ECC_HW, but a separate read_page handle */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5042 | if (!ecc->calculate || !ecc->correct || !ecc->hwctl) { |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 5043 | pr_warn("No ECC functions supplied; hardware ECC not possible\n"); |
Sandeep Paulraj | dea4070 | 2009-08-10 13:27:56 -0400 | [diff] [blame] | 5044 | BUG(); |
| 5045 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5046 | if (!ecc->read_page) |
| 5047 | ecc->read_page = nand_read_page_hwecc_oob_first; |
Sandeep Paulraj | dea4070 | 2009-08-10 13:27:56 -0400 | [diff] [blame] | 5048 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5049 | case NAND_ECC_HW: |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 5050 | /* Use standard hwecc read page function? */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5051 | if (!ecc->read_page) |
| 5052 | ecc->read_page = nand_read_page_hwecc; |
| 5053 | if (!ecc->write_page) |
| 5054 | ecc->write_page = nand_write_page_hwecc; |
| 5055 | if (!ecc->read_page_raw) |
| 5056 | ecc->read_page_raw = nand_read_page_raw; |
| 5057 | if (!ecc->write_page_raw) |
| 5058 | ecc->write_page_raw = nand_write_page_raw; |
| 5059 | if (!ecc->read_oob) |
| 5060 | ecc->read_oob = nand_read_oob_std; |
| 5061 | if (!ecc->write_oob) |
| 5062 | ecc->write_oob = nand_write_oob_std; |
| 5063 | if (!ecc->read_subpage) |
| 5064 | ecc->read_subpage = nand_read_subpage; |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 5065 | if (!ecc->write_subpage && ecc->hwctl && ecc->calculate) |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5066 | ecc->write_subpage = nand_write_subpage_hwecc; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 5067 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5068 | case NAND_ECC_HW_SYNDROME: |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5069 | if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) && |
| 5070 | (!ecc->read_page || |
| 5071 | ecc->read_page == nand_read_page_hwecc || |
| 5072 | !ecc->write_page || |
| 5073 | ecc->write_page == nand_write_page_hwecc)) { |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 5074 | pr_warn("No ECC functions supplied; hardware ECC not possible\n"); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5075 | BUG(); |
| 5076 | } |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 5077 | /* Use standard syndrome read/write page function? */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5078 | if (!ecc->read_page) |
| 5079 | ecc->read_page = nand_read_page_syndrome; |
| 5080 | if (!ecc->write_page) |
| 5081 | ecc->write_page = nand_write_page_syndrome; |
| 5082 | if (!ecc->read_page_raw) |
| 5083 | ecc->read_page_raw = nand_read_page_raw_syndrome; |
| 5084 | if (!ecc->write_page_raw) |
| 5085 | ecc->write_page_raw = nand_write_page_raw_syndrome; |
| 5086 | if (!ecc->read_oob) |
| 5087 | ecc->read_oob = nand_read_oob_syndrome; |
| 5088 | if (!ecc->write_oob) |
| 5089 | ecc->write_oob = nand_write_oob_syndrome; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 5090 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5091 | if (mtd->writesize >= ecc->size) { |
| 5092 | if (!ecc->strength) { |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 5093 | pr_warn("Driver must set ecc.strength when using hardware ECC\n"); |
| 5094 | BUG(); |
| 5095 | } |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5096 | break; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 5097 | } |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 5098 | pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n", |
| 5099 | ecc->size, mtd->writesize); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5100 | ecc->mode = NAND_ECC_SOFT; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 5101 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5102 | case NAND_ECC_SOFT: |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5103 | ecc->calculate = nand_calculate_ecc; |
| 5104 | ecc->correct = nand_correct_data; |
| 5105 | ecc->read_page = nand_read_page_swecc; |
| 5106 | ecc->read_subpage = nand_read_subpage; |
| 5107 | ecc->write_page = nand_write_page_swecc; |
| 5108 | ecc->read_page_raw = nand_read_page_raw; |
| 5109 | ecc->write_page_raw = nand_write_page_raw; |
| 5110 | ecc->read_oob = nand_read_oob_std; |
| 5111 | ecc->write_oob = nand_write_oob_std; |
| 5112 | if (!ecc->size) |
| 5113 | ecc->size = 256; |
| 5114 | ecc->bytes = 3; |
| 5115 | ecc->strength = 1; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 5116 | break; |
| 5117 | |
Christian Hitz | 55f7bca | 2011-10-12 09:31:59 +0200 | [diff] [blame] | 5118 | case NAND_ECC_SOFT_BCH: |
| 5119 | if (!mtd_nand_has_bch()) { |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 5120 | pr_warn("CONFIG_MTD_NAND_ECC_BCH not enabled\n"); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5121 | BUG(); |
Christian Hitz | 55f7bca | 2011-10-12 09:31:59 +0200 | [diff] [blame] | 5122 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5123 | ecc->calculate = nand_bch_calculate_ecc; |
| 5124 | ecc->correct = nand_bch_correct_data; |
| 5125 | ecc->read_page = nand_read_page_swecc; |
| 5126 | ecc->read_subpage = nand_read_subpage; |
| 5127 | ecc->write_page = nand_write_page_swecc; |
| 5128 | ecc->read_page_raw = nand_read_page_raw; |
| 5129 | ecc->write_page_raw = nand_write_page_raw; |
| 5130 | ecc->read_oob = nand_read_oob_std; |
| 5131 | ecc->write_oob = nand_write_oob_std; |
Christian Hitz | 55f7bca | 2011-10-12 09:31:59 +0200 | [diff] [blame] | 5132 | /* |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 5133 | * Board driver should supply ecc.size and ecc.strength values |
| 5134 | * to select how many bits are correctable. Otherwise, default |
| 5135 | * to 4 bits for large page devices. |
Christian Hitz | 55f7bca | 2011-10-12 09:31:59 +0200 | [diff] [blame] | 5136 | */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5137 | if (!ecc->size && (mtd->oobsize >= 64)) { |
| 5138 | ecc->size = 512; |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 5139 | ecc->strength = 4; |
Christian Hitz | 55f7bca | 2011-10-12 09:31:59 +0200 | [diff] [blame] | 5140 | } |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 5141 | |
| 5142 | /* See nand_bch_init() for details. */ |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 5143 | ecc->bytes = 0; |
| 5144 | ecc->priv = nand_bch_init(mtd); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5145 | if (!ecc->priv) { |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 5146 | pr_warn("BCH ECC initialization failed!\n"); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5147 | BUG(); |
| 5148 | } |
Christian Hitz | 55f7bca | 2011-10-12 09:31:59 +0200 | [diff] [blame] | 5149 | break; |
| 5150 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5151 | case NAND_ECC_NONE: |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 5152 | pr_warn("NAND_ECC_NONE selected by board driver. This is not recommended!\n"); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5153 | ecc->read_page = nand_read_page_raw; |
| 5154 | ecc->write_page = nand_write_page_raw; |
| 5155 | ecc->read_oob = nand_read_oob_std; |
| 5156 | ecc->read_page_raw = nand_read_page_raw; |
| 5157 | ecc->write_page_raw = nand_write_page_raw; |
| 5158 | ecc->write_oob = nand_write_oob_std; |
| 5159 | ecc->size = mtd->writesize; |
| 5160 | ecc->bytes = 0; |
| 5161 | ecc->strength = 0; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 5162 | break; |
| 5163 | |
| 5164 | default: |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5165 | pr_warn("Invalid NAND_ECC_MODE %d\n", ecc->mode); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5166 | BUG(); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 5167 | } |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 5168 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 5169 | /* For many systems, the standard OOB write also works for raw */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5170 | if (!ecc->read_oob_raw) |
| 5171 | ecc->read_oob_raw = ecc->read_oob; |
| 5172 | if (!ecc->write_oob_raw) |
| 5173 | ecc->write_oob_raw = ecc->write_oob; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 5174 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5175 | /* |
| 5176 | * The number of bytes available for a client to place data into |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 5177 | * the out of band area. |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5178 | */ |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 5179 | mtd->oobavail = 0; |
| 5180 | if (ecc->layout) { |
| 5181 | for (i = 0; ecc->layout->oobfree[i].length; i++) |
| 5182 | mtd->oobavail += ecc->layout->oobfree[i].length; |
| 5183 | } |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 5184 | |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 5185 | /* ECC sanity check: warn if it's too weak */ |
| 5186 | if (!nand_ecc_strength_good(mtd)) |
| 5187 | pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n", |
| 5188 | mtd->name); |
| 5189 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5190 | /* |
| 5191 | * Set the number of read / write steps for one page depending on ECC |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 5192 | * mode. |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5193 | */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5194 | ecc->steps = mtd->writesize / ecc->size; |
| 5195 | if (ecc->steps * ecc->size != mtd->writesize) { |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 5196 | pr_warn("Invalid ECC parameters\n"); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5197 | BUG(); |
| 5198 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5199 | ecc->total = ecc->steps * ecc->bytes; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 5200 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 5201 | /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5202 | if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) { |
| 5203 | switch (ecc->steps) { |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5204 | case 2: |
| 5205 | mtd->subpage_sft = 1; |
| 5206 | break; |
| 5207 | case 4: |
| 5208 | case 8: |
Sandeep Paulraj | fd9874d | 2009-11-07 14:24:34 -0500 | [diff] [blame] | 5209 | case 16: |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5210 | mtd->subpage_sft = 2; |
| 5211 | break; |
| 5212 | } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 5213 | } |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5214 | chip->subpagesize = mtd->writesize >> mtd->subpage_sft; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 5215 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5216 | /* Initialize state */ |
| 5217 | chip->state = FL_READY; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 5218 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 5219 | /* Invalidate the pagebuffer reference */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5220 | chip->pagebuf = -1; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 5221 | |
Joe Hershberger | 7a38ffa | 2012-11-05 06:46:31 +0000 | [diff] [blame] | 5222 | /* Large page NAND with SOFT_ECC should support subpage reads */ |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 5223 | switch (ecc->mode) { |
| 5224 | case NAND_ECC_SOFT: |
| 5225 | case NAND_ECC_SOFT_BCH: |
| 5226 | if (chip->page_shift > 9) |
| 5227 | chip->options |= NAND_SUBPAGE_READ; |
| 5228 | break; |
| 5229 | |
| 5230 | default: |
| 5231 | break; |
| 5232 | } |
Joe Hershberger | 7a38ffa | 2012-11-05 06:46:31 +0000 | [diff] [blame] | 5233 | |
Patrice Chotard | bee3287 | 2022-03-21 09:13:36 +0100 | [diff] [blame] | 5234 | mtd->flash_node = chip->flash_node; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 5235 | /* Fill in remaining MTD driver data */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5236 | mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH; |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 5237 | mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM : |
| 5238 | MTD_CAP_NANDFLASH; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 5239 | mtd->_erase = nand_erase; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5240 | mtd->_panic_write = panic_nand_write; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 5241 | mtd->_read_oob = nand_read_oob; |
| 5242 | mtd->_write_oob = nand_write_oob; |
| 5243 | mtd->_sync = nand_sync; |
| 5244 | mtd->_lock = NULL; |
| 5245 | mtd->_unlock = NULL; |
Ezequiel Garcia | fc9d57c | 2014-05-21 19:06:12 -0300 | [diff] [blame] | 5246 | mtd->_block_isreserved = nand_block_isreserved; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 5247 | mtd->_block_isbad = nand_block_isbad; |
| 5248 | mtd->_block_markbad = nand_block_markbad; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5249 | mtd->writebufsize = mtd->writesize; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 5250 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 5251 | /* propagate ecc info to mtd_info */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5252 | mtd->ecclayout = ecc->layout; |
| 5253 | mtd->ecc_strength = ecc->strength; |
| 5254 | mtd->ecc_step_size = ecc->size; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 5255 | /* |
| 5256 | * Initialize bitflip_threshold to its default prior scan_bbt() call. |
| 5257 | * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be |
| 5258 | * properly set. |
| 5259 | */ |
| 5260 | if (!mtd->bitflip_threshold) |
Scott Wood | 3ea94ed | 2015-06-26 19:03:26 -0500 | [diff] [blame] | 5261 | mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5262 | |
Rostislav Lisovy | dc17bdc | 2014-10-22 13:40:44 +0200 | [diff] [blame] | 5263 | return 0; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5264 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5265 | EXPORT_SYMBOL(nand_scan_tail); |
| 5266 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5267 | /** |
| 5268 | * nand_scan - [NAND Interface] Scan for the NAND device |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 5269 | * @mtd: MTD device structure |
| 5270 | * @maxchips: number of chips to scan for |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5271 | * |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 5272 | * This fills out all the uninitialized function pointers with the defaults. |
| 5273 | * The flash ID is read and the mtd/chip structures are filled with the |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 5274 | * appropriate values. |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5275 | */ |
| 5276 | int nand_scan(struct mtd_info *mtd, int maxchips) |
| 5277 | { |
| 5278 | int ret; |
| 5279 | |
Lei Wen | 75bde94 | 2011-01-06 09:48:18 +0800 | [diff] [blame] | 5280 | ret = nand_scan_ident(mtd, maxchips, NULL); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 5281 | if (!ret) |
| 5282 | ret = nand_scan_tail(mtd); |
| 5283 | return ret; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 5284 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5285 | EXPORT_SYMBOL(nand_scan); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 5286 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5287 | MODULE_LICENSE("GPL"); |
| 5288 | MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>"); |
| 5289 | MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>"); |
| 5290 | MODULE_DESCRIPTION("Generic NAND flash driver code"); |